Skip to content

Commit

Permalink
Refactor column name quoting to use double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyball710 committed Jun 23, 2023
1 parent 1f6a55c commit 072b2c2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-cherries-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"d1-orm": minor
---

Add the function of select columns
9 changes: 7 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,17 @@ export class Model<T extends Record<string, ModelColumn>> {
* @returns Returns the first row that matches the where clause, or null if no rows match.
*/
public async First(
options: Pick<GenerateQueryOptions<InferFromColumns<T>>, "where" | "columns">
options: Pick<
GenerateQueryOptions<InferFromColumns<T>>,
"where" | "columns"
>
): Promise<InferFromColumns<T> | null> {
const statement = GenerateQuery(
QueryType.SELECT,
this.tableName,
Object.assign(options, { limit: 1 }) as GenerateQueryOptions<InferFromColumns<T>>
Object.assign(options, { limit: 1 }) as GenerateQueryOptions<
InferFromColumns<T>
>
);
try {
return await this.D1Orm.prepare(statement.query)
Expand Down
4 changes: 2 additions & 2 deletions src/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*/
Expand Down Expand Up @@ -75,7 +75,7 @@ export function GenerateQuery<T extends object>(
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) {
Expand Down
4 changes: 2 additions & 2 deletions test/querybuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 072b2c2

Please sign in to comment.