jaysn
Lightweight JSON database for Node, Hybrid, and browser.
Last updated 2 years ago by lowsprofile .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install jaysn 
SYNC missed versions from official npm registry.

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/immutable@4.0.0-rc.9/dist/immutable.min.js"></script>
<script src="https://unpkg.com/superstruct@0.4.5/umd/superstruct.min.js"></script>
<script src="https://unpkg.com/jaysn@0.1.1/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 lowsprofile. All rights reserved.

Legal

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

Current Tags

  • 0.1.4                                ...           latest (2 years ago)

5 Versions

  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 6
Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |