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",
"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 <alice@gaudon.pro>",

View File

@ -94,8 +94,8 @@ export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M
return this;
}
public sortBy(field: string, direction: 'ASC' | 'DESC' = 'ASC'): this {
this._sortBy = field.split('.').map(v => 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;
}

View File

@ -7,6 +7,10 @@ describe('Test ModelQuery', () => {
const query = ModelQuery.select({table: 'model'} as unknown as ModelFactory<Model>)
.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<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', () => {