Skip to content

Commit 1be4f3e

Browse files
committed
Add multi database support
1 parent 597c20f commit 1be4f3e

13 files changed

+336
-59
lines changed

src/driver.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ class Driver {
109109

110110
/**
111111
* Verifies connectivity of this driver by trying to open a connection with the provided driver options.
112+
* @param {string} [db=''] the target database to verify connectivity for.
112113
* @returns {Promise<object>} promise resolved with server info or rejected with error.
113114
*/
114-
verifyConnectivity () {
115+
verifyConnectivity ({ db = '' } = {}) {
115116
const connectionProvider = this._getOrCreateConnectionProvider()
116117
const connectivityVerifier = new ConnectivityVerifier(connectionProvider)
117-
return connectivityVerifier.verify()
118+
return connectivityVerifier.verify({ db })
118119
}
119120

120121
/**
@@ -194,7 +195,13 @@ class Driver {
194195
const bookmark = bookmarkOrBookmarks
195196
? new Bookmark(bookmarkOrBookmarks)
196197
: Bookmark.empty()
197-
return new Session(sessionMode, connectionProvider, bookmark, this._config)
198+
return new Session({
199+
mode: sessionMode,
200+
db,
201+
connectionProvider,
202+
bookmark,
203+
config: this._config
204+
})
198205
}
199206

200207
static _validateSessionMode (rawMode) {

src/internal/connection-holder.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919

2020
import { newError } from '../error'
21+
import { assertString } from './util'
22+
import { ACCESS_MODE_WRITE } from './constants'
2123

2224
/**
2325
* Utility to lazily initialize connections and return them back to the pool when unused.
@@ -26,10 +28,12 @@ export default class ConnectionHolder {
2628
/**
2729
* @constructor
2830
* @param {string} mode - the access mode for new connection holder.
31+
* @param {string} db - the target database name.
2932
* @param {ConnectionProvider} connectionProvider - the connection provider to acquire connections from.
3033
*/
31-
constructor (mode, connectionProvider) {
34+
constructor ({ mode = ACCESS_MODE_WRITE, db = '', connectionProvider } = {}) {
3235
this._mode = mode
36+
this._db = db ? assertString(db, 'db') : ''
3337
this._connectionProvider = connectionProvider
3438
this._referenceCount = 0
3539
this._connectionPromise = Promise.resolve(null)
@@ -43,14 +47,23 @@ export default class ConnectionHolder {
4347
return this._mode
4448
}
4549

50+
/**
51+
* Returns the target database name
52+
* @returns {string} db name
53+
*/
54+
db () {
55+
return this._db
56+
}
57+
4658
/**
4759
* Make this holder initialize new connection if none exists already.
4860
* @return {undefined}
4961
*/
5062
initializeConnection () {
5163
if (this._referenceCount === 0) {
5264
this._connectionPromise = this._connectionProvider.acquireConnection(
53-
this._mode
65+
this._mode,
66+
this._db
5467
)
5568
}
5669
this._referenceCount++

src/internal/connection-providers.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import RoutingUtil from './routing-util'
2727
const UNAUTHORIZED_ERROR_CODE = 'Neo.ClientError.Security.Unauthorized'
2828

2929
class ConnectionProvider {
30-
acquireConnection (mode) {
30+
acquireConnection (accessMode, db) {
3131
throw new Error('Abstract function')
3232
}
3333

@@ -50,7 +50,7 @@ export class DirectConnectionProvider extends ConnectionProvider {
5050
this._driverOnErrorCallback = driverOnErrorCallback
5151
}
5252

53-
acquireConnection (mode) {
53+
acquireConnection (accessMode, db) {
5454
const connectionPromise = this._connectionPool.acquire(this._hostPort)
5555
return this._withAdditionalOnErrorCallback(
5656
connectionPromise,
@@ -81,7 +81,7 @@ export class LoadBalancer extends ConnectionProvider {
8181
this._useSeedRouter = false
8282
}
8383

84-
acquireConnection (accessMode) {
84+
acquireConnection (accessMode, db) {
8585
const connectionPromise = this._freshRoutingTable(accessMode).then(
8686
routingTable => {
8787
if (accessMode === READ) {
@@ -282,7 +282,7 @@ export class LoadBalancer extends ConnectionProvider {
282282
.acquire(routerAddress)
283283
.then(connection => {
284284
const connectionProvider = new SingleConnectionProvider(connection)
285-
return new Session(READ, connectionProvider)
285+
return new Session({ mode: READ, connectionProvider })
286286
})
287287
.catch(error => {
288288
// unable to acquire connection towards the given router
@@ -340,7 +340,7 @@ export class SingleConnectionProvider extends ConnectionProvider {
340340
this._connection = connection
341341
}
342342

343-
acquireConnection (mode) {
343+
acquireConnection (mode, db) {
344344
const connection = this._connection
345345
this._connection = null
346346
return Promise.resolve(connection)

src/internal/connectivity-verifier.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export default class ConnectivityVerifier {
3737
* Try to obtain a working connection from the connection provider.
3838
* @returns {Promise<object>} promise resolved with server info or rejected with error.
3939
*/
40-
verify () {
41-
return acquireAndReleaseDummyConnection(this._connectionProvider)
40+
verify ({ db = '' } = {}) {
41+
return acquireAndReleaseDummyConnection(this._connectionProvider, db)
4242
}
4343
}
4444

@@ -47,8 +47,12 @@ export default class ConnectivityVerifier {
4747
* @param {ConnectionProvider} connectionProvider the provider to obtain connections from.
4848
* @return {Promise<object>} promise resolved with server info or rejected with error.
4949
*/
50-
function acquireAndReleaseDummyConnection (connectionProvider) {
51-
const connectionHolder = new ConnectionHolder(READ, connectionProvider)
50+
function acquireAndReleaseDummyConnection (connectionProvider, db) {
51+
const connectionHolder = new ConnectionHolder({
52+
mode: READ,
53+
db,
54+
connectionProvider
55+
})
5256
connectionHolder.initializeConnection()
5357
const dummyObserver = new StreamObserver()
5458
const connectionPromise = connectionHolder.getConnection(dummyObserver)

src/internal/http/http-driver.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ export default class HttpDriver extends Driver {
2828
}
2929

3030
session () {
31-
return new HttpSession(
32-
this._hostPort,
33-
this._authToken,
34-
this._config,
35-
this._sessionTracker
36-
)
31+
return new HttpSession({
32+
url: this._hostPort,
33+
authToken: this._authToken,
34+
config: this._config,
35+
sessionTracker: this._sessionTracker
36+
})
3737
}
3838

3939
close () {

src/internal/http/http-session.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ import { EMPTY_CONNECTION_HOLDER } from '../connection-holder'
2626
import Result from '../../result'
2727

2828
export default class HttpSession extends Session {
29-
constructor (url, authToken, config, sessionTracker) {
30-
super(WRITE, null, null, config)
29+
constructor ({ url, authToken, config, db = '', sessionTracker } = {}) {
30+
super({ mode: WRITE, connectionProvider: null, bookmark: null, db, config })
3131
this._ongoingTransactionIds = []
3232
this._serverInfoSupplier = createServerInfoSupplier(url)
3333
this._requestRunner = new HttpRequestRunner(url, authToken)
3434
this._sessionTracker = sessionTracker
3535
this._sessionTracker.sessionOpened(this)
3636
}
3737

38-
run (statement, parameters = {}) {
38+
run (statement, parameters = {}, transactionConfig) {
3939
const { query, params } = validateStatementAndParameters(
4040
statement,
4141
parameters

src/session.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,19 @@ class Session {
6262
* @param {Bookmark} bookmark - the initial bookmark for this session.
6363
* @param {Object} [config={}] - this driver configuration.
6464
*/
65-
constructor (mode, connectionProvider, bookmark, config) {
65+
constructor ({ mode, connectionProvider, bookmark, db, config }) {
6666
this._mode = mode
67-
this._readConnectionHolder = new ConnectionHolder(
68-
ACCESS_MODE_READ,
67+
this._db = db
68+
this._readConnectionHolder = new ConnectionHolder({
69+
mode: ACCESS_MODE_READ,
70+
db,
6971
connectionProvider
70-
)
71-
this._writeConnectionHolder = new ConnectionHolder(
72-
ACCESS_MODE_WRITE,
72+
})
73+
this._writeConnectionHolder = new ConnectionHolder({
74+
mode: ACCESS_MODE_WRITE,
75+
db,
7376
connectionProvider
74-
)
77+
})
7578
this._open = true
7679
this._hasTx = false
7780
this._lastBookmark = bookmark
@@ -100,7 +103,8 @@ class Session {
100103
connection.protocol().run(query, params, streamObserver, {
101104
bookmark: this._lastBookmark,
102105
txConfig: autoCommitTxConfig,
103-
mode: this._mode
106+
mode: this._mode,
107+
db: this._db
104108
})
105109
)
106110
}

src/transaction.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class Transaction {
5151
conn.protocol().beginTransaction(streamObserver, {
5252
bookmark: bookmark,
5353
txConfig: txConfig,
54-
mode: this._connectionHolder.mode()
54+
mode: this._connectionHolder.mode(),
55+
db: this._connectionHolder.db()
5556
})
5657
)
5758
.catch(error => streamObserver.onError(error))
@@ -184,7 +185,8 @@ let _states = {
184185
conn.protocol().run(statement, parameters, observer, {
185186
bookmark: bookmark,
186187
txConfig: txConfig,
187-
mode: connectionHolder.mode()
188+
mode: connectionHolder.mode(),
189+
db: connectionHolder.db()
188190
})
189191
)
190192
.catch(error => observer.onError(error))

0 commit comments

Comments
 (0)