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