Fix limit / nested query

Fixes #168, fixes #179
This commit is contained in:
rijkvanzanten
2020-09-02 16:47:43 -04:00
parent 177266d530
commit 9d30b17a73
4 changed files with 38 additions and 7 deletions

View File

@@ -44,6 +44,11 @@ export default async function runAST(ast: AST, query = ast.query) {
// Query defaults
query.limit = query.limit || 100;
if (query.limit === -1) {
delete query.limit;
}
query.sort = query.sort || [{ column: primaryKeyField, order: 'asc' }];
await applyQuery(ast.name, dbQuery, query);
@@ -116,7 +121,7 @@ export default async function runAST(ast: AST, query = ast.query) {
*/
if (batchQuery.limit) {
tempLimit = batchQuery.limit;
delete batchQuery.limit;
batchQuery.limit = -1;
}
}

View File

@@ -40,9 +40,10 @@ export default class FieldsService {
if (collection) {
fields = (await nonAuthorizedItemsService.readByQuery({
filter: { collection: { _eq: collection } },
limit: -1
})) as FieldMeta[];
} else {
fields = (await nonAuthorizedItemsService.readByQuery({})) as FieldMeta[];
fields = (await nonAuthorizedItemsService.readByQuery({ limit: -1 })) as FieldMeta[];
}
fields = (await this.payloadService.processValues('read', fields)) as FieldMeta[];

View File

@@ -17,7 +17,7 @@ import env from '../env';
type Action = 'create' | 'read' | 'update';
type Transformers = {
[type: string]: (action: Action, value: any, payload: Partial<Item>) => Promise<any>;
[type: string]: (action: Action, value: any, payload: Partial<Item>, accountability: Accountability | null) => Promise<any>;
};
export default class PayloadService {
@@ -93,6 +93,30 @@ export default class PayloadService {
async conceal(action, value) {
if (action === 'read') return value ? '**********' : null;
return value;
},
async 'user-created'(action, value, payload, accountability) {
if (action === 'create') return accountability?.user || null;
return value;
},
async 'user-updated'(action, value, payload, accountability) {
if (action === 'update') return accountability?.user || null;
return value;
},
async 'role-created'(action, value, payload, accountability) {
if (action === 'create') return accountability?.role || null;
return value;
},
async 'role-updated'(action, value, payload, accountability) {
if (action === 'update') return accountability?.role || null;
return value;
},
async 'date-created'(action, value) {
if (action === 'create') return new Date();
return value;
},
async 'date-updated'(action, value) {
if (action === 'update') return new Date();
return value;
}
};
@@ -124,7 +148,7 @@ export default class PayloadService {
processedPayload.map(async (record: any) => {
await Promise.all(
specialFieldsInCollection.map(async (field) => {
const newValue = await this.processField(field, record, action);
const newValue = await this.processField(field, record, action, this.accountability);
if (newValue !== undefined) record[field.field] = newValue;
})
);
@@ -151,7 +175,8 @@ export default class PayloadService {
async processField(
field: Pick<FieldMeta, 'field' | 'special'>,
payload: Partial<Item>,
action: Action
action: Action,
accountability: Accountability | null
) {
if (!field.special) return payload[field.field];
@@ -162,7 +187,7 @@ export default class PayloadService {
for (const special of fieldSpecials) {
if (this.transformers.hasOwnProperty(special)) {
value = await this.transformers[special](action, value, payload);
value = await this.transformers[special](action, value, payload, accountability);
}
}

View File

@@ -56,7 +56,7 @@ const localTypeMap: Record<string, { type: typeof types[number]; useTimezone?: b
// Postgres
json: { type: 'json' },
uuid: { type: 'string' },
uuid: { type: 'uuid' },
int2: { type: 'integer' },
serial4: { type: 'integer' },
int4: { type: 'integer' },