diff --git a/.changeset/quiet-d1-types.md b/.changeset/quiet-d1-types.md new file mode 100644 index 0000000000..e1ac04c6d3 --- /dev/null +++ b/.changeset/quiet-d1-types.md @@ -0,0 +1,5 @@ +--- +"@better-auth/core": patch +--- + +Restore database option type inference for projects that do not use Cloudflare Workers. diff --git a/packages/core/package.json b/packages/core/package.json index ba6637574c..471821e4b5 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -184,7 +184,6 @@ "peerDependencies": { "@better-auth/utils": "catalog:", "@better-fetch/fetch": "catalog:", - "@cloudflare/workers-types": ">=4", "@opentelemetry/api": "^1.9.0", "better-call": "catalog:", "jose": "^6.1.0", @@ -192,9 +191,6 @@ "nanostores": "catalog:peer" }, "peerDependenciesMeta": { - "@cloudflare/workers-types": { - "optional": true - }, "@opentelemetry/api": { "optional": true } diff --git a/packages/core/src/types/database.test.ts b/packages/core/src/types/database.test.ts new file mode 100644 index 0000000000..471e7f0ce6 --- /dev/null +++ b/packages/core/src/types/database.test.ts @@ -0,0 +1,9 @@ +import type { D1Database as CloudflareD1Database } from "@cloudflare/workers-types"; +import { describe, expectTypeOf, it } from "vitest"; +import type { D1Database } from "./database"; + +describe("D1Database", () => { + it("accepts Cloudflare's D1 database type", () => { + expectTypeOf().toExtend(); + }); +}); diff --git a/packages/core/src/types/database.ts b/packages/core/src/types/database.ts new file mode 100644 index 0000000000..519c361c88 --- /dev/null +++ b/packages/core/src/types/database.ts @@ -0,0 +1,21 @@ +type D1Result = { + results: T[]; + meta: { + changes: number; + last_row_id: number; + }; +}; + +interface D1PreparedStatement { + bind(...values: unknown[]): D1PreparedStatement; + all>(): Promise>; +} + +/** + * A Cloudflare D1-compatible database. + */ +export interface D1Database { + prepare(query: string): D1PreparedStatement; + batch(statements: D1PreparedStatement[]): Promise[]>; + exec(query: string): Promise; +} diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index d9efc3bd1b..223a4be99b 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -14,6 +14,7 @@ export type { BetterAuthCookies, CookieCachePayload, } from "./cookie"; +export type { D1Database } from "./database"; export type * from "./helper"; export type { BaseURLConfig, diff --git a/packages/core/src/types/init-options.ts b/packages/core/src/types/init-options.ts index ba1d76b15b..85f43eb00e 100644 --- a/packages/core/src/types/init-options.ts +++ b/packages/core/src/types/init-options.ts @@ -1,6 +1,5 @@ import type { Database as BunDatabase } from "bun:sqlite"; import type { DatabaseSync } from "node:sqlite"; -import type { D1Database } from "@cloudflare/workers-types"; import type { CookieOptions } from "better-call"; import type { Dialect, @@ -28,10 +27,12 @@ import type { BaseVerification } from "../db/schema/verification"; import type { Logger } from "../env"; import type { SocialProviderList, SocialProviders } from "../social-providers"; import type { AuthContext, GenericEndpointContext } from "./context"; +import type { D1Database } from "./database"; import type { Awaitable, LiteralString, LiteralUnion } from "./helper"; import type { BetterAuthPlugin } from "./plugin"; type KyselyDatabaseType = "postgres" | "mysql" | "sqlite" | "mssql"; + type Optional = { [P in keyof T]?: T[P] | undefined; }; diff --git a/packages/kysely-adapter/src/d1-sqlite-dialect.ts b/packages/kysely-adapter/src/d1-sqlite-dialect.ts index fbfa7f9536..98d6d74740 100644 --- a/packages/kysely-adapter/src/d1-sqlite-dialect.ts +++ b/packages/kysely-adapter/src/d1-sqlite-dialect.ts @@ -1,4 +1,4 @@ -import type { D1Database } from "@cloudflare/workers-types"; +import type { D1Database } from "@better-auth/core"; import type { CompiledQuery, DatabaseConnection,