CrystalDB has updated. Please make sure you are running the newest version of CrystalDB (v1.2.7), for best preformance, and more functions, by running npm update This update was huge, and it is IMPORTANT that you check the change log, to fix any issues in your code. https://github.com/ErikMCM/CrystalDB/blob/master/CHANGELOG.md

CrystalDB Documentation

For CrystalDB v1.2.7





Getting Started

CrystalDB is a Node.js module. This module is a NoSQL, JSON Database System. This system works similarly to other JSON Document Systems, but is boiled down, for simpler projects, where large database systems are not required. This system does all of the computing, and storing in the module, so all you have to do, is simply pass through, the file ID, and the data.




How to Install CrystalDB


Installing through the Command Prompt/Terminal.

You'll want to make sure you have Node.js installed, to install it, go to the Node.js Downloads Page.

CrystalDB's node module is "crystaldb". If you have installed node modules before, feel free to do it on your own, and skip ahead. If you have not, please watch the YouTube video below.





Library Reference

Initializing

Initializing the Database will create all directories required. It is a good idea to keep it inside your code, incase a directory gets deleted, and needs to be restored, or if the function gets updated in a future update.

                                    const db = require("crystaldb");

                                    db.initialize();
                                

Writing

When you write data, you will need to pass through 2 arguments, the file ID, and the JSON Data. The file ID can be anything, from "foo" to "1036". The max size is 140 characters, and minimum of 1. In order to create a new file, simply write to it, as if it existed, it will automatically create it.

                                    const db = require("crystaldb");
                                    db.initialize();

                                    const jsonData = `{"foo":"bar"}`;
                                    db.write("file-ID", jsonData)
                                    .then(respose => console.log("Data has been written! " + respose))
                                    .catch(e => console.log(e))
                                

Getting

Getting data is a promise. When you get data, you will need to pass through 1 argument, the file ID. There is an option to add an object to return, instead of the whole array. If there is an error, such as the file not existing, or variable not existing, the promise will be rejected. The data returned is parsed JSON, or if you added the object option, the object.

                                    const db = require("crystaldb");
                                    db.initialize();
                                    
                                    //Getting the file
                                    db.get("file-ID")
                                    .then(fileData =>{
                                        console.log(fileData); //Logs a parsed JSON array (like v1.2.6 and earlier) since in our JSON we had {"foo":"bar"}
                                    }).catch(e => console.log(e));

                                    //Getting an object in the file
                                    db.get("file-ID", {object: "foo"})
                                    .then(data =>{
                                        console.log(data); //Logs "bar" since in our JSON we had {"foo":"bar"}
                                    }).catch(e => console.log(e));
                                

Grabbing

Grabbing data is not a promise. When you grab data, you will need to pass through 1 argument, the file ID. There is an option to add an object to return, instead of the whole array. If there is an error, such as the file not existing, or variable not existing, the promise will be rejected. The data returned is parsed JSON, or if you added the object option, the object, or if there was an error, an error.

                                                            const db = require("crystaldb");
                                                            db.initialize();
                                                            
                                                            //Grabbing the file
                                                            let fileData = db.grab("file-ID");
                                                            console.log(fileData); //Logs a parsed JSON array since in our JSON we had {"foo":"bar"}

                                                            //Grabbing an object in the file
                                                            let data = db.grab("fileID", {object: "foo"})
                                                            console.log(data) //Logs "bar" since in our JSON we had {"foo":"bar"}
                                                        

Existance

Checking the existance is not a promise, which allows you to use it in If statements easily. You have 1 argument you need to pass through, and one that is optional. The first one is the file id, if the file exists, it will return true, the optional second one is the object in the file. If the file doesn't exist, then it'll return false. If the object is not existant in the file, it'll also return false.

                                                                                    const db = require("crystaldb");
                                                                                    db.initialize();
                                                                                    
                                                                                    //Checking existance of a file
                                                                                    let fileExists = db.exists("fileID")
                                                                                    console.log(fileExists) //Logs true if the file exists, otherwise, it's false

                                                                                    //Checking existance of a object in a file
                                                                                    let variableExists = db.exists("fileID", {object: "foo"})
                                                                                    console.log(variableExists) //Logs true if the file AND variable exists, otherwise, it's false
                                                                                

Deleting

Deleting data will delete any files with the file ID passed through, or an object in the file. This action is irreversible, and can not be undone If you only put in the file ID, it will delete the file. If you put in the file ID, and the optional object, it will delete the object. There will be an error if something is using the file, or the file is locked.

                                    const db = require("crystaldb");
                                    db.initialize();
                                    
                                    //Deleting a file
                                    db.delete("fileID")
                                    .then(result => console.log(result)) //Deletes the file, then logs the success message
                                    .catch(e => console.log(e))
                                    
                                    //Deleting an object in a file
                                    db.delete("fileID", {object: "foo"})
                                    .then(result => console.log(result)) //Deletes the variable in the file, then logs the success message
                                    .catch(e => console.log(e))
                                



Copyright, License and Links

©ErikMCM 2020

CrystalDB code is released under the MIT License.

GitHub NPM