@mangar2/persist
Version:
Persists an object in a file
94 lines (63 loc) • 2.73 kB
Markdown
<!-- 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` | |