Comparing version 0.11.2 to 0.11.3
45
index.js
@@ -46,7 +46,26 @@ 'use strict'; | ||
db.read = storage.read; | ||
db.write = storage.write ? function () { | ||
var dest = arguments.length <= 0 || arguments[0] === undefined ? source : arguments[0]; | ||
return storage.write(dest, db.object, db.serialize); | ||
} : null; | ||
if (storage.read) { | ||
db.read = function () { | ||
var s = arguments.length <= 0 || arguments[0] === undefined ? source : arguments[0]; | ||
var res = storage.read(s, db.deserialize); | ||
if (isPromise(res)) { | ||
return res.then(function (obj) { | ||
db.object = obj; | ||
return db; | ||
}); | ||
} | ||
db.object = res; | ||
return db; | ||
}; | ||
} | ||
if (storage.write) { | ||
db.write = function () { | ||
var dest = arguments.length <= 0 || arguments[0] === undefined ? source : arguments[0]; | ||
return storage.write(dest, db.object, db.serialize); | ||
}; | ||
} | ||
})(); | ||
@@ -99,17 +118,9 @@ } | ||
// Init | ||
if (db.source && db.read) { | ||
var res = db.read(db.source, db.deserialize); | ||
if (isPromise(res)) { | ||
return res.then(function (obj) { | ||
db.object = obj; | ||
return db; | ||
}); | ||
} else { | ||
db.object = res; | ||
} | ||
if (db.read) { | ||
return db.read(source); | ||
} else { | ||
return db; | ||
} | ||
return db; | ||
} | ||
module.exports = low; |
{ | ||
"name": "lowdb", | ||
"version": "0.11.2", | ||
"version": "0.11.3", | ||
"description": "JSON database for Node and the browser powered by lodash", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -34,2 +34,7 @@ # 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) | ||
```js | ||
// For Node 0.12 | ||
var db = low('db.json', { storage: storage }) | ||
``` | ||
_Please note that lowdb can only be run in one instance of Node, it doesn't support Cluster._ | ||
@@ -160,10 +165,11 @@ | ||
Full method signature: | ||
You can also define custom storages and formats: | ||
```js | ||
low(source, { | ||
storage: { | ||
read: (source, deserialize) => // obj or a Promise | ||
write: (dest, obj, serialize) => // undefined or a Promise | ||
}, | ||
const myStorage = { | ||
read: (source, deserialize) => // obj or a Promise | ||
write: (dest, obj, serialize) => // undefined or a Promise | ||
} | ||
const myFormat = { | ||
format: { | ||
@@ -173,3 +179,5 @@ deserialize: (data) => // obj | ||
} | ||
}, writeOnChange) | ||
} | ||
low(source, { storage: myStorage, format: myFormat }, writeOnChange) | ||
``` | ||
@@ -188,4 +196,4 @@ | ||
var song1 = db('songs').first() | ||
var song2 = db('songs').second() | ||
const song1 = db('songs').first() | ||
const song2 = db('songs').second() | ||
``` | ||
@@ -215,10 +223,22 @@ | ||
Persists database using `storage.write` method. | ||
Persists database using `storage.write` method. Depending on the storage, it may return a promise. | ||
Note: by default, lowdb automatically calls it when database changes. | ||
```js | ||
const db = low('db.json') | ||
db.write() // writes to db.json | ||
db.write('copy.json') // writes to copy.json | ||
const db = low('db.json', { storage }) | ||
db.write() // writes to db.json | ||
db.write('copy.json') // writes to copy.json | ||
``` | ||
__db.read([source])__ | ||
Reads source using `storage.read` method. Depending on the storage, it may return a promise. | ||
```js | ||
const db = low('db.json', { storage }) | ||
db.read() // re-reads db.json | ||
db.read('copy.json') // reads copy.json | ||
``` | ||
## Guide | ||
@@ -288,8 +308,8 @@ | ||
```js | ||
var db = low('db.json') | ||
const db = low('db.json') | ||
db._.mixin(require('underscore-db')) | ||
var songId = db('songs').insert({ title: 'low!' }).id | ||
var song = db('songs').getById(songId) | ||
const songId = db('songs').insert({ title: 'low!' }).id | ||
const song = db('songs').getById(songId) | ||
``` | ||
@@ -300,6 +320,6 @@ | ||
```js | ||
var uuid = require('uuid') | ||
const uuid = require('uuid') | ||
var songId = db('songs').push({ id: uuid(), title: 'low!' }).id | ||
var song = db('songs').find({ id: songId }) | ||
const songId = db('songs').push({ id: uuid(), title: 'low!' }).id | ||
const song = db('songs').find({ id: songId }) | ||
``` | ||
@@ -306,0 +326,0 @@ |
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
17072
170
384