@slsplus/db

Serverless JSON database using COS(Cloud Object Storage)


Keywords
serverless, cos, flat, file, local, database, storage, JSON, lodash, localStorage, electron, embed, embeddable
License
MIT
Install
npm install @slsplus/db@0.1.1

Documentation

@slsplus/db

Build Status npm NPM downloads

Serverless JSON database using COS(Cloud Object Storage), forked by lowdb

Usage

npm install @slsplus/db --save
const slsdb = require('@slsplus/db');

const COSAsync = require('@slsplus/db/adapters/COSAsync');
const adapter = new COSAsync('serverless-db.json', {
  region: 'COS region',
  bucket: 'COS bucket name with appid',
  secretId: 'COS SecretId',
  secretKey: 'COS SecretKey',
});
const db = await slsdb(adapter);

// Set some defaults (required if your JSON file is empty)
await db.defaults({ posts: [], user: {}, count: 0 }).write();

// Add a post
await db
  .get('posts')
  .push({ id: 1, title: 'lowdb is awesome' })
  .write();

// Set a user using Lodash shorthand syntax
await db.set('user.name', 'typicode').write();

// Increment count
await db.update('count', (n) => n + 1).write();

// Get all posts
await db.get('posts').value();

Data is saved to serverless-db.json in COS

{
  "posts": [{ "id": 1, "title": "lowdb is awesome" }],
  "user": {
    "name": "typicode"
  },
  "count": 1
}

You can use any of the powerful lodash functions, like _.get and _.find with shorthand syntax.

// For performance, use .value() instead of .write() if you're only reading from db
await db
  .get('posts')
  .find({ id: 1 })
  .value();

It only supports Node and uses lodash API, so it's very simple to learn. Actually, if you know Lodash, you already know how to use lowdb 😉

Important

  • Because @slsplus/db interacts with cloud, so it's suggested to call write() method(save data to cloud), after all database operations.
  • @slsplus/db doesn't support Cluster and may have issues with very large JSON files (~200MB).

Install

npm install @slsplus/db --save

Alternatively, if you're using yarn

yarn add @slsplus/db

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

<script src="https://unpkg.com/lodash/lodash.min.js"></script>
<script src="https://unpkg.com/@slsplus/db/dist/slsdb.min.js"></script>
<script src="https://unpkg.com/@slsplus/db/dist/LocalStorage.min.js"></script>
<script>
  var adapter = new LocalStorage('db')
  var db = low(adapter)
</script>

API

low(adapter)

Returns a lodash chain with additional properties and functions described below.

db.[...].write() and db.[...].value()

write() writes database to state.

On the other hand, value() is just _.prototype.value() and should be used to execute a chain that doesn't change database state.

await db.set('user.name', 'yugasun').write();

Please note that db.[...].write() is syntactic sugar and equivalent to

await db.set('user.name', 'yugasun').value();

await db.write();

db._

Database lodash instance. Use it to add your own utility functions or third-party mixins like underscore-contrib or lodash-id.

db._.mixin({
  second: function(array) {
    return array[1];
  },
});

db.get('posts')
  .second()
  .value();

db.getState()

Returns database state.

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

db.setState(newState)

Replaces database state.

const newState = {};
db.setState(newState);

db.write()

Persists database using adapter.write, it will write data to a JSON file, and upload to COS.

db.write().then(() => console.log('State has been saved'));

db.read()

Reads source using storage.read option, it will download a JSON file from COS, and read data from it.

db.read().then(() => console.log('State has been updated'));

Adapters API

Please note this only applies to adapters bundled with Lowdb. Third-party adapters may have different options.

For convenience, COSAsync accept the following options:

  • defaultValue if file doesn't exist, this value will be used to set the initial state (default: {})
  • serialize/deserialize functions used before writing and after reading (default: JSON.stringify and JSON.parse)
const adapter = new COSAsync('array.yaml', {
  region: 'COS region',
  bucket: 'COS bucket name with appid',
  secretId: 'COS SecretId',
  secretKey: 'COS SecretKey',
  defaultValue: [],
  serialize: (array) => toYamlString(array),
  deserialize: (string) => fromYamlString(string),
});

Guide

Examples

Check if posts exists.

db.has('posts').value();

Set posts.

db.set('posts', []).write();

Sort the top five posts.

db.get('posts')
  .filter({ published: true })
  .sortBy('views')
  .take(5)
  .value();

Get post titles.

db.get('posts')
  .map('title')
  .value();

Get the number of posts.

db.get('posts')
  .size()
  .value();

Get the title of first post using a path.

db.get('posts[0].title').value();

Update a post.

db.get('posts')
  .find({ title: 'low!' })
  .assign({ title: 'hi!' })
  .write();

Remove posts.

db.get('posts')
  .remove({ title: 'low!' })
  .write();

Remove a property.

db.unset('user.name').write();

Make a deep clone of posts.

db.get('posts')
  .cloneDeep()
  .value();

How to use id based resources

Being able to get data using an id can be quite useful, particularly in servers. To add id-based resources support to lowdb, you have 2 options.

shortid is more minimalist and returns a unique id that you can use when creating resources.

const shortid = require('shortid');

const postId = await db
  .get('posts')
  .push({ id: shortid.generate(), title: 'low!' })
  .write().id;

const post = await db
  .get('posts')
  .find({ id: postId })
  .value();

lodash-id provides a set of helpers for creating and manipulating id-based resources.

const lodashId = require('lodash-id');
const COSAsync = require('@slsplus/db/adapters/COSAsync');

const adapter = new COSAsync('serverless-db.json', {
  region: 'COS region',
  bucket: 'COS bucket name with appid',
  secretId: 'COS SecretId',
  secretKey: 'COS SecretKey',
});
const db = await low(adapter);

db._.mixin(lodashId);

// We need to set some default values, if the collection does not exist yet
// We also can store our collection
const collection = db.defaults({ posts: [] }).get('posts');

// Insert a new post...
const newPost = collection.insert({ title: 'low!' }).write();

// ...and retrieve it using its id
const post = collection.getById(newPost.id).value();

How to encrypt data

COSAsync, FileAsync and LocalStorage accept custom serialize and deserialize functions. You can use them to add encryption logic.

const adapter = new COSAsync('serverless-db.json', {
  region: 'COS region',
  bucket: 'COS bucket name with appid',
  secretId: 'COS SecretId',
  secretKey: 'COS SecretKey',
  serialize: (data) => encrypt(JSON.stringify(data)),
  deserialize: (data) => JSON.parse(decrypt(data)),
});

Limits

@slsplus/db is a convenient method for storing data without setting up a database server. It is fast enough and safe to be used as an embedded database.

However, if you seek high performance and scalability more than simplicity, you should probably stick to traditional databases like MongoDB.

License

MIT