Home Reference Source
import Driver from 'neo4j-driver-core/lib/driver.js'
public class | source

Driver

A driver maintains one or more Sessions with a remote Neo4j instance. Through the Sessions you can send queries and retrieve results from the database.

Drivers are reasonably expensive to create - you should strive to keep one driver instance around per Neo4j Instance you connect to.

Member Summary

Public Members
public get

The bookmark managed used by Driver.executeQuery

Method Summary

Public Methods
public
public

close(): Promise<void>

Close all open sessions and other associated resources.

public

executeQuery(query: string | {text: string, parameters?: object}, parameters: Object, config: QueryConfig<T>): Promise<T>

Executes a query in a retriable context and returns a EagerResult.

public

getNegotiatedProtocolVersion(): Promise<number>

Returns the protocol version negotiated via handshake.

public

getServerInfo(param: Object): Promise<ServerInfo>

Get ServerInfo for the giver database.

public

isEncrypted(): boolean

Returns boolean to indicate if driver has been configured with encryption enabled.

public

Acquire a session to communicate with the database.

public

supportsMultiDb(): Promise<boolean>

Returns whether the server supports multi database capabilities based on the protocol version negotiated via handshake.

public

supportsSessionAuth(): Promise<boolean>

Returns whether the driver session re-auth functionality capabilities based on the protocol version negotiated via handshake.

public

supportsTransactionConfig(): Promise<boolean>

Returns whether the server supports transaction config capabilities based on the protocol version negotiated via handshake.

public

supportsUserImpersonation(): Promise<boolean>

Returns whether the server supports user impersonation capabilities based on the protocol version negotiated via handshake.

public

verifyAuthentication(param: object): Promise<boolean>

This method verifies the authorization credentials work by trying to acquire a connection to one of the servers with the given credentials.

public

verifyConnectivity(param: Object): Promise<ServerInfo>

this method was deprecated. This return of this method will change in 6.0.0 to not async return the {@link ServerInfo} and async return {@link void} instead. If you need to use the server info, use {@link getServerInfo} instead.

Verifies connectivity of this driver by trying to open a connection with the provided driver options.

Public Members

public get executeQueryBookmarkManager: BookmarkManager source

The bookmark managed used by Driver.executeQuery

Public Methods

public [Symbol.asyncDispose](): * source

Return:

*

public close(): Promise<void> source

Close all open sessions and other associated resources. You should make sure to use this when you are done with this driver instance.

Return:

Promise<void>

promise resolved when the driver is closed.

public executeQuery(query: string | {text: string, parameters?: object}, parameters: Object, config: QueryConfig<T>): Promise<T> source

Executes a query in a retriable context and returns a EagerResult.

This method is a shortcut for a Session#executeRead and Session#executeWrite.

