Comparing version 0.7.3 to 0.8.0
{ | ||
"name": "lowdb", | ||
"version": "0.7.3", | ||
"version": "0.8.0", | ||
"description": "Flat JSON file database", | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
"scripts": { | ||
"test": "mocha", | ||
"test": "standard && mocha", | ||
"precommit": "npm test" | ||
@@ -36,11 +36,12 @@ }, | ||
"lodash": "^3.1.0", | ||
"steno": "^0.3.2" | ||
"steno": "^0.4.1" | ||
}, | ||
"devDependencies": { | ||
"husky": "^0.7.0", | ||
"mocha": "^1.21.4", | ||
"mocha": "^2.2.5", | ||
"rimraf": "^2.2.8", | ||
"sinon": "^1.12.2", | ||
"standard": "^4.0.1", | ||
"underscore-db": "^0.8.0" | ||
} | ||
} |
@@ -24,3 +24,3 @@ # 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) | ||
You can query and manipulate it using __any lodash 3 method__ | ||
You can query and manipulate it using __any__ [lodash](https://lodash.com/docs) __method__ | ||
@@ -47,3 +47,3 @@ ```javascript | ||
_lowdb powers [json-server](https://github.com/typicode/json-server) package and [jsonplaceholder](http://jsonplaceholder.typicode.com/) website._ | ||
_lowdb powers [json-server](https://github.com/typicode/json-server) package, [jsonplaceholder](http://jsonplaceholder.typicode.com/) website and [other projects](https://www.npmjs.com/browse/depended/lowdb)._ | ||
@@ -66,3 +66,3 @@ ## API | ||
autosave: true, // automatically save database on change (default: true) | ||
async: true // asyncrhonous write (default: true) | ||
async: true // asynchronous write (default: true) | ||
}) | ||
@@ -69,0 +69,0 @@ ``` |
var fs = require('fs') | ||
var path = require('path') | ||
var _ = require('lodash') | ||
var steno = require('steno') | ||
function getTempFile(file) { | ||
return path.join( | ||
path.dirname(file), | ||
'.~' + path.basename(file) | ||
) | ||
} | ||
module.exports = { | ||
read: function (file) { | ||
if (fs.existsSync(file)) return fs.readFileSync(file) | ||
if (fs.existsSync(file)) { | ||
return fs.readFileSync(file, 'utf-8') | ||
} | ||
}, | ||
write: function(file, data) { | ||
steno(getTempFile(file)) | ||
.setCallback(function(err, data, next) { | ||
if (err) throw err | ||
fs.rename(getTempFile(file), file, function(err) { | ||
if (err) throw err | ||
next() | ||
}) | ||
}) | ||
.write(data) | ||
write: function (file, data) { | ||
steno.writeFile(file, data, function (err) { | ||
if (err) throw err | ||
}) | ||
}, | ||
writeSync: function(file, data) { | ||
fs.writeFileSync(getTempFile(file), data) | ||
fs.renameSync(getTempFile(file), file) | ||
writeSync: function (file, data) { | ||
steno.writeFileSync(file, data) | ||
} | ||
} |
@@ -1,10 +0,16 @@ | ||
var fs = require('fs') | ||
var _ = require('lodash') | ||
var disk = require('./disk') | ||
function lodashChain(array, cb) { | ||
// 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) { | ||
function addCallbackOnValue (c) { | ||
c.value = _.flow(c.value, function (arg) { | ||
cb() | ||
@@ -18,4 +24,4 @@ return arg | ||
_.functions(chain) | ||
.forEach(function(method) { | ||
chain[method] = _.flow(chain[method], function(arg) { | ||
.forEach(function (method) { | ||
chain[method] = _.flow(chain[method], function (arg) { | ||
var isChain = _.isObject(arg) && arg.__chain__ | ||
@@ -30,8 +36,16 @@ if (isChain) addCallbackOnValue(arg) | ||
function lowChain(array, cb) { | ||
// Returns a lodash chain that calls .value() and cb() | ||
// automatically after the first .method() | ||
// | ||
// For example: | ||
// lodashChain(array, cb).method() | ||
// | ||
// is the same as: | ||
// _.chain(array).method().value(); cb() | ||
function lowChain (array, cb) { | ||
var chain = _.chain(array) | ||
_.functions(chain) | ||
.forEach(function(method) { | ||
chain[method] = _.flow(chain[method], function(arg) { | ||
.forEach(function (method) { | ||
chain[method] = _.flow(chain[method], function (arg) { | ||
var res = arg.value ? arg.value() : arg | ||
@@ -46,3 +60,3 @@ cb() | ||
function low(file, options) { | ||
function low (file, options) { | ||
var checksum | ||
@@ -55,3 +69,3 @@ | ||
function save() { | ||
function save () { | ||
if (file && options.autosave) { | ||
@@ -65,7 +79,8 @@ var str = low.stringify(db.object) | ||
function db(key) { | ||
function db (key) { | ||
var array | ||
if (db.object[key]) { | ||
var array = db.object[key] | ||
array = db.object[key] | ||
} else { | ||
var array = db.object[key] = [] | ||
array = db.object[key] = [] | ||
save() | ||
@@ -75,3 +90,3 @@ } | ||
var short = lowChain(array, save) | ||
short.chain = function() { | ||
short.chain = function () { | ||
return lodashChain(array, save) | ||
@@ -82,3 +97,3 @@ } | ||
db.save = function(f) { | ||
db.save = function (f) { | ||
f = f ? f : file | ||
@@ -88,3 +103,3 @@ disk.write(f, low.stringify(db.object)) | ||
db.saveSync = function(f) { | ||
db.saveSync = function (f) { | ||
f = f ? f : file | ||
@@ -98,6 +113,7 @@ disk.writeSync(f, low.stringify(db.object)) | ||
var data = disk.read(file) | ||
if (data) { | ||
if (data && data.trim() !== '') { | ||
try { | ||
db.object = low.parse(data) | ||
} catch (e) { | ||
if (e instanceof SyntaxError) e.message = 'Malformed JSON' | ||
e.message += ' in file:' + file | ||
@@ -114,11 +130,11 @@ throw e | ||
low.mixin = function(arg) { | ||
low.mixin = function (arg) { | ||
_.mixin(arg) | ||
} | ||
low.stringify = function(obj) { | ||
low.stringify = function (obj) { | ||
return JSON.stringify(obj, null, 2) | ||
} | ||
low.parse = function(str) { | ||
low.parse = function (str) { | ||
return JSON.parse(str) | ||
@@ -125,0 +141,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
10560
8
133
1
6
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedsteno@0.4.4(transitive)
- Removedsteno@0.3.2(transitive)
Updatedsteno@^0.4.1