feat: introduce @auth/neo4j-adapter (#7804)

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/neo4j-adapter": "0.0.0",
+  "@auth/neo4j-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-14 13:26:38 +02:00
committed by GitHub
parent 8a0b11fcd6
commit 6e4516a9f8
10 changed files with 130 additions and 113 deletions

View File

@@ -27,7 +27,7 @@ body:
- "@auth/firebase-adapter"
- "@auth/mikro-orm-adapter"
- "@auth/mongodb-adapter"
- "@next-auth/neo4j-adapter"
- "@auth/neo4j-adapter"
- "@next-auth/pouchdb-adapter"
- "@auth/prisma-adapter"
- "@next-auth/sequelize-adapter"

View File

@@ -19,7 +19,7 @@ mongodb:
- "@auth/mongodb-adapter"
neo4j:
- "@next-auth/neo4j-adapter"
- "@auth/neo4j-adapter"
pouchdb:
- "@next-auth/pouchdb-adapter"

View File

@@ -8,14 +8,14 @@
</a>
<h3 align="center"><b>Neo4j Adapter</b> - NextAuth.js / Auth.js</a></h3>
<p align="center" style="align: center;">
<a href="https://npm.im/@next-auth/neo4j-adapter">
<a href="https://npm.im/@auth/neo4j-adapter">
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
</a>
<a href="https://npm.im/@next-auth/neo4j-adapter">
<img alt="npm" src="https://img.shields.io/npm/v/@next-auth/neo4j-adapter?color=green&label=@next-auth/neo4j-adapter&style=flat-square">
<a href="https://npm.im/@auth/neo4j-adapter">
<img alt="npm" src="https://img.shields.io/npm/v/@auth/neo4j-adapter?color=green&label=@auth/neo4j-adapter&style=flat-square">
</a>
<a href="https://www.npmtrends.com/@next-auth/neo4j-adapter">
<img src="https://img.shields.io/npm/dm/@next-auth/neo4j-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
<a href="https://www.npmtrends.com/@auth/neo4j-adapter">
<img src="https://img.shields.io/npm/dm/@auth/neo4j-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/neo4j-adapter",
"version": "1.0.6",
"description": "neo4j adapter for next-auth.",
"name": "@auth/neo4j-adapter",
"version": "0.0.0",
"description": "neo4j adapter for Auth.js",
"homepage": "https://authjs.dev",
"repository": "https://github.com/nextauthjs/next-auth",
"bugs": {
@@ -11,7 +11,19 @@
"contributors": [
"Balázs Orbán <info@balazsorban.com>"
],
"main": "dist/index.js",
"type": "module",
"types": "./index.d.ts",
"files": [
"*.js",
"*.d.ts*",
"src"
],
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
}
},
"license": "ISC",
"keywords": [
"next-auth",
@@ -28,26 +40,20 @@
"test": "./tests/test.sh",
"build": "tsc"
},
"files": [
"README.md",
"dist"
],
"peerDependencies": {
"neo4j-driver": "^4.0.0 || ^5.7.0",
"next-auth": "^4"
"neo4j-driver": "^4.0.0 || ^5.7.0"
},
"devDependencies": {
"@next-auth/adapter-test": "workspace:*",
"@next-auth/tsconfig": "workspace:*",
"@types/uuid": "^8.3.3",
"jest": "^27.4.3",
"neo4j-driver": "^5.7.0",
"next-auth": "workspace:*"
"neo4j-driver": "^5.7.0"
},
"dependencies": {
"uuid": "^8.3.2"
"@auth/core": "workspace:*"
},
"jest": {
"preset": "@next-auth/adapter-test/jest"
}
}
}

View File

