@baasix/sdk 0.1.0

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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1197 -0
  3. package/dist/client-DeXa-R9w.d.ts +680 -0
  4. package/dist/client-VT7NckyI.d.cts +680 -0
  5. package/dist/index.cjs +4567 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +1788 -0
  8. package/dist/index.d.ts +1788 -0
  9. package/dist/index.js +4543 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/modules/auth.cjs +650 -0
  12. package/dist/modules/auth.cjs.map +1 -0
  13. package/dist/modules/auth.d.cts +384 -0
  14. package/dist/modules/auth.d.ts +384 -0
  15. package/dist/modules/auth.js +648 -0
  16. package/dist/modules/auth.js.map +1 -0
  17. package/dist/modules/files.cjs +269 -0
  18. package/dist/modules/files.cjs.map +1 -0
  19. package/dist/modules/files.d.cts +187 -0
  20. package/dist/modules/files.d.ts +187 -0
  21. package/dist/modules/files.js +267 -0
  22. package/dist/modules/files.js.map +1 -0
  23. package/dist/modules/items.cjs +640 -0
  24. package/dist/modules/items.cjs.map +1 -0
  25. package/dist/modules/items.d.cts +465 -0
  26. package/dist/modules/items.d.ts +465 -0
  27. package/dist/modules/items.js +637 -0
  28. package/dist/modules/items.js.map +1 -0
  29. package/dist/modules/schemas.cjs +322 -0
  30. package/dist/modules/schemas.cjs.map +1 -0
  31. package/dist/modules/schemas.d.cts +260 -0
  32. package/dist/modules/schemas.d.ts +260 -0
  33. package/dist/modules/schemas.js +320 -0
  34. package/dist/modules/schemas.js.map +1 -0
  35. package/dist/storage/index.cjs +162 -0
  36. package/dist/storage/index.cjs.map +1 -0
  37. package/dist/storage/index.d.cts +96 -0
  38. package/dist/storage/index.d.ts +96 -0
  39. package/dist/storage/index.js +157 -0
  40. package/dist/storage/index.js.map +1 -0
  41. package/dist/types-BdjsGANq.d.cts +40 -0
  42. package/dist/types-BdjsGANq.d.ts +40 -0
  43. package/package.json +108 -0
