UNPKG

724 BJavaScriptView Raw
1const debug = require('debug')('json-server-reset')
2const jsonServerReset = require('./reset')
3
4function initJsonServerReset(options = {}) {
5 debug('init options keys %o', Object.keys(options))
6 if (options.db && options.clear) {
7 const db = options.db
8 debug('resources to clear', options.clear)
9 const state = db.getState()
10 let changedState = false
11 Object.keys(state).forEach((key) => {
12 if (options.clear.includes(key)) {
13 debug('clearing %s', key)
14 state[key] = []
15 changedState = true
16 }
17 })
18 if (changedState) {
19 debug('saving updated DB')
20 db.setState(state)
21 db.write()
22 }
23 }
24 return jsonServerReset
25}
26
27module.exports = initJsonServerReset