@tspvivek/baasix-sdk 0.1.0-alpha.1 → 0.1.0-alpha.3

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.
@@ -1,4 +1,4 @@
1
- import { H as HttpClient, e as PaginatedResponse, l as SchemaInfo, m as SchemaDefinition, n as RelationshipDefinition, I as IndexDefinition } from '../client-aXK_gEyr.cjs';
1
+ import { H as HttpClient, S as Sort, e as PaginatedResponse, l as SchemaInfo, m as SchemaDefinition, n as RelationshipDefinition, I as IndexDefinition } from '../client-aXK_gEyr.cjs';
2
2
  import '../types-BdjsGANq.cjs';
3
3
 
4
4
  interface SchemasModuleConfig {
@@ -42,9 +42,18 @@ declare class SchemasModule {
42
42
  * ```typescript
43
43
  * const { data } = await baasix.schemas.find();
44
44
  * console.log(data.map(s => s.collectionName));
45
+ *
46
+ * // With pagination
47
+ * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });
45
48
  * ```
46
49
  */
47
- find(): Promise<PaginatedResponse<SchemaInfo>>;
50
+ find(params?: {
51
+ page?: number;
52
+ limit?: number;
53
+ sort?: Sort;
54
+ search?: string;
55
+ filter?: Record<string, unknown>;
56
+ }): Promise<PaginatedResponse<SchemaInfo>>;
48
57
  /**
49
58
  * Get schema for a specific collection
50
59
  *
@@ -163,6 +172,18 @@ declare class SchemasModule {
163
172
  * ```
164
173
  */
165
174
  deleteRelationship(collection: string, relationshipName: string): Promise<void>;
175
+ /**
176
+ * Update a relationship
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * await baasix.schemas.updateRelationship('posts', 'author', {
181
+ * alias: 'authoredPosts',
182
+ * onDelete: 'CASCADE'
183
+ * });
184
+ * ```
185
+ */
186
+ updateRelationship(collection: string, relationshipName: string, data: Partial<RelationshipDefinition>): Promise<void>;
166
187
  /**
167
188
  * Create an index on a collection
168
189
  *
@@ -1,4 +1,4 @@
1
- import { H as HttpClient, e as PaginatedResponse, l as SchemaInfo, m as SchemaDefinition, n as RelationshipDefinition, I as IndexDefinition } from '../client-CzF9B60b.js';
1
+ import { H as HttpClient, S as Sort, e as PaginatedResponse, l as SchemaInfo, m as SchemaDefinition, n as RelationshipDefinition, I as IndexDefinition } from '../client-CzF9B60b.js';
2
2
  import '../types-BdjsGANq.js';
3
3
 
4
4
  interface SchemasModuleConfig {
@@ -42,9 +42,18 @@ declare class SchemasModule {
42
42
  * ```typescript
43
43
  * const { data } = await baasix.schemas.find();
44
44
  * console.log(data.map(s => s.collectionName));
45
+ *
46
+ * // With pagination
47
+ * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });
45
48
  * ```
46
49
  */
47
- find(): Promise<PaginatedResponse<SchemaInfo>>;
50
+ find(params?: {
51
+ page?: number;
52
+ limit?: number;
53
+ sort?: Sort;
54
+ search?: string;
55
+ filter?: Record<string, unknown>;
56
+ }): Promise<PaginatedResponse<SchemaInfo>>;
48
57
  /**
49
58
  * Get schema for a specific collection
50
59
  *
@@ -163,6 +172,18 @@ declare class SchemasModule {
163
172
  * ```
164
173
  */
165
174
  deleteRelationship(collection: string, relationshipName: string): Promise<void>;
175
+ /**
176
+ * Update a relationship
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * await baasix.schemas.updateRelationship('posts', 'author', {
181
+ * alias: 'authoredPosts',
182
+ * onDelete: 'CASCADE'
183
+ * });
184
+ * ```
185
+ */
186
+ updateRelationship(collection: string, relationshipName: string, data: Partial<RelationshipDefinition>): Promise<void>;
166
187
  /**
167
188
  * Create an index on a collection
168
189
  *
@@ -1,3 +1,32 @@
1
+ // src/utils/sort.ts
2
+ function normalizeSort(sort) {
3
+ if (!sort) return void 0;
4
+ if (typeof sort === "string") {
5
+ return sort;
6
+ }
7
+ if (Array.isArray(sort)) {
8
+ if (sort.length === 0) return void 0;
9
+ if (typeof sort[0] === "object" && "column" in sort[0]) {
10
+ return sort.map((s) => `${s.column}:${s.order.toLowerCase()}`).join(",");
11
+ }
12
+ return sort.map((s) => {
13
+ if (s.startsWith("-")) {
14
+ return `${s.substring(1)}:desc`;
15
+ }
16
+ if (s.includes(":")) {
17
+ return s;
18
+ }
19
+ return `${s}:asc`;
20
+ }).join(",");
21
+ }
22
+ if (typeof sort === "object") {
23
+ const entries = Object.entries(sort);
24
+ if (entries.length === 0) return void 0;
25
+ return entries.map(([field, direction]) => `${field}:${direction.toLowerCase()}`).join(",");
26
+ }
27
+ return void 0;
28
+ }
29
+
1
30
  // src/modules/schemas.ts
2
31
  var SchemasModule = class {
3
32
  client;
@@ -11,10 +40,17 @@ var SchemasModule = class {
11
40
  * ```typescript
12
41
  * const { data } = await baasix.schemas.find();
13
42
  * console.log(data.map(s => s.collectionName));
43
+ *
44
+ * // With pagination
45
+ * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });
14
46
  * ```
15
47
  */
16
- async find() {
17
- return this.client.get("/schemas");
48
+ async find(params) {
49
+ const normalizedParams = params ? {
50
+ ...params,
51
+ sort: normalizeSort(params.sort)
52
+ } : void 0;
53
+ return this.client.get("/schemas", { params: normalizedParams });
18
54
  }
19
55
  /**
20
56
  * Get schema for a specific collection
@@ -157,6 +193,23 @@ var SchemasModule = class {
157
193
  `/schemas/${collection}/relationships/${relationshipName}`
158
194
  );
159
195
  }
196
+ /**
197
+ * Update a relationship
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * await baasix.schemas.updateRelationship('posts', 'author', {
202
+ * alias: 'authoredPosts',
203
+ * onDelete: 'CASCADE'
204
+ * });
205
+ * ```
206
+ */
207
+ async updateRelationship(collection, relationshipName, data) {
208
+ await this.client.patch(
209
+ `/schemas/${collection}/relationships/${relationshipName}`,
210
+ data
211
+ );
212
+ }
160
213
  /**
161
214
  * Create an index on a collection
162
215
  *
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/modules/schemas.ts"],"names":[],"mappings":";AAyCO,IAAM,gBAAN,MAAoB;AAAA,EACjB,MAAA;AAAA,EAER,YAAY,MAAA,EAA6B;AACvC,IAAA,IAAA,CAAK,SAAS,MAAA,C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AA,MAAM,KAAK,MAAA,CAAO,IAAA,CAAK,iBAAA,EAAmB,EAAE,SAAS,CAAA;AAAA,EACvD;AACF","file":"schemas.js","sourcesContent":["import type { HttpClient } from \"../client\";\nimport type {\n IndexDefinition,\n PaginatedResponse,\n RelationshipDefinition,\n SchemaDefinition,\n SchemaInfo,\n} from \"../types\";\n\nexport interface SchemasModuleConfig {\n client: HttpClient;\n}\n\n/**\n * Schemas module for managing database schemas, relationships, and indexes.\n *\n * @example\n * ```typescript\n * // Create a new collection\n * await baasix.schemas.create({\n * collectionName: 'products',\n * schema: {\n * name: 'Product',\n * timestamps: true,\n * fields: {\n * id: { type: 'UUID', primaryKey: true, defaultValue: { type: 'UUIDV4' } },\n * name: { type: 'String', allowNull: false },\n * price: { type: 'Decimal', values: { precision: 10, scale: 2 } }\n * }\n * }\n * });\n *\n * // Add a relationship\n * await baasix.schemas.createRelationship('products', {\n * type: 'M2O',\n * target: 'categories',\n * name: 'category',\n * alias: 'products'\n * });\n * ```\n */\nexport class SchemasModule {\n private client: HttpClient;\n\n constructor(config: SchemasModuleConfig) {\n this.client = config.client;\n }\n\n /**\n * List all schemas\n *\n * @example\n * ```typescript\n * const { data } = await baasix.schemas.find();\n * console.log(data.map(s => s.collectionName));\n * ```\n */\n async find(): Promise<PaginatedResponse<SchemaInfo>> {\n return this.client.get<PaginatedResponse<SchemaInfo>>(\"/schemas\");\n }\n\n /**\n * Get schema for a specific collection\n *\n * @example\n * ```typescript\n * const schema = await baasix.schemas.findOne('products');\n * console.log(schema.fields);\n * ```\n */\n async findOne(collection: string): Promise<SchemaInfo> {\n const response = await this.client.get<{ data: SchemaInfo }>(\n `/schemas/${collection}`\n );\n return response.data;\n }\n\n /**\n * Create a new collection/schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.create({\n * collectionName: 'orders',\n * schema: {\n * name: 'Order',\n * timestamps: true,\n * paranoid: true,\n * fields: {\n * id: {\n * type: 'UUID',\n * primaryKey: true,\n * defaultValue: { type: 'UUIDV4' }\n * },\n * orderNumber: {\n * type: 'String',\n * allowNull: false,\n * unique: true\n * },\n * total: {\n * type: 'Decimal',\n * values: { precision: 10, scale: 2 },\n * allowNull: false,\n * defaultValue: 0\n * },\n * status: {\n * type: 'String',\n * allowNull: false,\n * defaultValue: 'pending'\n * },\n * items: {\n * type: 'JSONB',\n * allowNull: true,\n * defaultValue: []\n * }\n * }\n * }\n * });\n * ```\n */\n async create(data: {\n collectionName: string;\n schema: SchemaDefinition;\n }): Promise<SchemaInfo> {\n const response = await this.client.post<{ data: SchemaInfo }>(\n \"/schemas\",\n data\n );\n return response.data;\n }\n\n /**\n * Update an existing schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.update('products', {\n * schema: {\n * name: 'Product',\n * timestamps: true,\n * fields: {\n * // Updated fields\n * description: { type: 'Text', allowNull: true }\n * }\n * }\n * });\n * ```\n */\n async update(\n collection: string,\n data: { schema: Partial<SchemaDefinition> }\n ): Promise<SchemaInfo> {\n const response = await this.client.patch<{ data: SchemaInfo }>(\n `/schemas/${collection}`,\n data\n );\n return response.data;\n }\n\n /**\n * Delete a schema (drops the table)\n *\n * @example\n * ```typescript\n * await baasix.schemas.delete('old_collection');\n * ```\n */\n async delete(collection: string): Promise<void> {\n await this.client.delete(`/schemas/${collection}`);\n }\n\n /**\n * Create a relationship between collections\n *\n * @example\n * ```typescript\n * // Many-to-One (BelongsTo)\n * await baasix.schemas.createRelationship('posts', {\n * type: 'M2O',\n * target: 'baasix_User',\n * name: 'author',\n * alias: 'posts'\n * });\n *\n * // Many-to-Many\n * await baasix.schemas.createRelationship('posts', {\n * type: 'M2M',\n * target: 'tags',\n * name: 'tags',\n * alias: 'posts'\n * });\n * ```\n */\n async createRelationship(\n collection: string,\n relationship: RelationshipDefinition\n ): Promise<void> {\n await this.client.post(\n `/schemas/${collection}/relationships`,\n relationship\n );\n }\n\n /**\n * Delete a relationship\n *\n * @example\n * ```typescript\n * await baasix.schemas.deleteRelationship('posts', 'author');\n * ```\n */\n async deleteRelationship(\n collection: string,\n relationshipName: string\n ): Promise<void> {\n await this.client.delete(\n `/schemas/${collection}/relationships/${relationshipName}`\n );\n }\n\n /**\n * Create an index on a collection\n *\n * @example\n * ```typescript\n * // Unique index\n * await baasix.schemas.createIndex('users', {\n * name: 'idx_users_email',\n * fields: ['email'],\n * unique: true\n * });\n *\n * // Composite index\n * await baasix.schemas.createIndex('orders', {\n * name: 'idx_orders_status_created',\n * fields: ['status', 'createdAt']\n * });\n * ```\n */\n async createIndex(\n collection: string,\n index: IndexDefinition\n ): Promise<void> {\n await this.client.post(`/schemas/${collection}/indexes`, index);\n }\n\n /**\n * Delete an index\n *\n * @example\n * ```typescript\n * await baasix.schemas.deleteIndex('users', 'idx_users_email');\n * ```\n */\n async deleteIndex(collection: string, indexName: string): Promise<void> {\n await this.client.delete(`/schemas/${collection}/indexes/${indexName}`);\n }\n\n /**\n * Add a field to an existing schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.addField('products', 'rating', {\n * type: 'Decimal',\n * values: { precision: 3, scale: 2 },\n * allowNull: true,\n * defaultValue: 0\n * });\n * ```\n */\n async addField(\n collection: string,\n fieldName: string,\n fieldDefinition: SchemaDefinition[\"fields\"][string]\n ): Promise<SchemaInfo> {\n const currentSchema = await this.findOne(collection);\n const updatedFields = {\n ...currentSchema.schema.fields,\n [fieldName]: fieldDefinition,\n };\n\n return this.update(collection, {\n schema: {\n ...currentSchema.schema,\n fields: updatedFields,\n },\n });\n }\n\n /**\n * Remove a field from a schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.removeField('products', 'deprecated_field');\n * ```\n */\n async removeField(\n collection: string,\n fieldName: string\n ): Promise<SchemaInfo> {\n const currentSchema = await this.findOne(collection);\n const { [fieldName]: _, ...remainingFields } = currentSchema.schema.fields;\n\n return this.update(collection, {\n schema: {\n ...currentSchema.schema,\n fields: remainingFields,\n },\n });\n }\n\n /**\n * Export all schemas as JSON\n *\n * @example\n * ```typescript\n * const schemas = await baasix.schemas.export();\n * // Save to file for backup\n * ```\n */\n async export(): Promise<SchemaInfo[]> {\n const response = await this.client.get<{ data: SchemaInfo[] }>(\n \"/schemas/export\"\n );\n return response.data;\n }\n\n /**\n * Import schemas from JSON\n *\n * @example\n * ```typescript\n * await baasix.schemas.import(savedSchemas);\n * ```\n */\n async import(schemas: SchemaInfo[]): Promise<void> {\n await this.client.post(\"/schemas/import\", { schemas });\n }\n}\n\n// Re-export types\nexport type {\n IndexDefinition,\n RelationshipDefinition,\n SchemaDefinition,\n SchemaInfo,\n};\n"]}
1
+ {"version":3,"sources":["../../src/utils/sort.ts","../../src/modules/schemas.ts"],"names":[],"mappings":";AAkBO,SAAS,cAAc,IAAA,EAA4C;AACxE,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAGlB,EAAA,IAAI,OAAO,SAAS,QAAA,EAAU;AAC5B,IAAA,OAAO,IAAA;AAAA,EACT;AAGA,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA,EAAG;AACvB,IAAA,IAAI,IAAA,CAAK,MAAA,KAAW,CAAA,EAAG,OAAO,MAAA;AAG9B,IAAA,IAAI,OAAO,KAAK,CAAC,CAAA,KAAM,YAAY,QAAA,IAAY,IAAA,CAAK,CAAC,CAAA,EAAG;AACtD,MAAA,OAAQ,IAAA,CACL,GAAA,CAAI,CAAC,CAAA,KAAM,GAAG,CAAA,CAAE,MAAM,CAAA,CAAA,EAAI,CAAA,CAAE,MAAM,WAAA,EAAa,CAAA,CAAE,CAAA,CACjD,KAAK,GAAG,CAAA;AAAA,IACb;AAGA,IAAA,OAAQ,IAAA,CACL,GAAA,CAAI,CAAC,CAAA,KAAM;AACV,MAAA,IAAI,CAAA,CAAE,UAAA,CAAW,GAAG,CAAA,EAAG;AACrB,QAAA,OAAO,CAAA,EAAG,CAAA,CAAE,SAAA,CAAU,CAAC,CAAC,CAAA,KAAA,CAAA;AAAA,MAC1B;AACA,MAAA,IAAI,CAAA,CAAE,QAAA,CAAS,GAAG,C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sourcesContent":["import type { Sort, SortDirection } from \"../types\";\n\n/**\n * Normalize sort parameter to string format \"field:direction,field2:direction2\"\n * This is the format expected by the API for query string parameters.\n * \n * @example\n * ```typescript\n * normalizeSort({ name: 'asc', createdAt: 'desc' })\n * // Returns: \"name:asc,createdAt:desc\"\n * \n * normalizeSort(['-createdAt', 'name'])\n * // Returns: \"createdAt:desc,name:asc\"\n * \n * normalizeSort('name:asc')\n * // Returns: \"name:asc\"\n * ```\n */\nexport function normalizeSort(sort: Sort | undefined): string | undefined {\n if (!sort) return undefined;\n\n // Already a string\n if (typeof sort === \"string\") {\n return sort;\n }\n\n // Array of strings like ['-createdAt', 'name']\n if (Array.isArray(sort)) {\n if (sort.length === 0) return undefined;\n \n // Check if it's array of objects { column, order }\n if (typeof sort[0] === \"object\" && \"column\" in sort[0]) {\n return (sort as { column: string; order: SortDirection }[])\n .map((s) => `${s.column}:${s.order.toLowerCase()}`)\n .join(\",\");\n }\n \n // Array of strings like ['-name', 'createdAt']\n return (sort as string[])\n .map((s) => {\n if (s.startsWith(\"-\")) {\n return `${s.substring(1)}:desc`;\n }\n if (s.includes(\":\")) {\n return s;\n }\n return `${s}:asc`;\n })\n .join(\",\");\n }\n\n // Object like { name: 'asc', createdAt: 'desc' }\n if (typeof sort === \"object\") {\n const entries = Object.entries(sort as Record<string, SortDirection>);\n if (entries.length === 0) return undefined;\n \n return entries\n .map(([field, direction]) => `${field}:${direction.toLowerCase()}`)\n .join(\",\");\n }\n\n return undefined;\n}\n","import type { HttpClient } from \"../client\";\nimport type {\n IndexDefinition,\n PaginatedResponse,\n RelationshipDefinition,\n SchemaDefinition,\n SchemaInfo,\n Sort,\n} from \"../types\";\nimport { normalizeSort } from \"../utils/sort\";\n\nexport interface SchemasModuleConfig {\n client: HttpClient;\n}\n\n/**\n * Schemas module for managing database schemas, relationships, and indexes.\n *\n * @example\n * ```typescript\n * // Create a new collection\n * await baasix.schemas.create({\n * collectionName: 'products',\n * schema: {\n * name: 'Product',\n * timestamps: true,\n * fields: {\n * id: { type: 'UUID', primaryKey: true, defaultValue: { type: 'UUIDV4' } },\n * name: { type: 'String', allowNull: false },\n * price: { type: 'Decimal', values: { precision: 10, scale: 2 } }\n * }\n * }\n * });\n *\n * // Add a relationship\n * await baasix.schemas.createRelationship('products', {\n * type: 'M2O',\n * target: 'categories',\n * name: 'category',\n * alias: 'products'\n * });\n * ```\n */\nexport class SchemasModule {\n private client: HttpClient;\n\n constructor(config: SchemasModuleConfig) {\n this.client = config.client;\n }\n\n /**\n * List all schemas\n *\n * @example\n * ```typescript\n * const { data } = await baasix.schemas.find();\n * console.log(data.map(s => s.collectionName));\n * \n * // With pagination\n * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });\n * ```\n */\n async find(params?: {\n page?: number;\n limit?: number;\n sort?: Sort;\n search?: string;\n filter?: Record<string, unknown>;\n }): Promise<PaginatedResponse<SchemaInfo>> {\n const normalizedParams = params ? {\n ...params,\n sort: normalizeSort(params.sort),\n } : undefined;\n return this.client.get<PaginatedResponse<SchemaInfo>>(\"/schemas\", { params: normalizedParams });\n }\n\n /**\n * Get schema for a specific collection\n *\n * @example\n * ```typescript\n * const schema = await baasix.schemas.findOne('products');\n * console.log(schema.fields);\n * ```\n */\n async findOne(collection: string): Promise<SchemaInfo> {\n const response = await this.client.get<{ data: SchemaInfo }>(\n `/schemas/${collection}`\n );\n return response.data;\n }\n\n /**\n * Create a new collection/schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.create({\n * collectionName: 'orders',\n * schema: {\n * name: 'Order',\n * timestamps: true,\n * paranoid: true,\n * fields: {\n * id: {\n * type: 'UUID',\n * primaryKey: true,\n * defaultValue: { type: 'UUIDV4' }\n * },\n * orderNumber: {\n * type: 'String',\n * allowNull: false,\n * unique: true\n * },\n * total: {\n * type: 'Decimal',\n * values: { precision: 10, scale: 2 },\n * allowNull: false,\n * defaultValue: 0\n * },\n * status: {\n * type: 'String',\n * allowNull: false,\n * defaultValue: 'pending'\n * },\n * items: {\n * type: 'JSONB',\n * allowNull: true,\n * defaultValue: []\n * }\n * }\n * }\n * });\n * ```\n */\n async create(data: {\n collectionName: string;\n schema: SchemaDefinition;\n }): Promise<SchemaInfo> {\n const response = await this.client.post<{ data: SchemaInfo }>(\n \"/schemas\",\n data\n );\n return response.data;\n }\n\n /**\n * Update an existing schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.update('products', {\n * schema: {\n * name: 'Product',\n * timestamps: true,\n * fields: {\n * // Updated fields\n * description: { type: 'Text', allowNull: true }\n * }\n * }\n * });\n * ```\n */\n async update(\n collection: string,\n data: { schema: Partial<SchemaDefinition> }\n ): Promise<SchemaInfo> {\n const response = await this.client.patch<{ data: SchemaInfo }>(\n `/schemas/${collection}`,\n data\n );\n return response.data;\n }\n\n /**\n * Delete a schema (drops the table)\n *\n * @example\n * ```typescript\n * await baasix.schemas.delete('old_collection');\n * ```\n */\n async delete(collection: string): Promise<void> {\n await this.client.delete(`/schemas/${collection}`);\n }\n\n /**\n * Create a relationship between collections\n *\n * @example\n * ```typescript\n * // Many-to-One (BelongsTo)\n * await baasix.schemas.createRelationship('posts', {\n * type: 'M2O',\n * target: 'baasix_User',\n * name: 'author',\n * alias: 'posts'\n * });\n *\n * // Many-to-Many\n * await baasix.schemas.createRelationship('posts', {\n * type: 'M2M',\n * target: 'tags',\n * name: 'tags',\n * alias: 'posts'\n * });\n * ```\n */\n async createRelationship(\n collection: string,\n relationship: RelationshipDefinition\n ): Promise<void> {\n await this.client.post(\n `/schemas/${collection}/relationships`,\n relationship\n );\n }\n\n /**\n * Delete a relationship\n *\n * @example\n * ```typescript\n * await baasix.schemas.deleteRelationship('posts', 'author');\n * ```\n */\n async deleteRelationship(\n collection: string,\n relationshipName: string\n ): Promise<void> {\n await this.client.delete(\n `/schemas/${collection}/relationships/${relationshipName}`\n );\n }\n\n /**\n * Update a relationship\n *\n * @example\n * ```typescript\n * await baasix.schemas.updateRelationship('posts', 'author', {\n * alias: 'authoredPosts',\n * onDelete: 'CASCADE'\n * });\n * ```\n */\n async updateRelationship(\n collection: string,\n relationshipName: string,\n data: Partial<RelationshipDefinition>\n ): Promise<void> {\n await this.client.patch(\n `/schemas/${collection}/relationships/${relationshipName}`,\n data\n );\n }\n\n /**\n * Create an index on a collection\n *\n * @example\n * ```typescript\n * // Unique index\n * await baasix.schemas.createIndex('users', {\n * name: 'idx_users_email',\n * fields: ['email'],\n * unique: true\n * });\n *\n * // Composite index\n * await baasix.schemas.createIndex('orders', {\n * name: 'idx_orders_status_created',\n * fields: ['status', 'createdAt']\n * });\n * ```\n */\n async createIndex(\n collection: string,\n index: IndexDefinition\n ): Promise<void> {\n await this.client.post(`/schemas/${collection}/indexes`, index);\n }\n\n /**\n * Delete an index\n *\n * @example\n * ```typescript\n * await baasix.schemas.deleteIndex('users', 'idx_users_email');\n * ```\n */\n async deleteIndex(collection: string, indexName: string): Promise<void> {\n await this.client.delete(`/schemas/${collection}/indexes/${indexName}`);\n }\n\n /**\n * Add a field to an existing schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.addField('products', 'rating', {\n * type: 'Decimal',\n * values: { precision: 3, scale: 2 },\n * allowNull: true,\n * defaultValue: 0\n * });\n * ```\n */\n async addField(\n collection: string,\n fieldName: string,\n fieldDefinition: SchemaDefinition[\"fields\"][string]\n ): Promise<SchemaInfo> {\n const currentSchema = await this.findOne(collection);\n const updatedFields = {\n ...currentSchema.schema.fields,\n [fieldName]: fieldDefinition,\n };\n\n return this.update(collection, {\n schema: {\n ...currentSchema.schema,\n fields: updatedFields,\n },\n });\n }\n\n /**\n * Remove a field from a schema\n *\n * @example\n * ```typescript\n * await baasix.schemas.removeField('products', 'deprecated_field');\n * ```\n */\n async removeField(\n collection: string,\n fieldName: string\n ): Promise<SchemaInfo> {\n const currentSchema = await this.findOne(collection);\n const { [fieldName]: _, ...remainingFields } = currentSchema.schema.fields;\n\n return this.update(collection, {\n schema: {\n ...currentSchema.schema,\n fields: remainingFields,\n },\n });\n }\n\n /**\n * Export all schemas as JSON\n *\n * @example\n * ```typescript\n * const schemas = await baasix.schemas.export();\n * // Save to file for backup\n * ```\n */\n async export(): Promise<SchemaInfo[]> {\n const response = await this.client.get<{ data: SchemaInfo[] }>(\n \"/schemas/export\"\n );\n return response.data;\n }\n\n /**\n * Import schemas from JSON\n *\n * @example\n * ```typescript\n * await baasix.schemas.import(savedSchemas);\n * ```\n */\n async import(schemas: SchemaInfo[]): Promise<void> {\n await this.client.post(\"/schemas/import\", { schemas });\n }\n}\n\n// Re-export types\nexport type {\n IndexDefinition,\n RelationshipDefinition,\n SchemaDefinition,\n SchemaInfo,\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tspvivek/baasix-sdk",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.3",
4
4
  "description": "Official JavaScript/TypeScript SDK for Baasix Backend-as-a-Service",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",