shridb
The .json file & promise based javascript database for nodejs, nwjs, electron etc...
- Create & Interact with each json file as a table.
- Make essential queries required for database operation
- Also make queries on array level instead of table
- Backup & Restore Database
- lodash, file-extra based persistent database
🙏 Help, Support, Encourage
Please, support shridb development by sending money: Donate
Installation
npm install shridb
Usage
var Shridb = require('shridb');
var db = databaseName; // Creates a folder with `databaseName` in project directory, // if `databaseName` is Empty then default folder named 'Shridb' will be created.// Examplevar db = nwAppdataPath + '/yourDBName'; // node webkit or nwjsvar db = path; // node webkit or nwjs with path.join method (requires path module);
Arguments:
databaseName(String): The name of database or path to database.
Methods:
Let's consider our table or data(array) named 'products' contains the following collection for performing queries.
"id": 1 "type": "donut" "name": "Cake" "batters": "batter": "id": 1001 "type": "Regular" "id": 1002 "type": "Chocolate" "id": 1003 "type": "Blueberry" "id": 1004 "type": "Devil's Food" "topping": "id": 5001 "type": "None" "price": 0 "id": 5002 "type": "Glazed" "price": 85 "id": 5005 "type": "Sugar" "price": 60 "id": 5007 "type": "Powdered Sugar" "price": 48 "id": 5006 "type": "Chocolate with Sprinkles" "price": 75 "id": 5003 "type": "Chocolate" "price": 50 "id": 5004 "type": "Maple" "price": 14 "price": 470 "id": 2 "type": "meal" "name": "Pizza" "size": "small": "info":"A Small Pizza" "offer":"Single with coke" "medium": "info":"A Medium Pizza" "offer":"Combo with krushers" "large":"info":"A Large Pizza" "offer":"All in one" "topping": "id": 7001 "type": "Pepperoni" "price": 46 "id": 7002 "type": "Mushrooms" "price": 52 "id": 7005 "type": "Onions" "price": 60 "id": 7007 "type": "Bacon" "price": 90 "id": 7006 "type": "Green peppers" "price": 35 "id": 7003 "type": "Spinach" "price": 29 "id": 7004 "type": "Sousage" "price": 12 "price": 795
Note: data(consider above one) defined in this documentation is supposed to be in array format.
db.getTable(tableName)
- Creates/adds table named tableName.json to database, returns its data if already exists or create one.
db; /* We defined our collection as a table here, No need to add a table, just call this function and it will create a separate file with .json extension, if the file does not exist then it will be created(containing [] empty array) or if exists then Gets table data from the database and supplies the result. */ // Example db;
Arguments:
- table (String): The name of table or collection to add/retrieve data.
db.saveTable(table, tableData)
- Save data to table
// Exampledb;
See Also: db.insert() method alternative to push method without calling db.saveTable() everytime
Arguments:
- table (String): The name of table.
- tableData (Array): Data to save to table (overwrites whole data with tableData)
db.insert(table, object/arrayOfObjects)
- Insert single object or array of objects to table
db; // Example: 1 - Insert Single Objectdb; // Example: 2 - Insert Array of objectsdb; // Example: 3 - Using getMax() to insert incremental id'sdb;
Arguments:
- table (String): The name of table.
- object/arrayOfObjects: Single object- {} or array of objects [{..}, {..}]
db.getMax(table/array, property)
- Returns maximum value of property from table/array.
db; // Example// query to tabledb; // query to arraydb;
Arguments:
- table (String/array): The name of table or an array
- property (data): property of which to get maximum value
db.getMin(table/array, property)
- Returns minimum value of property from table/array.
db; // Example// query to tabledb; // query to array instead of tabledb;
Arguments:
- table (String/array): The name of table or an array
- property (data): property of which to get minimum value
db.updateOne(table/array, oldObj, newObj)
- Update the old object(first occurance) in table/array with new object(or only specific property in new object). Note: if table is defined, then it directly updates the table in database(no need to call saveTable) and if array is defined, then it returns updated array.
db; // Example// query to tabledb; // query to array db;
Arguments:
- table (String/array): The name of table or an array,
- oldObj(object): old object exists in table,
- newObj(object): new object to replace old object
db.updateAll(table/array, oldObj, newObj)
- Update all records(all occurances) of old object in table/array with new object(or only specific property in new object). Note: if table is defined, then it directly updates the table in database(no need to call saveTable) and if array is defined, then it returns updated array.
db;// Example// query to tabledb; // query to array db;
Arguments:
- table(String/array): The name of table or an array
- oldObj(object): old object that exists in table
- newObj(object): new object to replace old object
db.findOne(table/array, obj)
- Returns first record(first occurrence) from table/array that matches to
obj
.
db;// Example: 1db; // Example: 2db; // Example: 3 // find key of objectdb;
Arguments:
- table(String/array): The name of table or an array,
- obj(object): object to find one record
db.findAll(table/array, obj)
- Returns array of records from table/array that matches to obj.
db; // Example: 1db; // Example: 2db;
Arguments:
- table(String/array): The name of table or an array,
- obj(object): object to find all records
db.findIndex(table/array, obj)
- Returns the index of first record(first occurrence) from table/array that matches to
obj
.
db; // Example: 1db; // Example: 2db; // Example: 3db;
Arguments:
- table(String/array): The name of table or an array,
- obj(object): object to find index
db.findLast(table/array, obj)
- Returns last record(last occurrence) from table/array that matches to
obj
.
db;
Arguments:
- table(String/array): The name of table or an array,
- obj(object): object to find last record
db.findLastIndex(table/array, obj)
- Returns the index of last record(last occurance) from table/array that matches to obj.
db;
Arguments:
- table(String/array): The name of table or an array,
- obj(object): object to find last index
db.removeOne(table/array, obj)
- Removes first record(first occurance) from table/array that matches to
obj
.
db; // Exampledb;
Arguments:
- table(String/array): The name of table or an array,
- obj(object): object to remove
db.map(tableName, property)
- Extracts property value from each object, returns array.
db; // Example 1:db; // Example 2:db;
Arguments:
- table(String/array): The name of table or an array,
- property(String): property to extract from each object
db.removeTable(tableName)
- Remove/Delete table from database
db; // Exampledb;
Arguments:
- table(String): The name of table to remove
db.cleanTable(table/array/object)
- Removes all data in table or removes all data in array/object, returns empty array/object
db; // Example: 1 - Cleans tabledb; //Example: 2 - Cleans Arraydb; //Example: 3 - Cleans Objectdb;
Arguments:
- table(String/array/object): The name of table or array/object
db.fetchJson(path)
- Reads and returns data of defined path having
.json
extension
db; // Exampledb;
Arguments:
- path(String): path/to/file.json
db.doSum(table/array, property)
- Returns the sum of defined property in array
db; // Example: 1db; // Example: 2db;
Arguments:
- table(String/array): The name of table or an array,
- property(String): property of which to get sum value
db.groupBy(table/array, value)
- Returns grouped data by value
db; // Exampledb;
Arguments:
- table(String/array): The name of table or an array
- value(String): Value to get data by group
db.orderBy(table, sortPropety, orderBy)
- Returns sorted/ordered data by value
db; // Example 1:db; // Example 2:db;
Arguments:
- table(String/array): The name of table or an array
- sortProperty(String/array): Single property or array of property to sort
- orderBy(String/array): orderBy-['asc', 'desc'], (asc = ascending, desc = descending)
db.unique(table, property)
- Returns the new duplicate free array
db; // Example:db;
Arguments:
- table(String/array): The name of table or an array
- property(String): property value to get unique data
db.pushBefore(table, object, newObject)
- Push new object before existed object, returns array with pushed object
db; // Example 1:db; // Example 2:db;
Arguments:
- table(String/array): The name of table or an array
- object(object): The object exist in table
- newObject(object): New object to push before existed object
db.pushAfter(table, object, newObject)
- Push new object after existed object, returns array with pushed object
db; // Example 1:db; // Example 2:db;
Arguments:
- table(String/array): The name of table or an array
- object(object): The object exist in table
- newObject(object): New object to push after existed object
db.backupDatabase(destinationPath, folderName)
- Backup database to destination path by creating folder -
folderName
where you can store backup
db; // Exampledb;
Arguments:
- destinationPath(String): path/to/backup/directory,
- folderName(String): Folder name where you can store backup
db.restoreDatabase(sourcePath)
- Restore database from source path to your database path
db; // Exampledb;
Arguments:
- sourcePath(String): path/from/backup/directory
db.dateRangeFilter(table, dateProperty, startDate, endDate)
- Returns filtered array data between start date and end date
db // Examplevar employee = "id": 1 "name": "Aarav" "joinDate": "2019-07-14T12:00:00-06:30" "id": 2 "name": "Govind" "joinDate": "2019-06-10T12:00:00-06:30" "id": 3 "name": "Bhumkar" "joinDate": "2020-01-01T12:00:00-06:30" "id": 4 "name": "Om" "joinDate": "2020-01-02T12:00:00-06:30" "id": 5 "name": "Shri" "joinDate": "2019-12-25T12:00:00-06:30"; db;
Arguments:
- table(String/array): The name of table or an array
- dateProperty(String): Date property to filter
- startDate(Date): Start date
- endDate(Date): End Date
Utilities
db.utils.trimString(text)
- Removes leading, between, trailing whitespace
// Example 1var myName = dbutils;console; // Govind Bhumkar // Example 2var myName = dbutils;console; // Govind Sham Bhumkar // Example 3var myName = dbutils;console; // Govind Sham Bhumkar
Arguments:
- text(String): text to trim