node-flat-db
This is a fork of lowdb before they broke the api and no longer is as stable as before (fork of 12.5 lowdb)
Need a quick way to get a local database for a CLI, an Electron app, a small server or the browser?
Example
const flat = const storage = const db =
Database is automatically saved to db.json
.
"posts": "title": "node-flat-db is awesome"
You can query and manipulate it using any lodash method.
And access underlying database object any time.
dbobjectposts
Click here to try node-flat-db in the browser.
ES2015
Examples use ES2015 syntax for convenience, but you can use ES5 syntax too. For example:
var db =
Please note also that node-flat-db can only be run in one instance of Node, it doesn't support Cluster.
Installation
Using npm:
npm install node-flat-db --save
A standalone UMD build is also available on npmcdn:
Features
- Very small (~100 lines for core)
- lodash API
- Extendable:
- Custom storage (file, browser, in-memory, ...)
- Custom format (JSON, BSON, YAML, ...)
- Mixins (id support, ...)
- Encryption
node-flat-db is also very easy to learn since it has only a few methods and properties.
node-flat-db powers json-server package, jsonplaceholder website and many other great projects.
Usage examples
Depending on the context, you can use different storages and formats.
node-flat-db comes bundled with file-sync
, file-async
and browser
storages, but you can also write your own if needed.
CLI
For CLIs, it's easier to use node-flat-db/file-sync
synchronous file storage .
const flat = const storage = const db = const user =
Server
For servers, it's better to avoid blocking requests. Use node-flat-db/file-async
asynchronous file storage.
Important
- When you modify the database, a Promise is returned.
- When you read from the database, the result is immediately returned.
const flat = const storage = const db = app app
Browser
In the browser, node-flat-db/browser
will add localStorage
support.
const flat = const storage = const db = const user =
In-memory
For the best performance, use node-flat-db in-memory storage.
const flat = const db = const user =
Please note that, as an alternative, you can also disable writeOnChange
if you want to control when data is written.
Compression
compresses 1000 record db from
77kb
to7kb
The best way to use node-flat-db if space constraint is an issue, use lzstring adapter
const flat = const storage = const db = const user =
API
flat([filename, [storage, [writeOnChange = true]]])
Creates a new database instance. Here are some examples:
// in-memory // persisted // auto write disabled // To create read-only or write-only database,// set only storage.read or storage.writeconst fileSync = // write-only // read-only
You can also define custom storages and formats:
const myStorage = // obj or a Promise // undefined or a Promise const myFormat = format: // obj // data
db._
Database lodash instance. Use it to add your own utility functions or third-party mixins like underscore-contrib or underscore-db.
db_ const post1 = const post2 =
db.object
Use whenever you want to access or modify the underlying database object.
dbobject // { posts: [ ... ] }
If you directly modify the content of the database object, you will need to manually call write
to persist changes.
// Delete an arraydelete dbobjectpostsdb // Drop databasedbobject = {}db
db.write([source])
Persists database using storage.write
method. Depending on the storage, it may return a promise.
Note: by default, node-flat-db automatically calls it when database changes.
const db = db // writes to db.jsondb // writes to copy.json
db.read([source])
Reads source using storage.read
method. Depending on the storage, it may return a promise.
const db = db // re-reads db.jsondb // reads copy.json
Guide
How to query
With node-flat-db, you get access to the entire lodash API, so there is many ways to query and manipulate data. Here are a few examples to get you started.
Please note that data is returned by reference, this means that modifications to returned objects may change the database. To avoid such behaviour, you need to use .cloneDeep()
.
Also, the execution of chained methods is lazy, that is, execution is deferred until .value()
is called.
Examples
Sort the top five posts.
value
Retrieve post titles.
Get the number of posts.
size
Make a deep clone of posts.
Update a post.
value
Remove posts.
How to use id based resources
Being able to retrieve data using an id can be quite useful, particularly in servers. To add id-based resources support to node-flat-db, you have 2 options.
underscore-db provides a set of helpers for creating and manipulating id-based resources.
const db = db_ const postId = idconst post =
uuid is more minimalist and returns a unique id that you can use when creating resources.
const uuid = const postId = idconst post =
How to use custom format
By default, node-flat-db storages will use JSON
to parse
and stringify
database object.
But it's also possible to specify custom format.serializer
and format.deserializer
methods that will be passed by node-flat-db to storage.read
and storage.write
methods.
For example, if you want to store database in .bson
files (MongoDB file format):
const flat = const storage = const bson = const BSON = // Alternative ES2015 short syntaxconst bson = const format =
How to encrypt data
Simply encrypt
and decrypt
data in format.serialize
and format.deserialize
methods.
For example, using cryptr:
const Cryptr = const cryptr = 'my secret key' const db =
Changelog
See changes for each version in the release notes.
Limits
node-flat-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.
Benchmarks
npm install microtime
npm run benchmarks
License
MIT - Gabriel J. Csapo