node-object-sync
v1.0.2
Published
A small library for syncing on-disk config files with the in-memory object state.
Downloads
1
Readme
node-object-sync
This module is a small drop-in utility for automatically and transparently synchronizing the contents of on-disk configuration files with the in-memory state.
Documentation
Basic setup
The main functionality of this utility is simple - It lets you wrap an existing object. You can interact with that object through the "wrapper" completely transparently. Each time a property is added, reassigned or deleted, the state of your object will be written to the disk.
const myConfig = {
logLevel: "info",
useTimestamps: true,
maxLogFileSize: "20MB",
...
}
// Wrap "myConfig" and use the new wrapper as a proxy and specify
// the path to the location where the config is being saved to.
const myWrappedConfig = ObjectSync.wrap(myConfig, "/path/to/my-config.json")
// Modify "logLevel"
myWrappedConfig.logLevel = "debug"
// After the property is reassigned, the changes are immediately
// saved to the disk.
API
There is no API!
You can interact with your object directly like you have done to this time and the SyncedObject instance will act as a middleman, monitoring your changes and writing them to the configuration file for you.
Configuration options
param #0: targetType:
<Object>Defines the default content of the configuration file that is written to it if the file doesn't already exist.
param #1: fileLocationType:
<string>Defines the location where wrapped object is stored.
If the file already exists in the specified location then all of it's data will be loaded.
If not, a new file will be created and the content ofdefaultContentwill be written to it.recursiveType:
<boolean>
Default:falseAnalogue to a recursive
fs.mkDir.
Defines whether or not to create the file's parent directory recursively.saveType:
<"sync" | "async" | number>
Default:"sync"Defines the behavior of synchronizing the configuration file's content with the in-memory state.
<"sync">- Overwrites the configuration file immediately after any object modifications. This behavior is usingfs.writeFileSyncunder the hood and is blocking!
Note: Any write, permission or filesystem errors will be thrown on assign and delete actions!<"async">- Overwrites the configuration file immediately after any object modifications, but with use of the asynchronousfsAPI.
Note: Any errors thrown during the file save are silenced to prevent application crashes.<number>- Overwrites the configuration file after a specified timeout, asynchronously. This saving method is a "lazy" method and is best suited for when the target object is expected to receive lots of writes.
If set to1s, and two writes were received300msapart, the file would be overwritten exactly1safter the most recent write, so in this case after1.3s. If any writes are made within1sof the last one, the timeout is simply moved a second later, until the frequency is lower.
formatType:
<{ parse: Function, stringify: Function }>
Default:JSON.parse & JSON.stringifyDefines the parse and stringify methods used to read/write from/to the file. By default the
JSONformat is used, but these can be used to set any arbitrary configuration format, such as Yaml, XML, PKL, INI or any other.
