ModelQuery: allow specifying raw value for sortBy()

This commit is contained in:
Alice Gaudon 2020-09-06 12:24:58 +02:00
parent 23d54c4f03
commit 3a4606b328
3 changed files with 7 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "wms-core", "name": "wms-core",
"version": "0.22.0-rc.1", "version": "0.22.0-rc.2",
"description": "Node web application framework and toolbelt.", "description": "Node web application framework and toolbelt.",
"repository": "https://gitlab.com/ArisuOngaku/wms-core", "repository": "https://gitlab.com/ArisuOngaku/wms-core",
"author": "Alice Gaudon <alice@gaudon.pro>", "author": "Alice Gaudon <alice@gaudon.pro>",

View File

@ -94,8 +94,8 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
return this; return this;
} }
public sortBy(field: string, direction: 'ASC' | 'DESC' = 'ASC'): this { public sortBy(field: string, direction: 'ASC' | 'DESC' = 'ASC', raw: boolean = false): this {
this._sortBy = field.split('.').map(v => v.startsWith('`') ? v : `\`${v}\``).join('.'); this._sortBy = raw ? field : field.split('.').map(v => v.startsWith('`') ? v : `\`${v}\``).join('.');
this._sortDirection = direction; this._sortDirection = direction;
return this; return this;
} }

View File

@ -7,6 +7,10 @@ describe('Test ModelQuery', () => {
const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory<Model>) const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory<Model>)
.sortBy('model.f2', 'ASC'); .sortBy('model.f2', 'ASC');
expect(query.toString(true)).toBe('SELECT `model`.* FROM `model` ORDER BY `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<Model>)
.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', () => { test('update', () => {