Skip to content

Commit 4815c5d

Browse files
committed
Add db.read()
1 parent 4ca9bf9 commit 4815c5d

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,22 @@ low('db.json', {
161161
})
162162
```
163163

164-
Full method signature:
164+
You can also define custom storages and formats:
165165

166166
```js
167-
low(source, {
168-
storage: {
169-
read: (source, deserialize) => // obj or a Promise
170-
write: (dest, obj, serialize) => // undefined or a Promise
171-
},
167+
const myStorage = {
168+
read: (source, deserialize) => // obj or a Promise
169+
write: (dest, obj, serialize) => // undefined or a Promise
170+
}
171+
172+
const myFormat = {
172173
format: {
173174
deserialize: (data) => // obj
174175
serialize: (obj) => // data
175176
}
176-
}, writeOnChange)
177+
}
178+
179+
low(source, { storage: myStorage, format: myFormat }, writeOnChange)
177180
```
178181

179182
__db.___
@@ -213,12 +216,24 @@ db.write()
213216

214217
__db.write([source])__
215218

216-
Persists database using `storage.write` method.
219+
Persists database using `storage.write` method. Depending on the storage, it may return a promise.
220+
221+
Note: by default, lowdb automatically calls it when database changes.
217222

218223
```js
219-
const db = low('db.json')
220-
db.write() // writes to db.json
221-
db.write('copy.json') // writes to copy.json
224+
const db = low('db.json', { storage })
225+
db.write() // writes to db.json
226+
db.write('copy.json') // writes to copy.json
227+
```
228+
229+
__db.read([source])__
230+
231+
Reads source using `storage.read` method. Depending on the storage, it may return a promise.
232+
233+
```js
234+
const db = low('db.json', { storage })
235+
db.read() // re-reads db.json
236+
db.read('copy.json') // reads copy.json
222237
```
223238

224239
## Guide

src/index.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,26 @@ function low (source, options = {}, writeOnChange = true) {
3535
if (source) {
3636
if (options.storage) {
3737
const { storage } = options
38-
db.read = storage.read
39-
db.write = storage.write
40-
? (dest = source) => storage.write(dest, db.object, db.serialize)
41-
: null
38+
39+
if (storage.read) {
40+
db.read = (s = source) => {
41+
const res = storage.read(s, db.deserialize)
42+
43+
if (isPromise(res)) {
44+
return res.then((obj) => {
45+
db.object = obj
46+
return db
47+
})
48+
}
49+
50+
db.object = res
51+
return db
52+
}
53+
}
54+
55+
if (storage.write) {
56+
db.write = (dest = source) => storage.write(dest, db.object, db.serialize)
57+
}
4258
}
4359

4460
options.format && Object.assign(db, options.format)
@@ -81,19 +97,11 @@ function low (source, options = {}, writeOnChange = true) {
8197
db.source = source
8298

8399
// Init
84-
if (db.source && db.read) {
85-
const res = db.read(db.source, db.deserialize)
86-
if (isPromise(res)) {
87-
return res.then((obj) => {
88-
db.object = obj
89-
return db
90-
})
91-
} else {
92-
db.object = res
93-
}
100+
if (db.read) {
101+
return db.read(source)
102+
} else {
103+
return db
94104
}
95-
96-
return db
97105
}
98106

99107
module.exports = low

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ const _test = (str, { source, read, write, promise, writeOnChange} = {}) => {
6464
t.same(args, ['backup.json', {}, undefined])
6565
}
6666

67+
if (read) {
68+
// Read
69+
promise
70+
? await db.read()
71+
: db.read()
72+
73+
t.is(read.callCount, 2)
74+
75+
promise
76+
? await db.read('backup.json')
77+
: db.read('backup.json')
78+
79+
let args = read.args.slice(-1)[0]
80+
t.same(args, ['backup.json', undefined])
81+
}
82+
6783
t.end()
6884
} catch (err) {
6985
t.end(err)

0 commit comments

Comments
 (0)