Skip to content

Commit

Permalink
feat: provide support for groupby
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
  • Loading branch information
aaqilniz committed Dec 29, 2023
1 parent d76f00c commit 05ea0ec
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
58 changes: 57 additions & 1 deletion packages/repository-json-schema/src/filter-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,26 @@ export function getFilterJsonSchemaFor(
minimum: 1,
examples: [100],
},

sum: {
type: 'string',
examples: ['column1'],
},
min: {
type: 'string',
examples: ['column1'],
},
max: {
type: 'string',
examples: ['column1'],
},
avg: {
type: 'string',
examples: ['column1'],
},
count: {
type: 'string',
examples: ['column1'],
},
skip: {
type: 'integer',
minimum: 0,
Expand All @@ -120,6 +139,9 @@ export function getFilterJsonSchemaFor(
if (!excluded.includes('fields')) {
properties.fields = getFieldsJsonSchemaFor(modelCtor, options);
}
if (!excluded.includes('groupBy')) {
properties.fields = getGroupByJsonSchemaFor(modelCtor, options);
}

// Remove excluded properties
for (const p of excluded) {
Expand Down Expand Up @@ -235,3 +257,37 @@ export function getFieldsJsonSchemaFor(

return schema;
}

export function getGroupByJsonSchemaFor(
modelCtor: typeof Model,
options: FilterSchemaOptions = {},
): JsonSchema {
const schema: JsonSchema = {oneOf: []};
if (options.setTitle !== false) {
schema.title = `${modelCtor.modelName}.GroupBy`;
}

const properties = Object.keys(modelCtor.definition.properties);
const additionalProperties = modelCtor.definition.settings.strict === false;

schema.oneOf?.push({
type: 'object',
properties: properties.reduce(
(prev, crr) => ({...prev, [crr]: {type: 'boolean'}}),
{},
),
additionalProperties,
});

schema.oneOf?.push({
type: 'array',
items: {
type: 'string',
enum: properties.length && !additionalProperties ? properties : undefined,
examples: properties,
},
uniqueItems: true,
});

return schema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ export class DefaultCrudRepository<
}

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

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

0 comments on commit 05ea0ec

Please sign in to comment.