@@ -0,0 +1,322 @@
1
+ 'use strict';
2
+
3
+ // src/utils/sort.ts
4
+ function normalizeSort(sort) {
5
+ if (!sort) return void 0;
6
+ if (typeof sort === "string") {
7
+ return sort;
8
+ }
9
+ if (Array.isArray(sort)) {
10
+ if (sort.length === 0) return void 0;
11
+ if (typeof sort[0] === "object" && "column" in sort[0]) {
12
+ return sort.map((s) => `${s.column}:${s.order.toLowerCase()}`).join(",");
13
+ }
14
+ return sort.map((s) => {
15
+ if (s.startsWith("-")) {
16
+ return `${s.substring(1)}:desc`;
17
+ }
18
+ if (s.includes(":")) {
19
+ return s;
20
+ }
21
+ return `${s}:asc`;
22
+ }).join(",");
23
+ }
24
+ if (typeof sort === "object") {
25
+ const entries = Object.entries(sort);
26
+ if (entries.length === 0) return void 0;
27
+ return entries.map(([field, direction]) => `${field}:${direction.toLowerCase()}`).join(",");
28
+ }
29
+ return void 0;
30
+ }
31
+
32
+ // src/modules/schemas.ts
33
+ var SchemasModule = class {
34
+ client;
35
+ constructor(config) {
36
+ this.client = config.client;
37
+ }
38
+ /**
39
+ * List all schemas
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const { data } = await baasix.schemas.find();
44
+ * console.log(data.map(s => s.collectionName));
45
+ *
46
+ * // With pagination
47
+ * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });
48
+ * ```
49
+ */
50
+ async find(params) {
51
+ const normalizedParams = params ? {
52
+ ...params,
53
+ sort: normalizeSort(params.sort)
54
+ } : void 0;
55
+ return this.client.get("/schemas", { params: normalizedParams });
56
+ }
57
+ /**
58
+ * Get schema for a specific collection
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const schema = await baasix.schemas.findOne('products');
63
+ * console.log(schema.fields);
64
+ * ```
65
+ */
66
+ async findOne(collection) {
67
+ const response = await this.client.get(
68
+ `/schemas/${collection}`
69
+ );
70
+ return response.data;
71
+ }
72
+ /**
73
+ * Create a new collection/schema
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * await baasix.schemas.create({
78
+ * collectionName: 'orders',
79
+ * schema: {
80
+ * name: 'Order',
81
+ * timestamps: true,
82
+ * paranoid: true,
83
+ * fields: {
84
+ * id: {
85
+ * type: 'UUID',
86
+ * primaryKey: true,
87
+ * defaultValue: { type: 'UUIDV4' }
88
+ * },
89
+ * orderNumber: {
90
+ * type: 'String',
91
+ * allowNull: false,
92
+ * unique: true
93
+ * },
94
+ * total: {
95
+ * type: 'Decimal',
96
+ * values: { precision: 10, scale: 2 },
97
+ * allowNull: false,
98
+ * defaultValue: 0
99
+ * },
100
+ * status: {
101
+ * type: 'String',
102
+ * allowNull: false,
103
+ * defaultValue: 'pending'
104
+ * },
105
+ * items: {
106
+ * type: 'JSONB',
107
+ * allowNull: true,
108
+ * defaultValue: []
109
+ * }
110
+ * }
111
+ * }
112
+ * });
113
+ * ```
114
+ */
115
+ async create(data) {
116
+ const response = await this.client.post(
117
+ "/schemas",
118
+ data
119
+ );
120
+ return response.data;
121
+ }
122
+ /**
123
+ * Update an existing schema
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * await baasix.schemas.update('products', {
128
+ * schema: {
129
+ * name: 'Product',
130
+ * timestamps: true,
131
+ * fields: {
132
+ * // Updated fields
133
+ * description: { type: 'Text', allowNull: true }
134
+ * }
135
+ * }
136
+ * });
137
+ * ```
138
+ */
139
+ async update(collection, data) {
140
+ const response = await this.client.patch(
141
+ `/schemas/${collection}`,
142
+ data
143
+ );
144
+ return response.data;
145
+ }
146
+ /**
147
+ * Delete a schema (drops the table)
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * await baasix.schemas.delete('old_collection');
152
+ * ```
153
+ */
154
+ async delete(collection) {
155
+ await this.client.delete(`/schemas/${collection}`);
156
+ }
157
+ /**
158
+ * Create a relationship between collections
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * // Many-to-One (BelongsTo)
163
+ * await baasix.schemas.createRelationship('posts', {
164
+ * type: 'M2O',
165
+ * target: 'baasix_User',
166
+ * name: 'author',
167
+ * alias: 'posts'
168
+ * });
169
+ *
170
+ * // Many-to-Many
171
+ * await baasix.schemas.createRelationship('posts', {
172
+ * type: 'M2M',
173
+ * target: 'tags',
174
+ * name: 'tags',
175
+ * alias: 'posts'
176
+ * });
177
+ * ```
178
+ */
179
+ async createRelationship(collection, relationship) {
180
+ await this.client.post(
181
+ `/schemas/${collection}/relationships`,
182
+ relationship
183
+ );
184
+ }
185
+ /**
186
+ * Delete a relationship
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * await baasix.schemas.deleteRelationship('posts', 'author');
191
+ * ```
192
+ */
193
+ async deleteRelationship(collection, relationshipName) {
194
+ await this.client.delete(
195
+ `/schemas/${collection}/relationships/${relationshipName}`
196
+ );
197
+ }
198
+ /**
199
+ * Update a relationship
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * await baasix.schemas.updateRelationship('posts', 'author', {
204
+ * alias: 'authoredPosts',
205
+ * onDelete: 'CASCADE'
206
+ * });
207
+ * ```
208
+ */
209
+ async updateRelationship(collection, relationshipName, data) {
210
+ await this.client.patch(
211
+ `/schemas/${collection}/relationships/${relationshipName}`,
212
+ data
213
+ );
214
+ }
215
+ /**
216
+ * Create an index on a collection
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * // Unique index
221
+ * await baasix.schemas.createIndex('users', {
222
+ * name: 'idx_users_email',
223
+ * fields: ['email'],
224
+ * unique: true
225
+ * });
226
+ *
227
+ * // Composite index
228
+ * await baasix.schemas.createIndex('orders', {
229
+ * name: 'idx_orders_status_created',
230
+ * fields: ['status', 'createdAt']
231
+ * });
232
+ * ```
233
+ */
234
+ async createIndex(collection, index) {
235
+ await this.client.post(`/schemas/${collection}/indexes`, index);
236
+ }
237
+ /**
238
+ * Delete an index
239
+ *
240
+ * @example
241
+ * ```typescript
242
+ * await baasix.schemas.deleteIndex('users', 'idx_users_email');
243
+ * ```
244
+ */
245
+ async deleteIndex(collection, indexName) {
246
+ await this.client.delete(`/schemas/${collection}/indexes/${indexName}`);
247
+ }
248
+ /**
249
+ * Add a field to an existing schema
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * await baasix.schemas.addField('products', 'rating', {
254
+ * type: 'Decimal',
255
+ * values: { precision: 3, scale: 2 },
256
+ * allowNull: true,
257
+ * defaultValue: 0
258
+ * });
259
+ * ```
260
+ */
261
+ async addField(collection, fieldName, fieldDefinition) {
262
+ const currentSchema = await this.findOne(collection);
263
+ const updatedFields = {
264
+ ...currentSchema.schema.fields,
265
+ [fieldName]: fieldDefinition
266
+ };
267
+ return this.update(collection, {
268
+ schema: {
269
+ ...currentSchema.schema,
270
+ fields: updatedFields
271
+ }
272
+ });
273
+ }
274
+ /**
275
+ * Remove a field from a schema
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * await baasix.schemas.removeField('products', 'deprecated_field');
280
+ * ```
281
+ */
282
+ async removeField(collection, fieldName) {
283
+ const currentSchema = await this.findOne(collection);
284
+ const { [fieldName]: _, ...remainingFields } = currentSchema.schema.fields;
285
+ return this.update(collection, {
286
+ schema: {
287
+ ...currentSchema.schema,
288
+ fields: remainingFields
289
+ }
290
+ });
291
+ }
292
+ /**
293
+ * Export all schemas as JSON
294
+ *
295
+ * @example
296
+ * ```typescript
297
+ * const schemas = await baasix.schemas.export();
298
+ * // Save to file for backup
299
+ * ```
300
+ */
301
+ async export() {
302
+ const response = await this.client.get(
303
+ "/schemas/export"
304
+ );
305
+ return response.data;
306
+ }
307
+ /**
308
+ * Import schemas from JSON
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * await baasix.schemas.import(savedSchemas);
313
+ * ```
314
+ */
315
+ async import(schemas) {
316
+ await this.client.post("/schemas/import", { schemas });
317
+ }
318
+ };
319
+
320
+ exports.SchemasModule = SchemasModule;
321
+ //# sourceMappingURL=schemas.cjs.map
322
+ //# sourceMappingURL=schemas.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/utils/sort.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ACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,WAAA,CACJ,UAAA,EACA,SAAA,EACqB;AACrB,IAAA,MAAM,aAAA,GAAgB,MAAM,IAAA,CAAK,OAAA,CAAQ,UAAU,CAAA;AACnD,IAAA,MAAM,EAAE,CAAC,SAAS,GAAG,GAAG,GAAG,eAAA,EAAgB,GAAI,aAAA,CAAc,MAAA,CAAO,MAAA;AAEpE,IAAA,OAAO,IAAA,CAAK,OAAO,UAAA,EAAY;AAAA,MAC7B,MAAA,EAAQ;AAAA,QACN,GAAG,aAAA,CAAc,MAAA;AAAA,QACjB,MAAA,EAAQ;AAAA;AACV,KACD,CAAA;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAM,MAAA,GAAgC;AACpC,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,MAAA,CAAO,GAAA;AAAA,MACjC;AAAA,KACF;AACA,IAAA,OAAO,QAAA,CAAS,IAAA;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,OAAO,OAAA,EAAsC;AACjD,IAAA,MAAM,KAAK,MAAA,CAAO,IAAA,CAAK,iBAAA,EAAmB,EAAE,SAAS,CAAA;AAAA,EACvD;AACF","file":"schemas.cjs","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"]}
@@ -0,0 +1,260 @@
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-VT7NckyI.cjs';
2
+ import '../types-BdjsGANq.cjs';
3
+
4
+ interface SchemasModuleConfig {
5
+ client: HttpClient;
6
+ }
7
+ /**
8
+ * Schemas module for managing database schemas, relationships, and indexes.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * // Create a new collection
13
+ * await baasix.schemas.create({
14
+ * collectionName: 'products',
15
+ * schema: {
16
+ * name: 'Product',
17
+ * timestamps: true,
18
+ * fields: {
19
+ * id: { type: 'UUID', primaryKey: true, defaultValue: { type: 'UUIDV4' } },
20
+ * name: { type: 'String', allowNull: false },
21
+ * price: { type: 'Decimal', values: { precision: 10, scale: 2 } }
22
+ * }
23
+ * }
24
+ * });
25
+ *
26
+ * // Add a relationship
27
+ * await baasix.schemas.createRelationship('products', {
28
+ * type: 'M2O',
29
+ * target: 'categories',
30
+ * name: 'category',
31
+ * alias: 'products'
32
+ * });
33
+ * ```
34
+ */
35
+ declare class SchemasModule {
36
+ private client;
37
+ constructor(config: SchemasModuleConfig);
38
+ /**
39
+ * List all schemas
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const { data } = await baasix.schemas.find();
44
+ * console.log(data.map(s => s.collectionName));
45
+ *
46
+ * // With pagination
47
+ * const { data } = await baasix.schemas.find({ page: 1, limit: 50 });
48
+ * ```
49
+ */
50
+ find(params?: {
51
+ page?: number;
52
+ limit?: number;
53
+ sort?: Sort;
54
+ search?: string;
55
+ filter?: Record<string, unknown>;
56
+ }): Promise<PaginatedResponse<SchemaInfo>>;
57
+ /**
58
+ * Get schema for a specific collection
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const schema = await baasix.schemas.findOne('products');
63
+ * console.log(schema.fields);
64
+ * ```
65
+ */
66
+ findOne(collection: string): Promise<SchemaInfo>;
67
+ /**
68
+ * Create a new collection/schema
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * await baasix.schemas.create({
73
+ * collectionName: 'orders',
74
+ * schema: {
75
+ * name: 'Order',
76
+ * timestamps: true,
77
+ * paranoid: true,
78
+ * fields: {
79
+ * id: {
80
+ * type: 'UUID',
81
+ * primaryKey: true,
82
+ * defaultValue: { type: 'UUIDV4' }
83
+ * },
84
+ * orderNumber: {
85
+ * type: 'String',
86
+ * allowNull: false,
87
+ * unique: true
88
+ * },
89
+ * total: {
90
+ * type: 'Decimal',
91
+ * values: { precision: 10, scale: 2 },
92
+ * allowNull: false,
93
+ * defaultValue: 0
94
+ * },
95
+ * status: {
96
+ * type: 'String',
97
+ * allowNull: false,
98
+ * defaultValue: 'pending'
99
+ * },
100
+ * items: {
101
+ * type: 'JSONB',
102
+ * allowNull: true,
103
+ * defaultValue: []
104
+ * }
105
+ * }
106
+ * }
107
+ * });
108
+ * ```
109
+ */
110
+ create(data: {
111
+ collectionName: string;
112
+ schema: SchemaDefinition;
113
+ }): Promise<SchemaInfo>;
114
+ /**
115
+ * Update an existing schema
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * await baasix.schemas.update('products', {
120
+ * schema: {
121
+ * name: 'Product',
122
+ * timestamps: true,
123
+ * fields: {
124
+ * // Updated fields
125
+ * description: { type: 'Text', allowNull: true }
126
+ * }
127
+ * }
128
+ * });
129
+ * ```
130
+ */
131
+ update(collection: string, data: {
132
+ schema: Partial<SchemaDefinition>;
133
+ }): Promise<SchemaInfo>;
134
+ /**
135
+ * Delete a schema (drops the table)
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * await baasix.schemas.delete('old_collection');
140
+ * ```
141
+ */
142
+ delete(collection: string): Promise<void>;
143
+ /**
144
+ * Create a relationship between collections
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * // Many-to-One (BelongsTo)
149
+ * await baasix.schemas.createRelationship('posts', {
150
+ * type: 'M2O',
151
+ * target: 'baasix_User',
152
+ * name: 'author',
153
+ * alias: 'posts'
154
+ * });
155
+ *
156
+ * // Many-to-Many
157
+ * await baasix.schemas.createRelationship('posts', {
158
+ * type: 'M2M',
159
+ * target: 'tags',
160
+ * name: 'tags',
161
+ * alias: 'posts'
162
+ * });
163
+ * ```
164
+ */
165
+ createRelationship(collection: string, relationship: RelationshipDefinition): Promise<void>;
166
+ /**
167
+ * Delete a relationship
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * await baasix.schemas.deleteRelationship('posts', 'author');
172
+ * ```
173
+ */
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>;
187
+ /**
188
+ * Create an index on a collection
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * // Unique index
193
+ * await baasix.schemas.createIndex('users', {
194
+ * name: 'idx_users_email',
195
+ * fields: ['email'],
196
+ * unique: true
197
+ * });
198
+ *
199
+ * // Composite index
200
+ * await baasix.schemas.createIndex('orders', {
201
+ * name: 'idx_orders_status_created',
202
+ * fields: ['status', 'createdAt']
203
+ * });
204
+ * ```
205
+ */
206
+ createIndex(collection: string, index: IndexDefinition): Promise<void>;
207
+ /**
208
+ * Delete an index
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * await baasix.schemas.deleteIndex('users', 'idx_users_email');
213
+ * ```
214
+ */
215
+ deleteIndex(collection: string, indexName: string): Promise<void>;
216
+ /**
217
+ * Add a field to an existing schema
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * await baasix.schemas.addField('products', 'rating', {
222
+ * type: 'Decimal',
223
+ * values: { precision: 3, scale: 2 },
224
+ * allowNull: true,
225
+ * defaultValue: 0
226
+ * });
227
+ * ```
228
+ */
229
+ addField(collection: string, fieldName: string, fieldDefinition: SchemaDefinition["fields"][string]): Promise<SchemaInfo>;
230
+ /**
231
+ * Remove a field from a schema
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * await baasix.schemas.removeField('products', 'deprecated_field');
236
+ * ```
237
+ */
238
+ removeField(collection: string, fieldName: string): Promise<SchemaInfo>;
239
+ /**
240
+ * Export all schemas as JSON
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * const schemas = await baasix.schemas.export();
245
+ * // Save to file for backup
246
+ * ```
247
+ */
248
+ export(): Promise<SchemaInfo[]>;
249
+ /**
250
+ * Import schemas from JSON
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * await baasix.schemas.import(savedSchemas);
255
+ * ```
256
+ */
257
+ import(schemas: SchemaInfo[]): Promise<void>;
258
+ }
259
+
260
+ export { IndexDefinition, RelationshipDefinition, SchemaDefinition, SchemaInfo, SchemasModule, type SchemasModuleConfig };