Skip to content

Commit 1a49d33

Browse files
authored
Introducing the Neo4j Driver Lite (#692)
This is a slimmer version of the JS Driver without the Reactive API. This achieve by configuring the core module with the bolt-connection module.
1 parent a64dbed commit 1a49d33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+16461
-128
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ docs/build
1515
.nyc_output
1616
coverage
1717
.vscode
18-
*.code-workspace
18+
*.code-workspace
19+
/testkit/CAs

bolt-connection/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ docs/build
1515
.nyc_output
1616
coverage
1717
.vscode
18-
/types
1918
/docs
2019
/lib6
2120
*.code-workspace

bolt-connection/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "4.3.0",
44
"description": "Implements the connection with the Neo4j Database using the Bolt Protocol",
55
"main": "lib/index.js",
6+
"types": "types/index.d.ts",
67
"scripts": {
78
"build": "tsc",
89
"test": "jest --passWithNoTests",

bolt-connection/types/index.d.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import {
20+
ConnectionProvider
21+
} from 'neo4j-driver-core'
22+
23+
declare class DirectConnectionProvider extends ConnectionProvider {
24+
constructor(config: any)
25+
}
26+
27+
declare class RoutingConnectionProvider extends ConnectionProvider {
28+
constructor(config: any)
29+
}
30+
31+
export {
32+
DirectConnectionProvider,
33+
RoutingConnectionProvider
34+
}

core/src/driver.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ import {
2929
DEFAULT_POOL_ACQUISITION_TIMEOUT,
3030
DEFAULT_POOL_MAX_SIZE
3131
} from './internal/constants'
32-
import { Logger, LoggingConfig } from './internal/logger'
32+
import { Logger } from './internal/logger'
3333
import Session from './session'
3434
import { ServerInfo } from './result-summary'
3535
import { ENCRYPTION_ON, ENCRYPTION_OFF } from './internal/util'
36+
import {
37+
EncryptionLevel,
38+
LoggingConfig,
39+
TrustStrategy,
40+
SessionMode
41+
} from './types'
42+
import { ServerAddress } from './internal/server-address'
3643

3744
const DEFAULT_MAX_CONNECTION_LIFETIME: number = 60 * 60 * 1000 // 1 hour
3845

@@ -47,21 +54,21 @@ const DEFAULT_FETCH_SIZE: number = 1000
4754
* Should be used like this: `driver.session({ defaultAccessMode: neo4j.session.READ })`.
4855
* @type {string}
4956
*/
50-
const READ: string = ACCESS_MODE_READ
57+
const READ: SessionMode = ACCESS_MODE_READ
5158

5259
/**
5360
* Constant that represents write session access mode.
5461
* Should be used like this: `driver.session({ defaultAccessMode: neo4j.session.WRITE })`.
5562
* @type {string}
5663
*/
57-
const WRITE: string = ACCESS_MODE_WRITE
64+
const WRITE: SessionMode = ACCESS_MODE_WRITE
5865

5966
let idGenerator = 0
6067

6168
interface MetaInfo {
6269
routing: boolean
6370
typename: string
64-
address: string
71+
address: string | ServerAddress
6572
}
6673

6774
type CreateConnectionProvider = (
@@ -71,13 +78,8 @@ type CreateConnectionProvider = (
7178
hostNameResolver: ConfiguredCustomResolver
7279
) => ConnectionProvider
7380

74-
type TrustStrategy =
75-
| 'TRUST_ALL_CERTIFICATES'
76-
| 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES'
77-
| 'TRUST_SYSTEM_CA_SIGNED_CERTIFICATES'
78-
7981
interface DriverConfig {
80-
encrypted?: string
82+
encrypted?: EncryptionLevel | boolean
8183
trust?: TrustStrategy
8284
fetchSize?: number
8385
logging?: LoggingConfig
@@ -230,7 +232,7 @@ class Driver {
230232
database = '',
231233
fetchSize
232234
}: {
233-
defaultAccessMode?: string
235+
defaultAccessMode?: SessionMode
234236
bookmarks?: string | string[]
235237
database?: string
236238
fetchSize?: number
@@ -277,7 +279,7 @@ class Driver {
277279
reactive,
278280
fetchSize
279281
}: {
280-
defaultAccessMode: string
282+
defaultAccessMode: SessionMode
281283
bookmarkOrBookmarks?: string | string[]
282284
database: string
283285
reactive: boolean

core/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ import ResultSummary, {
6767
} from './result-summary'
6868
import Result, { QueryResult, ResultObserver } from './result'
6969
import ConnectionProvider from './connection-provider'
70+
import Connection from './connection'
7071
import Transaction from './transaction'
7172
import Session, { TransactionConfig } from './session'
7273
import Driver, * as driver from './driver'
74+
import * as types from './types'
7375
import * as internal from './internal' // todo: removed afterwards
7476

7577
/**
@@ -132,6 +134,8 @@ const forExport = {
132134
Transaction,
133135
Session,
134136
Driver,
137+
Connection,
138+
types,
135139
driver
136140
}
137141

@@ -186,10 +190,12 @@ export {
186190
QueryResult,
187191
ResultObserver,
188192
ConnectionProvider,
193+
Connection,
189194
Transaction,
190195
Session,
191196
TransactionConfig,
192197
Driver,
198+
types,
193199
driver
194200
}
195201

core/src/internal/constants.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const FETCH_ALL = -1
2121
const DEFAULT_POOL_ACQUISITION_TIMEOUT = 60 * 1000 // 60 seconds
2222
const DEFAULT_POOL_MAX_SIZE = 100
2323

24-
const ACCESS_MODE_READ: string = 'READ'
25-
const ACCESS_MODE_WRITE: string = 'WRITE'
24+
const ACCESS_MODE_READ: 'READ' = 'READ'
25+
const ACCESS_MODE_WRITE: 'WRITE' = 'WRITE'
2626

2727
const BOLT_PROTOCOL_V1: number = 1
2828
const BOLT_PROTOCOL_V2: number = 2

core/src/internal/logger.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@
1717
* limitations under the License.
1818
*/
1919
import { newError } from '../error'
20-
21-
export type LogLevel = 'warn' | 'error' | 'info' | 'debug'
22-
type LoggerFunction = (level: LogLevel, message: string) => unknown
23-
24-
export interface LoggingConfig {
25-
level?: LogLevel
26-
logger: LoggerFunction
27-
}
20+
import { LogLevel, LoggerFunction, LoggingConfig } from '../types'
2821

2922
const ERROR: 'error' = 'error'
3023
const WARN: 'warn' = 'warn'
@@ -61,7 +54,7 @@ export class Logger {
6154
* @param {Object} driverConfig the driver configuration as supplied by the user.
6255
* @return {Logger} a new logger instance or a no-op logger when not configured.
6356
*/
64-
static create(driverConfig: { logging?: LoggingConfig }) {
57+
static create(driverConfig: { logging?: LoggingConfig }): Logger {
6558
if (driverConfig && driverConfig.logging) {
6659
const loggingConfig = driverConfig.logging
6760
const level = extractConfiguredLevel(loggingConfig)

core/src/internal/util.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
*/
1919

2020
import Integer, { isInt } from '../integer'
21+
import { EncryptionLevel } from '../types'
2122

22-
const ENCRYPTION_ON: string = 'ENCRYPTION_ON'
23-
const ENCRYPTION_OFF: string = 'ENCRYPTION_OFF'
23+
const ENCRYPTION_ON: EncryptionLevel = 'ENCRYPTION_ON'
24+
const ENCRYPTION_OFF: EncryptionLevel = 'ENCRYPTION_OFF'
2425

2526
/**
2627
* Verifies if the object is null or empty

core/src/session.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { TransactionExecutor } from './internal/transaction-executor'
2727
import { Bookmark } from './internal/bookmark'
2828
import { TxConfig } from './internal/tx-config'
2929
import ConnectionProvider from './connection-provider'
30-
import { Query } from './types'
30+
import { Query, SessionMode } from './types'
3131
import Connection from './connection'
3232
import { NumberOrInteger } from './graph-types'
3333

@@ -47,7 +47,7 @@ interface TransactionConfig {
4747
* @access public
4848
*/
4949
class Session {
50-
private _mode: string
50+
private _mode: SessionMode
5151
private _database: string
5252
private _reactive: boolean
5353
private _fetchSize: number
@@ -80,9 +80,9 @@ class Session {
8080
reactive,
8181
fetchSize
8282
}: {
83-
mode: string
83+
mode: SessionMode
8484
connectionProvider: ConnectionProvider
85-
bookmark: Bookmark
85+
bookmark?: Bookmark
8686
database: string
8787
config: any
8888
reactive: boolean
@@ -106,7 +106,7 @@ class Session {
106106
})
107107
this._open = true
108108
this._hasTx = false
109-
this._lastBookmark = bookmark
109+
this._lastBookmark = bookmark || Bookmark.empty()
110110
this._transactionExecutor = _createTransactionExecutor(config)
111111
this._onComplete = this._onCompleteCallback.bind(this)
112112
}
@@ -233,7 +233,7 @@ class Session {
233233
return this._beginTransaction(this._mode, txConfig)
234234
}
235235

236-
_beginTransaction(accessMode: string, txConfig: TxConfig): Transaction {
236+
_beginTransaction(accessMode: SessionMode, txConfig: TxConfig): Transaction {
237237
if (!this._open) {
238238
throw newError('Cannot begin a transaction on a closed session.')
239239
}
@@ -333,7 +333,7 @@ class Session {
333333
}
334334

335335
_runTransaction<T>(
336-
accessMode: string,
336+
accessMode: SessionMode,
337337
transactionConfig: TxConfig,
338338
transactionWork: TransactionWork<T>
339339
): Promise<T> {
@@ -369,7 +369,7 @@ class Session {
369369
}
370370
}
371371

372-
_connectionHolderWithMode(mode: string): ConnectionHolder {
372+
_connectionHolderWithMode(mode: SessionMode): ConnectionHolder {
373373
if (mode === ACCESS_MODE_READ) {
374374
return this._readConnectionHolder
375375
} else if (mode === ACCESS_MODE_WRITE) {
@@ -391,7 +391,7 @@ class Session {
391391
/**
392392
* @protected
393393
*/
394-
static _validateSessionMode(rawMode?: string): string {
394+
static _validateSessionMode(rawMode?: SessionMode): SessionMode {
395395
const mode = rawMode || ACCESS_MODE_WRITE
396396
if (mode !== ACCESS_MODE_READ && mode !== ACCESS_MODE_WRITE) {
397397
throw newError('Illegal session mode ' + mode)

core/src/types.ts

+43
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,46 @@
2121
* @private
2222
*/
2323
export type Query = string | String | { text: string; parameters?: any }
24+
25+
export type EncryptionLevel = 'ENCRYPTION_ON' | 'ENCRYPTION_OFF'
26+
27+
export type LogLevel = 'error' | 'warn' | 'info' | 'debug'
28+
29+
export type LoggerFunction = (level: LogLevel, message: string) => unknown
30+
31+
export type SessionMode = 'READ' | 'WRITE'
32+
33+
export interface LoggingConfig {
34+
level?: LogLevel
35+
logger: LoggerFunction
36+
}
37+
38+
export type TrustStrategy =
39+
| 'TRUST_ALL_CERTIFICATES'
40+
| 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES'
41+
| 'TRUST_SYSTEM_CA_SIGNED_CERTIFICATES'
42+
43+
export type Parameters = { [key: string]: any }
44+
export interface AuthToken {
45+
scheme: string
46+
principal: string
47+
credentials: string
48+
realm?: string
49+
parameters?: Parameters
50+
}
51+
export interface Config {
52+
encrypted?: boolean | EncryptionLevel
53+
trust?: TrustStrategy
54+
trustedCertificates?: string[]
55+
knownHosts?: string
56+
fetchSize?: number
57+
maxConnectionPoolSize?: number
58+
maxTransactionRetryTime?: number
59+
maxConnectionLifetime?: number
60+
connectionAcquisitionTimeout?: number
61+
connectionTimeout?: number
62+
disableLosslessIntegers?: boolean
63+
logging?: LoggingConfig
64+
resolver?: (address: string) => string[] | Promise<string[]>
65+
userAgent?: string
66+
}

gulpfile.babel.js

-29
Original file line numberDiff line numberDiff line change
@@ -181,35 +181,6 @@ gulp.task('stop-neo4j', function (done) {
181181
done()
182182
})
183183

184-
gulp.task(
185-
'install-driver-into-testkit-backend',
186-
gulp.series('nodejs', function () {
187-
const dir = path.join('build', 'testkit-backend')
188-
fs.emptyDirSync(dir)
189-
190-
const packageJsonContent = JSON.stringify({
191-
private: true,
192-
dependencies: {
193-
'neo4j-driver': __dirname
194-
}
195-
})
196-
197-
return file('package.json', packageJsonContent, { src: true })
198-
.pipe(gulp.dest(dir))
199-
.pipe(install())
200-
})
201-
)
202-
203-
gulp.task(
204-
'testkit-backend',
205-
gulp.series('install-driver-into-testkit-backend', function () {
206-
return gulp
207-
.src('testkit-backend/**/*.js')
208-
.pipe(babel())
209-
.pipe(gulp.dest('build/testkit-backend'))
210-
})
211-
)
212-
213184
gulp.task('run-stress-tests', function () {
214185
return gulp
215186
.src('test/**/stress.test.js')

neo4j-driver-lite/.eslintignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build
2+
docs
3+
lib
4+
node_modules
5+
lib6
6+
types

0 commit comments

Comments
 (0)