From 072b2c29e393cd456f441210d40787645c2aba5c Mon Sep 17 00:00:00 2001 From: dannyball710 Date: Sat, 24 Jun 2023 02:20:46 +0800 Subject: [PATCH] Refactor column name quoting to use double quotes --- .changeset/silver-cherries-laugh.md | 5 +++++ src/model.ts | 9 +++++++-- src/queryBuilder.ts | 4 ++-- test/querybuilder.test.js | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .changeset/silver-cherries-laugh.md diff --git a/.changeset/silver-cherries-laugh.md b/.changeset/silver-cherries-laugh.md new file mode 100644 index 0000000..7661c00 --- /dev/null +++ b/.changeset/silver-cherries-laugh.md @@ -0,0 +1,5 @@ +--- +"d1-orm": minor +--- + +Add the function of select columns diff --git a/src/model.ts b/src/model.ts index 37e0f53..31f1be6 100644 --- a/src/model.ts +++ b/src/model.ts @@ -224,12 +224,17 @@ export class Model> { * @returns Returns the first row that matches the where clause, or null if no rows match. */ public async First( - options: Pick>, "where" | "columns"> + options: Pick< + GenerateQueryOptions>, + "where" | "columns" + > ): Promise | null> { const statement = GenerateQuery( QueryType.SELECT, this.tableName, - Object.assign(options, { limit: 1 }) as GenerateQueryOptions> + Object.assign(options, { limit: 1 }) as GenerateQueryOptions< + InferFromColumns + > ); try { return await this.D1Orm.prepare(statement.query) diff --git a/src/queryBuilder.ts b/src/queryBuilder.ts index 272e581..400be85 100644 --- a/src/queryBuilder.ts +++ b/src/queryBuilder.ts @@ -23,7 +23,7 @@ export enum QueryType { * **data** - The data to insert, or update with. This is an object with the column names as keys and the values as values. In the case of Upsert, `upsertOnlyUpdateData` is also required, and that will be the data to update with, if an `ON CONFLICT` clause is matched. * * **columns** - The columns to select. This is an array of column names. - * + * * **upsertOnlyUpdateData** - The data to update with, if an `ON CONFLICT` clause is matched. This is an object with the column names as keys and the values as values. * @typeParam T - The type of the object to query. This is generally not needed to be specified, but can be useful if you're calling this yourself instead of through a {@link Model}. */ @@ -75,7 +75,7 @@ export function GenerateQuery( case QueryType.SELECT: { let columns = "*"; if (options.columns && options.columns.length > 0) { - columns = options.columns.map((x) => `\`${String(x)}\``).join(", "); + columns = options.columns.map((x) => `"${String(x)}"`).join(", "); } query = `SELECT ${columns} FROM \`${tableName}\``; if (options.where) { diff --git a/test/querybuilder.test.js b/test/querybuilder.test.js index f201e86..ba01df0 100644 --- a/test/querybuilder.test.js +++ b/test/querybuilder.test.js @@ -114,9 +114,9 @@ describe("Query Builder", () => { }); it("should generate a query with a select columns", () => { const statement = GenerateQuery(QueryType.SELECT, "test", { - columns: ["id", "name"] + columns: ["id", "name"], }); - expect(statement.query).to.equal("SELECT `id`, `name` FROM `test`"); + expect(statement.query).to.equal('SELECT "id", "name" FROM `test`'); }); it("should ignore an empty where clause object", () => { const statement = GenerateQuery(QueryType.SELECT, "test", {