# @mangar2/persist

> Persists an object in a file

Latest version **1.0.5** (published 2020-12-10) · LGPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @mangar2/persist
pnpm add @mangar2/persist
yarn add @mangar2/persist
bun add @mangar2/persist
```

## 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.0.5 |
| Published | 2020-12-10 |
| First published | 2019-09-22 |
| Weekly downloads | 0 |
| License | LGPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Volker Böhm |
| Maintainers | mangar2 |
| Keywords | File, Persist |

## Links

- npm: https://www.npmjs.com/package/@mangar2/persist
- Repository: https://github.com/Mangar2/yaha/tree/master/node_modules/@mangar2/persist
- Homepage: https://github.com/Mangar2/yaha.git
- npm.io page: https://npm.io/package/@mangar2/persist

## Dependencies (2)

- [fs](https://npm.io/package/fs.md) 0.0.1-security
- [@mangar2/errorlog](https://npm.io/package/@mangar2/errorlog.md) latest

## 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.0.5 (latest) — 2020-12-10
- 1.0.4 — 2020-01-14
- 1.0.3 — 2019-10-15
- 1.0.2 — 2019-09-22
- 1.0.1 — 2019-09-22
- 1.0.0 — 2019-09-22

## README

<!-- This file is generated by jsmddoc version 0.1 -->

# Abstract

persists JSON objects to a file . Keeps several files and loads older files if the latest is broken

## Contents

- [Meta](#Meta)
- [Class Persist](#Class-Persist)
  - [Parameters](#Persist-Parameters)
  - [Methods](#Persist-Methods)
    - [readData](#readData)
    - [saveObjectToFile](#saveObjectToFile)

## Meta

| | |
| --- | --- |
| **File** | persist.js |
| **Abstract** | persists JSON objects to a file . Keeps several files and loads older files if the latest is broken |
| **Author** | Volker Böhm |
| **Copyright** | Copyright ( c ) 2020 Volker Böhm |
| **License** | This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3 . It is furnished "as is" , without any support , and with no warranty , express or implied , as to its usefulness for any purpose . |

## Class Persist

`new Persist(configuration)`

Creates a new persistance support class

### Example

```javascript
const persist = new Persist( { keepFiles: 5 })
persist.saveObjectToFile('.', 'helloworld.json', { message: 'hello world' } )
const dataRead = persist.readData('.', 'helloworld.json')
// prints 'hello world'
console.log(dataRead.message)
```

### Persist Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `configuration` | `Object` | configuration options | |

#### configuration properties

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `keepFiles` | `number` | amount of file versions to keep ( including the recently written file ) | |

### Persist Methods

#### readData

`readData (directory, filenameBasis) => {Object}`

Reads data from a file

##### readData Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `directory` | `string` | directory to delete file | |
| `filenameBasis` | `string` | basis filename of the file . The | |

##### readData returns

| Type | Description |
| ---- | ----------- |
| `Object` | the object read . |

#### saveObjectToFile

`async saveObjectToFile (directory, filenameBasis, objectToSave) => {undefined}`

Stringifies a JSON and writes it to a file . It will automatically add a timestamp to the provided "base" filename It does not throws errors , but logs write errors to the console

##### saveObjectToFile Parameters

| Name | Type | Description |
| ---------- | ------------ | ----------------- |
| `directory` | `string` | directory to delete file | |
| `filenameBasis` | `string` | basis filename of the file . The | |
| `objectToSave` | `Object` | object to save as JSON | |

##### saveObjectToFile returns

| Type | Description |
| ---- | ----------- |
| `undefined` |  |

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