|
| 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 | +} |
0 commit comments