Socket
Socket
Sign inDemoInstall

the-lopster

Package Overview
Dependencies
7
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    the-lopster

A small library for manipulating json files and using them to save data.


Version published
Weekly downloads
40
decreased by-13.04%
Maintainers
1
Install size
2.12 MB
Created
Weekly downloads
 

Readme

Source

The-Lopster

A small library for manipulating json files and using them to save data.

ES6

import DB from "the-lopster";

CommonJS

const DB = require("the-lopster/dist/cjs").default;

new DB

let db = new DB.DB(src, value);
  • src - is the path to your JSON file. (A mandatory parameter, but the file does not have to exist, but if it exists, it should contain {} as a minimum)

  • value - this is a preset, the value of which will be set in the file, but if the file already contains the data, the preference will be given to the data entered in this parameter (optional parameter)

That is, if in the file:

{
  "age": 25
}

And in the code:

const db = new DB.DB("path/to/file.js", {age: 20});

Then the file will be overwritten:

{
  "age": 20
}

DB.data

This is a field in which the values from the file are stored, through it you can get the values of the file and also change them, but it is not recommended to change them.

DB.set

Through this method, you can change the value of the file. Use exactly this method to change data.

const db = new DB.DB("path/to/file.json", {age: 20});

db.set(callback);

callback - This is a function:

db.set(function () {
	this.age = 25;
})

Or:

db.set((data) => {
	data.age = 25;
})

DB.watch

This function enables synchronization with the file in the group will automatically change this.data if there were any variables in the file. Calling again will disable sync.

:)

Any data changes are automatically written to the file. If the watch function is enabled, when changing the file, the variables from the file will be automatically pulled up.

DB.use

If you need to use other data storage methods, you can use extensions based on the DB.Extension class. At the moment, there is support for such files as JSON (default), INI, YAML, TOML, XML.

To use the extension there is a function .use

Example:

import DB from "the-lopster";

DB.use(DB.XML);

DB.Extension

This is an extension class that accepts three parameters ext, parse, stringify.

  • ext - file extension with a dot at the beginning.
  • parse - a function that takes string and returns an Object
  • stringify - a function that takes Object and returns an string
import DB from "the-lopster";

function parse(value: string): Object {
}

function stringify(value: Object): string {
}

let EXT = new DB.Extension(".ext", parse, stringify);

Using:

DB.use(EXT);

Keywords

FAQs

Last updated on 17 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc