From 3e9ec7ca103e5aea008062190cc9c32354901e05 Mon Sep 17 00:00:00 2001 From: Rahul Choubey <64599476+a-a-GiTHuB-a-a@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:07:12 -0500 Subject: [PATCH 1/3] Added fixes hmm... --- index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 25c64ac..a4b43e1 100644 --- a/index.js +++ b/index.js @@ -47,6 +47,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]; } @@ -146,13 +147,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}".`); From 6eeab1e001df28c4475bb225ed3f564dc79cb1f8 Mon Sep 17 00:00:00 2001 From: Rahul Choubey <64599476+a-a-GiTHuB-a-a@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:16:21 -0500 Subject: [PATCH 2/3] Added promise sync to typedefs --- index.d.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3a5c049..1f78c9a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,12 +1,18 @@ declare class JSONdb { - 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; JSON(storage?: Record) : Record; } From 66bcd340148cdfa0fdcc410833da7b9bcf26011e Mon Sep 17 00:00:00 2001 From: Rahul Choubey <64599476+a-a-GiTHuB-a-a@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:23:12 -0500 Subject: [PATCH 3/3] Added new options to JSDoc --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a4b43e1..502e83c 100644 --- a/index.js +++ b/index.js @@ -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) {