Git Product home page Git Product logo

json-file-store's Introduction

JSON file store

A simple JSON file store for node.js.

Build Status Dependency Status NPM version License

WARNING: Don't use it if you want to persist a large amount of objects. Use a real DB instead.

Install

npm install jfs --save

Usage

var Store = require("jfs");
var db = new Store("data");

var d = {
  foo: "bar"
};

// save with custom ID
db.save("anId", d, function(err){
  // now the data is stored in the file data/anId.json
});

// save with generated ID
db.save(d, function(err, id){
  // id is a unique ID
});

// save synchronously
var id = db.saveSync("anId", d);

db.get("anId", function(err, obj){
  // obj = { foo: "bar" }
})

// pretty print file content
var prettyDB = new Store("data",{pretty:true});
var id = prettyDB.saveSync({foo:{bar:"baz"}});
// now the file content is formated in this way:
{
  "foo": {
    "bar": "baz"
  }
}
// instead of this:
{"foo":{"bar":"baz"}}

// get synchronously
var obj = db.getSync("anId");

// get all available objects
db.all(function(err, objs){
  // objs is a map: ID => OBJECT
});

// get all synchronously
var objs = db.allSync()

// delete by ID
db.delete("myId", function(err){
  // the file data/myId.json was removed
});

// delete synchronously
db.delete("myId");

Single file DB

If you want to store all objects in a single file, set the type option to single:

var db = new Store("data",{type:'single'});

or point to a JSON file:

var db = new Store("./path/to/data.json");

In memory DB

If you don't want to persist your data, you can set type to memory:

var db = new Store("data",{type:'memory'});

ID storage

By default the ID is not stored within your object. If you like, you can change that behavior by setting saveId to true or a custom ID

var db = new Store("data",{saveId:'myKey'});

custom ID generator

We use uuid v4 for ID generation if you don't pass an id when save a data. If you want, you can pass custom generator.

var i = 0;
var db = new Store("data",{
  idGenerator: function() {
    i = i + 1;
    return i;
  }
});

Tests

npm test

License

This project is licensed under the MIT License.

json-file-store's People

Contributors

chentsulin avatar flosse avatar jonghunyu avatar marcbachmann avatar mithgol 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

json-file-store's Issues

both save and delete will not check existence of the record

so i need to wrap it with get() first. It'll be great if the library can take care for me, so that i can do

    db.delete(id, function(err) {
        console.log(err);
        if (err) {
            next(err);
        } else {
            res.json({status: "success"});
        }
    });

instead of

    db.get(id, function(err, item) {
        if (err) {
            next(err);
        } else {
            db.save(id, item, function(err) {
                if (err) {
                    next(err);
                } else {
                    item = db.getSync(id);
                    res.json(_attach_id(item, id));
                }
            });
        }
    });

Key / File Management

Hey, I find this idea of this kind of storage very interesting. But one thing is missing I think: a way to manage the files that got stored - meaning to read what files exist (maybe based on a regex pattern selectable).

Just a thought :)

Windows no getuid

C:\Users\irjudson\AppData\Roaming\npm\node_modules\nitrogen-cli\node_modules\jfs\lib\Store.js:46
owner = process.getuid() === stat.uid;
^
TypeError: Object # has no method 'getuid'
at canWrite (C:\Users\irjudson\AppData\Roaming\npm\node_modules\nitrogen-cli\node_modules\jfs\lib\Store.js:46:21)
at C:\Users\irjudson\AppData\Roaming\npm\node_modules\nitrogen-cli\node_modules\jfs\lib\Store.js:60:13
at Object.oncomplete (fs.js:107:15)

Cannot load an item from store if it is a string

I was using the jfs v0.2.6 from npm and everything was going well until I updated to v0.3.0

Now I can no longer do the following:

const Store = require('jfs');
const store = new Store('config.json', { type: 'single', pretty: true });
store.save('stringItem', 'asdf');
store.get('stringItem', console.log); // Error: could not load data

It appears that between the two versions items in the store have started to be checked for whether they are objects or not, where they used to be checked for null only.

id not stored with the item ?

thanks for this wonderful module, I have a quick question, I noticed that when I add a new record, the id is not stored inside the record like what I expected, ex.

{
"b5468c93-7799-43ce-97a5-9d8c9bd5547a": {
"title": "ABC"
},
// expected
"a34c41fb-124d-40c0-8383-858d344c48a9": {
"id": "a34c41fb-124d-40c0-8383-858d344c48a9",
"title": "ABC"
}
}

but i guess i can handle that after i call getSync(id) :)

Race condition.

Hi, I think there is a race condition in your project: Since I want to store all objects in a single file, set the type option to single. Then, I have two non-blocking requests r1 and r2. r1 saves object o1 with id 1 and r2 deletes the object by id 1. json-file-store will prepare different this._cache to asynchronously write into file. Since two file writing are asynchronously executed, the content of the file is non-deterministic. In other words, it is non-deterministic whether is object with id 1 exists in the file. Maybe a better solution is to utilize a queue, which sequentializes multiple requests.

FYI: jfs isn't maintained at the moment

I'm willing to merge PRs, publish new versions and help with some guidance for developers but I can't offer active development.
So if you like to help, please write testcases, create fixes and open pull requests.
Thank you all!

Possible race condition in saveObjectToFile()

saveObjectToFile() writes the object to a temporary file first, and then uses asynchronous fs.rename() to move the temporary file to the target file:

fs.writeFile(tmpFileName, json, 'utf8', (err) => {
if (err) return cb(err);
fs.rename(tmpFileName, file, cb);
});
});

If 2 fs.rename()s are called at almost the same time, there will be a race condition, and the execution result is not determined. One of them will lose its operations.

db.save("id", {number: 1}, function (err)
{
    if (err)
    {
        console.error(err);
    }
});

db.save("id", {number: 2}, function (err)
{
    if (err)
    {
        console.error(err);
    }
});

// {"number":1} or {"number":2} in id.json

This problem affects both save() and delete().

using javascript instead of coffee script

Is it possible to convert source code from coffee script to javascript with ES6?
I am planning to contribute this package because I like its simplicity.
But I haven't used coffee script for few years as ES6 supports lot of new features.
If you are OK, I would be happy to work with this and keep working with other issues.

canWrite denies access for root

Hey!

I came across an issue when using BotKit, which in turns use this package. I was running a script as root (it's not running as root anymore FYI) when I notices that it could not write to the store, which was created by my real user. After some debugging, I traced it to the canWrite function in this package.

The function only checks if the file owner and permissions matches the current user, which doesn't cover the case of root since that user should have access even though it doesn't own the file.

I don't really get CoffeeScript, but I think a more appropriate way to do this is either use fs.access(file, fs.constants.W_OK) or the recommended way of simply trying to write and check for an error at that time.

Best regards,
Erik

get sync always return true

if i call db.getSync(id), seems to me no matter what, I'm getting a record back. However this doesn't happen to get(id, function(err)) which gives the correct behavior.

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.