Skip to content

Commit 202ca57

Browse files
committed
Expose stringify and parse
1 parent 723491c commit 202ca57

File tree

5 files changed

+60
-46
lines changed

5 files changed

+60
-46
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ low.mixin({
6969
var song = db('songs').second().value()
7070
```
7171

72+
__low.stringify(obj)__ and __low.parse(str)__
73+
74+
Overwrite these methods to customize JSON stringifying and parsing.
75+
7276
__db.object__
7377

7478
Database object. Useful if you want to access the content of your JSON file and don't want to go through Lo-Dash methods.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"file",
88
"database",
99
"JSON",
10-
"JSONDatabase",
1110
"lo-dash",
1211
"lodash",
1312
"underscore",

src/index.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
var fs = require('fs')
12
var _ = require('lodash')
2-
var Store = require('./store')
3+
var Writer = require('./writer')
34

45
// Compose every function with fn
56
function composeAll(obj, fn) {
@@ -11,10 +12,14 @@ function composeAll(obj, fn) {
1112
}
1213

1314
function Low(filename) {
14-
// Create a new JSON file and get object
1515
if (filename) {
16-
var store = new Store(filename)
17-
var object = store.object
16+
if (fs.existsSync(filename)) {
17+
var object = low.parse(fs.readFileSync(filename))
18+
} else {
19+
fs.writeFileSync(filename, '{}')
20+
var object = {}
21+
}
22+
var writer = new Writer(filename)
1823
} else {
1924
var object = {}
2025
}
@@ -23,18 +28,19 @@ function Low(filename) {
2328
// Create empty array if it doesn't exist
2429
object[name] = object[name] || []
2530

26-
if (filename) store.save()
31+
// Save it
32+
if (filename) chain.save()
2733

28-
// Chain array
34+
// Create a Lo-Dash chained array
2935
var chainedArray = _.chain(object[name])
3036

3137
// Hack, wrap every Lo-Dash function to call save.
3238
// Save is however throttled and only happens if database object
3339
// has changed.
34-
// With Node 0.12 and Object.observe support, this bit of code should
40+
// With Node 0.12 and Object.observe support, this bit of code should be
3541
// removed :)
3642
if (filename) composeAll(chainedArray, function(arg) {
37-
store.save()
43+
chain.save()
3844
return arg
3945
})
4046

@@ -45,9 +51,9 @@ function Low(filename) {
4551
chain.object = object
4652

4753
// Call it to manually save database
48-
chain.save = function() {
49-
if (store) store.save()
50-
}
54+
chain.save = _.throttle(function() {
55+
if (filename) writer.write(low.stringify(object))
56+
}, 10)
5157

5258
return chain
5359
}
@@ -60,4 +66,12 @@ low.mixin = function(source) {
6066
_.mixin(source)
6167
}
6268

63-
module.exports = low
69+
low.stringify = function(obj) {
70+
return JSON.stringify(obj, null, 2)
71+
}
72+
73+
low.parse = function(str) {
74+
return JSON.parse(str)
75+
}
76+
77+
module.exports = low

src/store.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/index.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ describe('LowDB', function() {
6666
beforeEach(function() {
6767
fs.writeFileSync(dbPath, JSON.stringify({ foo: { a: 1 } }))
6868
db = low(dbPath)
69-
db('foo').push({ a: 1 })
7069
})
7170

7271
it('loads automatically file', function() {
@@ -75,7 +74,6 @@ describe('LowDB', function() {
7574

7675
})
7776

78-
7977
describe('In-memory', function() {
8078

8179
beforeEach(function() {
@@ -112,17 +110,15 @@ describe('LowDB', function() {
112110

113111
beforeEach(function() {
114112
db = low(dbPath)
115-
})
116-
117-
it('adds functions', function(done) {
118113
low.mixin({
119114
hello: function(array, word) {
120115
array.push('hello ' + word)
121116
}
122117
})
118+
})
123119

120+
it('adds functions', function(done) {
124121
db('foo').hello('world')
125-
126122
setTimeout(function() {
127123
assert.deepEqual(JSON.parse(fs.readFileSync(dbPath)), { foo: [ 'hello world' ] })
128124
done()
@@ -131,6 +127,34 @@ describe('LowDB', function() {
131127

132128
})
133129

130+
describe('stringify and parse', function() {
131+
132+
var stringify = low.stringify
133+
var parse = low.parse
134+
135+
beforeEach(function() {
136+
low.stringify = function() { return '{ foo: [] }' }
137+
low.parse = function() { return { bar: [] } }
138+
fs.writeFileSync(dbPath, '{}')
139+
db = low(dbPath)
140+
})
141+
142+
afterEach(function() {
143+
low.stringify = stringify
144+
low.parse = parse
145+
})
146+
147+
it('can be overriden', function(done) {
148+
assert.deepEqual(db.object, { bar: [] })
149+
db.save() // will stringify object
150+
setTimeout(function() {
151+
assert.equal(fs.readFileSync(dbPath, 'utf-8'), '{ foo: [] }')
152+
done()
153+
}, 100)
154+
})
155+
156+
})
157+
134158
describe('underscore-db', function() {
135159

136160
beforeEach(function() {
@@ -145,4 +169,3 @@ describe('LowDB', function() {
145169

146170
})
147171
})
148-

0 commit comments

Comments
 (0)