node-json-db
Advanced tools
Comparing version
import { JsonDBConfig } from './lib/JsonDBConfig'; | ||
export { Config } from './lib/JsonDBConfig'; | ||
export { DatabaseError, DataError } from './lib/Errors'; | ||
export declare type FindCallback = (entry: any, index: number | string) => boolean; | ||
export { Config } from './lib/JsonDBConfig'; | ||
export declare class JsonDB { | ||
@@ -8,2 +9,4 @@ private loaded; | ||
private readonly config; | ||
private readonly lock; | ||
private readonly lockKey; | ||
/** | ||
@@ -10,0 +13,0 @@ * JSONDB Constructor |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JsonDB = exports.Config = void 0; | ||
exports.JsonDB = exports.DataError = exports.DatabaseError = exports.Config = void 0; | ||
const Utils_1 = require("./lib/Utils"); | ||
@@ -8,4 +8,8 @@ const Errors_1 = require("./lib/Errors"); | ||
const ArrayInfo_1 = require("./lib/ArrayInfo"); | ||
const AsyncLock = require("async-lock"); | ||
var JsonDBConfig_1 = require("./lib/JsonDBConfig"); | ||
Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return JsonDBConfig_1.Config; } }); | ||
var Errors_2 = require("./lib/Errors"); | ||
Object.defineProperty(exports, "DatabaseError", { enumerable: true, get: function () { return Errors_2.DatabaseError; } }); | ||
Object.defineProperty(exports, "DataError", { enumerable: true, get: function () { return Errors_2.DataError; } }); | ||
class JsonDB { | ||
@@ -15,2 +19,4 @@ loaded = false; | ||
config; | ||
lock = new AsyncLock(); | ||
lockKey = 'jsonDb'; | ||
/** | ||
@@ -113,4 +119,6 @@ * JSONDB Constructor | ||
getData(dataPath) { | ||
const path = this.processDataPath(dataPath); | ||
return this.retrieveData(path, false); | ||
return this.lock.acquire(this.lockKey, async () => { | ||
const path = this.processDataPath(dataPath); | ||
return this.retrieveData(path, false); | ||
}); | ||
} | ||
@@ -240,29 +248,31 @@ /** | ||
async push(dataPath, data, override = true) { | ||
const dbData = await this.getParentData(dataPath, true); | ||
// if (!dbData) { | ||
// throw new Error('Data not found') | ||
// } | ||
let toSet = data; | ||
if (!override) { | ||
if (Array.isArray(data)) { | ||
let storedData = dbData.getData(); | ||
if (storedData === undefined) { | ||
storedData = []; | ||
return this.lock.acquire(this.lockKey, async () => { | ||
const dbData = await this.getParentData(dataPath, true); | ||
// if (!dbData) { | ||
// throw new Error('Data not found') | ||
// } | ||
let toSet = data; | ||
if (!override) { | ||
if (Array.isArray(data)) { | ||
let storedData = dbData.getData(); | ||
if (storedData === undefined) { | ||
storedData = []; | ||
} | ||
else if (!Array.isArray(storedData)) { | ||
throw new Errors_1.DataError("Can't merge another type of data with an Array", 3); | ||
} | ||
toSet = storedData.concat(data); | ||
} | ||
else if (!Array.isArray(storedData)) { | ||
throw new Errors_1.DataError("Can't merge another type of data with an Array", 3); | ||
else if (data === Object(data)) { | ||
if (Array.isArray(dbData.getData())) { | ||
throw new Errors_1.DataError("Can't merge an Array with an Object", 4); | ||
} | ||
toSet = (0, Utils_1.merge)(dbData.getData(), data); | ||
} | ||
toSet = storedData.concat(data); | ||
} | ||
else if (data === Object(data)) { | ||
if (Array.isArray(dbData.getData())) { | ||
throw new Errors_1.DataError("Can't merge an Array with an Object", 4); | ||
} | ||
toSet = (0, Utils_1.merge)(dbData.getData(), data); | ||
dbData.setData(toSet); | ||
if (this.config.saveOnPush) { | ||
await this.save(); | ||
} | ||
} | ||
dbData.setData(toSet); | ||
if (this.config.saveOnPush) { | ||
await this.save(); | ||
} | ||
}); | ||
} | ||
@@ -274,10 +284,12 @@ /** | ||
async delete(dataPath) { | ||
const dbData = await this.getParentData(dataPath, true); | ||
// if (!dbData) { | ||
// return | ||
// } | ||
dbData.delete(); | ||
if (this.config.saveOnPush) { | ||
await this.save(); | ||
} | ||
await this.lock.acquire(this.lockKey, async () => { | ||
const dbData = await this.getParentData(dataPath, true); | ||
// if (!dbData) { | ||
// return | ||
// } | ||
dbData.delete(); | ||
if (this.config.saveOnPush) { | ||
await this.save(); | ||
} | ||
}); | ||
} | ||
@@ -284,0 +296,0 @@ /** |
{ | ||
"name": "node-json-db", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "Database using JSON file as storage for Node.JS", | ||
@@ -32,2 +32,3 @@ "main": "dist/JsonDB.js", | ||
"dependencies": { | ||
"async-lock": "^1.3.2", | ||
"atomically": "^1.7.0" | ||
@@ -65,2 +66,3 @@ }, | ||
"@semantic-release/git": "^10.0.0", | ||
"@types/async-lock": "^1.1.5", | ||
"@types/jest": "^28.1.6", | ||
@@ -67,0 +69,0 @@ "@types/mkdirp": "^1.0.1", |
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
73390
1.92%1032
1.47%2
100%18
5.88%1
Infinity%+ Added
+ Added