Lightweight JSON database for Node, Hybrid, and Browser.
Powered by Immutable and Superstruct.
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);
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>
Returns an Immutable fromJS with additional properties and functions described below.
Writes database to file / local storage, and returns an Immutable of database state.
db.set('posts', [])
.write();
// Map { posts: List [ ... ] };
Get current database state from file / local storage.
db.getState();
// Map { posts: List [ ... ] };
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.
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 [ ... ] }
MIT License © 2017-Present eriestrisnadi. All rights reserved.
This is a free and open source. Use it at your own risk.