JSONDB
Manage local databases with JSON files and JSONDB Query Language (JQL)
What's that ?
JSONDB is a database manager using JSON files and a custom query language named JQL (JSONDB Query Language).
Features
- Database management with servers, databases and tables
- Secure connections to servers with username and password
- Sync and async operations
- Easy custom query language
- Supported JQL queries:
- select()
- insert()
- replace()
- delete()
- update()
- truncate()
- count()
Getting Started
Full API and documentation will be soon available on the JSONDB website...
Install using npm
JSONDB can be installed through npm:
$ npm install jsondb-js
Instantiate JSONDB
var JSONDB = ;var jdb = ;
Create a server
If you don't have created a server yet, then:
// Syncjdb; // Asyncjdbasync;
It's useful to check if the destination folder doesn't exist before create a server to avoid errors.
// Syncif !jdb // Then... create a server // Asyncjdbasync;
Connect to a server
Once instantiated, you have to connect to a server before send queries.
// Syncvar db = jdb;// or...var db = jdb; // Asyncjdbasync;// or...jdbasync;
- The
server_name
is the name of the folder which represents a server (a folder which contains databases). This folder have to be created withjdb.createServer()
- The
username
and thepassword
are the information used to connect to the server. These information are the same used when creating the server - The
database_name
is the name of the database to use with current connection. This parameter is optional and can be set manually later.
Create a database
After connection to a server, you can create a database:
// Syncdb; // Asyncdbasync;
You can also check if the database exist before the creation.
// Syncif !db // Then... create a database // Asyncdbasync;
Use a database
The database to use can be set using the jdb.connect()
method, or manually using jdb.setDatabase()
method after a connection to a server:
db;
Create a table
Once JSONDB is properly connected to a server and use a database, you can create a table in this database:
// Syncdb; // Asyncdbasync;
The prototype
is an object of column_name
: column_properties
pairs.
Column properties
There is a list of currently supported column properties:
type
: Defines the type of values that the column accepts. Supported types are:int
,integer
,number
decimal
,float
string
char
bool
,boolean
array
default
: Sets the default value of columnmax_length
: Used by some type:- When used with
float
, the number of decimals is reduced to his value - When used with
string
, the number of characters is reduced to his value (starting with the first character)
- When used with
auto_increment
: Defines if a column will be an auto incremented column. When used, the column is automatically set to UNIQUE KEYprimary_key
: Defines if a column is a PRIMARY KEYunique_key
: Defines if a column is an UNIQUE KEY
Send a query
JSONDB can send both direct and prepared queries.
Direct queries
// ---// Sync// ---var results = db; //// Specially for select() and count() queries// You can change the fecth moderesults;// or...results;// Explore results using a while loop (sync)while result = results // Do stuff with result...// Explore results using recursion (async)resultsasync; // ----- // ---// Async// ---dbasync;
Prepared queries
// ---// Sync// ---var query = db;query;query;query;query;query;// Execute query synchronously...var results = query;// Execute query asynchronously...`queryasync; // ----- // ---// Async// ---jdbasync;
JQL (JSONDB Query Language)
The JQL is the query language used in JSONDB. It's a very easy language based on extensions. A JQL query is in this form:
db;
Query Examples
select()
Select all from table users
where username
= id
and password
= pass
or where mail
= id
and password
= pass
var id = JSONDB;var pass = JSONDB;db;
Select username
and mail
from table users
where activated
= true
, order the results by username
with desc
endant method, limit the results to the 10
users after the 5
th.
db;
insert()
Insert a new user in table users
var username = JSONDB;var pass = JSONDB;var mail = JSONDB;db;
Multiple insertion...
db;
replace()
Replace information of the first user
db;
Multiple replacement...
db;
delete()
Delete all users
db;
Delete all banished users
db;
Delete a specific user
db;
update()
Activate all users
db;
Update my information ;-)
db;
truncate()
Reset the table users
db;
count()
Count all banished users
db;
Count all users and group by activated
db;
Query functions
sha1()
Returns the sha1 of a text. Exemple: Update an old password by a new one:
var old_password = ;var new_password = form_datanew;var query = db;query;query;query;
md5()
Returns the md5 of a text. Exemple:
var result = db;
time()
Returns the timestamp.
now()
Returns the date of today in the form year-month-day h:m:s
. You can change the form of the date by using identifiers as parameters:
Identifier | Value |
---|---|
%a | The day in 3 letters (Mon) |
%A | The full day (Monday) |
%d | The day of the month with a leading zero (06) |
%m | The month of the year with a leading zero (12) |
%e | The month of the wear without a leading zero |
%w | The day of the week without a leading zero |
%W | The day of the week with a leading zero |
%b | The month in 3 letters (Jan) |
%B | The full month (January) |
%y | The last two digits of the year (16) |
%Y | The full year (2016) |
%H | The hour with a leading 0 (09) |
%k | The hour without a leading 0 (9) |
%M | The minutes |
%S | The seconds |
Exemple:
db;
lowercase()
Returns the lower case version of a text.
uppercase()
Returns the upper case version of a text.
ucfirst()
Upper case the first letter and lower case all others in a text.
strlen()
Returns the number of characters in a text.
Supported JQL operators
a = b
:a
equal tob
a != b
:a
different thanb
a <> b
:a
different thanb
a >= b
:a
superior or equal tob
a <= b
:a
inferior or equal tob
a < b
:a
inferior tob
a > b
:a
superior tob
a %= b
:a % b === 0
a %! b
:a % b !== 0
Full example
Sync version
var JSONDB = ; var jdb = ; if !jdb jdb; var db = jdb; if !db db; db; if !db db; // A prepared queryvar query = db;query;query;query;query;query;query; // After some insertions... // Select all usersvar results = db; // Fetch with class mappingvar {};Userprototypeid = 0;Userprototypename = '';Userprototypelast_name = '';Userprototypeusername = '';Userprototype { return "The user with ID: " + thisid + "has the name: " + thisname + " " + thislast_name + " and the username " + thisusername + ".";}; while result = results console;
Async version
var JSONDB = ; var jdb = ; // Class used for mappingvar {};Userprototypeid = 0;Userprototypename = '';Userprototypelast_name = '';Userprototypeusername = '';Userprototype { return "The user with ID: " + thisid + " has the name: " + thisname + " " + thislast_name + " and the username " + thisusername + ".";}; jdbasync;
After the execution of (one of) these scripts, the table users will be a .json file which will contain:
Contribution
Found a bug? Have a feature request? Want to contribute to this project? Please, feel free to create a new issue on GitHub, or fork this code, hack it, and make a pull request !
Authors
- Axel Nana: ax.lnana@outlook.com - https://tutorialcenters.tk
Contributors
No one... maybe you !
Copyright
(c) 2016 Centers Technologies. Licensed under GPL-3.0 (read license).