feat: allow data query to have single string as sort

This commit is contained in:
dswbx
2025-01-07 18:08:50 +01:00
parent 064bbba8aa
commit 6026613d29
2 changed files with 31 additions and 8 deletions

View File

@@ -1,16 +1,15 @@
import { describe, expect, test } from "bun:test";
import type { QueryObject } from "ufo";
import { WhereBuilder, type WhereQuery } from "../../src/data/entities/query/WhereBuilder";
import { Value } from "../../src/core/utils";
import { WhereBuilder, type WhereQuery, querySchema } from "../../src/data";
import { getDummyConnection } from "./helper";
const t = "t";
describe("data-query-impl", () => {
function qb() {
const c = getDummyConnection();
const kysely = c.dummyConnection.kysely;
return kysely.selectFrom(t).selectAll();
return kysely.selectFrom("t").selectAll();
}
function compile(q: QueryObject) {
function compile(q: WhereQuery) {
const { sql, parameters } = WhereBuilder.addClause(qb(), q).compile();
return { sql, parameters };
}
@@ -90,3 +89,20 @@ describe("data-query-impl", () => {
}
});
});
describe("data-query-impl: Typebox", () => {
test("sort", async () => {
const decode = (input: any, expected: any) => {
const result = Value.Decode(querySchema, input);
expect(result.sort).toEqual(expected);
};
const _dflt = { by: "id", dir: "asc" };
decode({ sort: "" }, _dflt);
decode({ sort: "name" }, { by: "name", dir: "asc" });
decode({ sort: "-name" }, { by: "name", dir: "desc" });
decode({ sort: "-posts.name" }, { by: "posts.name", dir: "desc" });
decode({ sort: "-1name" }, _dflt);
decode({ sort: { by: "name", dir: "desc" } }, { by: "name", dir: "desc" });
});
});