nb-json-db
light weight, REST based , JSON db server.
Use case
Use only when you require simple and lightweight db with rest api and data saving in JSON.
Installation
npm i nb-json-db
Configuration
var nbServer = ;//Setting up directory where json files to be savednbServerrootPath = __dirname; //folder name under the rootPath where json files savednbServerdBFolderName = "data"; //provide allowOrigin to allow requests from specific domain.nbServerallowOrigin = "http://localhost:3434"; //server runs on mentioned portnbServerdBPort = 5654;
Declaring Tables and Fields
/* nbServer.ModelHash[<TableName>] = function(obj) { //Id field is used for unique identification purpose //nbServer.guid() return unique guid for each entry this.Id = obj["Id"] || nbServer.guid(); this.<FieldName> = obj[<FieldName>]; };*/ //In below example, sever will create json file like 'movies.json' under folder mentioned as earlier.nbServerModelHash"movies" = { thisId = obj"Id" || nbServer; thisName = obj"Name"; thisExpectedReleaseDate = obj"ExpectedReleaseDate"; //use timeStamp format to maintain consistency accross db. thisActualReleaseDate = obj"ActualReleaseDate";}; //table 'users' is meant for providing authentication to applicationnbServerModelHash"users" = { thisId = obj"Id" || nbServer; thisUsername = obj"Username"; thisPassword = obj"Password"; thisUserType = obj"UserType"; //ADMIN, USER thisEmailId = obj"EmailId"; ifisNew this_createdTime = ; else this_createdTime = obj"_createdTime"; ifisUpdate this_updatedTime = ; else this_updatedTime = obj"_updatedTime"; };
Setting up Authentication ( Optional )
//Declaring 'Password' field is encrypted. data will be saving as ecrypted formatted//Encrypted Fields will not return to API as they are very secured, can only modified by 'ADMIN' or 'ROOT'//nbServerModelHash"users"EncryptedFields = "Password"; //specifying 'users' table is the authentication table and 'username' and 'password' fields//and 'UserType' field values can be 'ADMIN' and 'USER' and 'ROOT'nbServerLoginFields = "TableName": "users" "Fields": "UserNameField": "Username" "PasswordField": "Password" "UserTypeField": "UserType" ; //able to hit http://localhost:5654 server from browser If 'IsDevelopment' is 'true'//otherwise returns Access Denied ErrornbServerIsDevelopment = true; //Root username and passowrd for DB//details will not save under application data.nbServerRootUserName = "murali";nbServerRootPassword = "murali";
Start server
// Will start server on '5654' port as mentioned earlier and create json files if it running on first timenbServer;
End Usage
open browser and hit "http://localhot:5654" to see list of tables in db.
Login to Applications
see 'Authenctication to DB' section to add first user in 'users' table.
After adding primary/admin user to 'users' table below url is using for authentication for application.
and '/login' will returns browser cookie and tokenObject as response.
All add, update and delete calls will be serving as it is.except for 'users' table and it is declared as 'LoginFields'
NOTE: works only when 'LoginFields' settings are passed.
Url : "/login?username=val&password=val",
Methods : "GET, POST",
Usage : "Authentication for DB",
Returns : {
"Body": {
"tokenObject": "70a6e008926df678dfd8829e1537e1809fc"
}
}
Add, update and delete
use any rest clients like postman...etc., or make traditional ajax requests from your app.
Adding entry to 'movies' table
Method : POST
Url : 'http://localhot:5654/table/movies'
Data : { "Name": "Dead Pool", "ActualReleaseDate": "1456986954475" }
Getting list of 'movies'
Method : GET
Url : 'http://localhot:5654/table/movies'
Updating entry in 'movies' table
Method : POST
Url : 'http://localhot:5654/table/movies'
Data : { "Id": "2df234sdf23fs324ddfds2342fd", "Name": "Dead Pool", "ActualReleaseDate": "1456986954475" }
Deleting entry in 'movies' table
Method : DELETE
Url : 'http://localhot:5654/table/movies?Id=2df234sdf23fs324ddfds2342fd'
Authentication to DB ( Optional - usefull only when 'LoginFields' are passed.)
application used loginToken which will generate by passing root username and root pasword to /authenticate
Url : "/authenticate?username=val&password=val",
Methods : "GET, POST",
Usage : "Authentication for DB",
Returns : {
"Body": {
"tokenObject": "70a6e008926df678dfd8829e1537e1809fc"
}
}
add primary user to 'users' table using below url
"Url" : "/table/users?lToken=70a6e008926df678dfd8829e1537e1809fc",
"Methods" : "POST",
"Data" : {
"Username": "murali",
"Password": "murali",
"UserType": "ADMIN"
}
Note:
Recent changes are fully compatable with older version. Main focus only on Authentication by using token concept and encrypting Password fields.
Upcoming:
-
UI access for DB tables, all CRUD operations can be done on tables from browser itself.
-
Better encryption for password field.