Coder Social home page Coder Social logo

node-json-db's Introduction

NodeJs codecov npm version

A simple "database" that uses JSON file for Node.JS.

Breaking changes since v1.x.x

v2.0.0

JsonDB is now using the concept of async/await for all its calls since we read from the database file on demand and depending on how the database is configured, we might write at each push.

  • You're now forced to use the Config object to setup JsonDB
  • Every method is now asynchronous

Installation

Add node-json-db to your existing Node.js project.

YARN:

yarn add node-json-db

NPM:

npm i node-json-db

Documentation

Auto Generated

Inner Working

Data

The module stores the data using JavaScript Object directly into a JSON file. You can easily traverse the data to directly reach the desired property using the DataPath. The principle of DataPath is the same as XMLPath.

Example

{
    test: {
        data1 : {
            array : ['test','array']
        },
        data2 : 5
    }
}

If you want to fetch the value of array, the DataPath is /test/data1/array To reach the value of data2 : /test/data2 You can of course also get the full object test : /test Or even the root : /

Usage

See test for more usage details.

import { JsonDB, Config } from 'node-json-db';

// The first argument is the database filename. If no extension is used, '.json' is assumed and automatically added.
// The second argument is used to tell the DB to save after each push
// If you set the second argument to false, you'll have to call the save() method.
// The third argument is used to ask JsonDB to save the database in a human readable format. (default false)
// The last argument is the separator. By default it's slash (/)
var db = new JsonDB(new Config("myDataBase", true, false, '/'));

// Pushing the data into the database
// With the wanted DataPath
// By default the push will override the old value
await db.push("/test1","super test");

// When pushing new data for a DataPath that doesn't exist, it automatically creates the hierarchy
await db.push("/test2/my/test",5);

// You can also push objects directly
await db.push("/test3", {test:"test", json: {test:["test"]}});

// If you don't want to override the data but to merge them
// The merge is recursive and works with Object and Array.
await db.push("/test3", {
    new:"cool",
    json: {
        important : 5
    }
}, false);

/*
This give you this results :
{
   "test":"test",
   "json":{
      "test":[
         "test"
      ],
      "important":5
   },
   "new":"cool"
}
*/

// You can't merge primitives.
// If you do this:
await db.push("/test2/my/test/",10,false);

// The data will be overriden

// Get the data from the root
var data = await db.getData("/");

// Or from a particular DataPath
var data = await db.getData("/test1");

// If you try to get some data from a DataPath that doesn't exist
// You'll get an Error
try {
    var data = await db.getData("/test1/test/dont/work");
} catch(error) {
    // The error will tell you where the DataPath stopped. In this case test1
    // Since /test1/test does't exist.
    console.error(error);
};

// Easier than try catch when the path doesn't lead to data
// This will return `myDefaultValue` if `/super/path` doesn't have data, otherwise it will return the data
var data = await db.getObjectDefault<string>("/super/path", "myDefaultValue");

// Deleting data
await db.delete("/test1");

// Save the data (useful if you disable the saveOnPush)
await db.save();

// In case you have an exterior change to the databse file and want to reload it
// use this method
await db.reload();

TypeScript Support

v0.8.0

As of v0.8.0, TypeScript types are included in this package, so using @types/node-json-db is no longer required.

v1.0.0

JsonDB isn't exported as default any more. You'll need to change how you load the library.

This change is done to follow the right way to import module.

import { JsonDB, Config } from 'node-json-db';

const db = new JsonDB(new Config("myDataBase", true, false, '/'));

Typing

With TypeScript, you have access to a new method: getObject that will take care of typing your return object.

import { JsonDB, Config } from 'node-json-db';

const db = new JsonDB(new Config("myDataBase", true, false, '/'));

interface FooBar {
    Hello: string
    World: number
}
const object = {Hello: "World", World: 5} as FooBar;

await db.push("/test", object);

// Will be typed as FooBar in your IDE
const result = await db.getObject<FooBar>("/test");

Array Support

You can also access the information stored in arrays and manipulate them.

import { JsonDB, Config } from 'node-json-db';

