0.2.3 • Published 2 years ago
@mmis1000/lowdb-split-json-adapter v0.2.3
Lowdb split JSON adapter
Unlike the original FileSync adapter from lowdb.
This adapter split the JSON into multi files.
And add additional template support.
So you can have multi small JSON file instead of a giant one that lag your editor when mock large Services.
Options
type AdapterOptions = {
// This must be a object and not array
defaultValue?: any
// customize encode method
serialize?: any => string
// customize decode method
deserialize?: string => any
}
class SplitJSONAdapter {
/**
* @param dirPath path to the directory that store JSON files
* @param options optional options
*/
constructor (dirPath: string, options: AdapterOptions = {}) {}
}
Storage structure
Each field was a single json file, and the usage is determined by the file extension.
Available extensions:
- .json
- type: regular field
- mode: r/w
- .template.json
- type: template a single field
- mode: r
- .template.js
- type: template a single field.
Value ofmodule.exports
was used - mode: r
- type: template a single field.
- .snapshot.json
- type: storage when try to write into read only
.template
field - mode: r/w
- type: storage when try to write into read only
- .js
- type: template a single field.
Value ofmodule.exports
was used.
Value was not persistent into disk as.snapshot.json
. - mode: r
- type: template a single field.
When read the following dir
a.json
b.template.json
c.template.js
d.js
Will result in
const object = {
a: [/* ... */],
b: [/* ... */],
c: [/* ... */],
d: [/* ... */]
}
When you save above object the into the above dir
You get
a.json // in place modified
b.template.json // untouched
b.snapshot.json // new
c.template.js // untouched
c.snapshot.json // new
d.js // untouched
So you can simply exclude all .snapshot.json
from git or whatever while still keep it locally persistent.