WastefulDB
A little custom made, document-oriented database project made with JavaScript and JSON for convenience.
Quick Setup
const Wasteful = require('wastefuldb');
const db = new Wasteful();
Overall Requirements:
NodeJS v16.15.0 or higher
NPM v8.5.5 or higher
Table of Content
In Depth
new Wasteful({feedback: false, log: false, path: `${__dirname}/info/`, standard: [false], serial: false, kill: false});
- feedback - Sends a confirmation via console when a function is executed successfully. (default:
false
) - log - Catalogs every time a function is executed or an error occurs, including the timestamp at which the event occurred. (default:
false
) - path - Provide a custom path where you wish JSON files to be written/read. (default:
./wastefuldb/data/
) - standard - When standard[0] is true, when a document doesn't exist when using
.find()
, a new document will be created with a default value given in standard[1]. WILL NOT work when serial is false. (default:false
) - serial - Automatically assigns filenames/identifiers based on the size of the set path/directory. (default:
false
) - kill - When set to true, the process will be kill when an error occurs in a try/catch statement. (defualt:
false
)
Specify Directories
Every function is capable of interacting with specific directories outside the "path" constructor option when specified within the {dir} option.
db.find({id: "1234"}, {dir: `${__dirname}/patrons/`});
db.insert({id: "5545", name: "Sherry", pass: "BB822", active: false}, {dir: `${__dirname}/accounts/`});
- dir - A directory's specific address to search, insert, and update in.
.insert()
Insert a file with as many variables as you wish. Always include an "id" variable if serial is set to false as that is what is use to read the JSON document in most cases.
db.insert({id: "1234", name: "seth", pass: "xyz"});
- id - The name of the file and what will be used in the .find() function
.insertBulk()
Insert a file with multiple Objects within an Array.
db.insertBulk( [{id: "5545"}, {first: "Sully", last: "V."}, {password: "password"}] );
.find()
Provides the information of the specified file with the matching identifier.
let info = db.find({id: "1234"});
console.log(info);
- id - The identifier of the file to find and display the information of.
.get()
db.find
, db.get
will read each JSON file within the directory and read each identifier within to locate the specified file.
Unlike db.get({id: "4321", dir: `${__dirname}/data/`}, async(res) => { console.log(await res) });
- id - The internal identifier of a file.
- dir - A specific directory to search in. (optional)
.update()
Searches the given or specified directory for the given ID and updates the given "key" with your "change". Set math to true for simple math. Set the change to "undefined" to delete the specified key. A key which doesn't exist will be added automatically. Not recommended when serialization is enabled.
db.update({id: "1234", key: "id", change: "4321", math: false});
- id - The name/id of the file to update
- key - What key of the file you want to update
- change - What change you want to make to it
- math? - Does the change require (simple) math?
.update() (with child value)
Similar to the standard function, declaring a "child" key will update an object within another object aka nested objects. Everything else is the same.
db.update({id: "5", key: "name", child: "last", change: "A."});
.mupdate(id, [Array of Objects])
.update()
for several changes in a single document.
Update multiple objects within a single document. A good alternative to db.mupdate("5", [ {key: "balance", change: -34, math: true}, {key: "name", child: "middle", change: "A."} ])
- id - The identifier of the file to update
- "Array of Objects" - An array containing several, properly formatted objects to update the specified document.
.collect({id?, key?, value?})
Reads, parses, then pushes information from each JSON file into one collection. Provide an id or key & value to filter results.
let data = db.collect();
data.forEach(info => {
if(info.active == true) {
console.log(info);
} else {
return;
}
})
.replicate(id, {to?, from?, force?})
Create a copy of a specified document from a given directory and place it within the given destination directory. Only one file can exist in a directory at a time.
db.replicate("1234", {to: __dirname, from: `${__dirname}/data/`);
db.replicate("1234", {force: true});
- id - The identifier of the file which is going to be replicated.
- to - What directory to place the replicated file. (default:
constructor.path
) - from - Which directory to find the file which is being replicated. (default:
constructor.path
) - force - Forces the function to replicate the file within the same directory. The file will have a suffix of "_rep". (default:
false
)
.set(id, {data}, {dir});
Overwrite a document completely, replacing the document data with the new data provided. The previous data is temporarily stored in a cache.
db.set("1234", {name: "Seth R. Richardson", balance: 1000, insured: true}, {dir: __dirname});
- id - The identifier of the document to be overwritten.
- data - The new data to be written in the document.
- dir - A directory which contains the document to be updated.
.undo();
Undo the most recent update to a document. The cache holding the most recent change will be cleared if your program session ends.
db.undo();