npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webgap/jsonfs

v0.1.7

Published

FileSystem JSON Database API

Downloads

10

Readme

json_file_system

Fork for using with WebGAP.

json_file_system (JSON FileDB like MongoDB)

Nodejs JSON Database like MongoDB, but only using the FileSystem (File IO).

Inspirations and Credits

##Contents

Getting Started

clone the source code locally :

$ git clone https://github.com/Jalalhejazi/jsonfs.git

Install the module globally :

$ npm install json_file_system -g
// to use the globally installed use: json_file_system
// to use the locally from git use: lib/jsonfs.js

var db = require('json_file_system');

db = db.connect('/path/to/db-folder', ['collection-name']);

// you can access the traditional JSON DB methods here
db.connect('','');

Documentation

Connect to DB

db.connect(pathToFolder, ['filename']);

Filename will be the name of the JSON file. You can omit the extension, jsonfs will take care of it for you.

var db = require('json_file_system');
db = db.connect('/demos/db', ['articles']);
// or simply
db.connect('/demos/db', ['articles']);

This will check for a directory at given path, if it does not exits, jsonfs will throw an error and exit.

If the directory exists but the file/collection does not exist, jsonfs will create it for you.

Load Collections

Alternatively you can also load collections like

var db = require('jsonfs');
// this
db = db.connect('/demos/db');
db.loadCollections(['articles']);
//or
db.connect('/demos/db');
db.loadCollections(['articles']);
//or
db.connect('/demos/db')
  .loadCollections(['articles']);
//or
db.connect('/demos/db', ['articles']);

Load Multiple Collections

var db = require('jsonfs');
db.connect('/demos/db', ['articles','comments','users']);

Write/Save to Collection

db.collectioName.save(object);

Once you have loaded a collection, you can access the collection's methods using the dot notation like

db.[collectionName].[methodname]

To save the data, you can use

var db = require('jsonfs');
db.connect('db', ['articles']);
var article = {
    title : "jsonfs works",
    published : "today",
    rating : "5 stars"
}
db.articles.save(article);
// or
db.articles.save([article]);

The saved data will be

[
    {
        "title": "jsonfs works",
        "published": "today",
        "rating": "5 stars",
        "_id": "0f6047c6c69149f0be0c8f5943be91be"
    }
]

You can also save multiple objects at once like

var db = require('jsonfs');
db.connect('db', ['articles']);
var article1 = {
    title : 'jsonfs works',
    published : 'today',
    rating : '5 stars'
}

var article2 = {
    title : 'jsonfs works',
    published : 'yesterday',
    rating : '5 stars'
}

var article3 = {
    title : 'jsonfs works',
    published : 'today',
    rating : '4 stars'
}
db.articles.save([article1, article2, article3]);

And this will return the inserted objects

[ { title: 'jsonfs works',
    published: 'today',
    rating: '4 stars',
    _id: 'b1cdbb3525b84e8c822fc78896d0ca7b' },
  { title: 'jsonfs works',
    published: 'yesterday',
    rating: '5 stars',
    _id: '42997c62e1714e9f9d88bf3b87901f3b' },
  { title: 'jsonfs works',
    published: 'today',
    rating: '5 stars',
    _id: '4ca1c1597ddc4020bc41b4418e7a568e' } ]

Read from Collection

There are 2 methods available for reading the JSON collection

  • db.collectioName.find(query)
  • db.collectioName.findOne(query)

db.collectioName.find()

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.find();

This will return all the records

[{
    title: 'jsonfs works',
    published: 'today',
    rating: '5 stars',
    _id: '0f6047c6c69149f0be0c8f5943be91be'
}]

You can also query with a criteria like

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.find({rating : "5 stars"});

This will return all the articles which have a rating of 5.

db.collectioName.findOne(query)

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.findOne();

If you do not pass a query, jsonfs will return the first article in the collection. If you pass a query, it will return first article in the filtered data

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.findOne({_id: '0f6047c6c69149f0be0c8f5943be91be'});

Update Collection

db.collectioName.update(query, data, options);

You can also update one or many objects in the collection

options = {
    multi: false, // update multiple - default false
    upsert: false // if object is not found, add it (update-insert) - default false
}

Usage

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);

var query = {
  title : 'jsonfs works'
};

var dataToBeUpdate = {
  title : 'jsonfs works again!',
};

var options = {
   multi: false,
   upsert: false
};

var updated = db.articles.update(query, dataToBeUpdate, options);
console.log(updated); // { updated: 1, inserted: 0 }

Remove Collection

db.collectioName.remove(query, multi);

You can remove the entire collection (including the file) or you can remove the matched objects by passing in a query. When you pass a query, you can either delete all the matched objects or only the first one by passing multi as false. The default value of multi is true.

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.remove({rating : "5 stars"});
var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.remove({rating : "5 stars"}, true); // remove all matched. Default - multi = true
var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.remove({rating : "5 stars"}, false); // remove only the first match

Using remove without any params will delete the file and will remove the db instance.

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.remove();

After the above operation db.articles is undefined.

Count

db.collectioName.count();

Will return the count of objects in the Collection

var db = require('jsonfs');
db.connect('/demos/db', ['articles']);
db.articles.count(); // will give the count

demos

Refer to the demos folder.

Performance

To validate jsonfs's performance and to check if it meets your needs, you can clone this repo and run

$ node performance/time.js

An average of few tests can be found below

Time taken to process x number of objects (in ms) vs Action Performed

Contributing

See the CONTRIBUTING Guidelines

Release History

  • 0.0.1
    • Base Module with
      • Connect to a Folder
      • Access a Collection/File
      • Create Read Update Delete on JSON object

License

Copyright (c) 2014 Jalal Hejazi. Licensed under the MIT license.