@auth/neo4j-adapter
Official Neo4j adapter for Auth.js / NextAuth.js.
Installation
npm install @auth/neo4j-adapter neo4j-driver
Neo4jOptions
This is the interface of the Neo4j adapter options. The Neo4j adapter takes a Neo4j session as its only argument.
Extends
Session
Methods
_acquireConnection()
private _acquireConnection<T>(connectionConsumer): Promise<T>
This method is used by Rediscovery on the neo4j-driver-bolt-protocol package.
Type parameters
Type parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
connectionConsumer | ConnectionConsumer <T > | The method which will use the connection |
Returns
Promise
<T
>
A connection promise
Inherited from
Session._acquireConnection
_assertSessionIsOpen()
private _assertSessionIsOpen(): void
Returns
void
Inherited from
Session._assertSessionIsOpen
_beginTransaction()
_beginTransaction(
accessMode,
txConfig,
apiTelemetryConfig?): TransactionPromise
Parameters
Parameter | Type |
---|---|
accessMode | SessionMode |
txConfig | TxConfig |
apiTelemetryConfig ? | NonAutoCommitApiTelemetryConfig |
Returns
TransactionPromise
Inherited from
Session._beginTransaction
_connectionHolderWithMode()
_connectionHolderWithMode(mode): ConnectionHolder
Parameters
Parameter | Type |
---|---|
mode | SessionMode |
Returns
ConnectionHolder
Inherited from
Session._connectionHolderWithMode
_onCompleteCallback()
private _onCompleteCallback(meta, previousBookmarks?): void
Parameters
Parameter | Type | Description |
---|---|---|
meta | Object | Connection metadatada |
meta.bookmark | string | string [] | - |
meta.db ? | string | - |
previousBookmarks ? | Bookmarks | - |
Returns
void
Inherited from
Session._onCompleteCallback
_onDatabaseNameResolved()
private _onDatabaseNameResolved(database?): void
Sets the resolved database name in the session context.
Parameters
Parameter | Type | Description |
---|---|---|
database ? | string | The resolved database name |
Returns
void
Inherited from
Session._onDatabaseNameResolved
_run()
_run<T>(
query,
parameters,
customRunner): Result<RecordShape>
Type parameters
Type parameter | Value |
---|---|
T extends ResultStreamObserver | ResultStreamObserver |
Parameters
Parameter | Type |
---|---|
query | Query |
parameters | any |
customRunner | ConnectionConsumer <T > |
Returns
Result
<RecordShape
>
Inherited from
Session._run
_runTransaction()
_runTransaction<T>(
accessMode,
transactionConfig,
transactionWork): Promise<T>
Type parameters
Type parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
accessMode | SessionMode |
transactionConfig | TxConfig |
transactionWork | TransactionWork <T > |
Returns
Promise
<T
>
Inherited from
Session._runTransaction
_transactionClosed()
private _transactionClosed(): void
Returns
void
Inherited from
Session._transactionClosed
_updateBookmarks()
private _updateBookmarks(
newBookmarks?,
previousBookmarks?,
database?): void
Update value of the last bookmarks.
Parameters
Parameter | Type | Description |
---|---|---|
newBookmarks ? | Bookmarks | The new bookmarks. |
previousBookmarks ? | Bookmarks | - |
database ? | string | - |
Returns
void
Inherited from
Session._updateBookmarks
beginTransaction()
beginTransaction(transactionConfig?): TransactionPromise
Begin a new transaction in this session. A session can have at most one transaction running at a time, if you want to run multiple concurrent transactions, you should use multiple concurrent sessions.
While a transaction is open the session cannot be used to run queries outside the transaction.
Parameters
Parameter | Type | Description |
---|---|---|
transactionConfig ? | TransactionConfig | Configuration for the new auto-commit transaction. |
Returns
TransactionPromise
New Transaction.
Inherited from
Session.beginTransaction
close()
close(): Promise<void>
Close this session.
Returns
Promise
<void
>
Inherited from
Session.close
executeRead()
executeRead<T>(transactionWork, transactionConfig?): Promise<T>
Execute given unit of work in a READ transaction.
Transaction will automatically be committed unless the given function throws or returns a rejected promise.
Some failures of the given function or the commit itself will be retried with exponential backoff with initial
delay of 1 second and maximum retry time of 30 seconds. Maximum retry time is configurable via driver config’s
maxTransactionRetryTime
property in milliseconds.
Type parameters
Type parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
transactionWork | ManagedTransactionWork <T > | Callback that executes operations against a given Transaction. |
transactionConfig ? | TransactionConfig | Configuration for all transactions started to execute the unit of work. |
Returns
Promise
<T
>
Resolved promise as returned by the given function or rejected promise when given function or commit fails.
Inherited from
Session.executeRead
executeWrite()
executeWrite<T>(transactionWork, transactionConfig?): Promise<T>
Execute given unit of work in a WRITE transaction.
Transaction will automatically be committed unless the given function throws or returns a rejected promise.
Some failures of the given function or the commit itself will be retried with exponential backoff with initial
delay of 1 second and maximum retry time of 30 seconds. Maximum retry time is configurable via driver config’s
maxTransactionRetryTime
property in milliseconds.
Type parameters
Type parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
transactionWork | ManagedTransactionWork <T > | Callback that executes operations against a given Transaction. |
transactionConfig ? | TransactionConfig | Configuration for all transactions started to execute the unit of work. |
Returns
Promise
<T
>
Resolved promise as returned by the given function or rejected promise when given function or commit fails.
Inherited from
Session.executeWrite
lastBookmark()
lastBookmark(): string[]
Return the bookmarks received following the last completed Transaction.
Returns
string
[]
A reference to a previous transaction.
Inherited from
Session.lastBookmark
Deprecated
This method will be removed in version 6.0. Please, use Session#lastBookmarks instead.
See
lastBookmarks()
lastBookmarks(): string[]
Return the bookmarks received following the last completed Transaction.
Returns
string
[]
A reference to a previous transaction.
Inherited from
Session.lastBookmarks
readTransaction()
readTransaction<T>(transactionWork, transactionConfig?): Promise<T>
Execute given unit of work in a READ transaction.
Transaction will automatically be committed unless the given function throws or returns a rejected promise.
Some failures of the given function or the commit itself will be retried with exponential backoff with initial
delay of 1 second and maximum retry time of 30 seconds. Maximum retry time is configurable via driver config’s
maxTransactionRetryTime
property in milliseconds.
Type parameters
Type parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
transactionWork | TransactionWork <T > | Callback that executes operations against a given Transaction. |
transactionConfig ? | TransactionConfig | Configuration for all transactions started to execute the unit of work. |
Returns
Promise
<T
>
Resolved promise as returned by the given function or rejected promise when given function or commit fails.
Inherited from
Session.readTransaction
Deprecated
This method will be removed in version 6.0. Please, use Session#executeRead instead.
See
run()
run<R>(
query,
parameters?,
transactionConfig?): Result<R>
Run Cypher query
Could be called with a query object i.e.: {text: "MATCH ...", parameters: {param: 1}}
or with the query and parameters as separate arguments.
Type parameters
Type parameter | Value |
---|---|
R extends RecordShape | RecordShape |
Parameters
Parameter | Type | Description |
---|---|---|
query | Query | Cypher query to execute |
parameters ? | any | Map with parameters to use in query |
transactionConfig ? | TransactionConfig | Configuration for the new auto-commit transaction. |
Returns
Result
<R
>
New Result.
Inherited from
Session.run
writeTransaction()
writeTransaction<T>(transactionWork, transactionConfig?): Promise<T>
Execute given unit of work in a WRITE transaction.
Transaction will automatically be committed unless the given function throws or returns a rejected promise.
Some failures of the given function or the commit itself will be retried with exponential backoff with initial
delay of 1 second and maximum retry time of 30 seconds. Maximum retry time is configurable via driver config’s
maxTransactionRetryTime
property in milliseconds.
Type parameters
Type parameter |
---|
T |
Parameters
Parameter | Type | Description |
---|---|---|
transactionWork | TransactionWork <T > | Callback that executes operations against a given Transaction. |
transactionConfig ? | TransactionConfig | Configuration for all transactions started to execute the unit of work. |
Returns
Promise
<T
>
Resolved promise as returned by the given function or rejected promise when given function or commit fails.
Inherited from
Session.writeTransaction
Deprecated
This method will be removed in version 6.0. Please, use Session#executeWrite instead.
See
format
const format: {
from: null | T;
to: Record<string, unknown>;
};
Type declaration
from()
Takes a Neo4j object and returns a plain old JavaScript object
Type parameters
Type parameter | Value |
---|---|
T | Record <string , unknown > |
Parameters
Parameter | Type |
---|---|
object ? | Record <string , any > |
Returns
null
| T
to()
Takes a plain old JavaScript object and turns it into a Neo4j compatible object
Parameters
Parameter | Type |
---|---|
object | Record <string , any > |
Returns
Record
<string
, unknown
>
Neo4jAdapter()
Neo4jAdapter(session): Adapter
Setup
Add this adapter to your pages/api/[...nextauth].js
Auth.js configuration object.
import neo4j from "neo4j-driver"
import { Neo4jAdapter } from "@auth/neo4j-adapter"
const driver = neo4j.driver(
"bolt://localhost",
neo4j.auth.basic("neo4j", "password")
)
const neo4jSession = driver.session()
// For more information on each option (and a full list of options) go to
// https://authjs.dev/reference/core/types#authconfig
export default NextAuth({
// https://authjs.dev/getting-started/authentication/oauth
providers: [],
adapter: Neo4jAdapter(neo4jSession),
})
Advanced usage
Schema
Node labels
The following node labels are used.
- User
- Account
- Session
- VerificationToken
Relationships
The following relationships and relationship labels are used.
(:User)-[:HAS_ACCOUNT]->(:Account)
(:User)-[:HAS_SESSION]->(:Session)
Properties
This schema is adapted for use in Neo4j and is based upon our main models. Please check there for the node properties. Relationships have no properties.
Indexes
Optimum indexes will vary on your edition of Neo4j i.e. community or enterprise, and in case you have your own additional data on the nodes. Below are basic suggested indexes.
- For both Community Edition & Enterprise Edition create constraints and indexes
CREATE CONSTRAINT user_id_constraint IF NOT EXISTS
ON (u:User) ASSERT u.id IS UNIQUE;
CREATE INDEX user_id_index IF NOT EXISTS
FOR (u:User) ON (u.id);
CREATE INDEX user_email_index IF NOT EXISTS
FOR (u:User) ON (u.email);
CREATE CONSTRAINT session_session_token_constraint IF NOT EXISTS
ON (s:Session) ASSERT s.sessionToken IS UNIQUE;
CREATE INDEX session_session_token_index IF NOT EXISTS
FOR (s:Session) ON (s.sessionToken);
- Indexes
2.1. For Community Edition only create single-property indexes
CREATE INDEX account_provider_index IF NOT EXISTS
FOR (a:Account) ON (a.provider);
CREATE INDEX account_provider_account_id_index IF NOT EXISTS
FOR (a:Account) ON (a.providerAccountId);
CREATE INDEX verification_token_identifier_index IF NOT EXISTS
FOR (v:VerificationToken) ON (v.identifier);
CREATE INDEX verification_token_token_index IF NOT EXISTS
FOR (v:VerificationToken) ON (v.token);
2.2. For Enterprise Edition only create composite node key constraints and indexes
CREATE CONSTRAINT account_provider_composite_constraint IF NOT EXISTS
ON (a:Account) ASSERT (a.provider, a.providerAccountId) IS NODE KEY;
CREATE INDEX account_provider_composite_index IF NOT EXISTS
FOR (a:Account) ON (a.provider, a.providerAccountId);
CREATE CONSTRAINT verification_token_composite_constraint IF NOT EXISTS
ON (v:VerificationToken) ASSERT (v.identifier, v.token) IS NODE KEY;
CREATE INDEX verification_token_composite_index IF NOT EXISTS
FOR (v:VerificationToken) ON (v.identifier, v.token);
Parameters
Parameter | Type |
---|---|
session | Session |