Skip to content

Commit

Permalink
fix(database): sequelize query sorting (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
kon14 committed Dec 23, 2022
1 parent 62c6844 commit a076852
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 39 deletions.
4 changes: 2 additions & 2 deletions libraries/grpc-sdk/src/classes/ConduitActiveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export class ConduitActiveSchema<T> extends ConduitSchema {
findMany(
query: Query<T>,
select?: string,
skip?: number,
skip?: -1 | 1,
limit?: number,
sort?: { [key: string]: number } | string[],
sort?: { [field: string]: -1 | 1 } | string[] | string,
populate?: string | string[],
): Promise<T[]> {
return this.dbInstance.findMany<T>(
Expand Down
17 changes: 6 additions & 11 deletions libraries/grpc-sdk/src/modules/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export class DatabaseProvider extends ConduitModule<typeof DatabaseProviderDefin
});
}

constructSortObj(sort: string[]) {
const sortObj: { [key: string]: number } = {};
private constructSortObj(sort: string[]) {
const sortObj: { [field: string]: -1 | 1 } = {};
sort.forEach((sortVal: string) => {
sortVal = sortVal.trim();
if (sortVal.indexOf('-') !== -1) {
Expand All @@ -136,16 +136,11 @@ export class DatabaseProvider extends ConduitModule<typeof DatabaseProviderDefin
select?: string,
skip?: number,
limit?: number,
sort?: { [key: string]: number } | string[],
sort?: { [field: string]: -1 | 1 } | string[] | string,
populate?: string | string[],
): Promise<T[]> {
let sortStr;
if (Array.isArray(sort)) {
sortStr = JSON.stringify(this.constructSortObj(sort));
} else {
sortStr = sort ? JSON.stringify(sort) : undefined;
}

if (typeof sort === 'string') sort = [sort];
const sortObj = Array.isArray(sort) ? this.constructSortObj(sort) : sort;
let populateArray = populate;
if (populate && !Array.isArray(populate)) {
populateArray = [populate];
Expand All @@ -156,7 +151,7 @@ export class DatabaseProvider extends ConduitModule<typeof DatabaseProviderDefin
select: select === null ? undefined : select,
skip,
limit,
sort: sortStr,
sort: sortObj,
populate: (populateArray as string[]) ?? [],
}).then(res => {
return JSON.parse(res.result);
Expand Down
23 changes: 13 additions & 10 deletions modules/database/src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,25 @@ export default class DatabaseModule extends ManagedModule<void> {

async findMany(call: GrpcRequest<FindRequest>, callback: GrpcResponse<QueryResponse>) {
try {
const skip = call.request.skip;
const limit = call.request.limit;
const select = call.request.select;
const sort: { [key: string]: number } = call.request.sort
? JSON.parse(call.request.sort)
: null;
const populate = call.request.populate;

const { skip, limit, select, populate } = call.request;
const sort = call.request.sort as { [key: string]: -1 | 1 | number } | undefined;
if (sort) {
Object.keys(sort).forEach(field => {
if (sort[field] !== 1 && sort[field] !== -1) {
return callback({
code: status.INVALID_ARGUMENT,
message: `Invalid sort field value "${sort[field]}" in field "${field}", should be -1 or 1.`,
});
}
});
}
const schemaAdapter = this._activeAdapter.getSchemaModel(call.request.schemaName);

const docs = await schemaAdapter.model.findMany(
call.request.query,
skip,
limit,
select,
sort,
sort as { [key: string]: -1 | 1 } | undefined,
populate,
);
callback(null, { result: JSON.stringify(docs) });
Expand Down
16 changes: 4 additions & 12 deletions modules/database/src/adapters/sequelize-adapter/SequelizeSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import {
} from '../../interfaces';
import { createWithPopulations, parseQuery } from './utils';
import { SequelizeAdapter } from './index';
import ConduitGrpcSdk, {
ConduitModelField,
ConduitSchema,
Indexable,
} from '@conduitplatform/grpc-sdk';
import ConduitGrpcSdk, { ConduitSchema, Indexable } from '@conduitplatform/grpc-sdk';

const deepdash = require('deepdash/standalone');

Expand Down Expand Up @@ -179,7 +175,7 @@ export class SequelizeSchema implements SchemaAdapter<ModelCtor<any>> {
skip?: number,
limit?: number,
select?: string,
sort?: { [key: string]: number },
sort?: { [field: string]: -1 | 1 },
populate?: string[],
): Promise<any> {
let parsedQuery: ParsedQuery | ParsedQuery[];
Expand Down Expand Up @@ -486,14 +482,10 @@ export class SequelizeSchema implements SchemaAdapter<ModelCtor<any>> {
return { exclude };
}

private parseSort(sort: { [key: string]: number }): Order {
private parseSort(sort: { [field: string]: -1 | 1 }) {
const order: Order = [];
Object.keys(sort).forEach(field => {
if (field[0] === '-') {
order.push([field.slice(1), 'DESC'] as OrderItem);
} else {
order.push([field, 'ASC'] as OrderItem);
}
order.push([field, sort[field] === 1 ? 'ASC' : 'DESC'] as OrderItem);
});
return order;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/database/src/database.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ message FindRequest {
optional string select = 3;
optional int32 skip = 4;
optional int32 limit = 5;
optional string sort = 6;
map<string, int32> sort = 6; // -1 | 1
repeated string populate = 7;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class CustomEndpointHandler {
);
}

let sort: { [key: string]: number } | undefined = undefined;
let sort: { [field: string]: -1 | 1 } | undefined = undefined;
if (endpoint.sorted && params.sort && params.sort.length > 0) {
sort = constructSortObj(params.sort);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/database/src/handlers/cms/crud.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CmsHandlers {
limitNumber = Number.parseInt(limit as string);
}

let parsedSort: { [key: string]: number } | undefined = undefined;
let parsedSort: { [key: string]: -1 | 1 } | undefined = undefined;
if (sort && sort.length > 0) {
parsedSort = constructSortObj(sort);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/database/src/handlers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function constructSortObj(sort: string[]) {
const sortObj: { [key: string]: number } = {};
const sortObj: { [field: string]: -1 | 1 } = {};
sort.forEach((sortVal: string) => {
sortVal = sortVal.trim();
if (sortVal.indexOf('-') !== -1) {
Expand Down

0 comments on commit a076852

Please sign in to comment.