# json-settings

> Basic file-based settings manager that allows you to flush to a file.

Latest version **1.2.0** (published 2016-07-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-settings
pnpm add json-settings
yarn add json-settings
bun add json-settings
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2016-07-13 |
| First published | 2016-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | the_rebel_g0d |
| Maintainers | the_rebel_g0d |

## Links

- npm: https://www.npmjs.com/package/json-settings
- Repository: https://github.com/TheRebelG0d/json-settings
- Homepage: https://github.com/TheRebelG0d/json-settings#readme
- Issues: https://github.com/TheRebelG0d/json-settings/issues
- npm.io page: https://npm.io/package/json-settings

## Dependencies (3)

- [touch](https://npm.io/package/touch.md) ^1.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.13.1
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1

## Recent versions

- 1.2.0 (latest) — 2016-07-13
- 1.1.0 — 2016-06-12
- 1.0.2 — 2016-06-01
- 1.0.1 — 2016-05-31

## README

#Json-Settings
Settings was something I threw together quickly for a project I'm working on. I was unable to quickly find anything that was file-based and allowed for auto flushing/reloading. The key word being quickly, there may be other projects out there much better than this one. But for now, this one works for my needs.

#Usage
```js
var settings = require("json-settings");
var main = new settings("./settings.json");
main.set("oauth", "s02m569hrro2gxxt65w5f5qa2x07d6");
main.get("oauth"); //s02m569hrro2gxxt65w5f5qa2x07d6
```

#API
####Constructor(string, object)
The constructor's signature is `function(string, object)`. The `string` is the **relative** path to the json file. The `object` is an options object, which consists of the following defaults:

* skipReload (`boolean`; default: `false`)
  * Settings won't be reloaded from the constructor. You'll have to reload them manually.
* reloadSync (`boolean`; default: `true`)
  * Settings will call `reloadSync` from the constructor (instead of regular reload)
* cb (`function`)
  * The callback for the async reload call if `reloadSync` and `skipReload` are both false.

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
```

####reload(callback)
Reload is asynchronous. It uses fs.readFile to grab the contents of the settings file and run JSON.parse on it. It then stores the parsed json in the raw_data object.

Reload's signature is `function(callback)` where the callback has the signature of `function(error)`.

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
main.reload(function(err) {
  if (!err) {
    console.error(err);
  } else {
    console.log("Settings reloaded");
  }
});
```

####reloadSync()
reloadSync is a synchronous version of `reload`. No errors are thrown, if there's an exception we reset the raw_data and swallow the exception.

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
main.reloadSync();
console.log("Reloaded!");
```

####save(callback)
save will flush the json encoded raw_data object to the file.

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
main.save(function(err) {
  if (!err) {
    console.log("Settings saved!");
  } else {
    console.error(err);
  }
});
```

####saveSync()
saveSync is the synchronous version of `save`

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
settings.saveSync();
```

####get(string, string)
`get`'s signature is `get(setting_name, default_value)`

`get` will return the value associated with the name provided.

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
console.log(main.get("oauth")); //assuming oauth exists and is set to 's02m569hrro2gxxt65w5f5qa2x07d6', 's02m569hrro2gxxt65w5f5qa2x07d6' will be printed to the console.
```

####set(string, mixed, object)
`set`'s signature is: `set(setting_name, setting_value, options)`

The options are as follows:
* saveSync (`boolean`; default: true)
  * Will save synchronously if true
* save (`boolean`; default: false)
  * If `saveSync` is false and this is true, we'll save asynchronously
* cb (`function`)
  * If `saveSync` is false and `save` is true, we'll set this function as the callback for `save`

####setAll(object, object)
`setAll`'s signature is: `setAll(object_containing_keys_and_values, options`

The `object_containing_keys_and_values` is something along the lines of
```js
{
    oauth: "abcdefghijklmnop",
    secret: "qrstuvwxyz"
}
```
The `options` here are the same from `set` above

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
main.setAll({
    oauth: "abcdefghijklmnop",
    secret: "qrstuvwxyz"
});
```
This will set `oauth` to `abcdefghijklmnop` and `secret` to `qrstuvwxyz` in the flatfile and save according to your `options`.


####getAll(array)
`getAll`'s signature is: `getAll(array_of_keys_to_get)` or `getAll(array_of_objects_to_get)`

The object array looks like:
```js
[{
    key: "setting_name",
    defaultValue: "default setting value"
}]
```

```js
var settings = require("json-settings");
var main = new settings("./settings.json");
console.log(main.getAll(["oauth", "secret", "non_existant_key"])); //{oauth: "abcdefghijklmnop", secret: "qrstuvwxyz", non_existant_key: undefined}
```

---
_Source: https://npm.io/package/json-settings · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
