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 <[email protected]>
  • Loading branch information
aaqilniz committed Dec 28, 2023
1 parent d76f00c commit 5154497
Showing 1 changed file with 57 additions and 1 deletion.
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;
}

0 comments on commit 5154497

Please sign in to comment.