Skip to content

Commit 686073e

Browse files
committed
move tx-config to core
1 parent 82c435a commit 686073e

File tree

3 files changed

+112
-77
lines changed

3 files changed

+112
-77
lines changed

core/src/internal/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@ import * as observer from './observers'
2323
import * as bookmark from './bookmark'
2424
import * as constants from './constants'
2525
import * as connectionHolder from './connection-holder'
26+
import * as txConfig from './tx-config'
2627

27-
export { util, temporalUtil, observer, bookmark, constants, connectionHolder }
28+
export {
29+
util,
30+
temporalUtil,
31+
observer,
32+
bookmark,
33+
constants,
34+
connectionHolder,
35+
txConfig
36+
}

core/src/internal/tx-config.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
20+
import * as util from './util'
21+
import { newError } from '../error'
22+
import Integer, { int } from '../integer'
23+
24+
/**
25+
* Internal holder of the transaction configuration.
26+
* It performs input validation and value conversion for further serialization by the Bolt protocol layer.
27+
* Users of the driver provide transaction configuration as regular objects `{timeout: 10, metadata: {key: 'value'}}`.
28+
* Driver converts such objects to {@link TxConfig} immediately and uses converted values everywhere.
29+
*/
30+
export class TxConfig {
31+
readonly timeout: Integer | null
32+
readonly metadata: any
33+
34+
/**
35+
* @constructor
36+
* @param {Object} config the raw configuration object.
37+
*/
38+
constructor(config: any) {
39+
assertValidConfig(config)
40+
this.timeout = extractTimeout(config)
41+
this.metadata = extractMetadata(config)
42+
}
43+
44+
/**
45+
* Get an empty config object.
46+
* @return {TxConfig} an empty config.
47+
*/
48+
static empty(): TxConfig {
49+
return EMPTY_CONFIG
50+
}
51+
52+
/**
53+
* Check if this config object is empty. I.e. has no configuration values specified.
54+
* @return {boolean} `true` if this object is empty, `false` otherwise.
55+
*/
56+
isEmpty(): boolean {
57+
return Object.values(this).every(value => value == null)
58+
}
59+
}
60+
61+
const EMPTY_CONFIG = new TxConfig({})
62+
63+
/**
64+
* @return {Integer|null}
65+
*/
66+
function extractTimeout(config: any): Integer | null {
67+
if (util.isObject(config) && (config.timeout || config.timeout === 0)) {
68+
util.assertNumberOrInteger(config.timeout, 'Transaction timeout')
69+
const timeout = int(config.timeout)
70+
if (timeout.isZero()) {
71+
throw newError('Transaction timeout should not be zero')
72+
}
73+
if (timeout.isNegative()) {
74+
throw newError('Transaction timeout should not be negative')
75+
}
76+
return timeout
77+
}
78+
return null
79+
}
80+
81+
/**
82+
* @return {object|null}
83+
*/
84+
function extractMetadata(config: any): any {
85+
if (util.isObject(config) && config.metadata) {
86+
const metadata = config.metadata
87+
util.assertObject(metadata, 'config.metadata')
88+
if (Object.keys(metadata).length !== 0) {
89+
// not an empty object
90+
return metadata
91+
}
92+
}
93+
return null
94+
}
95+
96+
function assertValidConfig(config: any): void {
97+
if (config) {
98+
util.assertObject(config, 'Transaction config')
99+
}
100+
}

src/internal/tx-config.js

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,6 @@
1717
* limitations under the License.
1818
*/
1919

20-
import * as util from './util'
21-
import { int, newError } from 'neo4j-driver-core'
20+
import { internal } from 'neo4j-driver-core'
2221

23-
/**
24-
* Internal holder of the transaction configuration.
25-
* It performs input validation and value conversion for further serialization by the Bolt protocol layer.
26-
* Users of the driver provide transaction configuration as regular objects `{timeout: 10, metadata: {key: 'value'}}`.
27-
* Driver converts such objects to {@link TxConfig} immediately and uses converted values everywhere.
28-
*/
29-
export default class TxConfig {
30-
/**
31-
* @constructor
32-
* @param {Object} config the raw configuration object.
33-
*/
34-
constructor (config) {
35-
assertValidConfig(config)
36-
this.timeout = extractTimeout(config)
37-
this.metadata = extractMetadata(config)
38-
}
39-
40-
/**
41-
* Get an empty config object.
42-
* @return {TxConfig} an empty config.
43-
*/
44-
static empty () {
45-
return EMPTY_CONFIG
46-
}
47-
48-
/**
49-
* Check if this config object is empty. I.e. has no configuration values specified.
50-
* @return {boolean} `true` if this object is empty, `false` otherwise.
51-
*/
52-
isEmpty () {
53-
return Object.values(this).every(value => value == null)
54-
}
55-
}
56-
57-
const EMPTY_CONFIG = new TxConfig({})
58-
59-
/**
60-
* @return {Integer|null}
61-
*/
62-
function extractTimeout (config) {
63-
if (util.isObject(config) && (config.timeout || config.timeout === 0)) {
64-
util.assertNumberOrInteger(config.timeout, 'Transaction timeout')
65-
const timeout = int(config.timeout)
66-
if (timeout.isZero()) {
67-
throw newError('Transaction timeout should not be zero')
68-
}
69-
if (timeout.isNegative()) {
70-
throw newError('Transaction timeout should not be negative')
71-
}
72-
return timeout
73-
}
74-
return null
75-
}
76-
77-
/**
78-
* @return {object|null}
79-
*/
80-
function extractMetadata (config) {
81-
if (util.isObject(config) && config.metadata) {
82-
const metadata = config.metadata
83-
util.assertObject(metadata)
84-
if (Object.keys(metadata).length !== 0) {
85-
// not an empty object
86-
return metadata
87-
}
88-
}
89-
return null
90-
}
91-
92-
function assertValidConfig (config) {
93-
if (config) {
94-
util.assertObject(config, 'Transaction config')
95-
}
96-
}
22+
export default internal.txConfig.TxConfig

0 commit comments

Comments
 (0)