@ponxa/potatobase-client 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.d.mts +105 -25
- package/dist/index.d.ts +105 -25
- package/dist/index.js +175 -1
- package/dist/index.mjs +174 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ import { PotatoBaseClient } from '@potatobase/client';
|
|
|
19
19
|
|
|
20
20
|
// Initialize the client
|
|
21
21
|
const client = new PotatoBaseClient({
|
|
22
|
-
apiUrl: process.env.NEXT_PUBLIC_API_URL || 'https://api.potatobase.
|
|
22
|
+
apiUrl: process.env.NEXT_PUBLIC_API_URL || 'https://api.potatobase.dev',
|
|
23
23
|
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
|
|
24
24
|
apiKey: process.env.NEXT_PUBLIC_API_KEY, // Optional, for frontend usage
|
|
25
25
|
});
|
|
@@ -113,7 +113,7 @@ const product = await products.create({
|
|
|
113
113
|
Create a `.env.local` file in your project:
|
|
114
114
|
|
|
115
115
|
```env
|
|
116
|
-
NEXT_PUBLIC_API_URL=https://api.potatobase.
|
|
116
|
+
NEXT_PUBLIC_API_URL=https://api.potatobase.dev
|
|
117
117
|
NEXT_PUBLIC_PROJECT_ID=your-project-id
|
|
118
118
|
NEXT_PUBLIC_API_KEY=your-api-key
|
|
119
119
|
```
|
|
@@ -166,6 +166,6 @@ MIT © PotatoBase Team
|
|
|
166
166
|
|
|
167
167
|
## Support
|
|
168
168
|
|
|
169
|
-
- Documentation: [https://docs.potatobase.
|
|
169
|
+
- Documentation: [https://docs.potatobase.dev](https://docs.potatobase.dev)
|
|
170
170
|
- GitHub Issues: [https://github.com/potatobase/client/issues](https://github.com/potatobase/client/issues)
|
|
171
171
|
- Discord: [Join our community](https://discord.gg/potatobase)
|
package/dist/index.d.mts
CHANGED
|
@@ -72,6 +72,33 @@ interface ApiError {
|
|
|
72
72
|
code?: string;
|
|
73
73
|
details?: any;
|
|
74
74
|
}
|
|
75
|
+
/** Supported field types for table schemas */
|
|
76
|
+
type FieldType = 'string' | 'number' | 'boolean' | 'map' | 'list';
|
|
77
|
+
/** Index definition (primary or GSI) */
|
|
78
|
+
interface IndexDefinition {
|
|
79
|
+
pk: string;
|
|
80
|
+
sk?: string;
|
|
81
|
+
}
|
|
82
|
+
/** Complete table schema definition */
|
|
83
|
+
interface TableSchema {
|
|
84
|
+
fields: Record<string, FieldType>;
|
|
85
|
+
primaryIndex: IndexDefinition;
|
|
86
|
+
globalIndexes?: Record<string, IndexDefinition>;
|
|
87
|
+
}
|
|
88
|
+
/** Table info returned from list/get operations */
|
|
89
|
+
interface TableInfo {
|
|
90
|
+
tableName: string;
|
|
91
|
+
schema: TableSchema;
|
|
92
|
+
recordCount: number;
|
|
93
|
+
createdAt: string;
|
|
94
|
+
updatedAt: string;
|
|
95
|
+
}
|
|
96
|
+
/** Result of addField/removeField operations */
|
|
97
|
+
interface SchemaOperationResult {
|
|
98
|
+
success: boolean;
|
|
99
|
+
tableName: string;
|
|
100
|
+
schema: TableSchema;
|
|
101
|
+
}
|
|
75
102
|
|
|
76
103
|
/**
|
|
77
104
|
* TableClient
|
|
@@ -213,6 +240,62 @@ declare class TableClient<TRecord extends {
|
|
|
213
240
|
from(): this;
|
|
214
241
|
}
|
|
215
242
|
|
|
243
|
+
/**
|
|
244
|
+
* SchemaClient
|
|
245
|
+
*
|
|
246
|
+
* Manages table schemas from the SDK.
|
|
247
|
+
* Provides methods to list, create, modify, and delete tables and their fields.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* const pb = createClient({ projectId: 'proj_123', apiKey: 'pk_...' });
|
|
252
|
+
*
|
|
253
|
+
* // Add a field
|
|
254
|
+
* await pb.schema.addField('products', 'discount', 'number');
|
|
255
|
+
*
|
|
256
|
+
* // List tables
|
|
257
|
+
* const tables = await pb.schema.listTables();
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
declare class SchemaClient {
|
|
261
|
+
private apiUrl;
|
|
262
|
+
private apiKey;
|
|
263
|
+
private projectId;
|
|
264
|
+
private debug;
|
|
265
|
+
constructor(apiUrl: string, apiKey: string, projectId: string, debug?: boolean);
|
|
266
|
+
private callAPI;
|
|
267
|
+
/**
|
|
268
|
+
* List all tables in the project
|
|
269
|
+
*/
|
|
270
|
+
listTables(): Promise<TableInfo[]>;
|
|
271
|
+
/**
|
|
272
|
+
* Get schema for a specific table
|
|
273
|
+
*/
|
|
274
|
+
getSchema(tableName: string): Promise<TableInfo | null>;
|
|
275
|
+
/**
|
|
276
|
+
* Create a new table with the given schema
|
|
277
|
+
*/
|
|
278
|
+
createTable(tableName: string, schema: TableSchema): Promise<TableInfo>;
|
|
279
|
+
/**
|
|
280
|
+
* Add a field to an existing table
|
|
281
|
+
*/
|
|
282
|
+
addField(tableName: string, fieldName: string, fieldType: FieldType): Promise<SchemaOperationResult>;
|
|
283
|
+
/**
|
|
284
|
+
* Remove a field from an existing table
|
|
285
|
+
*/
|
|
286
|
+
removeField(tableName: string, fieldName: string): Promise<SchemaOperationResult>;
|
|
287
|
+
/**
|
|
288
|
+
* Replace the entire schema of a table
|
|
289
|
+
*/
|
|
290
|
+
replaceSchema(tableName: string, schema: TableSchema): Promise<any>;
|
|
291
|
+
/**
|
|
292
|
+
* Delete a table and all its records
|
|
293
|
+
*/
|
|
294
|
+
deleteTable(tableName: string): Promise<{
|
|
295
|
+
success: boolean;
|
|
296
|
+
}>;
|
|
297
|
+
}
|
|
298
|
+
|
|
216
299
|
/**
|
|
217
300
|
* Helper functions for date range queries
|
|
218
301
|
*
|
|
@@ -352,30 +435,6 @@ declare function thisYear(field: string): RangeCondition;
|
|
|
352
435
|
*/
|
|
353
436
|
declare function today(field: string): RangeCondition;
|
|
354
437
|
|
|
355
|
-
/**
|
|
356
|
-
* @potatobase/client
|
|
357
|
-
*
|
|
358
|
-
* Official JavaScript/TypeScript client for PotatoBase.
|
|
359
|
-
* Type-safe, easy-to-use SDK for querying your DynamoDB data.
|
|
360
|
-
*
|
|
361
|
-
* @example
|
|
362
|
-
* ```typescript
|
|
363
|
-
* import { createClient } from '@potatobase/client';
|
|
364
|
-
* import type { Database } from './types/database';
|
|
365
|
-
*
|
|
366
|
-
* const pb = createClient<Database>({
|
|
367
|
-
* projectId: 'proj_abc123',
|
|
368
|
-
* apiKey: 'pk_live_xyz...',
|
|
369
|
-
* });
|
|
370
|
-
*
|
|
371
|
-
* // Type-safe queries!
|
|
372
|
-
* const products = await pb.table('products').query({
|
|
373
|
-
* index: 'byCategory',
|
|
374
|
-
* where: { category: 'clothing' }
|
|
375
|
-
* });
|
|
376
|
-
* ```
|
|
377
|
-
*/
|
|
378
|
-
|
|
379
438
|
/**
|
|
380
439
|
* PotatoBaseClient
|
|
381
440
|
*
|
|
@@ -385,6 +444,7 @@ declare function today(field: string): RangeCondition;
|
|
|
385
444
|
declare class PotatoBaseClient<TDatabase = any> {
|
|
386
445
|
private config;
|
|
387
446
|
private tableClients;
|
|
447
|
+
private _schemaClient?;
|
|
388
448
|
constructor(config: ClientConfig);
|
|
389
449
|
/**
|
|
390
450
|
* Access a table by name
|
|
@@ -419,6 +479,26 @@ declare class PotatoBaseClient<TDatabase = any> {
|
|
|
419
479
|
* ```
|
|
420
480
|
*/
|
|
421
481
|
generateTypes(): Promise<string>;
|
|
482
|
+
/**
|
|
483
|
+
* Access the schema management client
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* const tables = await pb.schema.listTables();
|
|
488
|
+
* await pb.schema.addField('products', 'discount', 'number');
|
|
489
|
+
* ```
|
|
490
|
+
*/
|
|
491
|
+
get schema(): SchemaClient;
|
|
492
|
+
/** Create a new table */
|
|
493
|
+
createTable(tableName: string, schema: TableSchema): Promise<TableInfo>;
|
|
494
|
+
/** Add a field to an existing table */
|
|
495
|
+
addField(tableName: string, fieldName: string, fieldType: FieldType): Promise<SchemaOperationResult>;
|
|
496
|
+
/** Remove a field from an existing table */
|
|
497
|
+
removeField(tableName: string, fieldName: string): Promise<SchemaOperationResult>;
|
|
498
|
+
/** List all tables in the project */
|
|
499
|
+
listTables(): Promise<TableInfo[]>;
|
|
500
|
+
/** Get schema for a specific table */
|
|
501
|
+
getSchema(tableName: string): Promise<TableInfo | null>;
|
|
422
502
|
/**
|
|
423
503
|
* Get the project ID
|
|
424
504
|
*/
|
|
@@ -457,4 +537,4 @@ declare class PotatoBaseClient<TDatabase = any> {
|
|
|
457
537
|
*/
|
|
458
538
|
declare function createClient<TDatabase = any>(config: ClientConfig): PotatoBaseClient<TDatabase>;
|
|
459
539
|
|
|
460
|
-
export { type ApiError, type ClientConfig, PotatoBaseClient, type QueryParams, type QueryResult, type RangeCondition, TableClient, type TypeGenerationResult, between, createClient, daysAgo, hoursAgo, lastDays, minutesAgo, since, thisMonth, thisYear, today, until };
|
|
540
|
+
export { type ApiError, type ClientConfig, type FieldType, type IndexDefinition, PotatoBaseClient, type QueryParams, type QueryResult, type RangeCondition, SchemaClient, type SchemaOperationResult, TableClient, type TableInfo, type TableSchema, type TypeGenerationResult, between, createClient, daysAgo, hoursAgo, lastDays, minutesAgo, since, thisMonth, thisYear, today, until };
|
package/dist/index.d.ts
CHANGED
|
@@ -72,6 +72,33 @@ interface ApiError {
|
|
|
72
72
|
code?: string;
|
|
73
73
|
details?: any;
|
|
74
74
|
}
|
|
75
|
+
/** Supported field types for table schemas */
|
|
76
|
+
type FieldType = 'string' | 'number' | 'boolean' | 'map' | 'list';
|
|
77
|
+
/** Index definition (primary or GSI) */
|
|
78
|
+
interface IndexDefinition {
|
|
79
|
+
pk: string;
|
|
80
|
+
sk?: string;
|
|
81
|
+
}
|
|
82
|
+
/** Complete table schema definition */
|
|
83
|
+
interface TableSchema {
|
|
84
|
+
fields: Record<string, FieldType>;
|
|
85
|
+
primaryIndex: IndexDefinition;
|
|
86
|
+
globalIndexes?: Record<string, IndexDefinition>;
|
|
87
|
+
}
|
|
88
|
+
/** Table info returned from list/get operations */
|
|
89
|
+
interface TableInfo {
|
|
90
|
+
tableName: string;
|
|
91
|
+
schema: TableSchema;
|
|
92
|
+
recordCount: number;
|
|
93
|
+
createdAt: string;
|
|
94
|
+
updatedAt: string;
|
|
95
|
+
}
|
|
96
|
+
/** Result of addField/removeField operations */
|
|
97
|
+
interface SchemaOperationResult {
|
|
98
|
+
success: boolean;
|
|
99
|
+
tableName: string;
|
|
100
|
+
schema: TableSchema;
|
|
101
|
+
}
|
|
75
102
|
|
|
76
103
|
/**
|
|
77
104
|
* TableClient
|
|
@@ -213,6 +240,62 @@ declare class TableClient<TRecord extends {
|
|
|
213
240
|
from(): this;
|
|
214
241
|
}
|
|
215
242
|
|
|
243
|
+
/**
|
|
244
|
+
* SchemaClient
|
|
245
|
+
*
|
|
246
|
+
* Manages table schemas from the SDK.
|
|
247
|
+
* Provides methods to list, create, modify, and delete tables and their fields.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* const pb = createClient({ projectId: 'proj_123', apiKey: 'pk_...' });
|
|
252
|
+
*
|
|
253
|
+
* // Add a field
|
|
254
|
+
* await pb.schema.addField('products', 'discount', 'number');
|
|
255
|
+
*
|
|
256
|
+
* // List tables
|
|
257
|
+
* const tables = await pb.schema.listTables();
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
declare class SchemaClient {
|
|
261
|
+
private apiUrl;
|
|
262
|
+
private apiKey;
|
|
263
|
+
private projectId;
|
|
264
|
+
private debug;
|
|
265
|
+
constructor(apiUrl: string, apiKey: string, projectId: string, debug?: boolean);
|
|
266
|
+
private callAPI;
|
|
267
|
+
/**
|
|
268
|
+
* List all tables in the project
|
|
269
|
+
*/
|
|
270
|
+
listTables(): Promise<TableInfo[]>;
|
|
271
|
+
/**
|
|
272
|
+
* Get schema for a specific table
|
|
273
|
+
*/
|
|
274
|
+
getSchema(tableName: string): Promise<TableInfo | null>;
|
|
275
|
+
/**
|
|
276
|
+
* Create a new table with the given schema
|
|
277
|
+
*/
|
|
278
|
+
createTable(tableName: string, schema: TableSchema): Promise<TableInfo>;
|
|
279
|
+
/**
|
|
280
|
+
* Add a field to an existing table
|
|
281
|
+
*/
|
|
282
|
+
addField(tableName: string, fieldName: string, fieldType: FieldType): Promise<SchemaOperationResult>;
|
|
283
|
+
/**
|
|
284
|
+
* Remove a field from an existing table
|
|
285
|
+
*/
|
|
286
|
+
removeField(tableName: string, fieldName: string): Promise<SchemaOperationResult>;
|
|
287
|
+
/**
|
|
288
|
+
* Replace the entire schema of a table
|
|
289
|
+
*/
|
|
290
|
+
replaceSchema(tableName: string, schema: TableSchema): Promise<any>;
|
|
291
|
+
/**
|
|
292
|
+
* Delete a table and all its records
|
|
293
|
+
*/
|
|
294
|
+
deleteTable(tableName: string): Promise<{
|
|
295
|
+
success: boolean;
|
|
296
|
+
}>;
|
|
297
|
+
}
|
|
298
|
+
|
|
216
299
|
/**
|
|
217
300
|
* Helper functions for date range queries
|
|
218
301
|
*
|
|
@@ -352,30 +435,6 @@ declare function thisYear(field: string): RangeCondition;
|
|
|
352
435
|
*/
|
|
353
436
|
declare function today(field: string): RangeCondition;
|
|
354
437
|
|
|
355
|
-
/**
|
|
356
|
-
* @potatobase/client
|
|
357
|
-
*
|
|
358
|
-
* Official JavaScript/TypeScript client for PotatoBase.
|
|
359
|
-
* Type-safe, easy-to-use SDK for querying your DynamoDB data.
|
|
360
|
-
*
|
|
361
|
-
* @example
|
|
362
|
-
* ```typescript
|
|
363
|
-
* import { createClient } from '@potatobase/client';
|
|
364
|
-
* import type { Database } from './types/database';
|
|
365
|
-
*
|
|
366
|
-
* const pb = createClient<Database>({
|
|
367
|
-
* projectId: 'proj_abc123',
|
|
368
|
-
* apiKey: 'pk_live_xyz...',
|
|
369
|
-
* });
|
|
370
|
-
*
|
|
371
|
-
* // Type-safe queries!
|
|
372
|
-
* const products = await pb.table('products').query({
|
|
373
|
-
* index: 'byCategory',
|
|
374
|
-
* where: { category: 'clothing' }
|
|
375
|
-
* });
|
|
376
|
-
* ```
|
|
377
|
-
*/
|
|
378
|
-
|
|
379
438
|
/**
|
|
380
439
|
* PotatoBaseClient
|
|
381
440
|
*
|
|
@@ -385,6 +444,7 @@ declare function today(field: string): RangeCondition;
|
|
|
385
444
|
declare class PotatoBaseClient<TDatabase = any> {
|
|
386
445
|
private config;
|
|
387
446
|
private tableClients;
|
|
447
|
+
private _schemaClient?;
|
|
388
448
|
constructor(config: ClientConfig);
|
|
389
449
|
/**
|
|
390
450
|
* Access a table by name
|
|
@@ -419,6 +479,26 @@ declare class PotatoBaseClient<TDatabase = any> {
|
|
|
419
479
|
* ```
|
|
420
480
|
*/
|
|
421
481
|
generateTypes(): Promise<string>;
|
|
482
|
+
/**
|
|
483
|
+
* Access the schema management client
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```typescript
|
|
487
|
+
* const tables = await pb.schema.listTables();
|
|
488
|
+
* await pb.schema.addField('products', 'discount', 'number');
|
|
489
|
+
* ```
|
|
490
|
+
*/
|
|
491
|
+
get schema(): SchemaClient;
|
|
492
|
+
/** Create a new table */
|
|
493
|
+
createTable(tableName: string, schema: TableSchema): Promise<TableInfo>;
|
|
494
|
+
/** Add a field to an existing table */
|
|
495
|
+
addField(tableName: string, fieldName: string, fieldType: FieldType): Promise<SchemaOperationResult>;
|
|
496
|
+
/** Remove a field from an existing table */
|
|
497
|
+
removeField(tableName: string, fieldName: string): Promise<SchemaOperationResult>;
|
|
498
|
+
/** List all tables in the project */
|
|
499
|
+
listTables(): Promise<TableInfo[]>;
|
|
500
|
+
/** Get schema for a specific table */
|
|
501
|
+
getSchema(tableName: string): Promise<TableInfo | null>;
|
|
422
502
|
/**
|
|
423
503
|
* Get the project ID
|
|
424
504
|
*/
|
|
@@ -457,4 +537,4 @@ declare class PotatoBaseClient<TDatabase = any> {
|
|
|
457
537
|
*/
|
|
458
538
|
declare function createClient<TDatabase = any>(config: ClientConfig): PotatoBaseClient<TDatabase>;
|
|
459
539
|
|
|
460
|
-
export { type ApiError, type ClientConfig, PotatoBaseClient, type QueryParams, type QueryResult, type RangeCondition, TableClient, type TypeGenerationResult, between, createClient, daysAgo, hoursAgo, lastDays, minutesAgo, since, thisMonth, thisYear, today, until };
|
|
540
|
+
export { type ApiError, type ClientConfig, type FieldType, type IndexDefinition, PotatoBaseClient, type QueryParams, type QueryResult, type RangeCondition, SchemaClient, type SchemaOperationResult, TableClient, type TableInfo, type TableSchema, type TypeGenerationResult, between, createClient, daysAgo, hoursAgo, lastDays, minutesAgo, since, thisMonth, thisYear, today, until };
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
PotatoBaseClient: () => PotatoBaseClient,
|
|
24
|
+
SchemaClient: () => SchemaClient,
|
|
24
25
|
TableClient: () => TableClient,
|
|
25
26
|
between: () => between,
|
|
26
27
|
createClient: () => createClient,
|
|
@@ -301,6 +302,138 @@ var TableClient = class {
|
|
|
301
302
|
}
|
|
302
303
|
};
|
|
303
304
|
|
|
305
|
+
// src/schema-client.ts
|
|
306
|
+
var SchemaClient = class {
|
|
307
|
+
constructor(apiUrl, apiKey, projectId, debug = false) {
|
|
308
|
+
this.apiUrl = apiUrl;
|
|
309
|
+
this.apiKey = apiKey;
|
|
310
|
+
this.projectId = projectId;
|
|
311
|
+
this.debug = debug;
|
|
312
|
+
}
|
|
313
|
+
async callAPI(procedure, input, method = "GET") {
|
|
314
|
+
const url = `${this.apiUrl}/${procedure}`;
|
|
315
|
+
if (method === "GET") {
|
|
316
|
+
const params = new URLSearchParams({
|
|
317
|
+
input: JSON.stringify(input)
|
|
318
|
+
});
|
|
319
|
+
const response = await fetch(`${url}?${params}`, {
|
|
320
|
+
method: "GET",
|
|
321
|
+
headers: {
|
|
322
|
+
"x-api-key": this.apiKey
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
if (!response.ok) {
|
|
326
|
+
const error = await response.text();
|
|
327
|
+
throw new Error(`API error: ${error}`);
|
|
328
|
+
}
|
|
329
|
+
const data = await response.json();
|
|
330
|
+
return data.result?.data || data;
|
|
331
|
+
} else {
|
|
332
|
+
const response = await fetch(url, {
|
|
333
|
+
method: "POST",
|
|
334
|
+
headers: {
|
|
335
|
+
"Content-Type": "application/json",
|
|
336
|
+
"x-api-key": this.apiKey
|
|
337
|
+
},
|
|
338
|
+
body: JSON.stringify(input)
|
|
339
|
+
});
|
|
340
|
+
if (!response.ok) {
|
|
341
|
+
const error = await response.text();
|
|
342
|
+
throw new Error(`API error: ${error}`);
|
|
343
|
+
}
|
|
344
|
+
const data = await response.json();
|
|
345
|
+
return data.result?.data || data;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* List all tables in the project
|
|
350
|
+
*/
|
|
351
|
+
async listTables() {
|
|
352
|
+
if (this.debug) {
|
|
353
|
+
console.log("[SchemaClient] Listing tables");
|
|
354
|
+
}
|
|
355
|
+
return this.callAPI("tables.list", this.projectId);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Get schema for a specific table
|
|
359
|
+
*/
|
|
360
|
+
async getSchema(tableName) {
|
|
361
|
+
if (this.debug) {
|
|
362
|
+
console.log(`[SchemaClient] Getting schema for: ${tableName}`);
|
|
363
|
+
}
|
|
364
|
+
const result = await this.callAPI("tables.getSchema", {
|
|
365
|
+
projectId: this.projectId,
|
|
366
|
+
tableName
|
|
367
|
+
});
|
|
368
|
+
return result || null;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Create a new table with the given schema
|
|
372
|
+
*/
|
|
373
|
+
async createTable(tableName, schema) {
|
|
374
|
+
if (this.debug) {
|
|
375
|
+
console.log(`[SchemaClient] Creating table: ${tableName}`);
|
|
376
|
+
}
|
|
377
|
+
return this.callAPI("tables.create", {
|
|
378
|
+
projectId: this.projectId,
|
|
379
|
+
tableName,
|
|
380
|
+
schema
|
|
381
|
+
}, "POST");
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Add a field to an existing table
|
|
385
|
+
*/
|
|
386
|
+
async addField(tableName, fieldName, fieldType) {
|
|
387
|
+
if (this.debug) {
|
|
388
|
+
console.log(`[SchemaClient] Adding field "${fieldName}" (${fieldType}) to ${tableName}`);
|
|
389
|
+
}
|
|
390
|
+
return this.callAPI("tables.addField", {
|
|
391
|
+
projectId: this.projectId,
|
|
392
|
+
tableName,
|
|
393
|
+
fieldName,
|
|
394
|
+
fieldType
|
|
395
|
+
}, "POST");
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Remove a field from an existing table
|
|
399
|
+
*/
|
|
400
|
+
async removeField(tableName, fieldName) {
|
|
401
|
+
if (this.debug) {
|
|
402
|
+
console.log(`[SchemaClient] Removing field "${fieldName}" from ${tableName}`);
|
|
403
|
+
}
|
|
404
|
+
return this.callAPI("tables.removeField", {
|
|
405
|
+
projectId: this.projectId,
|
|
406
|
+
tableName,
|
|
407
|
+
fieldName
|
|
408
|
+
}, "POST");
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Replace the entire schema of a table
|
|
412
|
+
*/
|
|
413
|
+
async replaceSchema(tableName, schema) {
|
|
414
|
+
if (this.debug) {
|
|
415
|
+
console.log(`[SchemaClient] Replacing schema for: ${tableName}`);
|
|
416
|
+
}
|
|
417
|
+
return this.callAPI("tables.updateSchema", {
|
|
418
|
+
projectId: this.projectId,
|
|
419
|
+
tableName,
|
|
420
|
+
schema
|
|
421
|
+
}, "POST");
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Delete a table and all its records
|
|
425
|
+
*/
|
|
426
|
+
async deleteTable(tableName) {
|
|
427
|
+
if (this.debug) {
|
|
428
|
+
console.log(`[SchemaClient] Deleting table: ${tableName}`);
|
|
429
|
+
}
|
|
430
|
+
return this.callAPI("tables.delete", {
|
|
431
|
+
projectId: this.projectId,
|
|
432
|
+
tableName
|
|
433
|
+
}, "POST");
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
304
437
|
// src/helpers.ts
|
|
305
438
|
function daysAgo(days) {
|
|
306
439
|
const date = new Date(Date.now() - days * 24 * 60 * 60 * 1e3);
|
|
@@ -377,7 +510,7 @@ var PotatoBaseClient = class {
|
|
|
377
510
|
}
|
|
378
511
|
this.config = {
|
|
379
512
|
...config,
|
|
380
|
-
apiUrl: config.apiUrl || "https://api.potatobase.
|
|
513
|
+
apiUrl: config.apiUrl || "https://api.potatobase.dev",
|
|
381
514
|
debug: config.debug || false
|
|
382
515
|
};
|
|
383
516
|
this.tableClients = /* @__PURE__ */ new Map();
|
|
@@ -466,6 +599,46 @@ var PotatoBaseClient = class {
|
|
|
466
599
|
throw new Error(`Failed to generate types: ${error.message}`);
|
|
467
600
|
}
|
|
468
601
|
}
|
|
602
|
+
/**
|
|
603
|
+
* Access the schema management client
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* const tables = await pb.schema.listTables();
|
|
608
|
+
* await pb.schema.addField('products', 'discount', 'number');
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
get schema() {
|
|
612
|
+
if (!this._schemaClient) {
|
|
613
|
+
this._schemaClient = new SchemaClient(
|
|
614
|
+
this.config.apiUrl,
|
|
615
|
+
this.config.apiKey,
|
|
616
|
+
this.config.projectId,
|
|
617
|
+
this.config.debug
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
return this._schemaClient;
|
|
621
|
+
}
|
|
622
|
+
/** Create a new table */
|
|
623
|
+
async createTable(tableName, schema) {
|
|
624
|
+
return this.schema.createTable(tableName, schema);
|
|
625
|
+
}
|
|
626
|
+
/** Add a field to an existing table */
|
|
627
|
+
async addField(tableName, fieldName, fieldType) {
|
|
628
|
+
return this.schema.addField(tableName, fieldName, fieldType);
|
|
629
|
+
}
|
|
630
|
+
/** Remove a field from an existing table */
|
|
631
|
+
async removeField(tableName, fieldName) {
|
|
632
|
+
return this.schema.removeField(tableName, fieldName);
|
|
633
|
+
}
|
|
634
|
+
/** List all tables in the project */
|
|
635
|
+
async listTables() {
|
|
636
|
+
return this.schema.listTables();
|
|
637
|
+
}
|
|
638
|
+
/** Get schema for a specific table */
|
|
639
|
+
async getSchema(tableName) {
|
|
640
|
+
return this.schema.getSchema(tableName);
|
|
641
|
+
}
|
|
469
642
|
/**
|
|
470
643
|
* Get the project ID
|
|
471
644
|
*/
|
|
@@ -502,6 +675,7 @@ function createClient(config) {
|
|
|
502
675
|
// Annotate the CommonJS export names for ESM import in node:
|
|
503
676
|
0 && (module.exports = {
|
|
504
677
|
PotatoBaseClient,
|
|
678
|
+
SchemaClient,
|
|
505
679
|
TableClient,
|
|
506
680
|
between,
|
|
507
681
|
createClient,
|
package/dist/index.mjs
CHANGED
|
@@ -263,6 +263,138 @@ var TableClient = class {
|
|
|
263
263
|
}
|
|
264
264
|
};
|
|
265
265
|
|
|
266
|
+
// src/schema-client.ts
|
|
267
|
+
var SchemaClient = class {
|
|
268
|
+
constructor(apiUrl, apiKey, projectId, debug = false) {
|
|
269
|
+
this.apiUrl = apiUrl;
|
|
270
|
+
this.apiKey = apiKey;
|
|
271
|
+
this.projectId = projectId;
|
|
272
|
+
this.debug = debug;
|
|
273
|
+
}
|
|
274
|
+
async callAPI(procedure, input, method = "GET") {
|
|
275
|
+
const url = `${this.apiUrl}/${procedure}`;
|
|
276
|
+
if (method === "GET") {
|
|
277
|
+
const params = new URLSearchParams({
|
|
278
|
+
input: JSON.stringify(input)
|
|
279
|
+
});
|
|
280
|
+
const response = await fetch(`${url}?${params}`, {
|
|
281
|
+
method: "GET",
|
|
282
|
+
headers: {
|
|
283
|
+
"x-api-key": this.apiKey
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
if (!response.ok) {
|
|
287
|
+
const error = await response.text();
|
|
288
|
+
throw new Error(`API error: ${error}`);
|
|
289
|
+
}
|
|
290
|
+
const data = await response.json();
|
|
291
|
+
return data.result?.data || data;
|
|
292
|
+
} else {
|
|
293
|
+
const response = await fetch(url, {
|
|
294
|
+
method: "POST",
|
|
295
|
+
headers: {
|
|
296
|
+
"Content-Type": "application/json",
|
|
297
|
+
"x-api-key": this.apiKey
|
|
298
|
+
},
|
|
299
|
+
body: JSON.stringify(input)
|
|
300
|
+
});
|
|
301
|
+
if (!response.ok) {
|
|
302
|
+
const error = await response.text();
|
|
303
|
+
throw new Error(`API error: ${error}`);
|
|
304
|
+
}
|
|
305
|
+
const data = await response.json();
|
|
306
|
+
return data.result?.data || data;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* List all tables in the project
|
|
311
|
+
*/
|
|
312
|
+
async listTables() {
|
|
313
|
+
if (this.debug) {
|
|
314
|
+
console.log("[SchemaClient] Listing tables");
|
|
315
|
+
}
|
|
316
|
+
return this.callAPI("tables.list", this.projectId);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Get schema for a specific table
|
|
320
|
+
*/
|
|
321
|
+
async getSchema(tableName) {
|
|
322
|
+
if (this.debug) {
|
|
323
|
+
console.log(`[SchemaClient] Getting schema for: ${tableName}`);
|
|
324
|
+
}
|
|
325
|
+
const result = await this.callAPI("tables.getSchema", {
|
|
326
|
+
projectId: this.projectId,
|
|
327
|
+
tableName
|
|
328
|
+
});
|
|
329
|
+
return result || null;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Create a new table with the given schema
|
|
333
|
+
*/
|
|
334
|
+
async createTable(tableName, schema) {
|
|
335
|
+
if (this.debug) {
|
|
336
|
+
console.log(`[SchemaClient] Creating table: ${tableName}`);
|
|
337
|
+
}
|
|
338
|
+
return this.callAPI("tables.create", {
|
|
339
|
+
projectId: this.projectId,
|
|
340
|
+
tableName,
|
|
341
|
+
schema
|
|
342
|
+
}, "POST");
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Add a field to an existing table
|
|
346
|
+
*/
|
|
347
|
+
async addField(tableName, fieldName, fieldType) {
|
|
348
|
+
if (this.debug) {
|
|
349
|
+
console.log(`[SchemaClient] Adding field "${fieldName}" (${fieldType}) to ${tableName}`);
|
|
350
|
+
}
|
|
351
|
+
return this.callAPI("tables.addField", {
|
|
352
|
+
projectId: this.projectId,
|
|
353
|
+
tableName,
|
|
354
|
+
fieldName,
|
|
355
|
+
fieldType
|
|
356
|
+
}, "POST");
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Remove a field from an existing table
|
|
360
|
+
*/
|
|
361
|
+
async removeField(tableName, fieldName) {
|
|
362
|
+
if (this.debug) {
|
|
363
|
+
console.log(`[SchemaClient] Removing field "${fieldName}" from ${tableName}`);
|
|
364
|
+
}
|
|
365
|
+
return this.callAPI("tables.removeField", {
|
|
366
|
+
projectId: this.projectId,
|
|
367
|
+
tableName,
|
|
368
|
+
fieldName
|
|
369
|
+
}, "POST");
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Replace the entire schema of a table
|
|
373
|
+
*/
|
|
374
|
+
async replaceSchema(tableName, schema) {
|
|
375
|
+
if (this.debug) {
|
|
376
|
+
console.log(`[SchemaClient] Replacing schema for: ${tableName}`);
|
|
377
|
+
}
|
|
378
|
+
return this.callAPI("tables.updateSchema", {
|
|
379
|
+
projectId: this.projectId,
|
|
380
|
+
tableName,
|
|
381
|
+
schema
|
|
382
|
+
}, "POST");
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Delete a table and all its records
|
|
386
|
+
*/
|
|
387
|
+
async deleteTable(tableName) {
|
|
388
|
+
if (this.debug) {
|
|
389
|
+
console.log(`[SchemaClient] Deleting table: ${tableName}`);
|
|
390
|
+
}
|
|
391
|
+
return this.callAPI("tables.delete", {
|
|
392
|
+
projectId: this.projectId,
|
|
393
|
+
tableName
|
|
394
|
+
}, "POST");
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
266
398
|
// src/helpers.ts
|
|
267
399
|
function daysAgo(days) {
|
|
268
400
|
const date = new Date(Date.now() - days * 24 * 60 * 60 * 1e3);
|
|
@@ -339,7 +471,7 @@ var PotatoBaseClient = class {
|
|
|
339
471
|
}
|
|
340
472
|
this.config = {
|
|
341
473
|
...config,
|
|
342
|
-
apiUrl: config.apiUrl || "https://api.potatobase.
|
|
474
|
+
apiUrl: config.apiUrl || "https://api.potatobase.dev",
|
|
343
475
|
debug: config.debug || false
|
|
344
476
|
};
|
|
345
477
|
this.tableClients = /* @__PURE__ */ new Map();
|
|
@@ -428,6 +560,46 @@ var PotatoBaseClient = class {
|
|
|
428
560
|
throw new Error(`Failed to generate types: ${error.message}`);
|
|
429
561
|
}
|
|
430
562
|
}
|
|
563
|
+
/**
|
|
564
|
+
* Access the schema management client
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```typescript
|
|
568
|
+
* const tables = await pb.schema.listTables();
|
|
569
|
+
* await pb.schema.addField('products', 'discount', 'number');
|
|
570
|
+
* ```
|
|
571
|
+
*/
|
|
572
|
+
get schema() {
|
|
573
|
+
if (!this._schemaClient) {
|
|
574
|
+
this._schemaClient = new SchemaClient(
|
|
575
|
+
this.config.apiUrl,
|
|
576
|
+
this.config.apiKey,
|
|
577
|
+
this.config.projectId,
|
|
578
|
+
this.config.debug
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
return this._schemaClient;
|
|
582
|
+
}
|
|
583
|
+
/** Create a new table */
|
|
584
|
+
async createTable(tableName, schema) {
|
|
585
|
+
return this.schema.createTable(tableName, schema);
|
|
586
|
+
}
|
|
587
|
+
/** Add a field to an existing table */
|
|
588
|
+
async addField(tableName, fieldName, fieldType) {
|
|
589
|
+
return this.schema.addField(tableName, fieldName, fieldType);
|
|
590
|
+
}
|
|
591
|
+
/** Remove a field from an existing table */
|
|
592
|
+
async removeField(tableName, fieldName) {
|
|
593
|
+
return this.schema.removeField(tableName, fieldName);
|
|
594
|
+
}
|
|
595
|
+
/** List all tables in the project */
|
|
596
|
+
async listTables() {
|
|
597
|
+
return this.schema.listTables();
|
|
598
|
+
}
|
|
599
|
+
/** Get schema for a specific table */
|
|
600
|
+
async getSchema(tableName) {
|
|
601
|
+
return this.schema.getSchema(tableName);
|
|
602
|
+
}
|
|
431
603
|
/**
|
|
432
604
|
* Get the project ID
|
|
433
605
|
*/
|
|
@@ -463,6 +635,7 @@ function createClient(config) {
|
|
|
463
635
|
}
|
|
464
636
|
export {
|
|
465
637
|
PotatoBaseClient,
|
|
638
|
+
SchemaClient,
|
|
466
639
|
TableClient,
|
|
467
640
|
between,
|
|
468
641
|
createClient,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ponxa/potatobase-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Official JavaScript/TypeScript client for PotatoBase",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"type": "git",
|
|
32
32
|
"url": "git+https://github.com/ponxa/potatobase-client.git"
|
|
33
33
|
},
|
|
34
|
-
"homepage": "https://potatobase.
|
|
34
|
+
"homepage": "https://potatobase.dev",
|
|
35
35
|
"bugs": {
|
|
36
36
|
"url": "https://github.com/ponxa/potatobase-client/issues"
|
|
37
37
|
},
|