Add order/limit support to find queries

This commit is contained in:
Endeavorance 2025-04-18 10:27:48 -04:00
parent b4eeb2c296
commit 153725f785
4 changed files with 91 additions and 18 deletions

View file

@ -222,6 +222,45 @@ test(".findAllWhere() returns a row based on basic criteria", () => {
expect(manyJacks[2]).toEqual({ user_id: 3, name: "Jack" });
});
test(".findAllWhere() can set a limit on returned rows", () => {
const table = makeTestTable();
table.insertPartial({ name: "Jack" });
table.insertPartial({ name: "Jack" });
table.insertPartial({ name: "Jack" });
const manyJacks = table.findAllWhere({ name: "Jack" }, { limit: 1 });
expect(manyJacks).toHaveLength(1);
expect(manyJacks[0]).toEqual({ user_id: 1, name: "Jack" });
});
test(".findAllWhere() can set an offset on a limit", () => {
const table = makeTestTable();
table.insertPartial({ name: "Jack" });
table.insertPartial({ name: "Jack" });
table.insertPartial({ name: "Jack" });
const manyJacks = table.findAllWhere({ name: "Jack" }, { limit: 1, skip: 1 });
expect(manyJacks).toHaveLength(1);
expect(manyJacks[0]).toEqual({ user_id: 2, name: "Jack" });
});
test(".findAllWhere() can set an order on the results", () => {
const table = makeTestTable();
table.insertPartial({ name: "Jack" });
table.insertPartial({ name: "Jack" });
table.insertPartial({ name: "Jack" });
const manyJacks = table.findAllWhere(
{ name: "Jack" },
{ order: "user_id DESC" },
);
expect(manyJacks).toHaveLength(3);
expect(manyJacks[0]).toEqual({ user_id: 3, name: "Jack" });
});
test(".findOneById() returns a single element by its primary column", () => {
const table = makeTestTable();