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

feat(adapters/surrealdb): update surrealdb.js to enable http connection strategy #8823

Merged
4 changes: 2 additions & 2 deletions packages/adapter-surrealdb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@auth/core": "workspace:*"
},
"peerDependencies": {
"surrealdb.js": "^0.9.0"
"surrealdb.js": "^0.9.1"
},
"devDependencies": {
"@auth/adapter-test": "workspace:*",
Expand All @@ -55,4 +55,4 @@
"jest": {
"preset": "@auth/adapter-test/jest"
}
}
}
103 changes: 103 additions & 0 deletions packages/adapter-surrealdb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
* <p style={{fontWeight: "normal"}}>Official <a href="https://www.surrealdb.com">SurrealDB</a> adapter for Auth.js / NextAuth.js.</p>
* <a href="https://www.surrealdb.com">
* <img style={{display: "block"}} src="https://authjs.dev/img/adapters/surrealdb.png" width="30" />
* </a>
* </div>
*
* ## Installation
*
* ```bash npm2yarn2pnpm
* npm install @auth/surrealdb-adapter surrealdb.js
* ```
*
* @module @auth/surrealdb-adapter
*/
import Surreal, { ExperimentalSurrealHTTP } from "surrealdb.js"
import type {
Adapter,
Expand All @@ -24,13 +40,15 @@ export type SessionDoc<T = string> = Document & { userId: T }

const extractId = (surrealId: string) => surrealId.split(":")[1] ?? surrealId

/** @internal */
// Convert DB object to AdapterUser
export const docToUser = (doc: UserDoc): AdapterUser => ({
...doc,
id: extractId(doc.id),
emailVerified: doc.emailVerified ? new Date(doc.emailVerified) : null,
})

/** @internal */
// Convert DB object to AdapterAccount
export const docToAccount = (doc: AccountDoc) => {
const account: AdapterAccount = {
Expand All @@ -41,6 +59,7 @@ export const docToAccount = (doc: AccountDoc) => {
return account
}

/** @internal */
// Convert DB object to AdapterSession
export const docToSession = (
doc: SessionDoc<string | UserDoc>
Expand All @@ -52,6 +71,7 @@ export const docToSession = (
sessionToken: doc.sessionToken ?? "",
})

/** @internal */
// Convert AdapterUser to DB object
const userToDoc = (
user: Omit<AdapterUser, "id"> | Partial<AdapterUser>
Expand All @@ -63,6 +83,7 @@ const userToDoc = (
return doc
}

/** @internal */
// Convert AdapterAccount to DB object
const accountToDoc = (account: AdapterAccount): Omit<AccountDoc, "id"> => {
const doc = {
Expand All @@ -72,6 +93,7 @@ const accountToDoc = (account: AdapterAccount): Omit<AccountDoc, "id"> => {
return doc
}

/** @internal */
// Convert AdapterSession to DB object
export const sessionToDoc = (
session: AdapterSession
Expand All @@ -83,6 +105,87 @@ export const sessionToDoc = (
return doc
}

/**
* ## Setup
*
* The SurrealDB adapter does not handle connections automatically, so you will have to make sure that you pass the Adapter a `SurrealDBClient` that is connected already. Below you can see an example how to do this.
*
* ### Add the SurrealDB client
*
* #### Option 1/2 – Using RPC:
*
* ```js
* import { Surreal } from "surrealdb.js";
*
* const connectionString = ... // i.e. "http://0.0.0.0:8000"
* const user = ...
* const pass = ...
* const ns = ...
* const db = ...
*
* const clientPromise = new Promise<Surreal>(async (resolve, reject) => {
* const db = new Surreal();
* try {
* await db.connect(`${connectionString}/rpc`, {
* ns, db, auth: { user, pass }
* })
* resolve(db)
* } catch (e) {
* reject(e)
* }
* })
*
* // Export a module-scoped MongoClient promise. By doing this in a
* // separate module, the client can be shared across functions.
* export default clientPromise
* ```
*
* #### Option 2/2 – Using HTTP:
*
* Usefull in serverlees environments like Vercel.
*
* ```js
* import { ExperimentalSurrealHTTP } from "surrealdb.js"
* import fetch from "node-fetch"
ThangHuuVu marked this conversation as resolved.
Show resolved Hide resolved
*
* const connectionString = ... // i.e. "http://0.0.0.0:8000"
* const user = ...
* const pass = ...
* const ns = ...
* const db = ...
*
* const clientPromise = new Promise<ExperimentalSurrealHTTP<typeof fetch>>(async (resolve, reject) => {
* try {
* const db = new ExperimentalSurrealHTTP(connectionString, {
* fetch,
* ns, db, auth: { user, pass }
* })
* resolve(db)
* } catch (e) {
* reject(e)
* }
* })
*
* // Export a module-scoped MongoClient promise. By doing this in a
* // separate module, the client can be shared across functions.
* export default clientPromise
* ```
*
* ### Configure Auth.js
*
* ```js
* import NextAuth from "next-auth"
* import { SurrealDBAdapter } from "@auth/surrealdb-adapter"
* import clientPromise from "../../../lib/surrealdb"
*
* // For more information on each option (and a full list of options) go to
* // https://authjs.dev/reference/providers/oauth
* export default NextAuth({
* adapter: SurrealDBAdapter(clientPromise),
* ...
* })
* ```
**/
export function SurrealDBAdapter<T>(
client: Promise<Surreal | ExperimentalSurrealHTTP<T>>
// options = {}
Expand Down
7 changes: 7 additions & 0 deletions packages/adapter-surrealdb/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export const config = (
db: {
async disconnect() {
const surreal = await clientPromise
try {
await surreal.delete("account")
await surreal.delete("session")
await surreal.delete("verification_token")
} catch (e) {
console.log(e)
}
if (surreal.close) surreal.close()
},
async user(id: string) {
Expand Down
30 changes: 26 additions & 4 deletions packages/adapter-surrealdb/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import Surreal from "surrealdb.js"
import Surreal, { ExperimentalSurrealHTTP } from "surrealdb.js"
import { runBasicTests } from "@auth/adapter-test"
import fetch from "node-fetch"

ThangHuuVu marked this conversation as resolved.
Show resolved Hide resolved
import { config } from "./common"

const clientPromise = new Promise<Surreal>(async (resolve, reject) => {
const db = new Surreal();
const db = new Surreal()
try {
await db.connect('http://0.0.0.0:8000/rpc', {
await db.connect("http://0.0.0.0:8000/rpc", {
ns: "test",
db: "test",
auth: {
user: "test",
pass: "test",
}
},
})
resolve(db)
} catch (e) {
Expand All @@ -21,3 +22,24 @@ const clientPromise = new Promise<Surreal>(async (resolve, reject) => {
})

runBasicTests(config(clientPromise))

const clientPromiseRest = new Promise<ExperimentalSurrealHTTP<typeof fetch>>(
async (resolve, reject) => {
try {
const db = new ExperimentalSurrealHTTP("http://0.0.0.0:8000", {
fetch,
auth: {
user: "test",
pass: "test",
},
ns: "test",
db: "test",
})
resolve(db)
} catch (e) {
reject(e)
}
}
)

runBasicTests(config(clientPromiseRest))
27 changes: 0 additions & 27 deletions packages/adapter-surrealdb/tests/rest.test.ts

This file was deleted.

Loading
Loading