// The first argument is the database filename. If no extension is used, '.json' is assumed and automatically added.
// The second argument is used to tell the DB to save after each push
// If you set the second argument to false, you'll have to call the save() method.
// The third argument is used to ask JsonDB to save the database in a human readable format. (default false)
const db = new JsonDB(new Config("myDataBase", true, false, '/'));

// This will create an array 'myarray' with the object '{obj:'test'}' at index 0
await db.push("/arraytest/myarray[0]", {
    obj:'test'
}, true);

// You can retrieve a property of an object included in an array
// testString = 'test';
var testString = await db.getData("/arraytest/myarray[0]/obj");

// Doing this will delete the object stored at the index 0 of the array.
// Keep in mind this won't delete the array even if it's empty.
await db.delete("/arraytest/myarray[0]");

Appending in Array

// You can also easily append a new item to an existing array
// This sets the next index with {obj: 'test'}
await db.push("/arraytest/myarray[]", {
    obj:'test'
}, true);


// The append feature can be used in conjuction with properties
// This will set the next index as an object {myTest: 'test'}
await db.push("/arraytest/myarray[]/myTest", 'test', true);

Last Item in Array

// Add basic array
await db.push("/arraytest/lastItemArray", [1, 2, 3], true);

// You can easily get the last item of the array with the index -1
// This will return 3
await db.getData("/arraytest/lastItemArray[-1]");


// You can delete the last item of an array with -1
// This will remove the integer "3" from the array
await db.delete("/arraytest/lastItemArray[-1]");

// This will return 2 since 3 just got removed
await db.getData("/arraytest/lastItemArray[-1]");

Count for Array

//
await db.push("/arraytest/list", [{id: 65464646155, name: "test"}], true);

// You can request for the total number of elements, in this case, 1
let numberOfElements = await db.count("/arraytest/list");

Get Index in Array

// You can have the current index of an object
await db.push("/arraytest/myarray", [{id: 65464646155, name: "test"}], true);
await db.getIndex("/arraytest/myarray", 65464646155);
// The default property is 'id'
// You can add another property instead
await db.getIndex("/arraytest/myarray", "test", "name");

// It's useful if you want to delete an object
await db.delete("/arraytest/myarray[" + await db.getIndex("/arraytest/myarray", 65464646155) + "]");

Nesting in Array

// You can easily access any nested array and their object
// You can also append another array to a nested array
await db.push("/arraytest/myarray",
[
  [
    {
      obj: 'test'
    },
    {
      obj: 'hello'
    }
  ],
  [
    {
      obj: 'world'
    }
  ]
]
, true);

// This will return the first object (obj: 'test')

await db.getData("/arraytest/myarray[0][0]");

Getting Item path in Array by id or Another key

await db.push("/myarray",
[
    {
      id: '1',
      obj: 'test'
    },
    {
      id: '2',
      obj: 'hello'
    },
    {
      id: '3',
      obj: 'hello',
      children:[
        {
          id: '1',
          desc: 'a sub item'
        }
      ]
    },
]
, true);

// You can easily get the path of any nested array and its child object by a property using the route style syntax, the default is the object's "id" property

const itemPath = db.fromPath("/myarray/3/children/1");

Exception/Error

Type

Type Explanation
DataError When the error is linked to the Data Given
DatabaseError Linked to a problem with the loading or saving of the Database.

Errors

Error Type Explanation
The Data Path can't be empty DataError The Database expects to receive at least the root separator as part of the DataPath.
Can't find dataPath: /XXX. Stopped at YYY DataError When the full hierarchy of the provided DataPath is not present in the Database, it indicates the extent of its validity. This error can occur when using the getData and delete operations.
Can't merge another type of data with an Array DataError This occurs if you chose not to override the data when pushing (merging) and the new data is an array, but the current data isn't an array (an Object for example).
Can't merge an Array with an Object DataError Similar to the previous message, you have an array as the current data and request to merge it with an object.
DataPath: /XXX. YYY is not an array. DataError When trying to access an object as an array.
DataPath: /XXX. Can't find index INDEX in array YYY DataError When trying to access a non-existent index in the array.
Only numerical values accepted for array index DataError An array can only use number for its indexes. For this use the normal object.
The entry at the path (/XXX) needs to be either an Object or an Array DataError When using the find method, the rootPath needs to point to an object or an array to search within it for the desired value.
Can't Load Database: XXXX DatabaseError JsonDB can't load the database for "err" reason. You can find the nested error in error.inner
Can't save the database: XXX DatabaseError JsonDB can't save the database for "err" reason. You can find the nested error in error.inner
DataBase not loaded. Can't write DatabaseError Since the database hasn't been loaded correctly, the module won't let you save the data to avoid erasing your database.

