@matrixai/db
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -6,2 +6,5 @@ /// <reference types="node" /> | ||
import Logger from '@matrixai/logger'; | ||
import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop'; | ||
interface DB extends CreateDestroyStartStop { | ||
} | ||
declare class DB { | ||
@@ -29,5 +32,3 @@ static createDB({ dbPath, crypto, lock, fs, logger, fresh, }: { | ||
protected _db: LevelDB<string | Buffer, Buffer>; | ||
protected _running: boolean; | ||
protected _destroyed: boolean; | ||
protected constructor({ dbPath, crypto, lock, fs, logger, }: { | ||
constructor({ dbPath, crypto, lock, fs, logger, }: { | ||
dbPath: string; | ||
@@ -44,4 +45,2 @@ crypto?: { | ||
get locked(): boolean; | ||
get running(): boolean; | ||
get destroyed(): boolean; | ||
start({ fresh, }?: { | ||
@@ -48,0 +47,0 @@ fresh?: boolean; |
213
dist/DB.js
@@ -14,2 +14,8 @@ "use strict"; | ||
}); | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
@@ -25,2 +31,3 @@ if (mod && mod.__esModule) return mod; | ||
}; | ||
var DB_1; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -32,9 +39,8 @@ const level_1 = __importDefault(require("level")); | ||
const logger_1 = __importDefault(require("@matrixai/logger")); | ||
const CreateDestroyStartStop_1 = require("@matrixai/async-init/dist/CreateDestroyStartStop"); | ||
const Transaction_1 = __importDefault(require("./Transaction")); | ||
const utils = __importStar(require("./utils")); | ||
const errors = __importStar(require("./errors")); | ||
class DB { | ||
let DB = DB_1 = class DB { | ||
constructor({ dbPath, crypto, lock, fs, logger, }) { | ||
this._running = false; | ||
this._destroyed = false; | ||
this.logger = logger; | ||
@@ -48,3 +54,3 @@ this.dbPath = dbPath; | ||
logger.info(`Creating ${this.name}`); | ||
const db = new DB({ | ||
const db = new DB_1({ | ||
dbPath, | ||
@@ -66,98 +72,68 @@ crypto, | ||
} | ||
get running() { | ||
return this._running; | ||
} | ||
get destroyed() { | ||
return this._destroyed; | ||
} | ||
async start({ fresh = false, } = {}) { | ||
return this.withLocks(async () => { | ||
if (this._running) { | ||
return; | ||
} | ||
if (this._destroyed) { | ||
throw new errors.ErrorDBDestroyed(); | ||
} | ||
this.logger.info(`Starting ${this.constructor.name}`); | ||
this.logger.info(`Setting DB path to ${this.dbPath}`); | ||
if (fresh) { | ||
try { | ||
await this.fs.promises.rm(this.dbPath, { | ||
force: true, | ||
recursive: true, | ||
}); | ||
} | ||
catch (e) { | ||
throw new errors.ErrorDBDelete(e.message, undefined, e); | ||
} | ||
} | ||
this.logger.info(`Starting ${this.constructor.name}`); | ||
this.logger.info(`Setting DB path to ${this.dbPath}`); | ||
if (fresh) { | ||
try { | ||
await this.fs.promises.mkdir(this.dbPath); | ||
await this.fs.promises.rm(this.dbPath, { | ||
force: true, | ||
recursive: true, | ||
}); | ||
} | ||
catch (e) { | ||
if (e.code !== 'EEXIST') { | ||
throw new errors.ErrorDBCreate(e.message, undefined, e); | ||
} | ||
throw new errors.ErrorDBDelete(e.message, undefined, e); | ||
} | ||
let dbLevel; | ||
try { | ||
dbLevel = await new Promise((resolve, reject) => { | ||
const db = level_1.default(this.dbPath, { | ||
keyEncoding: 'binary', | ||
valueEncoding: 'binary', | ||
}, (e) => { | ||
if (e) { | ||
reject(e); | ||
} | ||
else { | ||
resolve(db); | ||
} | ||
}); | ||
}); | ||
} | ||
catch (e) { | ||
} | ||
try { | ||
await this.fs.promises.mkdir(this.dbPath); | ||
} | ||
catch (e) { | ||
if (e.code !== 'EEXIST') { | ||
throw new errors.ErrorDBCreate(e.message, undefined, e); | ||
} | ||
this._db = dbLevel; | ||
this._running = true; | ||
this.logger.info(`Started ${this.constructor.name}`); | ||
}); | ||
} | ||
let dbLevel; | ||
try { | ||
dbLevel = await new Promise((resolve, reject) => { | ||
const db = level_1.default(this.dbPath, { | ||
keyEncoding: 'binary', | ||
valueEncoding: 'binary', | ||
}, (e) => { | ||
if (e) { | ||
reject(e); | ||
} | ||
else { | ||
resolve(db); | ||
} | ||
}); | ||
}); | ||
} | ||
catch (e) { | ||
throw new errors.ErrorDBCreate(e.message, undefined, e); | ||
} | ||
this._db = dbLevel; | ||
this.logger.info(`Started ${this.constructor.name}`); | ||
} | ||
async stop() { | ||
return this.withLocks(async () => { | ||
if (!this._running) { | ||
return; | ||
} | ||
this.logger.info(`Stopping ${this.constructor.name}`); | ||
await this.db.close(); | ||
this._running = false; | ||
this.logger.info(`Stopped ${this.constructor.name}`); | ||
}); | ||
this.logger.info(`Stopping ${this.constructor.name}`); | ||
await this.db.close(); | ||
this.logger.info(`Stopped ${this.constructor.name}`); | ||
} | ||
async destroy() { | ||
return this.withLocks(async () => { | ||
if (this._destroyed) { | ||
return; | ||
} | ||
if (this._running) { | ||
throw new errors.ErrorDBRunning(); | ||
} | ||
this.logger.info(`Destroying ${this.constructor.name}`); | ||
try { | ||
await this.fs.promises.rm(this.dbPath, { | ||
force: true, | ||
recursive: true, | ||
}); | ||
} | ||
catch (e) { | ||
throw new errors.ErrorDBDelete(e.message, { | ||
errno: e.errno, | ||
syscall: e.syscall, | ||
code: e.code, | ||
path: e.path, | ||
}); | ||
} | ||
this._destroyed = true; | ||
this.logger.info(`Destroyed ${this.constructor.name}`); | ||
}); | ||
this.logger.info(`Destroying ${this.constructor.name}`); | ||
try { | ||
await this.fs.promises.rm(this.dbPath, { | ||
force: true, | ||
recursive: true, | ||
}); | ||
} | ||
catch (e) { | ||
throw new errors.ErrorDBDelete(e.message, { | ||
errno: e.errno, | ||
syscall: e.syscall, | ||
code: e.code, | ||
path: e.path, | ||
}); | ||
} | ||
this.logger.info(`Destroyed ${this.constructor.name}`); | ||
} | ||
@@ -196,5 +172,2 @@ setWorkerManager(workerManager) { | ||
return this.withLocks(async () => { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
const tran = new Transaction_1.default({ db: this, logger: this.logger }); | ||
@@ -216,7 +189,4 @@ let value; | ||
async level(domain, dbLevel = this._db) { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
try { | ||
return new Promise((resolve) => { | ||
return await new Promise((resolve, reject) => { | ||
const dbLevelNew = subleveldown_1.default(dbLevel, domain, { | ||
@@ -226,2 +196,7 @@ keyEncoding: 'binary', | ||
open: (cb) => { | ||
// This `cb` is defaulted (hardcoded) to a function that emits an error event | ||
// When using `level`, we are able to provide a callback that overrides this `cb` | ||
// However `subleveldown` does not provide a callback parameter | ||
// It provides this `open` option, which requires us to call `cb` to finish | ||
// If we provide an exception as a parameter, it will be received by the `error` event handler | ||
cb(undefined); | ||
@@ -231,2 +206,7 @@ resolve(dbLevelNew); | ||
}); | ||
// @ts-ignore error event for subleveldown | ||
dbLevelNew.on('error', (e) => { | ||
// Errors during construction of the sublevel will be emitted as events | ||
reject(e); | ||
}); | ||
}); | ||
@@ -243,5 +223,2 @@ } | ||
async count(dbLevel = this._db) { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
let count = 0; | ||
@@ -254,5 +231,2 @@ for await (const _ of dbLevel.createKeyStream()) { | ||
async get(domain, key, raw = false) { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
let data; | ||
@@ -271,5 +245,2 @@ try { | ||
async put(domain, key, value, raw = false) { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
const data = await this.serializeEncrypt(value, raw); | ||
@@ -279,11 +250,5 @@ return this._db.put(utils.domainPath(domain, key), data); | ||
async del(domain, key) { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
return this._db.del(utils.domainPath(domain, key)); | ||
} | ||
async batch(ops) { | ||
if (!this._running) { | ||
throw new errors.ErrorDBNotRunning(); | ||
} | ||
const opsP = []; | ||
@@ -359,4 +324,28 @@ for (const op of ops) { | ||
} | ||
} | ||
}; | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "transact", null); | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "level", null); | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "count", null); | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "get", null); | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "put", null); | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "del", null); | ||
__decorate([ | ||
CreateDestroyStartStop_1.ready(new errors.ErrorDBNotRunning()) | ||
], DB.prototype, "batch", null); | ||
DB = DB_1 = __decorate([ | ||
CreateDestroyStartStop_1.CreateDestroyStartStop(new errors.ErrorDBRunning(), new errors.ErrorDBDestroyed()) | ||
], DB); | ||
exports.default = DB; | ||
//# sourceMappingURL=DB.js.map |
{ | ||
"name": "@matrixai/db", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"author": "Roger Qiu", | ||
@@ -20,5 +20,6 @@ "description": "DB", | ||
"docs": "rm -r ./docs || true; typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src && touch ./docs/.nojekyll", | ||
"bench": "ts-node -r tsconfig-paths/register ./benches" | ||
"bench": "rm -r ./benches/results || true; ts-node -r tsconfig-paths/register ./benches" | ||
}, | ||
"dependencies": { | ||
"@matrixai/async-init": "^1.6.0", | ||
"@matrixai/logger": "^2.0.1", | ||
@@ -36,10 +37,15 @@ "@matrixai/workers": "^1.2.3", | ||
"devDependencies": { | ||
"@types/abstract-leveldown": "^7.2.0", | ||
"@types/jest": "^26.0.20", | ||
"@types/level": "^6.0.0", | ||
"@types/levelup": "^5.1.0", | ||
"@types/node": "^14.14.35", | ||
"@types/node-forge": "^0.10.4", | ||
"@typescript-eslint/eslint-plugin": "^4.12.0", | ||
"@typescript-eslint/parser": "^4.12.0", | ||
"@types/subleveldown": "^4.1.1", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "^5.4.0", | ||
"benny": "^3.6.15", | ||
"eslint": "^7.17.0", | ||
"eslint-config-prettier": "^7.1.0", | ||
"eslint-plugin-import": "^2.25.3", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
@@ -52,3 +58,3 @@ "jest": "^26.6.3", | ||
"ts-jest": "^26.4.4", | ||
"ts-node": "^9.1.1", | ||
"ts-node": "^10.4.0", | ||
"tsconfig-paths": "^3.9.0", | ||
@@ -55,0 +61,0 @@ "typedoc": "^0.21.5", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61665
11
24
870
+ Added@matrixai/async-init@^1.6.0