Skip to content

💾 Lightweight JSON database for Node, Hybrid, and Browser. Powered by Immutable and Superstruct.

License

Notifications You must be signed in to change notification settings

eriestrisnadi/jaysn

Repository files navigation

Jaysn

Coverage Status npm travisci donate

Lightweight JSON database for Node, Hybrid, and Browser.
Powered by Immutable and Superstruct.

Getting Started

Node / Hybrid

Install jaysn using node packager: yarn, or npm

# Install using yarn
yarn add jaysn

# Or, using npm
npm install jaysn --save

Then require it into any module

const { jaysn } = require('jaysn');
const options = {use: 'File', source: 'db.json'};
const schema = {
  posts: {
    id: 'number',
    title: 'string',
  },
};
const db = jaysn(schema, options);

Browser

A UMD build is also available on unpkg for testing and quick prototyping:

<script src="https://unpkg.com/[email protected]/dist/immutable.min.js"></script>
<script src="https://unpkg.com/[email protected]/umd/superstruct.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/jaysn.min.js"></script>
<script>
  var jaysn = Jaysn.jaysn;
  var options = {use: 'LocalStorage', source: 'MyDB'};
  var schema = {
    posts: {
      id: 'number',
      title: 'string',
    },
  };
  var db = jaysn(schema, options);
</script>

Live Examples

API

jaysn(schema, options?)

Returns an Immutable fromJS with additional properties and functions described below.

db.write()

Writes database to file / local storage, and returns an Immutable of database state.

db.set('posts', [])
  .write();
// Map { posts: List [ ... ] };

db.getState()

Get current database state from file / local storage.

db.getState();
// Map { posts: List [ ... ] };

Quick Guides

Recommended to learn Immutable API, so after that you can know how to query and manipulate data. Here are a few example to get you started.

Examples

Set posts.

db.set('posts', [])
  .write();
// Map { posts: List [ ... ] }

Get posts.

db.get('posts');
// Map { posts: List [ ... ] }

Adding or updating posts.

const data = { id: 1, title: 'Hello Jaysn!'};
const data2 = Object.assign({}, data);
data2.id = 2;

// You can do something like this
db.set('posts', db.get('posts').push(data))
  .write();
// Map { posts: List [ Map { id: 1, title: 'Hello Jaysn!' } ] }


// But I will prefer this method
db.update('posts', o => o.push(data2))
  .write();
// Map { posts: List [ Map { id: 1, title: 'Hello Jaysn!' }, Map { id: 2, title: 'Hello Jaysn!' } ] }

Find a post with specific data.

db.get('posts')
  .find('posts', o => o.get('id') === 2);
// Map { id: 2, title: 'Hello Jaysn!' }

Delete a post with specific data.

// You can do something like this
const index = db.get('posts').findIndex(o => o.get('id') === 1);
const index2 = db.get('posts').findIndex(o => o.get('id') === 2);
db.update('posts', o => o.delete(index)).write();
  .write();
// Map { posts: List [ Map { id: 2, title: 'Hello Jaysn!' } ] }


// But I will prefer this method
db.deleteIn(['posts', index2]);
  .write();
// Map { posts: List [ ... ] }

License

MIT License © 2017-Present eriestrisnadi. All rights reserved.

Legal

This is a free and open source. Use it at your own risk.

About

💾 Lightweight JSON database for Node, Hybrid, and Browser. Powered by Immutable and Superstruct.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published