feat: introduce @auth/mikro-orm-adapter (#7794)

Database adapters are not dependent on Next.js features, so it makes sense to republish them under the `@auth/*` scope.

This PR is part of a series to convert adapters, using `@auth/core` for types.

BREAKING CHANGE:
If you are coming from the previous adapter, change your `package.json`:

```diff
-  "@next-auth/mikro-orm-adapter": "0.0.0",
+  "@auth/mikro-orm-adapter": "0.0.0",
```

And run `npm install`, `yarn install` or `pnpm install` respectively.

**Note:** This packages is published as ESM-only

This package assumes that `globalThis.crypto` is available.

In older Node.js versions, you can polyfill by adding:

`globalThis.crypto ??= require("node:crypto").webcrypto`
This commit is contained in:
Balázs Orbán
2023-06-13 16:39:43 +02:00
committed by GitHub
parent cd92aa0c82
commit 1d29b0d220
10 changed files with 73 additions and 57 deletions

View File

@@ -25,7 +25,7 @@ body:
- "@auth/dynamodb-adapter"
- "@next-auth/fauna-adapter"
- "@next-auth/firebase-adapter"
- "@next-auth/mikro-orm-adapter"
- "@auth/mikro-orm-adapter"
- "@auth/mongodb-adapter"
- "@next-auth/neo4j-adapter"
- "@next-auth/pouchdb-adapter"

View File

@@ -13,7 +13,7 @@ firebase:
- "@next-auth/firebase-adapter"
mikro-orm:
- "@next-auth/mikro-orm-adapter"
- "@auth/mikro-orm-adapter"
mongodb:
- "@auth/mongodb-adapter"

View File

@@ -8,14 +8,14 @@
</a>
<h3 align="center"><b>MikroORM Adapter</b> - NextAuth.js / Auth.js</a></h3>
<p align="center" style="align: center;">
<a href="https://npm.im/@next-auth/mikro-orm-adapter">
<a href="https://npm.im/@auth/mikro-orm-adapter">
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
</a>
<a href="https://npm.im/@next-auth/mikro-orm-adapter">
<img alt="npm" src="https://img.shields.io/npm/v/@next-auth/mikro-orm-adapter?color=green&label=@next-auth/mikro-orm-adapter&style=flat-square">
<a href="https://npm.im/@auth/mikro-orm-adapter">
<img alt="npm" src="https://img.shields.io/npm/v/@auth/mikro-orm-adapter?color=green&label=@auth/mikro-orm-adapter&style=flat-square">
</a>
<a href="https://www.npmtrends.com/@next-auth/mikro-orm-adapter">
<img src="https://img.shields.io/npm/dm/@next-auth/mikro-orm-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
<a href="https://www.npmtrends.com/@auth/mikro-orm-adapter">
<img src="https://img.shields.io/npm/dm/@auth/mikro-orm-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
</a>
<a href="https://github.com/nextauthjs/next-auth/stargazers">
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="Github Stars" />

View File

@@ -1,7 +1,7 @@
{
"name": "@next-auth/mikro-orm-adapter",
"version": "3.0.1",
"description": "MikroORM adapter for next-auth.",
"name": "@auth/mikro-orm-adapter",
"version": "0.0.0",
"description": "MikroORM adapter for Auth.js",
"homepage": "https://authjs.dev",
"repository": "https://github.com/nextauthjs/next-auth",
"bugs": {
@@ -11,7 +11,20 @@
"contributors": [
"Balázs Orbán <info@balazsorban.com>"
],
"main": "dist/index.js",
"type": "module",
"types": "./index.d.ts",
"files": [
"*.js",
"*.d.ts*",
"lib",
"src"
],
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
}
},
"license": "ISC",
"keywords": [
"next-auth",
@@ -27,25 +40,18 @@
"test": "jest --runInBand",
"build": "tsc"
},
"files": [
"README.md",
"dist"
],
"peerDependencies": {
"@mikro-orm/core": "^5",
"next-auth": "^4"
"@mikro-orm/core": "^5"
},
"devDependencies": {
"@mikro-orm/core": "^5",
"@mikro-orm/sqlite": "^5",
"@next-auth/adapter-test": "workspace:*",
"@next-auth/tsconfig": "workspace:*",
"@types/uuid": ">=8",
"jest": "^29",
"next-auth": "workspace:*"
"jest": "^29"
},
"dependencies": {
"uuid": "^9"
"@auth/core": "workspace:*"
},
"jest": {
"preset": "@next-auth/adapter-test/jest"

View File

@@ -9,10 +9,10 @@
* ## Installation
*
* ```bash npm2yarn2pnpm
* npm install next-auth @next-auth/dynamodb-adapter @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
* npm install @mikro-orm/core @auth/mikro-orm-adapter
* ```
*
* @module @next-auth/dynamodb-adapter
* @module @auth/mikro-orm-adapter
*/
import type {
Connection,
@@ -20,11 +20,11 @@ import type {
Options as ORMOptions,
} from "@mikro-orm/core"
import type { Adapter } from "next-auth/adapters"
import type { Adapter } from "@auth/core/adapters"
import { MikroORM, wrap } from "@mikro-orm/core"
import * as defaultEntities from "./entities"
import * as defaultEntities from "./lib/entities"
export { defaultEntities }
@@ -35,7 +35,7 @@ export { defaultEntities }
*
* ```typescript title="pages/api/auth/[...nextauth].ts"
* import NextAuth from "next-auth"
* import { MikroOrmAdapter } from "@next-auth/mikro-orm-adapter"
* import { MikroOrmAdapter } from "@auth/mikro-orm-adapter"
*
* export default NextAuth({
* adapter: MikroOrmAdapter({
@@ -67,7 +67,7 @@ export { defaultEntities }
* Property,
* Unique,
* } from "@mikro-orm/core"
* import { defaultEntities } from "@next-auth/mikro-orm-adapter"
* import { defaultEntities } from "@auth/mikro-orm-adapter"
*
* const { Account, Session } = defaultEntities
*
@@ -124,7 +124,7 @@ export { defaultEntities }
*
* ```typescript title="config/mikro-orm.ts"
* import { Options } from "@mikro-orm/core";
* import { defaultEntities } from "@next-auth/mikro-orm-adapter"
* import { defaultEntities } from "@auth/mikro-orm-adapter"
*
* const config: Options = {
* ...
@@ -186,7 +186,6 @@ export function MikroOrmAdapter<
* Method used in testing. You won't need to call this in your app.
* @internal
*/
// @ts-expect-error
async __disconnect() {
const em = await getEM()
await em.getConnection().close()
@@ -240,6 +239,7 @@ export function MikroOrmAdapter<
return wrap(user).toObject()
},
// @ts-expect-error
async linkAccount(data) {
const em = await getEM()
const user = await em.findOne(UserModel, { id: data.userId })
@@ -251,6 +251,7 @@ export function MikroOrmAdapter<
return wrap(account).toObject()
},
// @ts-expect-error
async unlinkAccount(provider_providerAccountId) {
const em = await getEM()
const account = await em.findOne(AccountModel, {

View File

@@ -1,5 +1,3 @@
import { v4 as randomUUID } from "uuid"
import {
Property,
Unique,
@@ -16,8 +14,7 @@ import type {
AdapterAccount,
AdapterSession,
VerificationToken as AdapterVerificationToken,
} from "next-auth/adapters"
import type { ProviderType } from "next-auth/providers"
} from "@auth/core/adapters"
type RemoveIndex<T> = {
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -27,7 +24,7 @@ type RemoveIndex<T> = {
@Entity()
export class User implements RemoveIndex<AdapterUser> {
@PrimaryKey()
id: string = randomUUID()
id: string = crypto.randomUUID()
@Property({ type: types.string, nullable: true })
name?: string
@@ -63,7 +60,7 @@ export class User implements RemoveIndex<AdapterUser> {
export class Session implements AdapterSession {
@PrimaryKey()
@Property({ type: types.string })
id: string = randomUUID()
id: string = crypto.randomUUID()
@ManyToOne({
entity: "User",
@@ -88,7 +85,7 @@ export class Session implements AdapterSession {
export class Account implements RemoveIndex<AdapterAccount> {
@PrimaryKey()
@Property({ type: types.string })
id: string = randomUUID()
id: string = crypto.randomUUID()
@ManyToOne({
entity: "User",
@@ -101,7 +98,7 @@ export class Account implements RemoveIndex<AdapterAccount> {
userId!: string
@Property({ type: types.string })
type!: ProviderType
type!: AdapterAccount["type"]
@Property({ type: types.string })
provider!: string

View File

@@ -13,13 +13,15 @@ import {
Options,
types,
} from "@mikro-orm/core"
import { randomUUID, runBasicTests } from "@next-auth/adapter-test"
import { runBasicTests } from "@next-auth/adapter-test"
globalThis.crypto ??= require("node:crypto").webcrypto
@Entity()
export class User implements defaultEntities.User {
@PrimaryKey()
@Property({ type: types.string })
id: string = randomUUID()
id: string = crypto.randomUUID()
@Property({ type: types.string, nullable: true })
name?: string
@@ -60,7 +62,7 @@ export class User implements defaultEntities.User {
export class VeryImportantEntity {
@PrimaryKey()
@Property({ type: types.string })
id: string = randomUUID()
id: string = crypto.randomUUID()
@Property({ type: types.boolean })
important = true

View File

@@ -1,13 +1,27 @@
{
"extends": "@next-auth/tsconfig/tsconfig.adapters.json",
"extends": "@next-auth/tsconfig/tsconfig.base.json",
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"allowJs": true,
"baseUrl": ".",
"isolatedModules": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"outDir": ".",
"rootDir": "src",
"outDir": "dist",
"stripInternal": true
"skipDefaultLibCheck": true,
"strictNullChecks": true,
"stripInternal": true,
"declarationMap": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": ["src"],
"exclude": ["dist", "test", "node_modules"]
}
"include": [
"src/**/*",
],
"exclude": [
"*.js",
"*.d.ts",
]
}

8
pnpm-lock.yaml generated
View File

@@ -327,24 +327,20 @@ importers:
packages/adapter-mikro-orm:
specifiers:
'@auth/core': workspace:*
'@mikro-orm/core': ^5
'@mikro-orm/sqlite': ^5
'@next-auth/adapter-test': workspace:*
'@next-auth/tsconfig': workspace:*
'@types/uuid': '>=8'
jest: ^29
next-auth: workspace:*
uuid: ^9
dependencies:
uuid: 9.0.0
'@auth/core': link:../core
devDependencies:
'@mikro-orm/core': 5.2.1_@mikro-orm+sqlite@5.2.1
'@mikro-orm/sqlite': 5.2.1_@mikro-orm+core@5.2.1
'@next-auth/adapter-test': link:../adapter-test
'@next-auth/tsconfig': link:../tsconfig
'@types/uuid': 8.3.4
jest: 29.3.0
next-auth: link:../next-auth
packages/adapter-mongodb:
specifiers:

View File

@@ -59,7 +59,7 @@
"@auth/dynamodb-adapter#build",
"@next-auth/fauna-adapter#build",
"@next-auth/firebase-adapter#build",
"@next-auth/mikro-orm-adapter#build",
"@auth/mikro-orm-adapter#build",
"@auth/mongodb-adapter#build",
"@next-auth/neo4j-adapter#build",
"@next-auth/pouchdb-adapter#build",
@@ -82,7 +82,7 @@
"@auth/dynamodb-adapter#build",
"@next-auth/fauna-adapter#build",
"@next-auth/firebase-adapter#build",
"@next-auth/mikro-orm-adapter#build",
"@auth/mikro-orm-adapter#build",
"@auth/mongodb-adapter#build",
"@next-auth/neo4j-adapter#build",
"@next-auth/pouchdb-adapter#build",