Comparing version 0.3.2 to 0.4.0
{ | ||
"name": "lowdb", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Flat JSON file database", | ||
@@ -18,8 +18,6 @@ "keywords": [ | ||
], | ||
"main": "lib/index.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "grunt mochaTest", | ||
"prepublish": "grunt build", | ||
"precommit": "npm test", | ||
"benchmark": "grunt build && cd benchmark && node index.js" | ||
"test": "mocha", | ||
"precommit": "npm test" | ||
}, | ||
@@ -37,17 +35,12 @@ "repository": { | ||
"dependencies": { | ||
"underscore.db": "~0.6.0", | ||
"lodash": "~2.4.1" | ||
"graceful-fs": "^3.0.2", | ||
"lodash": "~2.4.1", | ||
"temp-write": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"b": "^2.0.1", | ||
"grunt": "^0.4.3", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-coffeelint": "0.0.8", | ||
"grunt-contrib-clean": "^0.5.0", | ||
"grunt-contrib-coffee": "^0.10.1", | ||
"grunt-contrib-watch": "^0.5.3", | ||
"grunt-mocha-test": "^0.9.4", | ||
"husky": "^0.4.3", | ||
"sinon": "~1.8.2" | ||
"husky": "^0.6.1", | ||
"mocha": "^1.21.4", | ||
"rimraf": "^2.2.8", | ||
"underscore.db": "^0.7.0" | ||
} | ||
} |
194
README.md
@@ -1,2 +0,2 @@ | ||
# LowDB [![NPM version](https://badge.fury.io/js/lowdb.svg)](http://badge.fury.io/js/lowdb) [![Build Status](https://travis-ci.org/typicode/lowdb.svg)](https://travis-ci.org/typicode/lowdb) | ||
# LowDB [![NPM version](https://badge.fury.io/js/lowdb.svg)](http://badge.fury.io/js/lowdb) [![Build Status](https://travis-ci.org/typicode/lowdb.svg?branch=master)](https://travis-ci.org/typicode/lowdb) | ||
@@ -6,12 +6,12 @@ > Flat JSON file database for Node | ||
* Serverless | ||
* Speedy | ||
* Evented | ||
* 50+ methods coming from Lo-Dash | ||
* Multiple databases | ||
* In-memory or disk-based | ||
* 80+ methods from Lo-Dash API | ||
* Asynchronous and fault-tolerant writing | ||
* Extendable | ||
LowDB is built on Lo-Dash, this makes it quite different and unique compared to other serverless databases often based on MongoDB API. | ||
LowDB uses Lo-Dash functional progamming API instead of a MongoDB-like API. This makes it quite unique and different. | ||
_LowDB powers [JSON Server](https://github.com/typicode/json-server) and [JSONPlaceholder](http://jsonplaceholder.typicode.com/)._ | ||
_LowDB powers [JSON Server](https://github.com/typicode/json-server) and [JSONPlaceholder](http://jsonplaceholder.typicode.com/). If you need something similar for the browser, check [Underscore.db](https://github.com/typicode/underscore.db)._ | ||
_If you need something similar for the browser, check [Underscore.db](https://github.com/typicode/underscore.db)._ | ||
## Usage | ||
@@ -21,3 +21,5 @@ | ||
var low = require('lowdb') | ||
low('songs').insert({title: 'low!'}) | ||
var db = low('db.json') | ||
db('songs').push({ title: 'low!'}) | ||
``` | ||
@@ -31,4 +33,3 @@ | ||
{ | ||
"title": "low!", | ||
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c" | ||
"title": "low!" | ||
} | ||
@@ -39,153 +40,152 @@ ] | ||
To query data, you can use Lo-Dash methods. | ||
Data can be queried and manipulated using any Lo-Dash method. | ||
```javascript | ||
var songs = low('songs').where({ title: 'low!' }).value() | ||
var song = db('songs').find({ title: 'low!' }).value() | ||
db('songs').remove({ title: 'low!' }) | ||
``` | ||
Or LowDB equivalent short syntax. | ||
You can also use id-based methods by extending LowDB with [Underscore.db](https://github.com/typicode/underscore.db). | ||
## API | ||
__low([filename])__ | ||
Creates a disk-based or in-memory database instance. If a filename is provided, it loads or creates it. | ||
```javascript | ||
var songs = low('songs', { title: 'low!' }) | ||
var db = low() // in-memory | ||
var db = low('db.json') // disk-based | ||
``` | ||
Changes can also be monitored. | ||
__low.mixin(source)__ | ||
Use it to extend Lo-Dash globally with your own utility functions or third-party libraries. | ||
```javascript | ||
low.on('add', function(name, object) { | ||
console.log(object + 'added to' + name) | ||
// Must be called before calling db('songs') for functions to be available. | ||
low.mixin({ | ||
second: function(array) { | ||
if (array.length >= 2) return array[1] | ||
} | ||
}) | ||
var song = db('songs').second().value() | ||
``` | ||
## Benchmark | ||
__db.object__ | ||
Database object. Useful if you want to access the content of your JSON file and don't want to go through Lo-Dash methods. | ||
```javascript | ||
console.log(db.object) // { songs: [ { title: 'low!' } ] } | ||
db('songs').value() === db.object.songs | ||
``` | ||
get x 1000 0.837708 ms | ||
update x 1000 4.433322 ms | ||
insert x 1000 11.78481 ms | ||
remove x 1000 24.60179 ms | ||
__db.save()__ | ||
LowDB automatically saves to disk. However, in case you directly modify the content of the database object, you'll need to manually call `save`. | ||
```javascript | ||
delete db.object.songs | ||
db.save() | ||
``` | ||
_To run the benchmark on your machine, clone the project and run `npm install && npm run benchmark`._ | ||
## Documentation | ||
## API | ||
### Operations | ||
__low(collection)__ | ||
With LowDB you get access to the entire [Lo-Dash API](http://lodash.com/), so there's many, many ways to query and manipulate data. Here are a few examples to get you started. | ||
Returns or create a [Lo-Dash](http://lodash.com/docs) wrapped array. | ||
Sort the top five songs. | ||
You can then use methods like: `where`, `find`, `filter`, `sortBy`, `groupBy`, ... and also methods from [Underscore.db](https://github.com/typicode/underscore.db). | ||
```javascript | ||
var topFiveSongs = low('songs') | ||
db('songs') | ||
.where({published: true}) | ||
.sortBy('views') | ||
.first(5) | ||
.value(); | ||
var songTitles = low('songs') | ||
.pluck('titles') | ||
.value() | ||
var total = low('songs').size() | ||
``` | ||
_If you just want to modify the database, without getting the returned array or object, you can omit `.value()`_ | ||
Retrieve song titles. | ||
__low.save([path])__ | ||
```javascript | ||
db('songs') | ||
.pluck('titles') | ||
.value() | ||
``` | ||
Saves database to `path` or `low.path`. By default `db.json`. | ||
Get the number of songs. | ||
__low.load([path])__ | ||
```javascript | ||
db('songs').size() | ||
``` | ||
Loads database from `path` or `low.path`. By default `db.json`. | ||
Make a deep clone of songs. | ||
__low.path__ | ||
```javascript | ||
db('songs').cloneDeep().value | ||
``` | ||
Database location. By default `db.json`. | ||
Update a song. | ||
```javascript | ||
low.path = '/some/path/file.json' | ||
db('songs').find({ title: 'low!' }).assign({ title: 'hi!'}) | ||
``` | ||
__autoSave__ | ||
Remove songs. | ||
Set to `false` to disable save on change, this turns LowDB into a read-only in-memory database. By default `true`. | ||
```javascript | ||
low.autoSave = true | ||
db('songs').remove({ title: 'low!' }) | ||
``` | ||
## Events | ||
### Id-based resources support | ||
* add(collectionName, insertedDoc) | ||
* update(collectionName, updatedDoc, previousDoc) | ||
* remove(collectionName, removedDoc) | ||
* change() | ||
Being able to retrieve data using an id can be quite useful, particularly in servers. To add id-based resources support to LowDB, you have 2 options. | ||
## Short syntax | ||
[Underscore.db](https://github.com/typicode/underscore.db) provides a set of helpers for creating and manipulating id-based resources. | ||
LowDB short syntax covers only the most common operations but lets you write really concise code. | ||
```javascript | ||
low('songs', id) | ||
// == low('songs').get(id).value() | ||
``` | ||
low.mixin(require('underscore.db')) | ||
```javascript | ||
low('songs', {title: 'low!'}) | ||
// == low('songs').where({title: 'low!'}).value() | ||
``` | ||
var db = low('db.json') | ||
```javascript | ||
low('songs', {title: 'low!'}, +1) | ||
// == low('songs').insert({title: 'low!'}).value() | ||
var songId = db.insert({ title: 'low!' }).value().id | ||
var song = db.get(songId).value() | ||
``` | ||
```javascript | ||
low('songs', {title: 'low!'}, -1) | ||
// == low('songs').removeWhere({title: 'low!'}).value() | ||
``` | ||
Or simply use [uuid](https://github.com/broofa/node-uuid). | ||
```javascript | ||
low('songs', id, -1) | ||
// == low('songs').remove(id).value() | ||
``` | ||
var uuid = require('uuid') | ||
```javascript | ||
low('songs', id, {title: 'new title'}) | ||
// == low('songs').update(id, {title: 'new title'}).value() | ||
var songId = db.push({ id: uuid(), title: 'low!' }).value().id | ||
var song = db.find({ id: songId }).value() | ||
``` | ||
In both cases, your `db.json` will then look like this. | ||
```javascript | ||
low('songs', {published: false}, {published: true}) | ||
// == low('songs').updateWhere({published: false}, {published: true}).value() | ||
{ | ||
"songs": [ | ||
{ | ||
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c", | ||
"title": "low!" | ||
} | ||
] | ||
} | ||
``` | ||
## FAQ | ||
## Changelog | ||
__How is database saved?__ | ||
See details changes for each version in the [release notes](https://github.com/typicode/lowdb/releases). | ||
Database is only saved to disk when you call `insert`, `update`, `updateWhere`, `remove`, `removeWhere`. | ||
Also writing is synchronous but throttled to keep things fast. | ||
## Limits | ||
Here's an example to illustrate: | ||
LowDB is a convenient method for storing data without setting up a database server. It's fast enough and safe to be used as an embedded database. | ||
```javascript | ||
low('posts').insert({ title: 'foo' }) // database is persisted synchronously | ||
low('posts').insert({ title: 'foo' }) // database is not persisted | ||
low('posts').insert({ title: 'foo' }) // database is not persisted | ||
// 100 ms later database will be persisted synchronously | ||
``` | ||
However, if you need high performance and scalability more than simplicity, you should stick to databases like MongoDB. | ||
So in 1 second, LowDB will make, at most, 10 synchronous writes. | ||
_Future versions of LowDB may be fully asynchronous._ | ||
__Does it support concurrency?__ | ||
Yes. Node being single threaded and changes to database being synchronously written, there's no risk of having concurrency problems. | ||
## License | ||
LowDB is released under the MIT License. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1009110
4
3
107
1
1
1
+ Addedgraceful-fs@^3.0.2
+ Addedtemp-write@^1.0.0
+ Addedgraceful-fs@3.0.124.2.11(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addednatives@1.1.6(transitive)
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedtemp-write@1.1.2(transitive)
+ Addeduuid@2.0.3(transitive)
- Removedunderscore.db@~0.6.0
- Removedunderscore.db@0.6.1(transitive)