@tspvivek/baasix-sdk 0.1.0-alpha.2 → 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 +80 -10
- 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 +80 -10
- package/dist/index.js.map +1 -1
- package/dist/modules/schemas.cjs +34 -1
- package/dist/modules/schemas.cjs.map +1 -1
- package/dist/modules/schemas.d.cts +2 -2
- package/dist/modules/schemas.d.ts +2 -2
- package/dist/modules/schemas.js +34 -1
- package/dist/modules/schemas.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
|
@@ -1976,6 +1976,35 @@ var FilesModule = class {
|
|
|
1976
1976
|
}
|
|
1977
1977
|
};
|
|
1978
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
|
+
|
|
1979
2008
|
// src/modules/schemas.ts
|
|
1980
2009
|
var SchemasModule = class {
|
|
1981
2010
|
client;
|
|
@@ -1995,7 +2024,11 @@ var SchemasModule = class {
|
|
|
1995
2024
|
* ```
|
|
1996
2025
|
*/
|
|
1997
2026
|
async find(params) {
|
|
1998
|
-
|
|
2027
|
+
const normalizedParams = params ? {
|
|
2028
|
+
...params,
|
|
2029
|
+
sort: normalizeSort(params.sort)
|
|
2030
|
+
} : void 0;
|
|
2031
|
+
return this.client.get("/schemas", { params: normalizedParams });
|
|
1999
2032
|
}
|
|
2000
2033
|
/**
|
|
2001
2034
|
* Get schema for a specific collection
|
|
@@ -2617,7 +2650,7 @@ var ReportsModule = class {
|
|
|
2617
2650
|
this.client = config.client;
|
|
2618
2651
|
}
|
|
2619
2652
|
/**
|
|
2620
|
-
* Generate a report for a collection
|
|
2653
|
+
* Generate a report for a collection using POST method
|
|
2621
2654
|
*
|
|
2622
2655
|
* @example
|
|
2623
2656
|
* ```typescript
|
|
@@ -2627,7 +2660,7 @@ var ReportsModule = class {
|
|
|
2627
2660
|
* revenue: { function: 'sum', field: 'total' },
|
|
2628
2661
|
* orders: { function: 'count', field: 'id' }
|
|
2629
2662
|
* },
|
|
2630
|
-
* groupBy: 'month',
|
|
2663
|
+
* groupBy: ['month'],
|
|
2631
2664
|
* dateRange: {
|
|
2632
2665
|
* start: '2025-01-01',
|
|
2633
2666
|
* end: '2025-12-31'
|
|
@@ -2643,19 +2676,56 @@ var ReportsModule = class {
|
|
|
2643
2676
|
return response;
|
|
2644
2677
|
}
|
|
2645
2678
|
/**
|
|
2646
|
-
*
|
|
2679
|
+
* Query a report for a collection using GET method with query params
|
|
2647
2680
|
*
|
|
2648
2681
|
* @example
|
|
2649
2682
|
* ```typescript
|
|
2650
|
-
* const
|
|
2651
|
-
*
|
|
2683
|
+
* const report = await baasix.reports.query('orders', {
|
|
2684
|
+
* aggregate: {
|
|
2685
|
+
* total: { function: 'sum', field: 'amount' }
|
|
2686
|
+
* },
|
|
2687
|
+
* groupBy: ['status']
|
|
2688
|
+
* });
|
|
2652
2689
|
* ```
|
|
2653
2690
|
*/
|
|
2654
|
-
async
|
|
2655
|
-
const response = await this.client.get(
|
|
2656
|
-
|
|
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
|
|
2700
|
+
*
|
|
2701
|
+
* @example
|
|
2702
|
+
* ```typescript
|
|
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
|
+
* ]);
|
|
2722
|
+
* ```
|
|
2723
|
+
*/
|
|
2724
|
+
async getStats(stats) {
|
|
2725
|
+
const response = await this.client.post(`/reports/stats`, {
|
|
2726
|
+
stats
|
|
2657
2727
|
});
|
|
2658
|
-
return response
|
|
2728
|
+
return response;
|
|
2659
2729
|
}
|
|
2660
2730
|
/**
|
|
2661
2731
|
* Generate an aggregation query
|