Skip to content

Introducing the Neo4j Driver Lite #692

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

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ docs/build
.nyc_output
coverage
.vscode
*.code-workspace
*.code-workspace
/testkit/CAs
1 change: 0 additions & 1 deletion bolt-connection/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ docs/build
.nyc_output
coverage
.vscode
/types
/docs
/lib6
*.code-workspace
1 change: 1 addition & 0 deletions bolt-connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "4.3.0",
"description": "Implements the connection with the Neo4j Database using the Bolt Protocol",
"main": "lib/index.js",
"types": "types/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest --passWithNoTests",
Expand Down
34 changes: 34 additions & 0 deletions bolt-connection/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
ConnectionProvider
} from 'neo4j-driver-core'

declare class DirectConnectionProvider extends ConnectionProvider {
constructor(config: any)
}

declare class RoutingConnectionProvider extends ConnectionProvider {
constructor(config: any)
}

export {
DirectConnectionProvider,
RoutingConnectionProvider
}
26 changes: 14 additions & 12 deletions core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ import {
DEFAULT_POOL_ACQUISITION_TIMEOUT,
DEFAULT_POOL_MAX_SIZE
} from './internal/constants'
import { Logger, LoggingConfig } from './internal/logger'
import { Logger } from './internal/logger'
import Session from './session'
import { ServerInfo } from './result-summary'
import { ENCRYPTION_ON, ENCRYPTION_OFF } from './internal/util'
import {
EncryptionLevel,
LoggingConfig,
TrustStrategy,
SessionMode
} from './types'
import { ServerAddress } from './internal/server-address'

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

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

/**
* Constant that represents write session access mode.
* Should be used like this: `driver.session({ defaultAccessMode: neo4j.session.WRITE })`.
* @type {string}
*/
const WRITE: string = ACCESS_MODE_WRITE
const WRITE: SessionMode = ACCESS_MODE_WRITE

let idGenerator = 0

interface MetaInfo {
routing: boolean
typename: string
address: string
address: string | ServerAddress
}