Limitations

Object with separator in key

Object pushed with key containing the separator character won't be reachable. See #75.

Please consider the separator as a reserved character by node-json-await db.

Contributors

Jamie Davis
Jamie Davis

πŸ›‘οΈ
sidblommerswork
sidblommerswork

πŸ’» ⚠️
Max Huber
Max Huber

πŸ’»
Adam
Adam

πŸ’» πŸ“–
Divine Anum
Divine Anum

πŸ“–
MichaΕ‚ Fedyna
MichaΕ‚ Fedyna

πŸ“–
alex.query
alex.query

πŸ’»
Tony Trupe
Tony Trupe

⚠️

Special Thanks

James Davis for helping to fix a regular expression vulnerable to catastrophic backtracking.

License

![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FBelphemur%2Fnode-json-await db.svg?type=large)

node-json-db's People

Contributors

adamhl8 avatar alexromberg avatar alexsad avatar allcontributors[bot] avatar alphamarket avatar andrewshulgin avatar antonytrupe avatar belphemur avatar chr-onicles avatar ckrooss avatar dependabot[bot] avatar dpeukert avatar drarig29 avatar drproktor avatar etzbetz avatar fossabot avatar github-actions[bot] avatar greenkeeper[bot] avatar kodiakhq[bot] avatar lynchmaniac avatar maxime-agusti avatar michalfedyna avatar mingderwang avatar mterrel avatar semantic-release-bot avatar sidblommerswork avatar sradforth avatar stellaremnant avatar totominc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-json-db's Issues

Append to an array?

What if I want to push to an Array? Hypothetical code below!

db.push("/arraytest/myarray[0]", "0000", true);
db.push("/arraytest/myarray[]", "0001", true);

It will be much easier to simply push to an array than keep track of the array index for every array push!!

I believe this feature would be very easy to implement with minimal code change! Change the regex to accept empty index and simply push to that array when index is empty.

