lowdb
Advanced tools
Comparing version 0.8.1 to 0.9.0
{ | ||
"name": "lowdb", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Flat JSON file database", | ||
@@ -44,4 +44,4 @@ "keywords": [ | ||
"standard": "^4.0.1", | ||
"underscore-db": "^0.8.0" | ||
"underscore-db": "^0.9.0" | ||
} | ||
} |
@@ -33,4 +33,4 @@ # 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) | ||
```bash | ||
npm install lowdb --save | ||
```` | ||
npm install lowdb --save | ||
``` | ||
@@ -69,9 +69,12 @@ ## Features | ||
__low.mixin(source)__ | ||
__low.stringify(obj)__ and __low.parse(str)__ | ||
Use it to extend lodash globally with your own utility functions or third-party libraries. | ||
Overwrite these methods to customize JSON stringifying and parsing. | ||
__db.mixin(source)__ | ||
Use it to extend db with your own utility functions or third-party libraries. | ||
```javascript | ||
// Must be called before calling db('songs') for functions to be available. | ||
low.mixin({ | ||
db.mixin({ | ||
second: function(array) { | ||
@@ -86,6 +89,2 @@ return array[1] | ||
__low.stringify(obj)__ and __low.parse(str)__ | ||
Overwrite these methods to customize JSON stringifying and parsing. | ||
__db.object__ | ||
@@ -183,8 +182,8 @@ | ||
```javascript | ||
low.mixin(require('underscore-db')) | ||
var db = low('db.json') | ||
db.mixin(require('underscore-db')) | ||
var songId = db('songs').insert({ title: 'low!' }).id | ||
var song = db('songs').get(songId) | ||
var song = db('songs').getById(songId) | ||
``` | ||
@@ -191,0 +190,0 @@ |
@@ -1,35 +0,4 @@ | ||
var _ = require('lodash') | ||
var lodash = require('lodash') | ||
var disk = require('./disk') | ||
// Returns a lodash chain that calls cb() just after .value() | ||
// | ||
// For example: | ||
// lodashChain(array, cb).method().method().value() | ||
// | ||
// is the same as: | ||
// _.chain(array).method().method().value(); cb() | ||
function lodashChain (array, cb) { | ||
var chain = _.chain(array) | ||
function addCallbackOnValue (c) { | ||
c.value = _.flow(c.value, function (arg) { | ||
cb() | ||
return arg | ||
}) | ||
} | ||
addCallbackOnValue(chain) | ||
_.functions(chain) | ||
.forEach(function (method) { | ||
chain[method] = _.flow(chain[method], function (arg) { | ||
var isChain = _.isObject(arg) && arg.__chain__ | ||
if (isChain) addCallbackOnValue(arg) | ||
return arg | ||
}) | ||
}) | ||
return chain | ||
} | ||
// Returns a lodash chain that calls .value() and cb() | ||
@@ -43,3 +12,3 @@ // automatically after the first .method() | ||
// _.chain(array).method().value(); cb() | ||
function lowChain (array, cb) { | ||
function lowChain (_, array, cb) { | ||
var chain = _.chain(array) | ||
@@ -60,3 +29,4 @@ | ||
function low (file, options) { | ||
var checksum | ||
// Create a fresh copy of lodash | ||
var _ = lodash.runInContext() | ||
@@ -68,5 +38,17 @@ options = _.assign({ | ||
// Modify value function to call save before returning result | ||
var value = _.prototype.value | ||
_.prototype.value = function () { | ||
var res = value.apply(this, arguments) | ||
save() | ||
return res | ||
} | ||
// db.object checksum | ||
var checksum | ||
function save () { | ||
if (file && options.autosave) { | ||
var str = low.stringify(db.object) | ||
// Don't write if there's no changes | ||
if (str === checksum) return | ||
@@ -87,5 +69,5 @@ checksum = str | ||
var short = lowChain(array, save) | ||
var short = lowChain(_, array, save) | ||
short.chain = function () { | ||
return lodashChain(array, save) | ||
return _.chain(array) | ||
} | ||
@@ -105,2 +87,6 @@ return short | ||
db.mixin = function (arg) { | ||
_.mixin(arg) | ||
} | ||
db.object = {} | ||
@@ -126,6 +112,2 @@ | ||
low.mixin = function (arg) { | ||
_.mixin(arg) | ||
} | ||
low.stringify = function (obj) { | ||
@@ -132,0 +114,0 @@ return JSON.stringify(obj, null, 2) |
10164
118
230