type CreateConnectionProvider = (
Expand All @@ -71,13 +78,8 @@ type CreateConnectionProvider = (
hostNameResolver: ConfiguredCustomResolver
) => ConnectionProvider

type TrustStrategy =
| 'TRUST_ALL_CERTIFICATES'
| 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES'
| 'TRUST_SYSTEM_CA_SIGNED_CERTIFICATES'

interface DriverConfig {
encrypted?: string
encrypted?: EncryptionLevel | boolean
trust?: TrustStrategy
fetchSize?: number
logging?: LoggingConfig
Expand Down Expand Up @@ -230,7 +232,7 @@ class Driver {
database = '',
fetchSize
}: {
defaultAccessMode?: string
defaultAccessMode?: SessionMode
bookmarks?: string | string[]
database?: string
fetchSize?: number
Expand Down Expand Up @@ -277,7 +279,7 @@ class Driver {
reactive,
fetchSize
}: {
defaultAccessMode: string
defaultAccessMode: SessionMode
bookmarkOrBookmarks?: string | string[]
database: string
reactive: boolean
Expand Down
6 changes: 6 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ import ResultSummary, {
} from './result-summary'
import Result, { QueryResult, ResultObserver } from './result'
import ConnectionProvider from './connection-provider'
import Connection from './connection'
import Transaction from './transaction'
import Session, { TransactionConfig } from './session'
import Driver, * as driver from './driver'
import * as types from './types'
import * as internal from './internal' // todo: removed afterwards

/**
Expand Down Expand Up @@ -132,6 +134,8 @@ const forExport = {
Transaction,
Session,
Driver,
Connection,
types,
driver
}

Expand Down Expand Up @@ -186,10 +190,12 @@ export {
QueryResult,
ResultObserver,
ConnectionProvider,
Connection,
Transaction,
Session,
TransactionConfig,
Driver,
types,
driver
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const FETCH_ALL = -1
const DEFAULT_POOL_ACQUISITION_TIMEOUT = 60 * 1000 // 60 seconds
const DEFAULT_POOL_MAX_SIZE = 100

const ACCESS_MODE_READ: string = 'READ'
const ACCESS_MODE_WRITE: string = 'WRITE'
const ACCESS_MODE_READ: 'READ' = 'READ'
const ACCESS_MODE_WRITE: 'WRITE' = 'WRITE'

const BOLT_PROTOCOL_V1: number = 1
const BOLT_PROTOCOL_V2: number = 2
Expand Down
11 changes: 2 additions & 9 deletions core/src/internal/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@
* limitations under the License.
*/
import { newError } from '../error'

export type LogLevel = 'warn' | 'error' | 'info' | 'debug'
type LoggerFunction = (level: LogLevel, message: string) => unknown

export interface LoggingConfig {
level?: LogLevel
logger: LoggerFunction
}
import { LogLevel, LoggerFunction, LoggingConfig } from '../types'

const ERROR: 'error' = 'error'
const WARN: 'warn' = 'warn'
Expand Down Expand Up @@ -61,7 +54,7 @@ export class Logger {
* @param {Object} driverConfig the driver configuration as supplied by the user.
* @return {Logger} a new logger instance or a no-op logger when not configured.
*/
static create(driverConfig: { logging?: LoggingConfig }) {
static create(driverConfig: { logging?: LoggingConfig }): Logger {
if (driverConfig && driverConfig.logging) {
const loggingConfig = driverConfig.logging
const level = extractConfiguredLevel(loggingConfig)
Expand Down
5 changes: 3 additions & 2 deletions core/src/internal/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
*/

import Integer, { isInt } from '../integer'
import { EncryptionLevel } from '../types'

const ENCRYPTION_ON: string = 'ENCRYPTION_ON'
const ENCRYPTION_OFF: string = 'ENCRYPTION_OFF'
const ENCRYPTION_ON: EncryptionLevel = 'ENCRYPTION_ON'
const ENCRYPTION_OFF: EncryptionLevel = 'ENCRYPTION_OFF'

/**
* Verifies if the object is null or empty
Expand Down
18 changes: 9 additions & 9 deletions core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { TransactionExecutor } from './internal/transaction-executor'
import { Bookmark } from './internal/bookmark'
import { TxConfig } from './internal/tx-config'
import ConnectionProvider from './connection-provider'
import { Query } from './types'
import { Query, SessionMode } from './types'
import Connection from './connection'
import { NumberOrInteger } from './graph-types'

Expand All @@ -47,7 +47,7 @@ interface TransactionConfig {
* @access public
*/
class Session {
private _mode: string
private _mode: SessionMode
private _database: string
private _reactive: boolean
private _fetchSize: number
Expand Down Expand Up @@ -80,9 +80,9 @@ class Session {
reactive,
fetchSize
}: {
mode: string
mode: SessionMode
connectionProvider: ConnectionProvider
bookmark: Bookmark
bookmark?: Bookmark
database: string
config: any
reactive: boolean
Expand All @@ -106,7 +106,7 @@ class Session {
})
this._open = true
this._hasTx = false
this._lastBookmark = bookmark
this._lastBookmark = bookmark || Bookmark.empty()
this._transactionExecutor = _createTransactionExecutor(config)
this._onComplete = this._onCompleteCallback.bind(this)
}
Expand Down Expand Up @@ -233,7 +233,7 @@ class Session {
return this._beginTransaction(this._mode, txConfig)
}

_beginTransaction(accessMode: string, txConfig: TxConfig): Transaction {
_beginTransaction(accessMode: SessionMode, txConfig: TxConfig): Transaction {
if (!this._open) {
throw newError('Cannot begin a transaction on a closed session.')
}
Expand Down Expand Up @@ -333,7 +333,7 @@ class Session {
}

_runTransaction<T>(
accessMode: string,
accessMode: SessionMode,
transactionConfig: TxConfig,
transactionWork: TransactionWork<T>
): Promise<T> {
Expand Down Expand Up @@ -369,7 +369,7 @@ class Session {
}
}

_connectionHolderWithMode(mode: string): ConnectionHolder {
_connectionHolderWithMode(mode: SessionMode): ConnectionHolder {
if (mode === ACCESS_MODE_READ) {
return this._readConnectionHolder
} else if (mode === ACCESS_MODE_WRITE) {
Expand All @@ -391,7 +391,7 @@ class Session {
/**
* @protected
*/
static _validateSessionMode(rawMode?: string): string {
static _validateSessionMode(rawMode?: SessionMode): SessionMode {
const mode = rawMode || ACCESS_MODE_WRITE
if (mode !== ACCESS_MODE_READ && mode !== ACCESS_MODE_WRITE) {
throw newError('Illegal session mode ' + mode)
Expand Down
43 changes: 43 additions & 0 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,46 @@
* @private
*/
export type Query = string | String | { text: string; parameters?: any }

export type EncryptionLevel = 'ENCRYPTION_ON' | 'ENCRYPTION_OFF'

export type LogLevel = 'error' | 'warn' | 'info' | 'debug'

export type LoggerFunction = (level: LogLevel, message: string) => unknown

export type SessionMode = 'READ' | 'WRITE'

export interface LoggingConfig {
level?: LogLevel
logger: LoggerFunction
}

export type TrustStrategy =
| 'TRUST_ALL_CERTIFICATES'
| 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES'
| 'TRUST_SYSTEM_CA_SIGNED_CERTIFICATES'

export type Parameters = { [key: string]: any }
export interface AuthToken {
scheme: string
principal: string
credentials: string
realm?: string
parameters?: Parameters
}
export interface Config {
encrypted?: boolean | EncryptionLevel
trust?: TrustStrategy
trustedCertificates?: string[]
knownHosts?: string
fetchSize?: number
maxConnectionPoolSize?: number
maxTransactionRetryTime?: number
maxConnectionLifetime?: number
connectionAcquisitionTimeout?: number
connectionTimeout?: number
disableLosslessIntegers?: boolean
logging?: LoggingConfig
resolver?: (address: string) => string[] | Promise<string[]>
userAgent?: string
}
29 changes: 0 additions & 29 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,35 +181,6 @@ gulp.task('stop-neo4j', function (done) {
done()
})

gulp.task(
'install-driver-into-testkit-backend',
gulp.series('nodejs', function () {
const dir = path.join('build', 'testkit-backend')
fs.emptyDirSync(dir)

const packageJsonContent = JSON.stringify({
private: true,
dependencies: {
'neo4j-driver': __dirname
}
})

return file('package.json', packageJsonContent, { src: true })
.pipe(gulp.dest(dir))
.pipe(install())
})
)

gulp.task(
'testkit-backend',
gulp.series('install-driver-into-testkit-backend', function () {
return gulp
.src('testkit-backend/**/*.js')
.pipe(babel())
.pipe(gulp.dest('build/testkit-backend'))
})
)

gulp.task('run-stress-tests', function () {
return gulp
.src('test/**/stress.test.js')
Expand Down
6 changes: 6 additions & 0 deletions neo4j-driver-lite/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build
docs
lib
node_modules
lib6
types
Loading