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 6975c7a306..766bddfc1c 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -178,15 +178,11 @@ "@better-fetch/fetch": "catalog:", "@opentelemetry/api": "^1.9.0", "better-call": "catalog:", - "@cloudflare/workers-types": ">=4", "jose": "^6.1.0", "kysely": "catalog:peer", "nanostores": "^1.0.1" }, "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 3545c194ea..c381c88276 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -12,6 +12,7 @@ export type { BetterAuthCookie, BetterAuthCookies, } 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 97f9a38476..2ec86b5888 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, @@ -29,10 +28,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/package.json b/packages/kysely-adapter/package.json index f3a90d27e6..e9af9bf8b8 100644 --- a/packages/kysely-adapter/package.json +++ b/packages/kysely-adapter/package.json @@ -59,7 +59,6 @@ } }, "devDependencies": { - "@cloudflare/workers-types": "^4.20250121.0", "@better-auth/core": "workspace:*", "@better-auth/utils": "catalog:", "kysely": "catalog:", diff --git a/packages/kysely-adapter/src/d1-sqlite-dialect.ts b/packages/kysely-adapter/src/d1-sqlite-dialect.ts index c4a952ad79..06629814a3 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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f9b9b98773..0a753734c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1289,9 +1289,6 @@ importers: '@better-auth/utils': specifier: 'catalog:' version: 0.4.2 - '@cloudflare/workers-types': - specifier: ^4.20250121.0 - version: 4.20260226.1 kysely: specifier: 'catalog:' version: 0.29.2