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

local-json-db

v0.0.6

Published

Local JSON database with overlays to be used as fixtures

Downloads

16

Readme

local-json-db Build Status Coverage Status NPM version

A local JSON database with overlays


Local JSON DB is exclusively designed for local environments and testing purpose. Globally, it's a memory database using overlays and that can persist to JSON files.

Let's say you want a local environment for your HTML5 application (could be any other type of application), but without the pain and overload of a VM or any other heavy install for a local API. You could have for example an extremely simple expresjs app delivering content hard-coded in that fake API, or source from some JSON.

But then you need your developers to be able to save locally some different records than the ones shared by the fake API, while still using the constantly updated core records pushed to the VCS. You also want to be able to have some other specific data to run unit tests of your client for example.

Well, local-json-db is here for that!

Usage

First of all, install it as a dependency of your project. In the root path of your project, run this:

npm install --save-dev local-json-db

Then, in your CoffeeScript file:

ljdb = require 'local-json-db'
db = new ljdb.Database('./data')
db.addOverlay 'local'

db.createRecord 'user', name: 'Huafu'
db.save()

or in javascript:

var ljdb = require('local-json-db');
var db = new ljdb.Database('./data');
db.addOverlay('local');

db.createRecord('user', {name: 'Huafu'});
db.save();

Full API documentation of Database is here.

Example

Here is the directory structure of your app:

app/
  # all the client files, in our case HTML5 application for example
mock-api/
  lib/
    # the simple API using `local-json-db` to provide read-only but also rw mocks
  data/
    # the core json files containing the records shared in all overlays
    users.json
    posts.json
    local/
      # ignored by the VCS, containing the overlays for local use only,
      # as for each developer for example

Assuming PROJECT_ROOT is a reference to the root directory of our project, when running the micro fake API delivering mocks:

var db = new Database(PROJECT_ROOT + 'mock-api/data');
db.addOverlay('local'); // relative to the path given in new Database()

You'll then have a database reading records from mock-api/data JSON files and merging them with the ones in mock-api/data/local. When saving the DB (with db.save()), only the difference will be saved in mock-api/data/local JSON files. Nothing will be changed in the JSON files located in mock-api/data, not even the deleted records.

Now you want to update the core files to share with others the addition of records or such. It's as easy as commenting the second line in the script above. Without overlay, the changes will be saved directly in the root directory given as parameter of new Database()

Also you might want to keep the records volatile and not saving the database after a run. Well, again as simple as commenting out the db.save() you'd usually place in the shutdown of your app.


You might use this for unit testing as well, having multiple set of data but with a core data being constant, without duplicating it over and over in all unit tests.

You can add as many overlay as you want, just keep in mind tha db.save() will save the diff (ie the created, changed or deleted) records in the last overlay you added before using the database object.

Database API:

Any modelName will be dasherized and pluralized to get the file name used as a record store for that model. Also whatever you are going to use in the following methods as model name will always meet the same base model name (for example userPost, userPosts, user_post, ... will all point to the same model, and so the same store in each overlay)

An important thing to note is that updating a record MUST be done with db.updateRecord(). For faster processing and to avoid unwanted changes, the returned record by any method of the API is a copy of the original one, with id property being read-only.

The API documentation can be found here.

More examples and features on their way ;-)