Comparing version 1.0.6 to 1.0.7
{ | ||
"name": "qql", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -1,2 +0,2 @@ | ||
import {canonicalizeJoins} from "../src/qql-util.js"; | ||
import {canonicalizeJoins, canonicalizeSort} from "../src/qql-util.js"; | ||
@@ -30,2 +30,31 @@ describe("qql-util",()=>{ | ||
}) | ||
it("can canonicalize sort",()=>{ | ||
expect( | ||
canonicalizeSort("hello") | ||
).toEqual( | ||
{"hello":"asc"} | ||
); | ||
expect( | ||
canonicalizeSort(["hello","asc"]) | ||
).toEqual( | ||
{"hello":"asc"} | ||
); | ||
expect( | ||
canonicalizeSort([["hello","asc"],["bla","DESC"]]) | ||
).toEqual( | ||
{"hello":"asc","bla":"desc"} | ||
); | ||
expect(canonicalizeSort()).toEqual({}); | ||
expect(canonicalizeSort({ | ||
hello: "ASC", | ||
world: "DESC" | ||
})).toEqual({ | ||
hello: "asc", | ||
world: "desc" | ||
}); | ||
}) | ||
}) |
@@ -21,2 +21,40 @@ export function canonicalizeJoins(joins) { | ||
return []; | ||
} | ||
export function canonicalizeSort(sort) { | ||
function sortDirection(cand) { | ||
if (!cand) | ||
return "asc"; | ||
cand=cand.toLowerCase(); | ||
if (!["asc","desc"].includes(cand)) | ||
throw new Error("Unknown sort direction: "+cand); | ||
return cand; | ||
} | ||
if (!sort) | ||
return {}; | ||
if (typeof sort=="string") | ||
return {[sort]: sortDirection()} | ||
if (Array.isArray(sort) && | ||
Array.isArray(sort[0])) { | ||
let res={}; | ||
for (let item of sort) | ||
res[item[0]]=sortDirection(item[1]); | ||
return res; | ||
} | ||
if (Array.isArray(sort)) { | ||
return {[sort[0]]: sortDirection(sort[1])} | ||
} | ||
sort={...sort}; | ||
for (let k in sort) | ||
sort[k]=sortDirection(sort[k]); | ||
return sort; | ||
} |
@@ -83,2 +83,6 @@ import {splitPath, getFileExt} from "./js-util.js"; | ||
let sort; | ||
if (url.searchParams.get("sort")) | ||
sort=JSON.parse(url.searchParams.get("sort")); | ||
let query={ | ||
@@ -88,3 +92,4 @@ manyFrom: argv[0], | ||
offset: range[0], | ||
limit: range[1]-range[0]+1 | ||
limit: range[1]-range[0]+1, | ||
sort: sort | ||
}; | ||
@@ -91,0 +96,0 @@ |
import Field from "./Field.js"; | ||
import Reference from "./Reference.js"; | ||
import {arrayOnlyUnique, assertAllowedKeys, arrayify, jsonClone} from "./js-util.js"; | ||
import {canonicalizeJoins} from "./qql-util.js"; | ||
import {canonicalizeJoins, canonicalizeSort} from "./qql-util.js"; | ||
@@ -454,3 +454,3 @@ export default class Table { | ||
async queryManyFrom(env, query) { | ||
assertAllowedKeys(query,["select","manyFrom","limit","offset","where","join"]); | ||
assertAllowedKeys(query,["select","manyFrom","limit","offset","where","join","sort"]); | ||
this.assertReadAccess(env); | ||
@@ -472,2 +472,9 @@ | ||
let sort=canonicalizeSort(query.sort); | ||
if (Object.keys(sort).length) { | ||
s+=" ORDER BY "+Object.keys(sort) | ||
.map(k=>this.qql.escapeId(k)+" "+sort[k]) | ||
.join(","); | ||
} | ||
if (query.offset && !query.limit) | ||
@@ -474,0 +481,0 @@ throw new Error("Can't have offset without limit"); |
57712
32
1819