1.0.5 • Published 3 years ago

@mangar2/persist v1.0.5

Weekly downloads
6
License
LGPL-3.0-or-later
Repository
github
Last release
3 years ago

Abstract

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

Contents

Meta

Filepersist.js
Abstractpersists JSON objects to a file . Keeps several files and loads older files if the latest is broken
AuthorVolker Böhm
CopyrightCopyright ( c ) 2020 Volker Böhm
LicenseThis 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

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

NameTypeDescription
configurationObjectconfiguration options

configuration properties

NameTypeDescription
keepFilesnumberamount of file versions to keep ( including the recently written file )

Persist Methods

readData

readData (directory, filenameBasis) => {Object}

Reads data from a file

readData Parameters
NameTypeDescription
directorystringdirectory to delete file
filenameBasisstringbasis filename of the file . The
readData returns
TypeDescription
Objectthe 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
NameTypeDescription
directorystringdirectory to delete file
filenameBasisstringbasis filename of the file . The
objectToSaveObjectobject to save as JSON
saveObjectToFile returns
TypeDescription
undefined