forked from f3rno/bfx-hf-models-adapter-lowdb
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
41 lines (33 loc) · 993 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict'
const debug = require('debug')('bfx:hf:models:adapter-lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const _isString = require('lodash/isString')
const _isObject = require('lodash/isObject')
const _isEmpty = require('lodash/isEmpty')
const lowdb = require('lowdb')
const mapMethods = require('./lib/map_methods')
const genericMethods = require('./lib/generic_methods')
const collectionMethods = require('./lib/collection_methods')
module.exports = ({
dbPath,
defaultData,
Backend = FileSync
}) => {
if (!_isString(dbPath) || _isEmpty(dbPath)) {
throw new Error('DB path required')
}
debug('loading from %s', dbPath)
const db = lowdb(new Backend(dbPath))
if (_isObject(defaultData) && !_isEmpty(defaultData)) {
db.defaults(defaultData).write()
}
const close = db.write.bind(db) // can't close lowdb, just flush changes
return {
db,
close,
mapMethods,
collectionMethods,
genericMethods,
name: 'LowDB'
}
}