Comparing version
@@ -97,2 +97,4 @@ (function() { | ||
low.autoSave = true; | ||
low.save = function(path) { | ||
@@ -119,13 +121,19 @@ if (path == null) { | ||
low.on('add', function() { | ||
return low.save(); | ||
return ee.emit('change'); | ||
}); | ||
low.on('update', function() { | ||
return low.save(); | ||
return ee.emit('change'); | ||
}); | ||
low.on('remove', function() { | ||
return low.save(); | ||
return ee.emit('change'); | ||
}); | ||
low.on('change', function() { | ||
if (low.autoSave) { | ||
return low.save(); | ||
} | ||
}); | ||
low.on('add', function(obj, coll) { | ||
@@ -132,0 +140,0 @@ coll._index || (coll._index = {}); |
{ | ||
"name": "lowdb", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "JSON database", | ||
@@ -44,4 +44,5 @@ "keywords": [ | ||
"grunt-contrib-coffee": "^0.10.1", | ||
"grunt-contrib-watch": "^0.5.3" | ||
"grunt-contrib-watch": "^0.5.3", | ||
"grunt-cli": "^0.1.13" | ||
} | ||
} |
@@ -1,25 +0,84 @@ | ||
# low | ||
# LowDB [](http://badge.fury.io/js/lowdb) [](https://travis-ci.org/typicode/lowdb) | ||
[](http://badge.fury.io/js/lowdb) | ||
```javascript | ||
var low = require('lowdb'); | ||
low('songs').insert({title: 'low!'}); | ||
``` | ||
## Example | ||
Database is saved to `db.json`: | ||
```bash | ||
$ npm i lowdb --save | ||
```javascript | ||
{ | ||
"songs": [ | ||
{ | ||
"title": "low", | ||
"id": "e31aa48c-a9d8-4f79-9fce-ded4c16c3c4c" | ||
} | ||
] | ||
} | ||
``` | ||
## API | ||
__low(collection)__ | ||
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. | ||
Therefore you can use any Lo-Dash collection or Underscore.DB methods. Don't forget to add `.value()` to get the returned value. | ||
Examples: | ||
```javascript | ||
var low = require('lowdb'); | ||
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 | ||
``` | ||
// insert a song | ||
low('songs').insert({title: 'low!'}); | ||
__low.save([path])__ | ||
// find all songs named 'low!' | ||
var songs = low('songs').where({title: 'low!'}).value(); | ||
Saves database to `path` or `low.path` which is `db.json` by default. | ||
__low.load([path])__ | ||
Loads database from `path` or `low.path` which is `db.json` by default. | ||
__low.path__ | ||
Use this property to change where the database is saved. Default to `db.json` | ||
```javascript | ||
low.path = '/some/path/file.json' | ||
``` | ||
Database is saved to `db.json`. | ||
__low.autoSave__ | ||
## API | ||
Use this property to save to file when database is changed. Default to `true` | ||
See [Underscore.db](https://github.com/typicode/underscore.db) and [Lo-Dash](http://lodash.com/docs). | ||
```javascript | ||
low.autoSave = true | ||
``` | ||
## Short syntax | ||
```javascript | ||
// get | ||
var song = low('songs', id); | ||
// where | ||
var songs = low('songs', {title: 'low!'}); | ||
// create | ||
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); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13006
19.86%146
4.29%85
226.92%9
12.5%