Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kysely Adapter' Database interface mismatch with provided schema in docs and kysely schema rules #10441

Open
AchalS-iglu opened this issue Mar 29, 2024 · 2 comments
Labels
adapters Changes related to the core code concerning database adapters bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@AchalS-iglu
Copy link

AchalS-iglu commented Mar 29, 2024

Adapter type

@auth/kysely-adapter

Environment

System:
OS: Linux 6.7 EndeavourOS
CPU: (8) x64 AMD Ryzen 7 3700U with Radeon Vega Mobile Gfx
Memory: 1.15 GB / 6.72 GB
Container: Yes
Shell: 5.2.26 - /bin/bash
Binaries:
Node: 21.4.0 - ~/.nvm/versions/node/v21.4.0/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v21.4.0/bin/yarn
npm: 10.2.4 - ~/.nvm/versions/node/v21.4.0/bin/npm
bun: 1.0.31 - ~/.bun/bin/bun
Browsers:
Chromium: 122.0.6261.128
npmPackages:
@auth/drizzle-adapter: ^0.7.0 => 0.7.0
@auth/kysely-adapter: ^0.6.1 => 0.6.1
next: ^14.1.3 => 14.1.4
next-auth: ^4.24.6 => 4.24.7
react: 18.2.0 => 18.2.0

Reproduction URL

https://github.com/traveltoindia-co-in/ta

Describe the issue

So, I used the schema provided in the docs but it is incompatible with the adapter's constructor KyselyAuth. Look at src/types/authTypes.ts. These types are then put into the Database interface in src/types/database.ts. Then that interface is further passed into the Adapter's constructor in file src/server/database.ts, however it is incompatible. The Adapter uses standard typescript typing rather than Kysely's format, meanwhile the docs have proper schema.

The adapter's internal database type refers to the core types.

Suggested interface for said type -

  id: GeneratedAlways<string>;
  name: string | null;
  email: string;
  emailVerified: Date | null;
  image: string | null;
}

interface AccountTable {
  id: GeneratedAlways<string>;
  userId: string;
  type: "oauth" | "oidc" | "email" | "webauthn"; // or however it is used
  provider: string;
  providerAccountId: string;
  refresh_token: string | null;
  access_token: string | null;
  expires_at: number | null;
  token_type: string | null;
  scope: string | null;
  id_token: string | null;
  session_state: string | null;
}

interface SessionTable {
  id: GeneratedAlways<string>;
  userId: string;
  sessionToken: string;
  expires: Date;
}

interface VerificationTokenTable {
  identifier: string;
  token: string;
  expires: Date;
}

I hope this is appropriate in terms of Kysely (I have actually never used kysely before). Also, Kysely tells to | null instead of optional ? or undefined. If I were to use Selectable instead of T (T being any of the table model above) then I believe it would match perfectly. However I am getting numerous errors on trying to assign Database schema to the adapter's constructor, there could be more, I do not know.

I hope I am correct I could be entirely wrong and using the lib incorrectly.

How to reproduce

Look at intellisense I suppose or run ts check

Expected behavior

No TS errors.

@AchalS-iglu AchalS-iglu added adapters Changes related to the core code concerning database adapters bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Mar 29, 2024
@AchalS-iglu
Copy link
Author

AchalS-iglu commented Mar 31, 2024

The error

  The types of 'User.id' are incompatible between these types.
    Type 'GeneratedAlways<string>' is not assignable to type 'string'.ts(2344)
