@informath/db.js

1.0.0-beta.0 • Public • Published

Informa-Db.js

All Contributors

Concept

The concept is to interact with storage units (such as Dbs or JSON files) by using javascript proxies.

How to install it:

npm i informa-db.js

How to use it:

Here's a code example on how to use it:

const Db = require('informa-db.js'); // Require the package

(async ()=>{
  const players = await new Db("players.json"); // Uses data.json to store data
  // I implemented these functions but they're boring:
  if (!players.exist(process.env.PLAYER))
    players.add(process.env.PLAYER,{
      inventory: Array(20),
      equipment: Array(5),
      temporaryData: {
        hp: 20,
        xp: 0
      }
    });
  // Instead, use this more elegant way of doing it:
  if(!players.value[process.env.PLAYER])
    players.value[process.env.PLAYER]={
      inventory: Array(20),
      equipment: Array(5),
      temporaryData: {
        hp: 20,
        xp: 0
      }
    };
})()

Before you ask, those all work.

Docs

new LocaleDb( options{path, defaultString} )

options.path

Path to file. Will throw an error if none provided or if type is incorrect

options.defaultString

Default string to write on file if it doesn't exist. Defaults to '{}' Will be ignored if this.isMongo is truthy (See isMongo)

new RemoteDb( options{path, db, collection} )

options.path

URI to mongo db server. Will throw an error if none provided or if type is incorrect Boolean indicating whether the provided path is a file or a mongodb server Defaults to true if the path starts with "mongodb", false otherwise.

Notice: If you need to interact with a mongodb server, you need to install mongodb yourself

options.db

Database name, defaulting to "infodbs"

options.collection

Collection name, defaulting to "db"

BaseDb class

BaseDb.readOnlyValue

File/collection content

BaseDb.value<Proxy>

Proxy to this.readOnlyValue

BaseDb.saveOnChange

If is true, runs this.update() everytime a change is made (this.add(), this.addOverwrite(), this.remove() and changing this.value ). Defaults to false.

BaseDb.exist(index<Number, String>)

Checks if this.readOnlyValue[index] exists

BaseDb.addSafe(index<Number, String>, value)<Boolean, Any>

Defines this.readOnlyValue[index] to value. If this.readOnlyValue[index] already exists, will ignore and return false.

BaseDb.add(index<Number, String>, value)

Defines this.readOnlyValue[index] to value.

BaseDb.remove(index<Number, String>)

Splices out/deletes this.readOnlyValue[index]

BaseDb.update()

Updates the file/db to this.readOnlyValue

Contributors

Thanks goes to these wonderful people (emoji key):


informathemusic

💻

Haouari haithem

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Readme

Keywords

none

Package Sidebar

Install

npm i @informath/db.js

Weekly Downloads

0

Version

1.0.0-beta.0

License

ISC

Unpacked Size

15.9 kB

Total Files

9

Last publish

Collaborators

  • informathemusic