Comparing version 0.3.0 to 0.3.1
{ | ||
"name": "lowdb", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Flat JSON file database", | ||
@@ -10,2 +10,3 @@ "keywords": [ | ||
"JSON", | ||
"JSONDatabase", | ||
"lo-dash", | ||
@@ -22,2 +23,3 @@ "lodash", | ||
"prepublish": "grunt build", | ||
"precommit": "npm test", | ||
"benchmark": "grunt build && cd benchmark && node index.js" | ||
@@ -40,12 +42,13 @@ }, | ||
"devDependencies": { | ||
"sinon": "~1.8.2", | ||
"b": "^2.0.1", | ||
"grunt": "^0.4.3", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-coffeelint": "0.0.8", | ||
"grunt-mocha-test": "^0.9.4", | ||
"grunt-contrib-clean": "^0.5.0", | ||
"grunt-contrib-coffee": "^0.10.1", | ||
"grunt-contrib-watch": "^0.5.3", | ||
"grunt-cli": "^0.1.13" | ||
"grunt-mocha-test": "^0.9.4", | ||
"husky": "^0.4.3", | ||
"sinon": "~1.8.2" | ||
} | ||
} |
178
README.md
# 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) | ||
> Flat JSON file database. Used in JSON-Server. | ||
> Flat JSON file database. | ||
## Serverless | ||
* Serverless | ||
* Speedy | ||
* Evented | ||
* 50+ methods coming from Lo-Dash | ||
Instantly ready to go. | ||
LowDB is built on Lo-Dash, this makes it quite different and unique compared to other serverless databases often based on MongoDB API. | ||
_LowDB powers [JSON Server](https://github.com/typicode/json-server) and [JSONPlaceholder](http://jsonplaceholder.typicode.com/)._ | ||
## Usage | ||
```javascript | ||
@@ -14,4 +21,2 @@ var low = require('lowdb') | ||
## Transparent | ||
Database is automatically created and saved to `db.json` in a readable format. | ||
@@ -30,94 +35,38 @@ | ||
## Speedy | ||
To query data, you can use Lo-Dash methods. | ||
Benchmarked on a 2013 PC. | ||
```javascript | ||
var songs = low('songs').where({ title: 'low!' }).value() | ||
``` | ||
get x 1000 0.837708 ms | ||
update x 1000 4.433322 ms | ||
insert x 1000 11.78481 ms | ||
remove x 1000 24.60179 ms | ||
``` | ||
Try it yourself: | ||
Or LowDB equivalent short syntax. | ||
```bash | ||
$ git clone https://github.com/typicode/lowdb.git && cd lowdb | ||
$ npm install | ||
$ npm run benchmark | ||
```javascript | ||
var songs = low('songs', { title: 'low' }) | ||
``` | ||
Changes can also be monitored. | ||
## Elegant | ||
To make requests, you can chain methods or you can use LowDB __unique short syntax__. | ||
The short syntax covers only the most common operations and lets you write really concise code. | ||
```javascript | ||
// ------------------------------------------------- | ||
// Chaining syntax (explicit and similar to Lo-Dash) | ||
// ------------------------------------------------- | ||
low.on('add', function(name, object) { | ||
console.log(object + 'added to' + name) | ||
}) | ||
``` | ||
// get | ||
var song = low('songs').get(id).value() | ||
## Benchmark | ||
// where | ||
var songs = low('songs').where({title: 'low!'}).value() | ||
// insert | ||
var song = low('songs').insert({title: 'low!'}).value() | ||
// update | ||
var song = low('songs').update(id, {title: 'new title'}).value() | ||
// updateWhere | ||
var songs = low('songs').updateWhere({published: false}, {published: true}).value() | ||
// remove | ||
var song = low('songs').remove(id).value() | ||
// removeWhere | ||
var songs = low('songs').removeWhere({title: 'low!'}).value() | ||
// -------------------------------- | ||
// Short syntax (really minimalist) | ||
// -------------------------------- | ||
// get | ||
var song = low('songs', id) | ||
// where | ||
var songs = low('songs', {title: 'low!'}) | ||
// insert | ||
var song = low('songs', {title: 'low!'}, 1) | ||
// update | ||
var song = low('songs', id, {title: 'new title'}) | ||
// updateWhere | ||
var songs = low('songs', {published: false}, {published: true}) | ||
// remove | ||
var song = low('songs', id, -1) | ||
// removeWhere | ||
var songs = low('songs', {title: 'low!'}, -1) | ||
``` | ||
get x 1000 0.837708 ms | ||
update x 1000 4.433322 ms | ||
insert x 1000 11.78481 ms | ||
remove x 1000 24.60179 ms | ||
``` | ||
## API | ||
### Methods | ||
__Collections methods__ | ||
LowDB is built on [Lo-Dash](http://lodash.com/docs) and [Underscore.db](https://github.com/typicode/underscore.db). Therefore you can use any of the __50+ collections methods__ of both libraries: where, find, filter, sortBy, groupBy, ... | ||
__low(collection)__ | ||
Returns or create a Lo-Dash wrapped array with Underscore.db methods. | ||
Returns or create a [Lo-Dash](http://lodash.com/docs) wrapped array. | ||
If the returned value is an object or array and you want to get its value, add `.value()`. It can be omitted though if you just want to modify the database. | ||
You can then use methods like: `where`, `find`, `filter`, `sortBy`, `groupBy`, ... and also methods from [Underscore.db](https://github.com/typicode/underscore.db). | ||
@@ -138,2 +87,4 @@ ```javascript | ||
_If you just want to modify the database, without getting the returned array or object, you can omit `.value()`_ | ||
__low.save([path])__ | ||
@@ -147,31 +98,66 @@ | ||
### Events | ||
__low.path__ | ||
- add (collectionName, insertedDoc) | ||
- update (collectionName, updatedDoc, previousDoc) | ||
- remove (collectionName, removedDoc) | ||
- change () | ||
Database location. By default `db.json`. | ||
```javascript | ||
low.on('add', function(name, doc) { | ||
console.log('new doc: ' + doc.title + ' added to ' + name) | ||
}) | ||
low.path = '/some/path/file.json' | ||
``` | ||
### Options | ||
__autoSave__ | ||
__low.path__ | ||
Set to `false` to disable save on change, this turns LowDB into a read-only in-memory database. By default `true`. | ||
Use this property to change where the database is saved. By default `db.json`. | ||
```javascript | ||
low.autoSave = true | ||
``` | ||
## Events | ||
* add(collectionName, insertedDoc) | ||
* update(collectionName, updatedDoc, previousDoc) | ||
* remove(collectionName, removedDoc) | ||
* change() | ||
## Short syntax | ||
LowDB short syntax covers only the most common operations but lets you write really concise code. | ||
```javascript | ||
low.path = '/some/path/file.json' | ||
low('songs', id) | ||
// -> low('songs').get(id).value() | ||
``` | ||
__low.autoSave__ | ||
```javascript | ||
low('songs', {title: 'low!'}) | ||
// -> low('songs').where({title: 'low!'}).value() | ||
``` | ||
Set to `false` to disable save on change. Great to turn LowDB into a read-only or in-memory database. By default `true`. | ||
```javascript | ||
low('songs', {title: 'low!'}, +1) | ||
// -> low('songs').insert({title: 'low!'}).value() | ||
``` | ||
```javascript | ||
low.autoSave = true | ||
``` | ||
low('songs', {title: 'low!'}, -1) | ||
// -> low('songs').removeWhere({title: 'low!'}).value() | ||
``` | ||
```javascript | ||
low('songs', id, -1) | ||
// -> low('songs').remove(id).value() | ||
``` | ||
```javascript | ||
low('songs', id, {title: 'new title'}) | ||
// -> low('songs').update(id, {title: 'new title'}).value() | ||
``` | ||
```javascript | ||
low('songs', {published: false}, {published: true}) | ||
// -> low('songs').updateWhere({published: false}, {published: true}).value() | ||
``` | ||
## Licence | ||
LowDB is released under the MIT License. |
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
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
15095
10
160