From 3a4606b328657612d1ef9431f9bd06f058fa75ea Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 6 Sep 2020 12:24:58 +0200 Subject: [PATCH] ModelQuery: allow specifying raw value for sortBy() --- package.json | 2 +- src/db/ModelQuery.ts | 4 ++-- test/ModelQuery.test.ts | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) 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', () => {