diff --git a/src/table.ts b/src/table.ts index 3c71a89..7c76cbb 100644 --- a/src/table.ts +++ b/src/table.ts @@ -362,8 +362,28 @@ export class Table { * * @returns {RowShape[]} An array of all rows in the table. */ - public findAll(): RowShape[] { - return this._db.prepare(`SELECT * FROM ${this}`).all(); + public findAll(options?: { + order?: string; + limit?: number; + skip?: number; + }): RowShape[] { + const optionParts: string[] = []; + + if (options?.order) { + optionParts.push(`ORDER BY ${options.order}`); + } + + if (options?.limit) { + optionParts.push(`LIMIT ${options.limit}`); + + if (options?.skip) { + optionParts.push(`OFFSET ${options.skip}`); + } + } + + return this._db + .prepare(`SELECT * FROM ${this} ${optionParts.join(" ")}`) + .all(); } /** @@ -377,7 +397,7 @@ export class Table { const whereParts = keys.map((key) => `"${key}" = $${key}`); const whereClause = whereParts.join(" AND "); const query = this._db.prepare( - `SELECT * FROM ${this} WHERE ${whereClause};`, + `SELECT * FROM ${this} WHERE ${whereClause}; `, ); return query.get(shapeForQuery(conditions)); @@ -417,25 +437,25 @@ export class Table { }, ): RowShape[] { const keys = Object.keys(conditions); - const whereParts = keys.map((key) => `"${key}" = $${key}`); + const whereParts = keys.map((key) => `"${key}" = $${key} `); const whereClause = whereParts.join(" AND "); const optionParts: string[] = []; if (options?.order) { - optionParts.push(`ORDER BY ${options.order}`); + optionParts.push(`ORDER BY ${options.order} `); } if (options?.limit) { - optionParts.push(`LIMIT ${options.limit}`); + optionParts.push(`LIMIT ${options.limit} `); if (options?.skip) { - optionParts.push(`OFFSET ${options.skip}`); + optionParts.push(`OFFSET ${options.skip} `); } } const query = this._db.prepare( - `SELECT * FROM ${this} WHERE ${whereClause} ${optionParts.join(" ")};`, + `SELECT * FROM ${this} WHERE ${whereClause} ${optionParts.join(" ")}; `, ); return query.all(shapeForQuery(conditions)); @@ -472,7 +492,7 @@ export class Table { ); } - this._deleteQuery.run(`${id}`); + this._deleteQuery.run(`${id} `); }; /** @@ -482,10 +502,10 @@ export class Table { */ public deleteWhere(conditions: Partial): void { const keys = Object.keys(conditions); - const whereParts = keys.map((key) => `"${key}" = $${key}`); + const whereParts = keys.map((key) => `"${key}" = $${key} `); const whereClause = whereParts.join(" AND "); const query = this._db.prepare( - `DELETE FROM ${this} WHERE ${whereClause};`, + `DELETE FROM ${this} WHERE ${whereClause}; `, ); query.run(shapeForQuery(conditions)); diff --git a/test/table.test.ts b/test/table.test.ts index ba48354..03550b6 100644 --- a/test/table.test.ts +++ b/test/table.test.ts @@ -183,6 +183,39 @@ test(".findAll() returns the full table", () => { expect(allUsers[2]).toEqual({ user_id: 3, name: "Sayid" }); }); +test(".findAll() can set a limit on returned rows", () => { + const table = makeTestTable(); + table.insertPartial({ name: "Jack" }); + table.insertPartial({ name: "Kate" }); + table.insertPartial({ name: "Sayid" }); + const allUsers = table.findAll({ limit: 1 }); + + expect(allUsers).toHaveLength(1); + expect(allUsers[0]).toEqual({ user_id: 1, name: "Jack" }); +}); + +test(".findAll() can set a skip and limit on returned rows", () => { + const table = makeTestTable(); + table.insertPartial({ name: "Jack" }); + table.insertPartial({ name: "Kate" }); + table.insertPartial({ name: "Sayid" }); + const allUsers = table.findAll({ limit: 1, skip: 1 }); + + expect(allUsers).toHaveLength(1); + expect(allUsers[0]).toEqual({ user_id: 2, name: "Kate" }); +}); + +test(".findAll() can set an order for returned rows", () => { + const table = makeTestTable(); + table.insertPartial({ name: "Jack" }); + table.insertPartial({ name: "Kate" }); + table.insertPartial({ name: "Sayid" }); + const allUsers = table.findAll({ order: "user_id DESC" }); + + expect(allUsers).toHaveLength(3); + expect(allUsers[0]).toEqual({ user_id: 3, name: "Sayid" }); +}); + test(".query() returns a prepared statement for the table", () => { const table = makeTestTable(); const query = table.query("SELECT * FROM Users WHERE name = ?");