Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added the features again, but better(hopefully) #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
declare class JSONdb<T = any> {
constructor(filePath: string, options?: { asyncWrite?: boolean, syncOnWrite?: boolean, jsonSpaces?: boolean, stringify?: (o:T) => string, parse?: (s:string) => (T | undefined) });
constructor(filePath: string, options?: {
asyncWrite?: boolean,
syncOnWrite?: boolean,
jsonSpaces?: boolean,
stringify?: (o:T) => string,
parse?: (s:string) => (T | undefined)
});

set(key: string, value: T) : void;
set(key: string, value: T) : undefined;
get(key: string) : T | undefined;
has(key: string) : boolean;
delete(key: string) : boolean | undefined;
deleteAll() : this;
sync() : void;
sync() : Promise<undefined> | undefined;
JSON(storage?: Record<string, T>) : Record<string, T>;
}

Expand Down
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ let validateJSON = function(fileContent) {
* @param {boolean} [options.asyncWrite] Enables the storage to be asynchronously written to disk. Disabled by default (synchronous behaviour).
* @param {boolean} [options.syncOnWrite] Makes the storage be written to disk after every modification. Enabled by default.
* @param {boolean} [options.syncOnWrite] Makes the storage be written to disk after every modification. Enabled by default.
* @param {number} [options.jsonSpaces] How many spaces to use for indentation in the output json files. Default = 4
* @param {number} [options.jsonSpaces] How many spaces to use for indentation in the output JSON files. Default = 4
* @param {Function} [options.stringify] A custom JSON stringifier to use in the database. Default = JSON.stringify
* @param {Function} [options.parse] A custom JSON parser for the database. Default = JSON.parse
* @constructor
*/
function JSONdb(filePath, options) {
Expand All @@ -47,6 +49,7 @@ function JSONdb(filePath, options) {

// Options parsing
if (options) {
this.origoptions = options;
for (let key in defaultOptions) {
if (!options.hasOwnProperty(key)) options[key] = defaultOptions[key];
}
Expand Down Expand Up @@ -146,13 +149,19 @@ JSONdb.prototype.deleteAll = function() {
* Writes the local storage object to disk.
*/
JSONdb.prototype.sync = function() {
let args = [this.storage];
if (this.origoptions && this.origoptions.stringify) {//custom stringifier
args = [this.storage, null, this.options.jsonSpaces];
}
if (this.options && this.options.asyncWrite) {
fs.writeFile(this.filePath, this.options.stringify(this.storage, null, this.options.jsonSpaces), (err) => {
if (err) throw err;
return new Promise((res, rej) => {
fs.writeFile(this.filePath, this.options.stringify(...args), (err) => {
if (err) rej(err); else res();
});
});
} else {
try {
fs.writeFileSync(this.filePath, this.options.stringify(this.storage, null, this.options.jsonSpaces));
fs.writeFileSync(this.filePath, this.options.stringify(...args));
} catch (err) {
if (err.code === 'EACCES') {
throw new Error(`Cannot access path "${this.filePath}".`);
Expand Down