Can you please implement this syntax and feature!

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.9 to 24.0.10.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/@types/jest-24.0.10 at 93.651% (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Transaction protection - question

I am entering final phases of implementing an Auction system using superb node-json-db as the primary database keeping track of guest bidding on items during auction. The Auction repo will be made public in about month from now once stable for forking.

The Problem:
There are two collections that require atomic update; one collection tracks bid requests, the other collection is the items being bid on. (ie: bid info stored on 'bids' collection is followed immediately by an update of the current price in the 'items' collection.) During tests under load I have discovered inconsistencies between the 'bids' collection and 'items' collection - which is not that surprising given the node event loop. (I could get into some 'Yak shaving' and play around with the ticks - but, as a last resort - I gotta get this project done).

The Question:
Has anyone already implemented atomic update in a fork of node-json-db? (awesome!)
If not :( I will implement - was thinking to use rwLock. But any suggestions or get arounds would be greatly appreciated.

Of course, if I do end up doing it and there is an interested in having transaction protection in node-json-db, I'll submit a pull request when done .

Thanks
@PotOfCoffee2Go

Use `fs.createWriteStream`to avoid data loss

Currently, we use node-json-db which called more than once can cause some issues, for this repo this could be problem if we have more than one instance, so if we implement fs.createWriteStram we could avoid such problems.

Slightly curious with this

Okay, so I'm using a project that included this as a basic json library. What i'm trying to do is create a part of the database with data for a gamemode. The data will include HP, level, exp, weapon and the username. How would I do this? I know it's a nooby question but i'm coming from Python where I used json.dumps and json.loads for this. Thanks if anyone can help me :P

Readme incorrect on NPM

Readme states that the Config is located at node-json-db/lib/JsonDBConfig
its actually at node-json-db/dist/lib/JsonDBConfig. Love this package btw, Thanks!

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 11.10.4 to 11.10.5.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

saveOnPush not valid for .delete()

Hi,
the .delete() function is not affected by saveOnPush option, is it intentional?
Can you avoid that the user has to .save() every time that a .delete() is called?
Thank you.

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 11.10.5 to 11.10.6.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/@types/node-11.10.6 at 93.651% (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Appending to array using [] isn't working

Doing this:
jdb.push('/path[]', {foo: bar}, true/false)

results in this:

{
    "path[]": {
      "foo":"bar"
   }
}

instead of what it used to do which was:

{
   "path": [
      { "foo":"bar" }
   ]
}

Can't Load the database

I am getting this error -

Uncaught Exception:
NestedError: Can't save the database
    at DatabaseError.Error (native)
    at DatabaseError.NestedError (/Users/akka/dev/zulipwork/zulip/dist/Zulip-darwin-x64/Zulip.app/Contents/Resources/app.asar/node_modules/node-json-db/lib/Errors.js:7:25)
    at new DatabaseError (/Users/akka/dev/zulipwork/zulip/dist/Zulip-darwin-x64/Zulip.app/Contents/Resources/app.asar/node_modules/node-json-db/lib/Errors.js:29:33)
    at JsonDB.save (/Users/akka/dev/zulipwork/zulip/dist/Zulip-darwin-x64/Zulip.app/Contents/Resources/app.asar/node_modules/node-json-db/JsonDB.js:243:25)
    at new JsonDB (/Users/akka/dev/zulipwork/zulip/dist/Zulip-darwin-x64/Zulip.app/Contents/Resources/app.asar/node_modules/node-json-db/JsonDB.js:33:18)
    at Object.<anonymous> (/Users/akka/dev/zulipwork/zulip/dist/Zulip-darwin-x64/Zulip.app/Contents/Resources/app.asar/app/main/main.js:16:12)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)

I am using it here. It works perfectly when app is not packed.

Usage section needs update

The API was broken with last update:
var JsonDB = require('node-json-db');
now
var JsonDB = require('node-json-db').JsonDB;

Add info on using with TypeScript

We've been using node-json-db in one of our TypeScript projects for a while, so I was excited to see that this project itself got updated to TypeScript! But I initially had trouble upgrading to 0.9. I eventually figured out how to make it work, but thought maybe others could benefit from having some TypeScript-specific instructions in the README.

The two bits of info that I'd suggest making sure are present in the README are:

  1. To use node-json-db, in your own TypeScript project, you MUST make sure that the you have the esModuleInterop compiler option set to true in your project's tsconfig.json. Because of the way the .js and .d.ts files in node-json-db are currently generated, node-json-db cannot be used in a project without esModuleInterop=true.

  2. Then, to use JsonDB from a TypeScript file, you import like this:

import JsonDB from "node-json-db";

Just that easy. And then the rest of the instructions should work great!

If you'd like me to make a pull request for the README changes, I'm happy to. Just let me know.

Thanks!

merge data error when the data value is null

`var JsonDB = require('node-json-db');
var db = new JsonDB("test.json", true, true);

db.push("/data", {a:null}, false);
db.push("/data", {a:"aaaa"}, false);`

then I got the json data
{ "data": { "a": { "0": "a", "1": "a", "2": "a", "3": "a" } } }

Auto increment

Hey there,

I love this and been using it for a while with no problems. One thing I would love to have though is a uniq ID identifying each DB entry. I've been coding this myself but it aint the same ;)

What do you think?

Exists & Find

Hey @Belphemur,

I had trouble querying my json files while working with node-json-db on a project recently. I needed a way to use data from my json db conditionally. I figured out a workaround and finished the project. Now I have a little time to tinker and I was wondering, could I contribute to your repo? I'd like to suggest a 'find' function.
Right now, getData can't be used for conditionals because it returns an error if the requested entry doesn't exist. Instead, a function like 'find' could return null. 'Find' could take an optional argument for a test that would return from the path specified by the first argument. I imagine 'find' could work for objects or arrays without too much trouble. Doing something like this then becomes easy and clean:

if(books.find('/shakespeare')){
    var shakespearean_comedies = books.find('/shakespeare', function(book){
        book.genre === 'Comedy'
    }
}

What do you think?

FS no longer supported

I am, using Angular 6 and I am running into the following error on build:

ERROR in my_dir/node_modules/mkdirp/index.js
Module not found: Error: Can't resolve 'fs' in 'my_dir/node_modules/mkdirp'
ERROR in my_dir/node_modules/node-json-db/dist/JsonDB.js
Module not found: Error: Can't resolve 'fs' in 'my_dir/node_modules/node-json-db/dist'

The second library that you are importing is fs.

import * as FS from 'fs'

This library is now not really a library, I guess. It's got an empty page on NPM that says it used to be a codebase, but isn't anymore. Have you experienced this yet, and is there a workaround/fix for this issue? I would love to use this JSON DB lib, because it looks like exactly what I would like, but this is a roadblock for me to use this.

Nested arrays are not recognized

Another issue is:

db.push('/comments/S244/165115/vote[0][]', data) => Only numerical values are accepted for array index

It doesn't matter whenever I put true or false as the third argument

"Deleting" whole "database" crashes

I have always used the following code to "delete" a "database", e.g. set it back to {}:

const jsondb = require('node-json-db');
const db = new jsondb('./mydb.json');

db.push("/id", "1");
db.delete("/");

Since 0.7.4, this fails with the following trace:

/home/ckrooss/sample/node_modules/node-json-db/lib/utils.js:19
        var match = arrayIndexRegex.exec(property.trim());
                                                  ^
TypeError: Cannot read property 'trim' of undefined
    at Object.processArray (/home/ckrooss/sample/node_modules/node-json-db/lib/utils.js:19:51)

Which I could easily fix by adding something like if (!property) return null; to processArray.
Would this be an acceptable solution worth a PR or is my use case just plain wrong?
I can always use db.push("/", {});, of course.

Crashing when it can't find datapath.

When I run my code then it just crashes. I have it in a try loop which is why I don't understand why it
crashes.
Code:

var bugIDcount = 1;
var continueWorking = true;
while (continueWorking) {
  console.log("In the loop again?");
  try {
    var data = bugs.getData(`/${bugIDcount}`);
  } catch (error) {
    console.error(error);
    continueWorking = false;
  }
  bugIDcount++;
}

Console Log:

  stack: 'NestedError: Can\'t find dataPath: /2. Stopped at 2
      at DataError.NestedError (/Users/Ben/git/crashlanding-discord/node_modules/node-json-db/lib/Errors.js:7:25)
          at new DataError (/Users/Ben/git/crashlanding-discord/node_modules/node-json-db/lib/Errors.js:37:33)
              at findData (/Users/Ben/git/crashlanding-discord/node_modules/node-json-db/JsonDB.js:100:27)
                  at recursiveProcessDataPath (/Users/Ben/git/crashlanding-discord/node_modules/node-json-db/JsonDB.js:127:17)
                      at JsonDB._getData (/Users/Ben/git/crashlanding-discord/node_modules/node-json-db/JsonDB.js:140:16)
                          at JsonDB.getData (/Users/Ben/git/crashlanding-discord/node_modules/node-json-db/JsonDB.js:70:21)
                              at Object.<anonymous> (/Users/Ben/git/crashlanding-discord/bot.js:27:21)
                                  at Module._compile (module.js:660:30)\n    at Object.Module._extensions..js (module.js:671:10)
                                      at Module.load (module.js:573:32)',
  message: 'Can\'t find dataPath: /2. Stopped at 2',
  inner: undefined,
  id: 5 }

Add "types" field to package.json

When importing node-json-db into my TypeScript project, TypeScript complains that it cannot find types for node-json-db. The issue appears to be that the types file JsonDB.d.ts is not co-located with JsonDB.js, so the TypeScript compiler does not know where to look for it.

The simplest fix is to add a "types" field to package.json that gives the location of the file:

{
  "name": "node-json-db",
  "version": "0.9.1",
  "description": "Database using JSON file as storage for Node.JS",
  "main": "dist/JsonDB.js",
  "types": "dist/types/JsonDB.d.ts",    <<< types field tells the compiler where to look for types
...
}

Array with non-numerical index

Hi,
I'm using the version 0.6.5 from npm, and I think that there is an issue with this command:
db.push("/arraytest/myarray[test]", {obj:'test'}, true);

it creates this:

{
    "arraytest": {
        "myarray": []
    }
}

without the index 'test'.

Delete method doesn't delete position.

db.delete(`./tasks[${taskDBIndex}]`);

If i try to delete a position, the position is not deleted, it just set it as null.
so i have an array of nulls.

    "tasks": [
        null,
        null,
        null,
    ]

Support for Nativescript

In {N}ativescript , I can use node/ npm packages alright,but node json db throws an error "stat.isDirectory is not a function" and in my logcat it shows an error concerning path something /. And heres how I initialize
db=new jsonDb('myDb',true,false);

Can't get or push any data with "/" in the key.

Example:

Code

let data = {
    "norm_key": "data",
    "bad/key":"other data"
}

db.push("normal_key", data);
db.push("bad/key", data);

DB

{
    "normal_key": {
        "norm_key": "data",
        "bad/key":"other data"
    },
    "bad": {
        "key": {
            "norm_key": "data",
            "bad/key":"other data"
        }
    }
}

So you can create a key that has a / in it, but you can push data that does. This makes the "other data" unreachable, since by entering the key /bad/key/bad/key you'll search for

{
    "bad": {
        "key": {
            "bad": {
                "key":"other data"
            }
        }
    }
}

That doesn't exist

In DEV env 'node-json-db' is not loading

In the app we are using React, ElectronJS and webpack 2. when we use 'node-json-db' in electronConfig file then in DEV env it is giving "Cannot find module 'node-json-db'" error, But in PROD env it is working fine.

don't know what is wrong in DEV env.

"DatabaseError: Can't Load Database" when push() immediately follows a database creation for non-existing file

Timing issue?
Env:
Node: v4.1.1, Windows 7
node-json-db: 0.4.6

To reproduce: run

var JsonDB = require('node-json-db');
var db = new JsonDB("myDataBase", true, false);
db.push("/test1","super test");

while the file "myDatabase.json" does not exists.
DatabaseError is thrown.

Work-around is to create myDataBase.json with empty json, then it works.

Would this be a cause?
on initial database save, mkdirp is called which is async

db.get Data inside an array

I have an array which i would like to delete the first index. How do i get to deleting the data?

It seems like db.delete does not accept [0]

Corruption & Async

Hello,

I have rolled my own system similar to this and am having trouble with corruption of the data when the app closes suddenly (or exits)

Just wondering if the Sync calls you make to the file system are sync because that solves that problem, and then by extension, why are they sync calls instead of Async? I would think that might hurt performance or is there a reason?

I know the answer is "Just try it out" but shoehorning your library in now would take a fair while, and it might not solve anything so I figure its worth a shot asking!

Kind regards,

Ryan

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 10.12.8 to 10.12.9.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).
  • βœ… License Compliance: All checks passed. (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 10.12.14 to 10.12.15.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • βœ… License Compliance: All checks passed. (Details).
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/@types/node-10.12.15 at 95.851% (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Performance? Scalability?

Hello,

I'm curious about the performance.

I think that having a database that writes and reads to plain JSON files is a great thing. I don't want to have to install mongdb/couchdb or whatever, with their proprietary binary formats.
JSON is an open and standardized format. I want my data to be decoupled from a particular database platform.

I had a look at the code, and by the preliminary looks of it, it does not seem that much performance or scalability optimization was done, which is my concern here.

Since I am not sure what is happening in the code, I think I'd best ask you about the topic - whether there is some indexing, partial loading or such optimization happening, or is the whole data just loaded in memory? If so, do you have plans to change that in the future?

Thanks!

Accessing DB through file tree

As far as I can tell there's no way to access a db through the working directory. So, for example, if you have a nested file you want to access a db (higher in the file tree) from, you're gonna error out:
const db = new jsonDB("../database.json", true, true);
Is there a work around, or do I need to tinker?

Add localestorage support

To be able to use it in browser easily and rely on the storage mechanism there.

  • Create a storage "interface"
  • Implement driver for hard storage (current one)
  • Implement storage for LocalStorage

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 11.11.1 to 11.11.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).
  • βœ… coverage/coveralls: First build on greenkeeper/@types/node-11.11.2 at 93.651% (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Export DataError and DatabaseError

When handling errors thrown by the lib, it'd be great if we could compare directly against the DataError and DatabaseError 'classes':

let JsonDB = require('node-json-db'),
    DataError = JsonDB.DataError,
    DatabaseError = JsonDB.DatabaseError;

// ...

try {
    let example = db.getData('/path/that/does/not/exist');
} catch (err) {
    if (err instanceof DataError) {
        // ...
    } else if (err instanceof DatabaseError) {
        // ...
    } else {
        // ...
    }
}

This can easily be done by add the Error classes as static members of the exported JsonDB class:

// line 250 of JsonDB.js
module.exports = JsonDB;
module.exports.DataError = DataError;
module.exports.DatabaseError = DatabaseError;

Appending to array doesn't seem to work for me

Hi, thanks for such a great library :)

I have tried
bdd.push('/comments/244[]', data, true)

But it create that entry in db : "244[]":{"nom":"anon","content":"blah"}}...

I have tried with a non-numeric index and it works just fine...
bdd.push('/comments/aa[]', data, true) => OK

Sorry, havent made a pull request cause I don't understand exactly how you process the []

Thanks ;)

