Package detail

sb-config-file

steelbrain50MITdeprecated4.0.0

Use lowdb instead

Manage JSON configuration files without race conditions

cli, app, config

readme

sb-config-file

Greenkeeper badge

sb-config-file is a Node.js module to help you manage your JSON configuration files without worrying about concurrency issues or double writes. It uses and therefore supports the lodash.get/lodash.set way of accessing properties

API

type Options = {
  prettyPrint: boolean = true,
  atomicWrites: boolean = true,
}

export default class ConfigFile {
  get(dotSeparatedKey: string, defaultValue = null): Promise<any>
  getSync(dotSeparatedKey: string, defaultValue = null): any
  set(dotSeparatedKey: string, value, strict = false): Promise<void>
  setSync(dotSeparatedKey: string, value, strict = false): void
  delete(dotSeparatedKey: string, strict = false): Promise<void>
  deleteSync(dotSeparatedKey: string, strict = false): void

  static get(filePath: string, defaultConfig: Object, options: Options): ConfigFile
}}

Example Usage

const Path = require('path')
const getConfigFile = require('sb-config-file')

getConfigFile(Path.join(__dirname, 'config.json')).then(function(configFile) {
  configFile.set('database.host', 'localhost')
  configFile.set('database.user', 'steelbrain')

  console.log(configFile.get('database.host')) // 'localhost'
  console.log(configFile.get('database.user')) // 'steelbrain'
  console.log(configFile.get('database'))      // { host: 'localhost', user: 'steelbrain' }

  configFile.delete('database.host')
  console.log(configFile.get('database'))      // { user: 'steelbrain' }

  configFile.set('someArray', [1, 2, 3])
  configFile.set('someArray.5', 50)
  console.log(configFile.get('someArray.0')) // 1
  console.log(configFile.get('someArray.1')) // 2
  console.log(configFile.get('someArray.2')) // 3
  console.log(configFile.get('someArray.3')) // undefined
  console.log(configFile.get('someArray.4')) // undefined
  console.log(configFile.get('someArray.5')) // 50
})

LICENSE

This package is licensed under the terms of MIT License. See the LICENSE file for more info.

changelog

4.0.0

  • Remove append and appendSync
  • Remove createIfNonExistent (we now NEVER crash on read if the file does not exist)
  • Rewrote to only re-read the file when it changes on file-system (by using FS watching)

3.0.3

  • Upgrade sb-fs to v3 to remove a lot of unwanted deps, making the package more lightweight

3.0.2

  • Fix a bug where reading would throw if a file did not exist and createIfNonExistent was set to false

3.0.1

  • Fix some critical typos

3.0.0

  • Make functions async by default and add their sync eqv

2.0.0

  • API Breaking: Remove supports for JSON comments during parse because it's known to break some really big JSON files

1.2.1

  • Fix an unknown deployment issue

1.2.0

  • Add noPrettyPrint option

1.1.0

  • Add .delete() method
  • Write default config on non existent instead of empty

1.0.0

  • Initial release