Skip to content

Commit 05ea0ec

Browse files
committed
feat: provide support for groupby
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent d76f00c commit 05ea0ec

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

packages/repository-json-schema/src/filter-json-schema.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,26 @@ export function getFilterJsonSchemaFor(
9393
minimum: 1,
9494
examples: [100],
9595
},
96-
96+
sum: {
97+
type: 'string',
98+
examples: ['column1'],
99+
},
100+
min: {
101+
type: 'string',
102+
examples: ['column1'],
103+
},
104+
max: {
105+
type: 'string',
106+
examples: ['column1'],
107+
},
108+
avg: {
109+
type: 'string',
110+
examples: ['column1'],
111+
},
112+
count: {
113+
type: 'string',
114+
examples: ['column1'],
115+
},
97116
skip: {
98117
type: 'integer',
99118
minimum: 0,
@@ -120,6 +139,9 @@ export function getFilterJsonSchemaFor(
120139
if (!excluded.includes('fields')) {
121140
properties.fields = getFieldsJsonSchemaFor(modelCtor, options);
122141
}
142+
if (!excluded.includes('groupBy')) {
143+
properties.fields = getGroupByJsonSchemaFor(modelCtor, options);
144+
}
123145

124146
// Remove excluded properties
125147
for (const p of excluded) {
@@ -235,3 +257,37 @@ export function getFieldsJsonSchemaFor(
235257

236258
return schema;
237259
}
260+
261+
export function getGroupByJsonSchemaFor(
262+
modelCtor: typeof Model,
263+
options: FilterSchemaOptions = {},
264+
): JsonSchema {
265+
const schema: JsonSchema = {oneOf: []};
266+
if (options.setTitle !== false) {
267+
schema.title = `${modelCtor.modelName}.GroupBy`;
268+
}
269+
270+
const properties = Object.keys(modelCtor.definition.properties);
271+
const additionalProperties = modelCtor.definition.settings.strict === false;
272+
273+
schema.oneOf?.push({
274+
type: 'object',
275+
properties: properties.reduce(
276+
(prev, crr) => ({...prev, [crr]: {type: 'boolean'}}),
277+
{},
278+
),
279+
additionalProperties,
280+
});
281+
282+
schema.oneOf?.push({
283+
type: 'array',
284+
items: {
285+
type: 'string',
286+
enum: properties.length && !additionalProperties ? properties : undefined,
287+
examples: properties,
288+
},
289+
uniqueItems: true,
290+
});
291+
292+
return schema;
293+
}

packages/repository/src/repositories/legacy-juggler-bridge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ export class DefaultCrudRepository<
739739
}
740740

741741
protected toEntity<R extends T>(model: juggler.PersistedModel): R {
742-
return new this.entityClass(model.toObject()) as R;
742+
return new this.entityClass(model.toObject({onlySchema: false})) as R;
743743
}
744744

745745
protected toEntities<R extends T>(models: juggler.PersistedModel[]): R[] {

0 commit comments

Comments
 (0)