How to Fetch a Key Value from Array

My Json Array:
{"paths":[{"id":74185, "name": "John"}, {"id":54874, "name": "Clark"}]}

How can i fetch and puch where id is equal to 74185.
I am trying like this:
var data = db.getData("/paths/id/74185");

But not working. Kindly guide me

db.delete('/test'); doesn't seem to be working

I am trying to delete an item from the database by doing the following:

deleteRoleOnDeletion: (guild, role) => {
    subbableRolesDB.delete(`/${role.name}`);
    // subbableRolesDB.save();
    log.info(`Deleted ${role.name} from db, id: ${role.id}`);
}`
`role.name` is the name of the item in the database, and it's actually right, but it doesn't seem to delete it.
No errors nothing.
Anyone have a clue what's going on?

Database has cache?

Hey there πŸ‘‹ , I'm using this wonderful module for music system and I realized when I delete an object which contains an array called queue and then I will log the data from the queue ('Check if it's not there and catch errors) I saw that it logs the array perfectly and it does not inside the JSON any more.

Minified JSON

Is there a switch/parameter to save the JSON file in "minified" form?
So, instead of this:

{
   "test":"test",
   "json":{
      "test":[
         "test"
      ],
      "important":5
   },
   "new":"cool"
}

We get this:

{"test":"test","json":{"test":["test"],"important":5},"new":"cool"}

If not, I would like to suggest you to add this feature.

Docs, readme.md, 2 issues

README.md, issues in
first example
1)
db.push("/test2/my/test/",5);
generates an intermittent bug, it might save the value 5 as an unnammed anonymous entry, resulting in:

"test2": {
        "my": {
            "test": {
                "": 5
            }
        }

OR it might crash, with this error:

this.data[this.parent] = toSet;
                                   ^
TypeError: Cannot assign to read only property '' of 5
    at DBParentData.setData

One possible fix is to remove the trailing slash

db.push("/test2/my/test",5);

[Which may or may not be what you intend to give as an example ?]
Results in

   "test2": {
        "my": {
            "test": 5
        }

Then c.a. line 24 should read:

At this point, the results are:
{
    "test1": "super test",
    "test2": {
        "my": {
            "test": 5
        }
    },
    "test3": {
        "test": "test",
        "json": {
            "testy": [
                "testy"
            ],
            "important": 5
        },
        "new": "cool"
    }
}

I would suggest testing live code and excerpting the results directly into the docs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.