simple-json-db
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -1,6 +0,6 @@ | ||
declare class JSONdb { | ||
constructor(filePath: string, options?: { asyncWrite?: boolean, syncOnWrite?: boolean }); | ||
declare class JSONdb<T = object> { | ||
constructor(filePath: string, options?: { asyncWrite?: boolean, syncOnWrite?: boolean, jsonSpaces?: boolean, stringify?: (o:T) => string, parse?: (s:string) => (T | undefined) }); | ||
set(key: string, value: object) : void; | ||
get(key: string) : object | undefined; | ||
set(key: string, value: T) : void; | ||
get(key: string) : T | undefined; | ||
has(key: string) : boolean; | ||
@@ -10,5 +10,5 @@ delete(key: string) : boolean | undefined; | ||
sync() : void; | ||
JSON(storage?: object) : object; | ||
JSON(storage?: Record<string, T>) : Record<string, T>; | ||
} | ||
export = JSONdb; |
19
index.js
@@ -10,3 +10,5 @@ const fs = require("fs"); | ||
syncOnWrite: true, | ||
jsonSpaces: 4 | ||
jsonSpaces: 4, | ||
stringify: JSON.stringify, | ||
parse: JSON.parse | ||
}; | ||
@@ -21,5 +23,6 @@ | ||
try { | ||
JSON.parse(fileContent); | ||
this.options.parse(fileContent); | ||
} catch (e) { | ||
throw new Error('Given filePath is not empty and its content is not valid JSON.'); | ||
console.error('Given filePath is not empty and its content is not valid JSON.'); | ||
throw e; | ||
} | ||
@@ -89,3 +92,3 @@ return true; | ||
} | ||
if (validateJSON(data)) this.storage = JSON.parse(data); | ||
if (validateJSON.bind(this)(data)) this.storage = this.options.parse(data); | ||
} | ||
@@ -150,3 +153,3 @@ } | ||
if (this.options && this.options.asyncWrite) { | ||
fs.writeFile(this.filePath, JSON.stringify(this.storage, null, this.options.jsonSpaces), (err) => { | ||
fs.writeFile(this.filePath, this.options.stringify(this.storage, null, this.options.jsonSpaces), (err) => { | ||
if (err) throw err; | ||
@@ -156,3 +159,3 @@ }); | ||
try { | ||
fs.writeFileSync(this.filePath, JSON.stringify(this.storage, null, this.options.jsonSpaces)); | ||
fs.writeFileSync(this.filePath, this.options.stringify(this.storage, null, this.options.jsonSpaces)); | ||
} catch (err) { | ||
@@ -176,3 +179,3 @@ if (err.code === 'EACCES') { | ||
try { | ||
JSON.parse(JSON.stringify(storage)); | ||
JSON.parse(this.options.stringify(storage)); | ||
this.storage = storage; | ||
@@ -183,5 +186,5 @@ } catch (err) { | ||
} | ||
return JSON.parse(JSON.stringify(this.storage)); | ||
return JSON.parse(this.options.stringify(this.storage)); | ||
}; | ||
module.exports = JSONdb; |
{ | ||
"name": "simple-json-db", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "A simple, no-frills, JSON storage engine for Node.JS", | ||
@@ -34,6 +34,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "4.2.0", | ||
"mocha": "7.0.0" | ||
}, | ||
"dependencies": {} | ||
"chai": "^4.3.6", | ||
"mocha": "^9.2.1" | ||
} | ||
} |
@@ -1,5 +0,7 @@ | ||
# Simple JSONdb [![npm](https://img.shields.io/npm/v/simple-json-db)](https://www.npmjs.com/package/simple-json-db) [![npm](https://img.shields.io/npm/dw/simple-json-db)](https://www.npmjs.com/package/simple-json-db) [![Minimum NodeJS version](https://img.shields.io/node/v/simple-json-db)](https://www.npmjs.com/package/simple-json-db) [![Dependents (via libraries.io)](https://img.shields.io/librariesio/dependents/npm/simple-json-db)](https://libraries.io/npm/simple-json-db/dependents) [![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/simple-json-db)](https://snyk.io/vuln/search?q=simple-json-db&type=npm) | ||
# Simple JSONdb [![npm](https://img.shields.io/npm/v/simple-json-db)](https://www.npmjs.com/package/simple-json-db) [![npm](https://img.shields.io/npm/dw/simple-json-db)](https://www.npmjs.com/package/simple-json-db) [![Minimum NodeJS version](https://img.shields.io/node/v/simple-json-db)](https://www.npmjs.com/package/simple-json-db) [![Dependents (via libraries.io)](https://img.shields.io/librariesio/dependents/npm/simple-json-db)](https://libraries.io/npm/simple-json-db/dependents) ![npm bundle size](https://img.shields.io/bundlephobia/min/simple-json-db) | ||
A simple, no-frills, JSON storage engine for Node.JS with full test coverage. | ||
A simple, no-frills, JSON **key-value** storage engine for Node.JS with full test coverage. | ||
> [What is a key-value storage and when to use it?](https://redislabs.com/nosql/key-value-databases/) | ||
## Installation | ||
@@ -14,3 +16,3 @@ | ||
const JSONdb = require('simple-json-db'); | ||
const db = new JSONdb('/path/to/your/database.json'); | ||
const db = new JSONdb('/path/to/your/storage.json'); | ||
``` | ||
@@ -21,3 +23,3 @@ | ||
``` | ||
const db = new JSONdb('/path/to/your/database.json', { ... }); | ||
const db = new JSONdb('/path/to/your/storage.json', { ... }); | ||
``` | ||
@@ -29,8 +31,12 @@ | ||
| **Key** | **Value type** | **Description** | **Default value** | | ||
|-------------|----------------|----------------------------------------------------------------|-------------------------------------| | ||
| asyncWrite | _Boolean_ | Enables the storage to be asynchronously written to disk. | _**false**_ (synchronous behaviour) | | ||
| syncOnWrite | _Boolean_ | Makes the storage be written to disk after every modification. | _**true**_ | | ||
| jsonSpaces | _Number_ | The number of spaces used for indentation in the output JSON. | _**4**_ | | ||
All keys are optional and will default to a safe value. | ||
| **Key** | **Value type** | **Description** | **Default value** | | ||
|-------------|------------------------------|-------------------------------------------------------------------|-------------------------------------| | ||
| asyncWrite | _Boolean_ | Enables the storage to be asynchronously written to disk. | _**false**_ (synchronous behaviour) | | ||
| syncOnWrite | _Boolean_ | Makes the storage be written to disk after every modification. | _**true**_ | | ||
| jsonSpaces | _Number_ | The number of spaces used for indentation in the output JSON. | _**4**_ | | ||
| stringify | _Function(object) => string_ | A stringifier function to serialize JS objects into JSON strings. | _**JSON.stringify**_ | | ||
| parse | _Function(string) => object_ | A parser function to deserialize JSON strings into JS objects. | _**JSON.parse**_ | | ||
### Set a key | ||
@@ -44,3 +50,3 @@ `db.set('key', 'value');` | ||
The `key` parameter must be a string. If the key exhists its value is returned, if it doesn't the function returns `undefined`. | ||
The `key` parameter must be a string. If the key exists its value is returned, if it doesn't the function returns `undefined`. | ||
@@ -50,3 +56,3 @@ ### Check a key | ||
The `key` parameter must be a string. If the key exhists `true` is returned, if it doesn't the function returns `false`. | ||
The `key` parameter must be a string. If the key exists `true` is returned, if it doesn't the function returns `false`. | ||
@@ -57,3 +63,3 @@ ### Delete a key | ||
The `key` parameter must be a string. The function returns [as per the _delete_ operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Return_value) if the key exhists, else it returns `undefined`. | ||
The `key` parameter must be a string. The function returns [as per the _delete_ operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Return_value) if the key exists, else it returns `undefined`. | ||
@@ -60,0 +66,0 @@ ### Sync to disk |
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
16406
8
296
78