@tspvivek/baasix-sdk 0.1.0-alpha.1

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 +942 -0
  3. package/dist/client-CzF9B60b.d.ts +614 -0
  4. package/dist/client-aXK_gEyr.d.cts +614 -0
  5. package/dist/index.cjs +4159 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +1498 -0
  8. package/dist/index.d.ts +1498 -0
  9. package/dist/index.js +4135 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/modules/auth.cjs +651 -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 +649 -0
  16. package/dist/modules/auth.js.map +1 -0
  17. package/dist/modules/files.cjs +266 -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 +264 -0
  22. package/dist/modules/files.js.map +1 -0
  23. package/dist/modules/items.cjs +654 -0
  24. package/dist/modules/items.cjs.map +1 -0
  25. package/dist/modules/items.d.cts +472 -0
  26. package/dist/modules/items.d.ts +472 -0
  27. package/dist/modules/items.js +651 -0
  28. package/dist/modules/items.js.map +1 -0
  29. package/dist/modules/schemas.cjs +269 -0
  30. package/dist/modules/schemas.cjs.map +1 -0
  31. package/dist/modules/schemas.d.cts +239 -0
  32. package/dist/modules/schemas.d.ts +239 -0
  33. package/dist/modules/schemas.js +267 -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 +107 -0
@@ -0,0 +1,651 @@
1
+ // src/modules/items.ts
2
+ var QueryBuilder = class {
3
+ collection;
4
+ client;
5
+ queryParams = {};
6
+ constructor(collection, client) {
7
+ this.collection = collection;
8
+ this.client = client;
9
+ }
10
+ /**
11
+ * Select specific fields to return
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * items.select(['id', 'name', 'author.*'])
16
+ * items.select('*', 'category.name')
17
+ * ```
18
+ */
19
+ select(...fields) {
20
+ const flatFields = fields.length === 1 && Array.isArray(fields[0]) ? fields[0] : fields;
21
+ this.queryParams.fields = flatFields;
22
+ return this;
23
+ }
24
+ /**
25
+ * Alias for select()
26
+ */
27
+ fields(...fields) {
28
+ return this.select(...fields);
29
+ }
30
+ /**
31
+ * Add filter conditions
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // Simple equality
36
+ * items.filter({ status: { eq: 'active' } })
37
+ *
38
+ * // Multiple conditions
39
+ * items.filter({
40
+ * AND: [
41
+ * { status: { eq: 'active' } },
42
+ * { price: { gte: 100 } }
43
+ * ]
44
+ * })
45
+ *
46
+ * // Relation filtering
47
+ * items.filter({ 'author.name': { like: 'John' } })
48
+ * ```
49
+ */
50
+ filter(filter) {
51
+ this.queryParams.filter = filter;
52
+ return this;
53
+ }
54
+ /**
55
+ * Alias for filter()
56
+ */
57
+ where(filter) {
58
+ return this.filter(filter);
59
+ }
60
+ /**
61
+ * Sort results
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * // Object notation
66
+ * items.sort({ createdAt: 'desc', name: 'asc' })
67
+ *
68
+ * // Array notation with prefix
69
+ * items.sort(['-createdAt', 'name'])
70
+ *
71
+ * // String shorthand
72
+ * items.sort('createdAt:desc')
73
+ * ```
74
+ */
75
+ sort(sort) {
76
+ this.queryParams.sort = sort;
77
+ return this;
78
+ }
79
+ /**
80
+ * Alias for sort()
81
+ */
82
+ orderBy(sort) {
83
+ return this.sort(sort);
84
+ }
85
+ /**
86
+ * Limit number of results
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * items.limit(20)
91
+ * items.limit(-1) // All results
92
+ * ```
93
+ */
94
+ limit(limit) {
95
+ this.queryParams.limit = limit;
96
+ return this;
97
+ }
98
+ /**
99
+ * Set page number (1-indexed)
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * items.page(2).limit(20)
104
+ * ```
105
+ */
106
+ page(page) {
107
+ this.queryParams.page = page;
108
+ return this;
109
+ }
110
+ /**
111
+ * Skip a number of results
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * items.offset(20)
116
+ * ```
117
+ */
118
+ offset(offset) {
119
+ this.queryParams.offset = offset;
120
+ return this;
121
+ }
122
+ /**
123
+ * Full-text search
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * items.search('keyword', ['title', 'description'])
128
+ * ```
129
+ */
130
+ search(query, fields) {
131
+ this.queryParams.search = query;
132
+ if (fields) {
133
+ this.queryParams.searchFields = fields;
134
+ }
135
+ return this;
136
+ }
137
+ /**
138
+ * Include soft-deleted items
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * items.withDeleted()
143
+ * ```
144
+ */
145
+ withDeleted() {
146
+ this.queryParams.paranoid = false;
147
+ return this;
148
+ }
149
+ /**
150
+ * Filter related items in O2M/M2M relations
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * // Only show approved comments
155
+ * items.relFilter({
156
+ * comments: { approved: { eq: true } }
157
+ * })
158
+ * ```
159
+ */
160
+ relFilter(conditions) {
161
+ this.queryParams.relConditions = conditions;
162
+ return this;
163
+ }
164
+ /**
165
+ * Get the built query parameters
166
+ */
167
+ getQuery() {
168
+ return { ...this.queryParams };
169
+ }
170
+ /**
171
+ * Execute the query and return results
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * const { data, totalCount } = await items
176
+ * .filter({ status: { eq: 'active' } })
177
+ * .sort({ createdAt: 'desc' })
178
+ * .limit(10)
179
+ * .get();
180
+ * ```
181
+ */
182
+ async get() {
183
+ return this.client.get(`/items/${this.collection}`, {
184
+ params: this.buildParams()
185
+ });
186
+ }
187
+ /**
188
+ * Execute the query and return the first result
189
+ *
190
+ * @example
191
+ * ```typescript
192
+ * const item = await items
193
+ * .filter({ slug: { eq: 'my-post' } })
194
+ * .first();
195
+ * ```
196
+ */
197
+ async first() {
198
+ const result = await this.limit(1).get();
199
+ return result.data[0] || null;
200
+ }
201
+ /**
202
+ * Count matching items
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * const count = await items.filter({ status: { eq: 'active' } }).count();
207
+ * ```
208
+ */
209
+ async count() {
210
+ const result = await this.client.get(
211
+ `/items/${this.collection}`,
212
+ {
213
+ params: {
214
+ ...this.buildParams(),
215
+ limit: 0
216
+ }
217
+ }
218
+ );
219
+ return result.totalCount || 0;
220
+ }
221
+ /**
222
+ * Build query parameters for the request
223
+ */
224
+ buildParams() {
225
+ const params = {};
226
+ if (this.queryParams.fields) {
227
+ params.fields = this.queryParams.fields;
228
+ }
229
+ if (this.queryParams.filter) {
230
+ params.filter = this.queryParams.filter;
231
+ }
232
+ if (this.queryParams.sort) {
233
+ params.sort = this.queryParams.sort;
234
+ }
235
+ if (this.queryParams.limit !== void 0) {
236
+ params.limit = this.queryParams.limit;
237
+ }
238
+ if (this.queryParams.page !== void 0) {
239
+ params.page = this.queryParams.page;
240
+ }
241
+ if (this.queryParams.offset !== void 0) {
242
+ params.offset = this.queryParams.offset;
243
+ }
244
+ if (this.queryParams.search) {
245
+ params.search = this.queryParams.search;
246
+ }
247
+ if (this.queryParams.searchFields) {
248
+ params.searchFields = this.queryParams.searchFields;
249
+ }
250
+ if (this.queryParams.paranoid !== void 0) {
251
+ params.paranoid = this.queryParams.paranoid;
252
+ }
253
+ if (this.queryParams.relConditions) {
254
+ params.relConditions = this.queryParams.relConditions;
255
+ }
256
+ if (this.queryParams.aggregate) {
257
+ params.aggregate = this.queryParams.aggregate;
258
+ }
259
+ if (this.queryParams.groupBy) {
260
+ params.groupBy = this.queryParams.groupBy;
261
+ }
262
+ return params;
263
+ }
264
+ };
265
+ var ItemsModule = class {
266
+ collection;
267
+ client;
268
+ constructor(collection, config) {
269
+ this.collection = collection;
270
+ this.client = config.client;
271
+ }
272
+ /**
273
+ * Create a query builder for fluent query construction
274
+ *
275
+ * @example
276
+ * ```typescript
277
+ * const results = await baasix.items('posts')
278
+ * .query()
279
+ * .select('*', 'author.*')
280
+ * .filter({ status: { eq: 'published' } })
281
+ * .sort({ createdAt: 'desc' })
282
+ * .limit(10)
283
+ * .get();
284
+ * ```
285
+ */
286
+ query() {
287
+ return new QueryBuilder(this.collection, this.client);
288
+ }
289
+ /**
290
+ * Find items with optional query parameters
291
+ *
292
+ * @example
293
+ * ```typescript
294
+ * // Simple query
295
+ * const { data } = await items.find();
296
+ *
297
+ * // With parameters
298
+ * const { data, totalCount } = await items.find({
299
+ * filter: { status: { eq: 'active' } },
300
+ * sort: { createdAt: 'desc' },
301
+ * limit: 20,
302
+ * page: 1,
303
+ * fields: ['id', 'name', 'price']
304
+ * });
305
+ * ```
306
+ */
307
+ async find(params) {
308
+ return this.client.get(`/items/${this.collection}`, {
309
+ params
310
+ });
311
+ }
312
+ /**
313
+ * Alias for find()
314
+ */
315
+ async findMany(params) {
316
+ return this.find(params);
317
+ }
318
+ /**
319
+ * Find a single item by ID
320
+ *
321
+ * @example
322
+ * ```typescript
323
+ * const product = await items.findOne('product-uuid');
324
+ *
325
+ * // With specific fields
326
+ * const product = await items.findOne('product-uuid', {
327
+ * fields: ['id', 'name', 'category.*']
328
+ * });
329
+ * ```
330
+ */
331
+ async findOne(id, params) {
332
+ const response = await this.client.get(
333
+ `/items/${this.collection}/${id}`,
334
+ { params }
335
+ );
336
+ return response.data;
337
+ }
338
+ /**
339
+ * Alias for findOne()
340
+ */
341
+ async get(id, params) {
342
+ return this.findOne(id, params);
343
+ }
344
+ /**
345
+ * Create a new item
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * const id = await items.create({
350
+ * name: 'New Product',
351
+ * price: 29.99,
352
+ * status: 'draft'
353
+ * });
354
+ * ```
355
+ */
356
+ async create(data) {
357
+ const response = await this.client.post(
358
+ `/items/${this.collection}`,
359
+ data
360
+ );
361
+ return response.data;
362
+ }
363
+ /**
364
+ * Alias for create()
365
+ */
366
+ async insert(data) {
367
+ return this.create(data);
368
+ }
369
+ /**
370
+ * Create multiple items at once
371
+ *
372
+ * @example
373
+ * ```typescript
374
+ * const ids = await items.createMany([
375
+ * { name: 'Product 1', price: 10 },
376
+ * { name: 'Product 2', price: 20 }
377
+ * ]);
378
+ * ```
379
+ */
380
+ async createMany(data) {
381
+ const response = await this.client.post(
382
+ `/items/${this.collection}/bulk`,
383
+ data
384
+ );
385
+ return response.data;
386
+ }
387
+ /**
388
+ * Alias for createMany()
389
+ */
390
+ async insertMany(data) {
391
+ return this.createMany(data);
392
+ }
393
+ /**
394
+ * Update an existing item
395
+ *
396
+ * @example
397
+ * ```typescript
398
+ * await items.update('product-uuid', {
399
+ * price: 24.99,
400
+ * status: 'published'
401
+ * });
402
+ * ```
403
+ */
404
+ async update(id, data) {
405
+ const response = await this.client.patch(
406
+ `/items/${this.collection}/${id}`,
407
+ data
408
+ );
409
+ return response.data;
410
+ }
411
+ /**
412
+ * Update multiple items at once
413
+ *
414
+ * @example
415
+ * ```typescript
416
+ * // Update by IDs
417
+ * await items.updateMany(['id1', 'id2'], { status: 'archived' });
418
+ * ```
419
+ */
420
+ async updateMany(ids, data) {
421
+ const response = await this.client.patch(
422
+ `/items/${this.collection}/bulk`,
423
+ { ids, data }
424
+ );
425
+ return response.data;
426
+ }
427
+ /**
428
+ * Upsert an item (create if not exists, update if exists)
429
+ *
430
+ * @example
431
+ * ```typescript
432
+ * const id = await items.upsert(
433
+ * { sku: 'WIDGET-001' },
434
+ * { name: 'Widget', price: 29.99, sku: 'WIDGET-001' }
435
+ * );
436
+ * ```
437
+ */
438
+ async upsert(filter, data) {
439
+ const existing = await this.find({ filter, limit: 1 });
440
+ if (existing.data.length > 0) {
441
+ return this.update(existing.data[0].id, data);
442
+ }
443
+ return this.create(data);
444
+ }
445
+ /**
446
+ * Delete an item by ID
447
+ *
448
+ * @example
449
+ * ```typescript
450
+ * await items.delete('product-uuid');
451
+ * ```
452
+ */
453
+ async delete(id) {
454
+ await this.client.delete(`/items/${this.collection}/${id}`);
455
+ }
456
+ /**
457
+ * Delete multiple items
458
+ *
459
+ * @example
460
+ * ```typescript
461
+ * await items.deleteMany(['id1', 'id2', 'id3']);
462
+ * ```
463
+ */
464
+ async deleteMany(ids) {
465
+ await this.client.delete(`/items/${this.collection}/bulk`, {
466
+ params: { ids }
467
+ });
468
+ }
469
+ /**
470
+ * Soft delete an item (if paranoid mode is enabled)
471
+ *
472
+ * @example
473
+ * ```typescript
474
+ * await items.softDelete('product-uuid');
475
+ * ```
476
+ */
477
+ async softDelete(id) {
478
+ await this.update(id, { deletedAt: (/* @__PURE__ */ new Date()).toISOString() });
479
+ }
480
+ /**
481
+ * Restore a soft-deleted item
482
+ *
483
+ * @example
484
+ * ```typescript
485
+ * await items.restore('product-uuid');
486
+ * ```
487
+ */
488
+ async restore(id) {
489
+ await this.update(id, { deletedAt: null });
490
+ }
491
+ /**
492
+ * Aggregate data with grouping
493
+ *
494
+ * @example
495
+ * ```typescript
496
+ * const results = await items.aggregate({
497
+ * aggregate: {
498
+ * total: { function: 'sum', field: 'amount' },
499
+ * count: { function: 'count', field: 'id' },
500
+ * avgPrice: { function: 'avg', field: 'price' }
501
+ * },
502
+ * groupBy: ['category', 'status'],
503
+ * filter: { createdAt: { gte: '$NOW-DAYS_30' } }
504
+ * });
505
+ * ```
506
+ */
507
+ async aggregate(params) {
508
+ const response = await this.client.get(
509
+ `/items/${this.collection}`,
510
+ { params }
511
+ );
512
+ return response.data;
513
+ }
514
+ // ===================
515
+ // Import Operations
516
+ // ===================
517
+ /**
518
+ * Import items from a CSV file
519
+ *
520
+ * @example
521
+ * ```typescript
522
+ * // Browser
523
+ * const fileInput = document.querySelector('input[type="file"]');
524
+ * const file = fileInput.files[0];
525
+ *
526
+ * const result = await baasix.items('products').importCSV(file, {
527
+ * delimiter: ',',
528
+ * skipFirstRow: true
529
+ * });
530
+ *
531
+ * console.log(`Imported ${result.created} items`);
532
+ * ```
533
+ */
534
+ async importCSV(file, options) {
535
+ const formData = new FormData();
536
+ if (file instanceof File) {
537
+ formData.append("file", file);
538
+ } else {
539
+ formData.append("file", file);
540
+ }
541
+ if (options?.delimiter) {
542
+ formData.append("delimiter", options.delimiter);
543
+ }
544
+ if (options?.skipFirstRow !== void 0) {
545
+ formData.append("skipFirstRow", String(options.skipFirstRow));
546
+ }
547
+ if (options?.dateFormat) {
548
+ formData.append("dateFormat", options.dateFormat);
549
+ }
550
+ if (options?.fieldMapping) {
551
+ formData.append("fieldMapping", JSON.stringify(options.fieldMapping));
552
+ }
553
+ const response = await this.client.post(
554
+ `/items/${this.collection}/import/csv`,
555
+ formData
556
+ );
557
+ return response.data;
558
+ }
559
+ /**
560
+ * Import items from a JSON file
561
+ *
562
+ * @example
563
+ * ```typescript
564
+ * const file = fileInput.files[0]; // JSON file
565
+ * const result = await baasix.items('products').importJSON(file);
566
+ *
567
+ * console.log(`Imported ${result.created} items`);
568
+ * ```
569
+ */
570
+ async importJSON(file, options) {
571
+ const formData = new FormData();
572
+ if (file instanceof File) {
573
+ formData.append("file", file);
574
+ } else {
575
+ formData.append("file", file);
576
+ }
577
+ if (options?.fieldMapping) {
578
+ formData.append("fieldMapping", JSON.stringify(options.fieldMapping));
579
+ }
580
+ const response = await this.client.post(
581
+ `/items/${this.collection}/import/json`,
582
+ formData
583
+ );
584
+ return response.data;
585
+ }
586
+ /**
587
+ * Import items from an array of objects
588
+ *
589
+ * @example
590
+ * ```typescript
591
+ * const data = [
592
+ * { name: 'Product 1', price: 29.99 },
593
+ * { name: 'Product 2', price: 39.99 }
594
+ * ];
595
+ *
596
+ * const result = await baasix.items('products').importData(data);
597
+ * ```
598
+ */
599
+ async importData(data) {
600
+ const response = await this.client.post(
601
+ `/items/${this.collection}/bulk`,
602
+ data
603
+ );
604
+ return response;
605
+ }
606
+ // ===================
607
+ // Sort Operations
608
+ // ===================
609
+ /**
610
+ * Sort/reorder items (move item before another)
611
+ *
612
+ * @example
613
+ * ```typescript
614
+ * // Move item1 before item2
615
+ * await baasix.items('products').sortItem('item1-uuid', 'item2-uuid');
616
+ * ```
617
+ */
618
+ async sortItem(itemId, beforeItemId) {
619
+ await this.client.post("/utils/sort", {
620
+ collection: this.collection,
621
+ item: itemId,
622
+ to: beforeItemId
623
+ });
624
+ }
625
+ /**
626
+ * Reorder multiple items
627
+ *
628
+ * @example
629
+ * ```typescript
630
+ * // Set explicit order
631
+ * await baasix.items('products').reorder([
632
+ * 'item3-uuid',
633
+ * 'item1-uuid',
634
+ * 'item2-uuid'
635
+ * ]);
636
+ * ```
637
+ */
638
+ async reorder(orderedIds) {
639
+ for (let i = 1; i < orderedIds.length; i++) {
640
+ await this.client.post("/utils/sort", {
641
+ collection: this.collection,
642
+ item: orderedIds[i],
643
+ to: orderedIds[i - 1]
644
+ });
645
+ }
646
+ }
647
+ };
648
+
649
+ export { ItemsModule, QueryBuilder };
650
+ //# sourceMappingURL=items.js.map
651
+ //# sourceMappingURL=items.js.map