/pseudodb

A data storage system inspired from firestore (firebase), that stores data in json file. Intended for front-end testing

Primary LanguageJavaScriptGNU General Public License v3.0GPL-3.0

PseudoDB

Lite & fast DB simulator for testing.

var pdb = require('pseudodb');

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js.

If this is a brand new project, make sure to create a package.json first with the npm init command.

Installation is done using the npm install command:

$ npm install pseudodb

Features

  • Makes code testing easier
  • Mimics Firestore
  • Now supports graphDB properties

Functions

To create a new DB:

pdb.createDb(db_name);

To add a new document with a random id:

var id = pdb.addDoc(db_name, data_Object); //Returns the randomly created id

To create a new document with a specific id or to modify an exisitng document

pdb.setDoc(db_name, id, data_Object);

To change a particular field of a document

pdb.setField(db_name, id, field, value);

To get details of a particular document

var result = pdb.getDoc(db_name, id);

To get details of a particular db

var result = pdb.getDb(db_name);

To find a particular document in a db

var id = pdb.findDoc(db_name, field_name, value); // FInds the first occuring match and returns the id

To find documents matching a criteria in a db

var id = pdb.findAll(db_name, field_name, value); // Finds all occuring matches and returns an array of ids


To add a relation to a doc with another doc

pdb.setRel(db, from_doc, relation, to_doc); // Sets relation from doc with id a to doc with id b

To add a relation group to a doc with another doc

pdb.setRelGrp(db, from_doc, relation, to_doc); // Adds a  relation from a to b in the relation group

To get a relation of a doc

console.log(relations.getRel(db, doc, relation)); // Logs the relation of the doc

To delete a doc

pdb.deleteDoc(db_name, doc_id);

To delete a db

pdb.deleteDb(db_name);

To Monitor Changes in a db

subscribe(db).on('dbChange', callBack(currentDbContent, previousDbContent));

Example Code

const pdb = require('pseudodb');

pdb.createDb('test'); //Create a db with name test

pdb.addDoc('test', {
	name: 'Elliot',
	age: 18,
}); /*Add a new document with a random id.
Take a look at the new file named test to see the added document
*/

pdb.setDoc('test', 'a', { name: 'Alwin', age: 23 }); //Creates a doc with the id 'a' and stores the object

pdb.setDoc('test', 'a', { name: 'Alwin', age: 20 }); //Changes the document with the id a

pdb.setField('test', 'a', 'name', 'Rex'); //Changes the field name of the document with id a

pdb.setField('test', 'a', 'Developer', 'No'); //Adds a new field Developer

pdb.setRel('test', 'a', 'brother', 'b'); // Sets brother relation from doc with id a to doc with id b

console.log(pdb.getDoc('test', 'a')); // Logs the content of the doc with id a to the console

console.log(pdb.getDb('test')); // Logs the content of the db to the console

console.log(pdb.getDoc('test', 'a').name); //Since the getdoc function returns an object, you can access the fields this way

console.log(pdb.findDoc('test', 'name', 'Alwin')); //Logs the id of the document which has the field 'name' as 'Alwin'

console.log(pdb.findAll('test', 'age', 20)); //Logs an array of ids of all docs which has age as 20

subscribe('test').on('dbChange', (curr, prev) =>
	console.log('Current: ', curr, ' Previous: ', prev)
); // Monitors the db and Logs current and previous content of the db when db is changed

console.log(relations.getRel('test', 'a', 'brother')); // Logs the brother relation of the doc a

pdb.deleteDoc('test', 'a'); // Deletes the doc with id a

pdb.deleteDb('test'); //Deletes the db