NOTE: Because it is an explicit transaction from the server point of view, Cypher queries using "CALL {} IN TRANSACTIONS" or the older "USING PERIODIC COMMIT" construct will not work (call Session#run for these).

Params:

NameTypeAttributeDescription
query string | {text: string, parameters?: object}

Cypher query to execute

parameters Object

Map with parameters to use in the query

config QueryConfig<T>

The query configuration

Return:

Promise<T>

Example:

// Run a simple write query
const { keys, records, summary } = await driver.executeQuery('CREATE (p:Person{ name: $name }) RETURN p', { name: 'Person1'})
// Run a read query
const { keys, records, summary } = await driver.executeQuery(
   'MATCH (p:Person{ name: $name }) RETURN p',
   { name: 'Person1'},
   { routing: neo4j.routing.READ})
// Run a read query returning a Person Nodes per elementId
const peopleMappedById = await driver.executeQuery(
   'MATCH (p:Person{ name: $name }) RETURN p',
   { name: 'Person1'},
   {
     resultTransformer: neo4j.resultTransformers.mappedResultTransformer({
       map(record) {
         const p = record.get('p')
         return [p.elementId, p]
       },
       collect(elementIdPersonPairArray) {
         return new Map(elementIdPersonPairArray)
       }
     })
   }
)

const person = peopleMappedById.get("<ELEMENT_ID>")
// these lines
const transformedResult = await driver.executeQuery(
   "<QUERY>",
   <PARAMETERS>,
   {
      routing: neo4j.routing.WRITE,
      resultTransformer: transformer,
      database: "<DATABASE>",
      impersonatedUser: "<USER>",
      bookmarkManager: bookmarkManager
   })
// are equivalent to those
const session = driver.session({
   database: "<DATABASE>",
   impersonatedUser: "<USER>",
   bookmarkManager: bookmarkManager
})

try {
   const transformedResult = await session.executeWrite(tx => {
       const result = tx.run("<QUERY>", <PARAMETERS>)
       return transformer(result)
   })
} finally {
   await session.close()
}

See:

public getNegotiatedProtocolVersion(): Promise<number> source

Returns the protocol version negotiated via handshake.

Note that this function call always causes a round-trip to the server.

Return:

Promise<number>

the protocol version negotiated via handshake.

Throw:

Error

When protocol negotiation fails

public getServerInfo(param: Object): Promise<ServerInfo> source

Get ServerInfo for the giver database.

Params:

NameTypeAttributeDescription
param Object

The object parameter

param.database string

The target database to verify connectivity for.

Return:

Promise<ServerInfo>

promise resolved with the ServerInfo or rejected with error.

public isEncrypted(): boolean source

Returns boolean to indicate if driver has been configured with encryption enabled.

Return:

boolean

public session(param: SessionConfig): Session source

Acquire a session to communicate with the database. The session will borrow connections from the underlying connection pool as required and should be considered lightweight and disposable.

This comes with some responsibility - make sure you always call close when you are done using a session, and likewise, make sure you don't close your session before you are done using it. Once it is closed, the underlying connection will be released to the connection pool and made available for others to use.

Params:

NameTypeAttributeDescription
param SessionConfig

The session configuration

Return:

Session

new session.

public supportsMultiDb(): Promise<boolean> source

Returns whether the server supports multi database capabilities based on the protocol version negotiated via handshake.

Note that this function call always causes a round-trip to the server.

Return:

Promise<boolean>

promise resolved with a boolean or rejected with error.

public supportsSessionAuth(): Promise<boolean> source

Returns whether the driver session re-auth functionality capabilities based on the protocol version negotiated via handshake.

Note that this function call always causes a round-trip to the server.

Return:

Promise<boolean>

promise resolved with a boolean or rejected with error.

public supportsTransactionConfig(): Promise<boolean> source

Returns whether the server supports transaction config capabilities based on the protocol version negotiated via handshake.

Note that this function call always causes a round-trip to the server.

Return:

Promise<boolean>

promise resolved with a boolean or rejected with error.

public supportsUserImpersonation(): Promise<boolean> source

Returns whether the server supports user impersonation capabilities based on the protocol version negotiated via handshake.

Note that this function call always causes a round-trip to the server.

Return:

Promise<boolean>

promise resolved with a boolean or rejected with error.

public verifyAuthentication(param: object): Promise<boolean> source

This method verifies the authorization credentials work by trying to acquire a connection to one of the servers with the given credentials.

Params:

NameTypeAttributeDescription
param object

object parameter

Return:

Promise<boolean>

promise resolved with true if succeed, false if failed with authentication issue and rejected with error if non-authentication error happens.

Return Properties:

NameTypeAttributeDescription
param.auth AuthToken

the target auth for the to-be-acquired connection

param.database string

the target database for the to-be-acquired connection

public verifyConnectivity(param: Object): Promise<ServerInfo> source

this method was deprecated. This return of this method will change in 6.0.0 to not async return the {@link ServerInfo} and async return {@link void} instead. If you need to use the server info, use {@link getServerInfo} instead.

Verifies connectivity of this driver by trying to open a connection with the provided driver options.

Params:

NameTypeAttributeDescription
param Object

The object parameter

param.database string

The target database to verify connectivity for.

Return:

Promise<ServerInfo>

promise resolved with server info or rejected with error.