Informa-Db.js
Concept
The concept is to interact with storage units (such as Dbs or JSON files) by defining variables.
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');
(async ()=>{
const players = await new Db("players.json");
if (!players.exist(process.env.PLAYER))
players.add(process.env.PLAYER,{
inventory: Array(20),
equipment: Array(5),
temporaryData: {
hp: 20,
xp: 0
}
});
if(!players.value[process.env.PLAYER])
players[process.env.PLAYER]={
inventory: Array(20),
equipment: Array(5),
temporaryData: {
hp: 20,
xp: 0
}
};
})()
Before you ask, those all work.
Docs
new Db( path, defaultString, isMongo, db, collection )
path
Path to file or URI to mongodb server.
Will throw an error if none provided or if type is incorrect
defaultString
Default string to write on file if it doesn't exist.
Defaults to '{}'
Will be ignored if this.isMongo is truthy (See isMongo)
isMongo
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
db
Database name, defaulting to "infodbs"
collection
Collection name, defaulting to "db"
Db class
Db.readOnlyValue
File/collection content
Db.value<Proxy>
Proxy to this.readOnlyValue
Db.saveOnChange
If is true, runs this.update() everytime a change is made (this.add(), this.addOverwrite(), this.remove() and changing this.value )
Db.exist(index<Number, String>)
Checks if this.readOnlyValue[index] exists
Db.add(index<Number, String>, value)<Boolean, Any>
Defines this.readOnlyValue[index] to value.
If this.readOnlyValue[index] already exists, will ignore and return false.
Db.addOverwrite(index<Number, String>, value)
Defines this.readOnlyValue[index] to value.
Db.remove(index<Number, String>)
Splices out/deletes this.readOnlyValue[index]
Db.update()
Updates the file/db to this.readOnlyValue