Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.
/ jaysn Public archive

💾 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 (Archived)

Important

JAYSN HAS MOVED HOME

Jaysn has moved home due to the loss of access to the npm account. This codebase is now effectively archived, and all new development, updates, and bug fixes are being done in Struma. Please refer to the new repository for continued support and updates.

Moving Forward

For all new features, bug fixes, and continued improvements, please visit the new repository:

Thanks for your interest and support in Jaysn, and hope you join us in continuing the journey with Struma!


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