@@ -9,17 +9,13 @@
* ## Installation
*
* ```bash npm2yarn2pnpm
* npm install next-auth @next-auth/neo4j-adapter neo4j-driver
* npm install @auth/neo4j-adapter neo4j-driver
* ```
*
* @module @next-auth/neo4j-adapter
* @module @auth/neo4j-adapter
*/
import type { Session } from "neo4j-driver"
import type { Adapter } from "next-auth/adapters"
import { v4 as uuid } from "uuid"
import { client, format } from "./utils"
export { format }
import { type Session, isInt, integer } from "neo4j-driver"
import type { Adapter } from "@auth/core/adapters"
/** This is the interface of the Neo4j adapter options. The Neo4j adapter takes a {@link https://neo4j.com/docs/bolt/current/driver-api/#driver-session Neo4j session} as its only argument. */
export interface Neo4jOptions extends Session {}
@@ -31,7 +27,7 @@ export interface Neo4jOptions extends Session {}
*
* ```javascript title="pages/api/auth/[...nextauth].js"
* import neo4j from "neo4j-driver"
* import { Neo4jAdapter } from "@next-auth/neo4j-adapter"
* import { Neo4jAdapter } from "@auth/neo4j-adapter"
*
* const driver = neo4j.driver(
* "bolt://localhost",
@@ -134,7 +130,7 @@ export function Neo4jAdapter(session: Session): Adapter {
return {
async createUser(data) {
const user: any = { id: uuid(), ...data }
const user = { id: crypto.randomUUID(), ...data }
await write(`CREATE (u:User $data)`, user)
return user
},
@@ -288,3 +284,69 @@ export function Neo4jAdapter(session: Session): Adapter {
},
}
}
// https://github.com/honeinc/is-iso-date/blob/master/index.js
const isoDateRE =
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/
function isDate(value: any) {
return value && isoDateRE.test(value) && !isNaN(Date.parse(value))
}
export const format = {
/** Takes a plain old JavaScript object and turns it into a Neo4j compatible object */
to(object: Record<string, any>) {
const newObject: Record<string, unknown> = {}
for (const key in object) {
const value = object[key]
if (value instanceof Date) newObject[key] = value.toISOString()
else newObject[key] = value
}
return newObject
},
/** Takes a Neo4j object and returns a plain old JavaScript object */
from<T = Record<string, unknown>>(object?: Record<string, any>): T | null {
const newObject: Record<string, unknown> = {}
if (!object) return null
for (const key in object) {
const value = object[key]
if (isDate(value)) {
newObject[key] = new Date(value)
} else if (isInt(value)) {
if (integer.inSafeRange(value)) newObject[key] = value.toNumber()
else newObject[key] = value.toString()
} else {
newObject[key] = value
}
}
return newObject as T
},
}
function client(session: Session) {
return {
/** Reads values from the database */
async read<T>(statement: string, values?: any): Promise<T | null> {
const result = await session.readTransaction((tx) =>
tx.run(statement, values)
)
return format.from<T>(result?.records[0]?.get(0)) ?? null
},
/**
* Reads/writes values from/to the database.
* Properties are available under `$data`
*/
async write<T extends Record<string, any>>(
statement: string,
values: T
): Promise<any> {
const result = await session.writeTransaction((tx) =>
tx.run(statement, { data: format.to(values) })
)
return format.from<T>(result?.records[0]?.toObject())
},
}
}

View File

@@ -1,68 +0,0 @@
import type { Session } from "neo4j-driver"
import { isInt, integer } from "neo4j-driver"
// https://github.com/honeinc/is-iso-date/blob/master/index.js
const isoDateRE =
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/
function isDate(value: any) {
return value && isoDateRE.test(value) && !isNaN(Date.parse(value))
}
export const format = {
/** Takes a plain old JavaScript object and turns it into a Neo4j compatible object */
to(object: Record<string, any>) {
const newObject: Record<string, unknown> = {}
for (const key in object) {
const value = object[key]
if (value instanceof Date) newObject[key] = value.toISOString()
else newObject[key] = value
}
return newObject
},
/** Takes a Neo4j object and returns a plain old JavaScript object */
from<T = Record<string, unknown>>(object?: Record<string, any>): T | null {
const newObject: Record<string, unknown> = {}
if (!object) return null
for (const key in object) {
const value = object[key]
if (isDate(value)) {
newObject[key] = new Date(value)
} else if (isInt(value)) {
if (integer.inSafeRange(value)) newObject[key] = value.toNumber()
else newObject[key] = value.toString()
} else {
newObject[key] = value
}
}
return newObject as T
},
}
export function client(session: Session) {
return {
/** Reads values from the database */
async read<T>(statement: string, values?: any): Promise<T | null> {
const result = await session.readTransaction((tx) =>
tx.run(statement, values)
)
return format.from<T>(result?.records[0]?.get(0)) ?? null
},
/**
* Reads/writes values from/to the database.
* Properties are available under `$data`
*/
async write<T extends Record<string, any>>(
statement: string,
values: T
): Promise<any> {
const result = await session.writeTransaction((tx) =>
tx.run(statement, { data: format.to(values) })
)
return format.from<T>(result?.records[0]?.toObject())
},
}
}

View File

@@ -4,6 +4,8 @@ import statements from "./resources/statements"
import { Neo4jAdapter, format } from "../src"
globalThis.crypto ??= require("node:crypto").webcrypto
const driver = neo4j.driver(
"bolt://localhost",
neo4j.auth.basic("neo4j", "password")

View File

@@ -1,8 +1,25 @@
{
"extends": "@next-auth/tsconfig/tsconfig.adapters.json",
"extends": "@next-auth/tsconfig/tsconfig.base.json",
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"isolatedModules": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"outDir": ".",
"rootDir": "src",
"outDir": "dist"
"skipDefaultLibCheck": true,
"strictNullChecks": true,
"stripInternal": true,
"declarationMap": true,
"declaration": true
},
"exclude": ["tests", "dist", "jest.config.js"]
}
"include": [
"src/**/*"
],
"exclude": [
"*.js",
"*.d.ts",
]
}

14
pnpm-lock.yaml generated
View File

@@ -54,9 +54,9 @@ importers:
apps/dev/nextjs:
specifiers:
'@auth/core': workspace:*
'@auth/fauna-adapter': workspace:*
'@auth/prisma-adapter': workspace:*
'@auth/typeorm-adapter': workspace:*
'@auth/fauna-adapter': workspace:*
'@next-auth/supabase-adapter': workspace:*
'@playwright/test': 1.29.2
'@prisma/client': ^3
@@ -78,9 +78,9 @@ importers:
typeorm: 0.3.7
dependencies:
'@auth/core': link:../../../packages/core
'@auth/fauna-adapter': link:../../../packages/adapter-fauna
'@auth/prisma-adapter': link:../../../packages/adapter-prisma
'@auth/typeorm-adapter': link:../../../packages/adapter-typeorm
'@auth/fauna-adapter': link:../../../packages/adapter-fauna
'@next-auth/supabase-adapter': link:../../../packages/adapter-supabase
'@prisma/client': 3.15.2_prisma@3.15.2
'@supabase/supabase-js': 2.0.5
@@ -104,9 +104,9 @@ importers:
apps/dev/nextjs-v4:
specifiers:
'@auth/fauna-adapter': workspace:*
'@auth/prisma-adapter': workspace:*
'@auth/typeorm-adapter': workspace:*
'@auth/fauna-adapter': workspace:*
'@next-auth/supabase-adapter': workspace:*
'@prisma/client': ^3
'@supabase/supabase-js': ^2.0.5
@@ -125,9 +125,9 @@ importers:
sqlite3: ^5.0.8
typeorm: 0.3.7
dependencies:
'@auth/fauna-adapter': link:../../../packages/adapter-fauna
'@auth/prisma-adapter': link:../../../packages/adapter-prisma
'@auth/typeorm-adapter': link:../../../packages/adapter-typeorm
'@auth/fauna-adapter': link:../../../packages/adapter-fauna
'@next-auth/supabase-adapter': link:../../../packages/adapter-supabase
'@prisma/client': 3.15.2_prisma@3.15.2
'@supabase/supabase-js': 2.0.5
@@ -361,22 +361,20 @@ importers:
packages/adapter-neo4j:
specifiers:
'@auth/core': workspace:*
'@next-auth/adapter-test': workspace:*
'@next-auth/tsconfig': workspace:*
'@types/uuid': ^8.3.3
jest: ^27.4.3
neo4j-driver: ^5.7.0
next-auth: workspace:*
uuid: ^8.3.2
dependencies:
uuid: 8.3.2
'@auth/core': link:../core
devDependencies:
'@next-auth/adapter-test': link:../adapter-test
'@next-auth/tsconfig': link:../tsconfig
'@types/uuid': 8.3.4
jest: 27.5.1
neo4j-driver: 5.7.0
next-auth: link:../next-auth
packages/adapter-pouchdb:
specifiers:

View File

@@ -61,7 +61,7 @@
"@auth/firebase-adapter#build",
"@auth/mikro-orm-adapter#build",
"@auth/mongodb-adapter#build",
"@next-auth/neo4j-adapter#build",
"@auth/neo4j-adapter#build",
"@next-auth/pouchdb-adapter#build",
"@next-auth/sequelize-adapter#build",
"@next-auth/supabase-adapter#build",
@@ -84,7 +84,7 @@
"@auth/firebase-adapter#build",
"@auth/mikro-orm-adapter#build",
"@auth/mongodb-adapter#build",
"@next-auth/neo4j-adapter#build",
"@auth/neo4j-adapter#build",
"@next-auth/pouchdb-adapter#build",
"@next-auth/sequelize-adapter#build",
"@next-auth/supabase-adapter#build",