diff --git a/lib/database.ts b/lib/database.ts index b80c8b7..738c59d 100644 --- a/lib/database.ts +++ b/lib/database.ts @@ -1,14 +1,9 @@ import * as fdb from './native' -import Transaction, { - RangeOptions, - Watch, - WatchOptions, -} from './transaction' +import Transaction, { RangeOptions, Watch } from './transaction' import {Transformer, defaultTransformer} from './transformer' import {NativeValue} from './native' import {KeySelector} from './keySelector' -import Subspace, { defaultSubspace, GetSubspace, isGetSubspace } from './subspace' -import {asBuf} from './util' +import Subspace, { root, GetSubspace, isGetSubspace } from './subspace' import {eachOption} from './opts' import {DatabaseOptions, TransactionOptions, @@ -38,7 +33,7 @@ export default class Database 0) { + if ((await txn.at(root).getRangeAllStartsWith(prefix, {limit: 1})).length > 0) { throw new DirectoryError('The database has keys stored at the prefix chosen by the automatic prefix allocator: ' + inspect(prefix)) } diff --git a/lib/index.ts b/lib/index.ts index 8f1d333..ec86671 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -9,7 +9,7 @@ import Database from './database' import {eachOption} from './opts' import {NetworkOptions, networkOptionData, DatabaseOptions} from './opts.g' import {Transformer} from './transformer' -import {defaultSubspace} from './subspace' +import {root} from './subspace' import {DirectoryLayer} from './directory' import * as apiVersion from './apiVersion' @@ -49,7 +49,7 @@ export {default as keySelector, KeySelector} from './keySelector' // always be constructed using open or via a cluster object. export {default as Database} from './database' export {default as Transaction, Watch} from './transaction' -export {default as Subspace, defaultSubspace} from './subspace' +export {default as Subspace, root} from './subspace' export {Directory, DirectoryLayer} from './directory' export { @@ -122,7 +122,7 @@ export function configNetwork(netOpts: NetworkOptions) { export function open(clusterFile?: string, dbOpts?: DatabaseOptions) { init() - const db = new Database(nativeMod.createDatabase(clusterFile), defaultSubspace) + const db = new Database(nativeMod.createDatabase(clusterFile), root) if (dbOpts) db.setNativeOptions(dbOpts) return db } diff --git a/lib/subspace.ts b/lib/subspace.ts index 8d2a149..eb52e0c 100644 --- a/lib/subspace.ts +++ b/lib/subspace.ts @@ -94,7 +94,7 @@ export default class Subspace { getSubspace(): Subspace diff --git a/lib/transaction.ts b/lib/transaction.ts index 18aeeaf..aac92f9 100644 --- a/lib/transaction.ts +++ b/lib/transaction.ts @@ -296,10 +296,11 @@ export default class Transaction | KeyIn): Promise { const sel = keySelector.from(_sel) return this._tn.getKey(this._keyEncoding.pack(sel.key), sel.orEqual, sel.offset, this.isSnapshot) - .then(key => { - if (key.length === 0 || !this.subspace.contains(key)) return undefined - else return this._keyEncoding.unpack(key) - }) + .then(key => ( + (key.length === 0 || !this.subspace.contains(key)) + ? undefined + : this._keyEncoding.unpack(key) + )) } /** Set the specified key/value pair in the database */ diff --git a/test/util.ts b/test/util.ts index 3785bba..0b2d231 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,6 +1,5 @@ import 'mocha' import * as fdb from '../lib' -import Subspace, { defaultSubspace } from '../lib/subspace' // We'll tuck everything behind this prefix and delete it all when the tests finish running. export const prefix = '__test_data__/'