Comparing version 0.2.0 to 0.3.0
@@ -1,26 +0,27 @@ | ||
var b = require('b'), | ||
low = require('../lib'), | ||
song; | ||
var b = require('b') | ||
var low = require('../lib') | ||
var song | ||
b('insert').run(1000, function() { | ||
low('songs').insert({title: 'low!'}); | ||
}); | ||
low('songs').insert({title: 'low!'}) | ||
}) | ||
song = low.db.songs[500]; | ||
song = low.db.songs[500] | ||
b('get').run(1000, function() { | ||
low('songs').get(song.id); | ||
}); | ||
low('songs').get(song.id) | ||
}) | ||
b('update').run(1000, function() { | ||
low('songs').update(song.id, {title: '!wol'}); | ||
}); | ||
low('songs').update(song.id, {title: '!wol'}) | ||
}) | ||
songs = low.db.songs; | ||
songs = low.db.songs | ||
b('remove').run(1000, function() { | ||
low.db.songs = songs; | ||
low('songs').remove(song.id); | ||
}); | ||
low.db.songs = songs | ||
low('songs').remove(song.id) | ||
}) | ||
console.log(); | ||
console.log() |
@@ -25,10 +25,14 @@ (function() { | ||
insert(coll, doc); | ||
ee.emit('add', doc, coll); | ||
ee.emit('add', low._currentCollName, doc); | ||
return doc; | ||
}), | ||
update: _.wrap(_.update, function(update, coll, id, attrs) { | ||
var doc; | ||
var doc, previousDoc; | ||
previousDoc = _.get(coll, id); | ||
if (previousDoc) { | ||
previousDoc = _.clone(previousDoc); | ||
} | ||
doc = update(coll, id, attrs); | ||
if (doc) { | ||
ee.emit('update', doc, coll); | ||
ee.emit('update', low._currentCollName, doc, previousDoc); | ||
} | ||
@@ -38,6 +42,8 @@ return doc; | ||
updateWhere: _.wrap(_.updateWhere, function(updateWhere, coll, whereAttrs, attrs) { | ||
var docs; | ||
var docs, previousDocs; | ||
previousDocs = _.where(coll, whereAttrs); | ||
previousDocs = _.clone(previousDocs); | ||
docs = updateWhere(coll, whereAttrs, attrs); | ||
if (docs.length > 0) { | ||
ee.emit('update', docs, coll); | ||
ee.emit('update', low._currentCollName, docs, previousDocs); | ||
} | ||
@@ -50,3 +56,3 @@ return docs; | ||
if (doc) { | ||
ee.emit('remove', doc, coll); | ||
ee.emit('remove', low._currentCollName, doc); | ||
} | ||
@@ -59,3 +65,3 @@ return doc; | ||
if (docs.length > 0) { | ||
ee.emit('remove', docs, coll); | ||
ee.emit('remove', low._currentCollName, docs); | ||
} | ||
@@ -68,2 +74,3 @@ return docs; | ||
var chain, _base; | ||
low._currentCollName = str; | ||
(_base = low.db)[str] || (_base[str] = []); | ||
@@ -111,2 +118,4 @@ chain = _(low.db[str]); | ||
low.throttledSave = _.throttle(low.save, 100); | ||
low.load = function(path) { | ||
@@ -139,7 +148,9 @@ if (path == null) { | ||
if (low.autoSave) { | ||
return low.save(); | ||
return low.throttledSave(); | ||
} | ||
}); | ||
low.on('add', function(obj, coll) { | ||
low.on('add', function(name, obj) { | ||
var coll; | ||
coll = low(name).value(); | ||
coll._index || (coll._index = {}); | ||
@@ -149,3 +160,5 @@ return coll._index[obj.id] = obj; | ||
low.on('remove', function(obj, coll) { | ||
low.on('remove', function(name, obj) { | ||
var coll; | ||
coll = low(name).value(); | ||
if (coll._index) { | ||
@@ -152,0 +165,0 @@ return delete coll._index[obj.id]; |
{ | ||
"name": "lowdb", | ||
"version": "0.2.0", | ||
"description": "JSON database", | ||
"version": "0.3.0", | ||
"description": "Flat JSON file database", | ||
"keywords": [ | ||
@@ -20,3 +20,4 @@ "flat", | ||
"test": "grunt mochaTest", | ||
"prepublish": "grunt build" | ||
"prepublish": "grunt build", | ||
"benchmark": "grunt build && cd benchmark && node index.js" | ||
}, | ||
@@ -23,0 +24,0 @@ "repository": { |
167
README.md
@@ -1,10 +0,18 @@ | ||
# LowDB [![NPM version](https://badge.fury.io/js/lowdb.png)](http://badge.fury.io/js/lowdb) [![Build Status](https://travis-ci.org/typicode/lowdb.png)](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)](https://travis-ci.org/typicode/lowdb) | ||
> Flat JSON file database. Used in JSON-Server. | ||
## Serverless | ||
Instantly ready to go. | ||
```javascript | ||
var low = require('lowdb'); | ||
low('songs').insert({title: 'low!'}); | ||
var low = require('lowdb') | ||
low('songs').insert({title: 'low!'}) | ||
``` | ||
Database is saved to `db.json`: | ||
## Transparent | ||
Database is automatically created and saved to `db.json` in a readable format. | ||
```javascript | ||
@@ -14,3 +22,3 @@ { | ||
{ | ||
"title": "low", | ||
"title": "low!", | ||
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c" | ||
@@ -22,65 +30,146 @@ } | ||
## API | ||
## Speedy | ||
__low(collection)__ | ||
Benchmarked on a 2013 PC. | ||
Returns or create a [Lo-Dash](http://lodash.com/docs) wrapped array. Low also adds [Underscore.db](https://github.com/typicode/underscore.db) methods to the wrapped array. | ||
``` | ||
get x 1000 0.837708 ms | ||
update x 1000 4.433322 ms | ||
insert x 1000 11.78481 ms | ||
remove x 1000 24.60179 ms | ||
``` | ||
Therefore you can use any Lo-Dash collection or Underscore.DB methods. Don't forget to add `.value()` to get the returned value. | ||
Try it yourself: | ||
Examples: | ||
```javascript | ||
var newSong = low('songs').insert({title: 'low!'}).value(); // Underscore.db | ||
var song = low('songs').get(newSong.id).value(); // Underscore.db | ||
var songs = low('songs').where({title: 'low!'}).value(); // Lo-Dash | ||
```bash | ||
$ git clone https://github.com/typicode/lowdb.git && cd lowdb | ||
$ npm install | ||
$ npm run benchmark | ||
``` | ||
__low.save([path])__ | ||
Saves database to `path` or `low.path` which is `db.json` by default. | ||
## Elegant | ||
__low.load([path])__ | ||
To make requests, you can chain methods or you can use LowDB __unique short syntax__. | ||
Loads database from `path` or `low.path` which is `db.json` by default. | ||
The short syntax covers only the most common operations and lets you write really concise code. | ||
__low.path__ | ||
```javascript | ||
// ------------------------------------------------- | ||
// Chaining syntax (explicit and similar to Lo-Dash) | ||
// ------------------------------------------------- | ||
Use this property to change where the database is saved. Default to `db.json` | ||
// get | ||
var song = low('songs').get(id).value() | ||
```javascript | ||
low.path = '/some/path/file.json' | ||
``` | ||
// where | ||
var songs = low('songs').where({title: 'low!'}).value() | ||
__low.autoSave__ | ||
// insert | ||
var song = low('songs').insert({title: 'low!'}).value() | ||
Use this property to save to file when database is changed. Default to `true` | ||
// update | ||
var song = low('songs').update(id, {title: 'new title'}).value() | ||
```javascript | ||
low.autoSave = true | ||
``` | ||
// updateWhere | ||
var songs = low('songs').updateWhere({published: false}, {published: true}).value() | ||
## Short syntax | ||
// remove | ||
var song = low('songs').remove(id).value() | ||
```javascript | ||
// removeWhere | ||
var songs = low('songs').removeWhere({title: 'low!'}).value() | ||
// -------------------------------- | ||
// Short syntax (really minimalist) | ||
// -------------------------------- | ||
// get | ||
var song = low('songs', id); | ||
var song = low('songs', id) | ||
// where | ||
var songs = low('songs', {title: 'low!'}); | ||
var songs = low('songs', {title: 'low!'}) | ||
// create | ||
var song = low('songs', {title: 'low!'}, 1); | ||
// insert | ||
var song = low('songs', {title: 'low!'}, 1) | ||
// update | ||
var song = low('songs', id, {title: 'new title'}); | ||
var song = low('songs', id, {title: 'new title'}) | ||
// updateWhere | ||
var songs = low('songs', {published: false}, {published: true}); | ||
var songs = low('songs', {published: false}, {published: true}) | ||
// remove | ||
var song = low('songs', id, -1); | ||
var song = low('songs', id, -1) | ||
// removeWhere | ||
var songs = low('songs', {title: 'low!'}, -1); | ||
var songs = low('songs', {title: 'low!'}, -1) | ||
``` | ||
## 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. | ||
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. | ||
```javascript | ||
var topFiveSongs = low('posts') | ||
.where({published: true}) | ||
.sortBy('views') | ||
.first(5) | ||
.value(); | ||
var songTitles = low('songs') | ||
.pluck('titles') | ||
.value() | ||
var total = low('songs').size() | ||
``` | ||
__low.save([path])__ | ||
Saves database to `path` or `low.path`. By default `db.json`. | ||
__low.load([path])__ | ||
Loads database from `path` or `low.path`. By default `db.json`. | ||
### Events | ||
- add (collectionName, insertedDoc) | ||
- update (collectionName, updatedDoc, previousDoc) | ||
- remove (collectionName, removedDoc) | ||
- change () | ||
```javascript | ||
low.on('add', function(name, doc) { | ||
console.log('new doc: ' + doc.title + ' added to ' + name) | ||
}) | ||
``` | ||
### Options | ||
__low.path__ | ||
Use this property to change where the database is saved. By default `db.json`. | ||
```javascript | ||
low.path = '/some/path/file.json' | ||
``` | ||
__low.autoSave__ | ||
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.autoSave = true | ||
``` |
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
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
15366
158
173
9