# reactivefile

> A lightweight library for creating a reactive ⚡️ binding between a JS object and a file.

Latest version **1.2.0** (published 2020-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install reactivefile
pnpm add reactivefile
yarn add reactivefile
bun add reactivefile
```

## 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 | 2020-07-29 |
| First published | 2020-07-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 168.3 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Mateusz Sowiński-Smętny |
| Maintainers | judehunter |
| Keywords | data, reactive, reactive-programming, file, file-system, fs, javascript, typescript, data-structures, typescript-definitions |

## Links

- npm: https://www.npmjs.com/package/reactivefile
- Repository: https://github.com/judehunter/reactivefile
- Homepage: https://github.com/judehunter/reactivefile#readme
- Issues: https://github.com/judehunter/reactivefile/issues
- npm.io page: https://npm.io/package/reactivefile

## Dependencies (3)

- [js-yaml](https://npm.io/package/js-yaml.md) ^3.14.0
- [@iarna/toml](https://npm.io/package/@iarna/toml.md) ^2.2.5
- [fast-xml-parser](https://npm.io/package/fast-xml-parser.md) ^3.17.4

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.2.0 (latest) — 2020-07-29
- 1.1.2 — 2020-07-25
- 1.1.1 — 2020-07-24
- 1.1.0 — 2020-07-24
- 1.0.2 — 2020-07-24
- 1.0.1 — 2020-07-24
- 1.0.0 — 2020-07-24

## README

<h1 align="center">
    <img alt="reactivefile logo" src="https://i.imgur.com/80tlZOU.png" width="150px"/>
	<br>
    ReactiveFile<br>
    <a href="https://www.npmjs.com/package/reactivefile"><img alt="npm-badge" src="https://img.shields.io/npm/v/reactivefile.svg?colorB=32CD32" height="20"></a>
    <a href="https://bundlephobia.com/result?p=reactivefile"><img src='https://img.shields.io/bundlephobia/minzip/reactivefile.svg'/></a>
    <a href="https://github.com/judehunter/reactivefile/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="license-badge" height="20"></a>
</h1>
<h4 align="center">
    A lightweight library for creating a reactive ⚡️ binding between a JS object and a file.<br>
    <br>
</h4>

## Description
ReactiveFile is a library which handles parsing a data file and then auto-saving it reactively whenever you change the object.

Most popular filetypes (`JSON`, `TOML`, `YAML`, `XML`) are handled automatically. The API provides a set of tools that let you easily implement parsers and serializers for other languages.

The reactivity of a ReactiveFile object is deep by default.

Currently only supports `Node.js` (browser implementation is on the way)

----
A simple example of a `JSON` file with a counter:
```jsonc
// data.json
{"counter": 0}
```
You can create a reactive binding with a few lines of code:
```ts
import * as ReactiveFile from 'reactivefile'

const data = await ReactiveFile.load('data.json')
data.$.counter++
```
The counter in `data.json` now has a value of 1.

## Setup
Just run:
```bash
yarn add reactivefile
```
or:
```bash
npm install reactivefile
```

## Usage
### Import the package
```ts
import * as ReactiveFile from 'reactivefile'
// or
const ReactiveFile = require('reactivefile')
```
### Create a reactive binding
```ts
const data = await ReactiveFile.load('data.json', options)
// use .loadSync(...) for synchronous loading instead
```
You can also create a `ReactiveFile` from an existing object and set a destination file
```ts
const data = ReactiveFile.from(response, {saveTo: 'destination.toml'})
```
### Alter your data
```ts
data.$.lastAccess = new Date()
// data.$ is an alias for data.val
```
Adding new keys to your objects requires you to refresh your object's reactivity
```ts
data.$.newKey = 100
data.react()
data.$.newKey = 200
```
### Create parsing and serializing functions for any data type and file extension
The implementation of the built-in `toml` type:
```ts
ReactiveFile.registerType('toml', str => toml.parse(str), obj => toml.stringify(obj))
```
You can also copy these functions from one type to another
```ts
ReactiveFile.assignType('yaml', 'yml')
// creates a yaml type with the same parser and serializer as yml
```
These types are also the file extensions — files with the `json` extension will be automatically handled by the `json` parser. You can override this behaviour by specifying the data type.
```ts
const data = await ReactiveFile.load('data.json', {type: 'toml'})
// reads toml from the data.json file (and saves in toml as well)
```
----
### Options
#### encoding
The encoding of the file. One of: `'ascii', 'base64', 'binary', 'hex', 'latin1', 'ucs-2', 'ucs2', 'utf-8', 'utf16le', 'utf8'`.
#### type
The type/language of the file. Defaults to the file's extension.
#### saveTo
The destination file path. If set, saves your data to another file instead. Otherwise overwrites the original file.
#### reactive
Turns on/off the reactive binding. The object can be then saved with `.save()` or `.saveSync()` instead. Defaults to `true`.
#### deep
Turns on/off deep reactivity. Defaults to `true`.
#### asyncSave
Turns on/off asynchronous saving (instead of synchronous). Defaults to `true`.

----
### License
MIT

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