diff --git a/package.json b/package.json index 9a06655..dead828 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wms-core", - "version": "0.22.0-rc.1", + "version": "0.22.0-rc.2", "description": "Node web application framework and toolbelt.", "repository": "https://gitlab.com/ArisuOngaku/wms-core", "author": "Alice Gaudon ", diff --git a/src/db/ModelQuery.ts b/src/db/ModelQuery.ts index 28f8250..ba73ff5 100644 --- a/src/db/ModelQuery.ts +++ b/src/db/ModelQuery.ts @@ -94,8 +94,8 @@ export default class ModelQuery implements WhereFieldConsumer v.startsWith('`') ? v : `\`${v}\``).join('.'); + public sortBy(field: string, direction: 'ASC' | 'DESC' = 'ASC', raw: boolean = false): this { + this._sortBy = raw ? field : field.split('.').map(v => v.startsWith('`') ? v : `\`${v}\``).join('.'); this._sortDirection = direction; return this; } diff --git a/test/ModelQuery.test.ts b/test/ModelQuery.test.ts index fdf81ad..c7f42f3 100644 --- a/test/ModelQuery.test.ts +++ b/test/ModelQuery.test.ts @@ -7,6 +7,10 @@ describe('Test ModelQuery', () => { const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory) .sortBy('model.f2', 'ASC'); expect(query.toString(true)).toBe('SELECT `model`.* FROM `model` ORDER BY `model`.`f2` ASC'); + + const queryRaw = ModelQuery.select({table: 'model'} as unknown as ModelFactory) + .sortBy('coalesce(model.f1, model.f2)', 'ASC', true); + expect(queryRaw.toString(true)).toBe('SELECT `model`.* FROM `model` ORDER BY coalesce(model.f1, model.f2) ASC'); }); test('update', () => {