From de7eb7d72f875aa4c3baa996c85b530f3e245eeb Mon Sep 17 00:00:00 2001 From: Skye <46286597+Skye-31@users.noreply.github.com> Date: Tue, 27 Sep 2022 17:46:15 +0100 Subject: [PATCH] First() returns null instead of D1_NORESULTS --- .changeset/five-lizards-try.md | 5 +++++ src/model.ts | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 .changeset/five-lizards-try.md diff --git a/.changeset/five-lizards-try.md b/.changeset/five-lizards-try.md new file mode 100644 index 0000000..61ff475 --- /dev/null +++ b/.changeset/five-lizards-try.md @@ -0,0 +1,5 @@ +--- +"d1-orm": patch +--- + +Chore: Make first() return null on a D1_NORESULTS error, as it's inconsistent with local and remote, and that error is near meaningless. diff --git a/src/model.ts b/src/model.ts index 05a3179..fa71721 100644 --- a/src/model.ts +++ b/src/model.ts @@ -164,20 +164,27 @@ export class Model { /** * @param options The options for the query, see {@link GenerateQueryOptions} - * @returns Returns the first row that matches the where clause. + * @returns Returns the first row that matches the where clause, or null if no rows match. */ public async First( options: Pick, "where"> - ): Promise { + ): Promise { const statement = GenerateQuery( QueryType.SELECT, this.tableName, Object.assign(options, { limit: 1 }) ); - return this.#D1Orm - .prepare(statement.query) - .bind(...statement.bindings) - .first(); + try { + return this.#D1Orm + .prepare(statement.query) + .bind(...statement.bindings) + .first(); + } catch (e) { + if ((e as Error).message === "D1_NORESULTS") { + return null; + } + throw e; + } } /**