Skip to content

Commit b6f7034

Browse files
committed
Add aggregates support to FindOptions
1 parent 019c55c commit b6f7034

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/DataStore.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export type SearchField = string | { [name: string]: SearchFieldType };
77
export type FindScope = { [name: string]: (string | number)[] };
88
export type FindResult<T> = { data: T[]; totalCount?: number };
99

10+
export type Aggregate = {
11+
type: string;
12+
selector: string;
13+
field?: string;
14+
filters?: any[];
15+
};
16+
1017
export type MediaParams = Record<string, string | number | Blob> & {
1118
name: string;
1219
type: string;
@@ -29,6 +36,7 @@ export interface Group {
2936
export interface FindOptions {
3037
filter?: any[];
3138
scopes?: FindScope;
39+
aggregates?: Aggregate[];
3240
search?: string | number;
3341
skip?: number;
3442
limit?: number;

src/transformers/OrionFindTransformer.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export class OrionFindTransformer implements Transformer<FindOptions> {
121121
only_trashed: onlyTrashed ? onlyTrashed[2] === true : null,
122122
};
123123

124+
const filters = this.buildFilter(data.filter);
125+
124126
const sort = data.sort?.map((order: any) => ({
125127
field: order.selector,
126128
direction: order.desc ? 'desc' : 'asc',
@@ -131,12 +133,23 @@ export class OrionFindTransformer implements Transformer<FindOptions> {
131133
parameters: Array.isArray(value) ? value : [value],
132134
}));
133135

134-
const filters = this.buildFilter(data.filter);
136+
const aggregates = data.aggregates?.map((item) =>
137+
omitBy(
138+
{
139+
type: item.type,
140+
relation: item.selector,
141+
field: item.field,
142+
filters: this.buildFilter(item.filters),
143+
},
144+
isNil,
145+
),
146+
);
135147

136148
const body = {
137149
sort,
138150
scopes,
139151
filters,
152+
aggregates,
140153
search: data.search ? { value: this.formatLike(data.search) } : null,
141154
[this.paramNames.group]: data.group?.map((group) => group.selector),
142155
};

tests/transformers/OrionFindTransformer.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('Orion - FindTransformer', () => {
3636
scopes: {
3737
whereCategory: [1],
3838
},
39+
aggregates: [{ type: 'exists', selector: 'tags' }],
3940
params: {
4041
pagination: 0,
4142
},
@@ -59,6 +60,7 @@ describe('Orion - FindTransformer', () => {
5960
],
6061
},
6162
],
63+
aggregates: [{ type: 'exists', relation: 'tags' }],
6264
scopes: [{ name: 'whereCategory', parameters: [1] }],
6365
search: { value: '%loren%ipsum%dolor%' },
6466
group: ['city'],

0 commit comments

Comments
 (0)