AND
	"resource": "/home/achals/repos/ta/src/server/auth.ts",
	"owner": "typescript",
	"code": "2322",
	"severity": 8,
	"message": "Type 'import(\"/home/achals/repos/ta/node_modules/@auth/kysely-adapter/node_modules/@auth/core/adapters\").Adapter' is not assignable to type 'import(\"/home/achals/repos/ta/node_modules/next-auth/adapters\").Adapter'.\n  Types of property 'createUser' are incompatible.\n    Type '((user: AdapterUser) => Awaitable<AdapterUser>) | undefined' is not assignable to type '((user: Omit<AdapterUser, \"id\">) => Awaitable<AdapterUser>) | undefined'.\n      Type '(user: AdapterUser) => Awaitable<AdapterUser>' is not assignable to type '(user: Omit<AdapterUser, \"id\">) => Awaitable<AdapterUser>'.\n        Types of parameters 'user' and 'user' are incompatible.\n          Property 'id' is missing in type 'Omit<AdapterUser, \"id\">' but required in type 'AdapterUser'.",
	"source": "ts",
	"startLineNumber": 50,
	"startColumn": 3,
	"endLineNumber": 50,
	"endColumn": 10,
	"relatedInformation": [
		{
			"startLineNumber": 174,
			"startColumn": 5,
			"endLineNumber": 174,
			"endColumn": 7,
			"message": "'id' is declared here.",
			"resource": "/home/achals/repos/ta/node_modules/@auth/kysely-adapter/node_modules/@auth/core/adapters.d.ts"
		},
		{
			"startLineNumber": 106,
			"startColumn": 5,
			"endLineNumber": 106,
			"endColumn": 12,
			"message": "The expected type comes from property 'adapter' which is declared here on type 'AuthOptions'",
			"resource": "/home/achals/repos/ta/node_modules/next-auth/core/types.d.ts"
		}
	]
},{
	"resource": "/home/achals/repos/ta/src/server/auth.ts",
	"owner": "typescript",
	"code": "2345",
	"severity": 8,
	"message": "Argument of type 'Kysely<Database>' is not assignable to parameter of type 'Kysely<DatabaseSchema>'.\n  The types of 'transaction().execute' are incompatible between these types.\n    Type '<T>(callback: (trx: Transaction<Database>) => Promise<T>) => Promise<T>' is not assignable to type '<T>(callback: (trx: Transaction<DatabaseSchema>) => Promise<T>) => Promise<T>'.\n      Types of parameters 'callback' and 'callback' are incompatible.\n        Types of parameters 'trx' and 'trx' are incompatible.\n          Type 'Transaction<Database>' is not assignable to type 'Transaction<DatabaseSchema>'.\n            The types returned by 'connection()' are incompatible between these types.\n              Type 'ConnectionBuilder<Database>' is not assignable to type 'ConnectionBuilder<DatabaseSchema>'.\n                Type 'DatabaseSchema' is not assignable to type 'Database'.",
	"source": "ts",
	"startLineNumber": 50,
	"startColumn": 26,
	"endLineNumber": 50,
	"endColumn": 28
},{
	"resource": "/home/achals/repos/ta/src/server/auth.ts",
	"owner": "eslint",
	"code": {
		"value": "@typescript-eslint/no-unused-vars",
		"target": {
			"$mid": 1,
			"path": "/rules/no-unused-vars",
			"scheme": "https",
			"authority": "typescript-eslint.io"
		}
	},
	"severity": 4,
	"message": "'DrizzleAdapter' is defined but never used.",
	"source": "eslint",
	"startLineNumber": 1,
	"startColumn": 10,
	"endLineNumber": 1,
	"endColumn": 24
},{
	"resource": "/home/achals/repos/ta/src/server/auth.ts",
	"owner": "eslint",
	"code": {
		"value": "@typescript-eslint/no-unused-vars",
		"target": {
			"$mid": 1,
			"path": "/rules/no-unused-vars",
			"scheme": "https",
			"authority": "typescript-eslint.io"
		}
	},
	"severity": 4,
	"message": "'Adapter' is defined but never used.",
	"source": "eslint",
	"startLineNumber": 8,
	"startColumn": 15,
	"endLineNumber": 8,
	"endColumn": 22
}]

@Aryan3212
Copy link

Are you using some kind of code gen? Then this snippet might help.

import type { Codegen } from "@auth/kysely-adapter"
new KyselyAuth<Database, Codegen>(...)

Found in the docs here: https://authjs.dev/reference/adapter/kysely#kyselyauthdb-t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants