@tspvivek/baasix-sdk 0.1.0-alpha.3 → 0.1.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -3
- package/dist/index.cjs +77 -48
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +67 -14
- package/dist/index.d.ts +67 -14
- package/dist/index.js +77 -48
- package/dist/index.js.map +1 -1
- package/dist/modules/files.cjs +1 -34
- package/dist/modules/files.cjs.map +1 -1
- package/dist/modules/files.js +1 -34
- package/dist/modules/files.js.map +1 -1
- package/dist/modules/items.cjs +1 -34
- package/dist/modules/items.cjs.map +1 -1
- package/dist/modules/items.js +1 -34
- package/dist/modules/items.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -507,27 +507,111 @@ await baasix.schemas.createIndex('products', {
|
|
|
507
507
|
|
|
508
508
|
## Reports & Analytics
|
|
509
509
|
|
|
510
|
+
### Generate Report (POST)
|
|
511
|
+
|
|
512
|
+
Use `generate()` to create a report with a POST request, sending the query in the request body:
|
|
513
|
+
|
|
510
514
|
```typescript
|
|
511
|
-
// Generate report
|
|
512
515
|
const report = await baasix.reports.generate('orders', {
|
|
513
516
|
aggregate: {
|
|
514
517
|
revenue: { function: 'sum', field: 'total' },
|
|
515
518
|
orders: { function: 'count', field: 'id' },
|
|
516
519
|
},
|
|
517
|
-
groupBy: 'category',
|
|
520
|
+
groupBy: ['category'],
|
|
518
521
|
filter: { status: { eq: 'completed' } },
|
|
519
522
|
dateRange: {
|
|
520
523
|
start: '2025-01-01',
|
|
521
524
|
end: '2025-12-31',
|
|
522
525
|
},
|
|
523
526
|
});
|
|
527
|
+
```
|
|
524
528
|
|
|
525
|
-
|
|
529
|
+
### Query Report (GET)
|
|
530
|
+
|
|
531
|
+
Use `query()` to fetch a report with a GET request, sending parameters as query strings:
|
|
532
|
+
|
|
533
|
+
```typescript
|
|
534
|
+
const report = await baasix.reports.query('orders', {
|
|
535
|
+
aggregate: {
|
|
536
|
+
total: { function: 'sum', field: 'amount' },
|
|
537
|
+
},
|
|
538
|
+
groupBy: ['status'],
|
|
539
|
+
filter: { createdAt: { gte: '$NOW-DAYS_30' } },
|
|
540
|
+
});
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
### Multi-Collection Stats
|
|
544
|
+
|
|
545
|
+
Get statistics for multiple collections in a single request:
|
|
546
|
+
|
|
547
|
+
```typescript
|
|
548
|
+
const stats = await baasix.reports.getStats([
|
|
549
|
+
{
|
|
550
|
+
name: 'total_products',
|
|
551
|
+
collection: 'products',
|
|
552
|
+
query: {
|
|
553
|
+
aggregate: { count: { function: 'count', field: '*' } },
|
|
554
|
+
},
|
|
555
|
+
},
|
|
556
|
+
{
|
|
557
|
+
name: 'total_orders',
|
|
558
|
+
collection: 'orders',
|
|
559
|
+
query: {
|
|
560
|
+
aggregate: {
|
|
561
|
+
count: { function: 'count', field: '*' },
|
|
562
|
+
total_amount: { function: 'sum', field: 'amount' },
|
|
563
|
+
},
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
name: 'products_by_category',
|
|
568
|
+
collection: 'products',
|
|
569
|
+
query: {
|
|
570
|
+
groupBy: ['categoryId'],
|
|
571
|
+
aggregate: {
|
|
572
|
+
count: { function: 'count', field: 'id' },
|
|
573
|
+
avg_price: { function: 'avg', field: 'price' },
|
|
574
|
+
},
|
|
575
|
+
fields: ['categoryId', 'category.name'],
|
|
576
|
+
},
|
|
577
|
+
},
|
|
578
|
+
]);
|
|
579
|
+
// Returns: [{ name: 'total_products', collection: 'products', data: [...] }, ...]
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
### Aggregation Query
|
|
583
|
+
|
|
584
|
+
Run aggregation queries directly on a collection (uses items endpoint):
|
|
585
|
+
|
|
586
|
+
```typescript
|
|
587
|
+
const results = await baasix.reports.aggregate('orders', {
|
|
588
|
+
aggregate: {
|
|
589
|
+
total: { function: 'sum', field: 'amount' },
|
|
590
|
+
count: { function: 'count', field: 'id' },
|
|
591
|
+
min: { function: 'min', field: 'amount' },
|
|
592
|
+
max: { function: 'max', field: 'amount' },
|
|
593
|
+
avg: { function: 'avg', field: 'amount' },
|
|
594
|
+
},
|
|
595
|
+
groupBy: ['status', 'paymentMethod'],
|
|
596
|
+
filter: { createdAt: { gte: '$NOW-DAYS_30' } },
|
|
597
|
+
});
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### Quick Count
|
|
601
|
+
|
|
602
|
+
```typescript
|
|
526
603
|
const activeUsers = await baasix.reports.count('users', {
|
|
527
604
|
status: { eq: 'active' },
|
|
528
605
|
});
|
|
529
606
|
```
|
|
530
607
|
|
|
608
|
+
### Distinct Values
|
|
609
|
+
|
|
610
|
+
```typescript
|
|
611
|
+
const categories = await baasix.reports.distinct('products', 'category');
|
|
612
|
+
// Returns: ['Electronics', 'Clothing', 'Books', ...]
|
|
613
|
+
```
|
|
614
|
+
|
|
531
615
|
## Workflows
|
|
532
616
|
|
|
533
617
|
```typescript
|
package/dist/index.cjs
CHANGED
|
@@ -1082,35 +1082,6 @@ var AuthModule = class {
|
|
|
1082
1082
|
}
|
|
1083
1083
|
};
|
|
1084
1084
|
|
|
1085
|
-
// src/utils/sort.ts
|
|
1086
|
-
function normalizeSort(sort) {
|
|
1087
|
-
if (!sort) return void 0;
|
|
1088
|
-
if (typeof sort === "string") {
|
|
1089
|
-
return sort;
|
|
1090
|
-
}
|
|
1091
|
-
if (Array.isArray(sort)) {
|
|
1092
|
-
if (sort.length === 0) return void 0;
|
|
1093
|
-
if (typeof sort[0] === "object" && "column" in sort[0]) {
|
|
1094
|
-
return sort.map((s) => `${s.column}:${s.order.toLowerCase()}`).join(",");
|
|
1095
|
-
}
|
|
1096
|
-
return sort.map((s) => {
|
|
1097
|
-
if (s.startsWith("-")) {
|
|
1098
|
-
return `${s.substring(1)}:desc`;
|
|
1099
|
-
}
|
|
1100
|
-
if (s.includes(":")) {
|
|
1101
|
-
return s;
|
|
1102
|
-
}
|
|
1103
|
-
return `${s}:asc`;
|
|
1104
|
-
}).join(",");
|
|
1105
|
-
}
|
|
1106
|
-
if (typeof sort === "object") {
|
|
1107
|
-
const entries = Object.entries(sort);
|
|
1108
|
-
if (entries.length === 0) return void 0;
|
|
1109
|
-
return entries.map(([field, direction]) => `${field}:${direction.toLowerCase()}`).join(",");
|
|
1110
|
-
}
|
|
1111
|
-
return void 0;
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
1085
|
// src/modules/items.ts
|
|
1115
1086
|
var QueryBuilder = class {
|
|
1116
1087
|
collection;
|
|
@@ -1418,12 +1389,8 @@ var ItemsModule = class {
|
|
|
1418
1389
|
* ```
|
|
1419
1390
|
*/
|
|
1420
1391
|
async find(params) {
|
|
1421
|
-
const normalizedParams = params ? {
|
|
1422
|
-
...params,
|
|
1423
|
-
sort: normalizeSort(params.sort)
|
|
1424
|
-
} : void 0;
|
|
1425
1392
|
return this.client.get(`/items/${this.collection}`, {
|
|
1426
|
-
params
|
|
1393
|
+
params
|
|
1427
1394
|
});
|
|
1428
1395
|
}
|
|
1429
1396
|
/**
|
|
@@ -1839,12 +1806,8 @@ var FilesModule = class {
|
|
|
1839
1806
|
* ```
|
|
1840
1807
|
*/
|
|
1841
1808
|
async find(params) {
|
|
1842
|
-
const normalizedParams = params ? {
|
|
1843
|
-
...params,
|
|
1844
|
-
sort: normalizeSort(params.sort)
|
|
1845
|
-
} : void 0;
|
|
1846
1809
|
return this.client.get("/files", {
|
|
1847
|
-
params
|
|
1810
|
+
params
|
|
1848
1811
|
});
|
|
1849
1812
|
}
|
|
1850
1813
|
/**
|
|
@@ -2013,6 +1976,35 @@ var FilesModule = class {
|
|
|
2013
1976
|
}
|
|
2014
1977
|
};
|
|
2015
1978
|
|
|
1979
|
+
// src/utils/sort.ts
|
|
1980
|
+
function normalizeSort(sort) {
|
|
1981
|
+
if (!sort) return void 0;
|
|
1982
|
+
if (typeof sort === "string") {
|
|
1983
|
+
return sort;
|
|
1984
|
+
}
|
|
1985
|
+
if (Array.isArray(sort)) {
|
|
1986
|
+
if (sort.length === 0) return void 0;
|
|
1987
|
+
if (typeof sort[0] === "object" && "column" in sort[0]) {
|
|
1988
|
+
return sort.map((s) => `${s.column}:${s.order.toLowerCase()}`).join(",");
|
|
1989
|
+
}
|
|
1990
|
+
return sort.map((s) => {
|
|
1991
|
+
if (s.startsWith("-")) {
|
|
1992
|
+
return `${s.substring(1)}:desc`;
|
|
1993
|
+
}
|
|
1994
|
+
if (s.includes(":")) {
|
|
1995
|
+
return s;
|
|
1996
|
+
}
|
|
1997
|
+
return `${s}:asc`;
|
|
1998
|
+
}).join(",");
|
|
1999
|
+
}
|
|
2000
|
+
if (typeof sort === "object") {
|
|
2001
|
+
const entries = Object.entries(sort);
|
|
2002
|
+
if (entries.length === 0) return void 0;
|
|
2003
|
+
return entries.map(([field, direction]) => `${field}:${direction.toLowerCase()}`).join(",");
|
|
2004
|
+
}
|
|
2005
|
+
return void 0;
|
|
2006
|
+
}
|
|
2007
|
+
|
|
2016
2008
|
// src/modules/schemas.ts
|
|
2017
2009
|
var SchemasModule = class {
|
|
2018
2010
|
client;
|
|
@@ -2658,7 +2650,7 @@ var ReportsModule = class {
|
|
|
2658
2650
|
this.client = config.client;
|
|
2659
2651
|
}
|
|
2660
2652
|
/**
|
|
2661
|
-
* Generate a report for a collection
|
|
2653
|
+
* Generate a report for a collection using POST method
|
|
2662
2654
|
*
|
|
2663
2655
|
* @example
|
|
2664
2656
|
* ```typescript
|
|
@@ -2668,7 +2660,7 @@ var ReportsModule = class {
|
|
|
2668
2660
|
* revenue: { function: 'sum', field: 'total' },
|
|
2669
2661
|
* orders: { function: 'count', field: 'id' }
|
|
2670
2662
|
* },
|
|
2671
|
-
* groupBy: 'month',
|
|
2663
|
+
* groupBy: ['month'],
|
|
2672
2664
|
* dateRange: {
|
|
2673
2665
|
* start: '2025-01-01',
|
|
2674
2666
|
* end: '2025-12-31'
|
|
@@ -2684,19 +2676,56 @@ var ReportsModule = class {
|
|
|
2684
2676
|
return response;
|
|
2685
2677
|
}
|
|
2686
2678
|
/**
|
|
2687
|
-
*
|
|
2679
|
+
* Query a report for a collection using GET method with query params
|
|
2680
|
+
*
|
|
2681
|
+
* @example
|
|
2682
|
+
* ```typescript
|
|
2683
|
+
* const report = await baasix.reports.query('orders', {
|
|
2684
|
+
* aggregate: {
|
|
2685
|
+
* total: { function: 'sum', field: 'amount' }
|
|
2686
|
+
* },
|
|
2687
|
+
* groupBy: ['status']
|
|
2688
|
+
* });
|
|
2689
|
+
* ```
|
|
2690
|
+
*/
|
|
2691
|
+
async query(collection, params) {
|
|
2692
|
+
const response = await this.client.get(
|
|
2693
|
+
`/reports/${collection}`,
|
|
2694
|
+
{ params }
|
|
2695
|
+
);
|
|
2696
|
+
return response;
|
|
2697
|
+
}
|
|
2698
|
+
/**
|
|
2699
|
+
* Get statistics for multiple collections in a single request
|
|
2688
2700
|
*
|
|
2689
2701
|
* @example
|
|
2690
2702
|
* ```typescript
|
|
2691
|
-
* const stats = await baasix.reports.getStats(
|
|
2692
|
-
*
|
|
2703
|
+
* const stats = await baasix.reports.getStats([
|
|
2704
|
+
* {
|
|
2705
|
+
* name: 'total_products',
|
|
2706
|
+
* collection: 'products',
|
|
2707
|
+
* query: {
|
|
2708
|
+
* aggregate: { count: { function: 'count', field: '*' } }
|
|
2709
|
+
* }
|
|
2710
|
+
* },
|
|
2711
|
+
* {
|
|
2712
|
+
* name: 'total_orders',
|
|
2713
|
+
* collection: 'orders',
|
|
2714
|
+
* query: {
|
|
2715
|
+
* aggregate: {
|
|
2716
|
+
* count: { function: 'count', field: '*' },
|
|
2717
|
+
* total_amount: { function: 'sum', field: 'amount' }
|
|
2718
|
+
* }
|
|
2719
|
+
* }
|
|
2720
|
+
* }
|
|
2721
|
+
* ]);
|
|
2693
2722
|
* ```
|
|
2694
2723
|
*/
|
|
2695
|
-
async getStats(
|
|
2696
|
-
const response = await this.client.
|
|
2697
|
-
|
|
2724
|
+
async getStats(stats) {
|
|
2725
|
+
const response = await this.client.post(`/reports/stats`, {
|
|
2726
|
+
stats
|
|
2698
2727
|
});
|
|
2699
|
-
return response
|
|
2728
|
+
return response;
|
|
2700
2729
|
}
|
|
2701
2730
|
/**
|
|
2702
2731
|
* Generate an aggregation query
|