Brief introduction:
JSONDB is a class library of JS, which is a data model based on JSON data format, which is implemented to modify JSON data. The JSONDB is built from an application requirement of HTML5 local storage, which can be used to add and revise JSON data through SQL, and the library provides the basic functions of the database such as where retrieval condition, data sorting, limit query condition restriction and so on. With Jsondb, you can easily maintain a library/table or multiple libraries/tables, without the need for additional JSON data maintenance, to simplify SQL operations after the class library is perfected, and to extend the coherent operation model based on the JSONDB core module, simplifying the operation of JSONDB and the probability of an error in the SQL statement.
First, jsondb core module function
1. JSONDB ([[Data],dbname])
Overview:
To create a JSONDB DB model instance
Parameters:
Data (optional)
JSON Library/Table data
DbName (optional)
Database/table name, default = json_db
return value: Jsondb
2. Init ([alias])
Overview:
Initializing the JSONDB database model, you can use alias to set an alias for the Jsondb () method, which facilitates data manipulation.
Parameters:
Alias (optional)
Set an alias for the Jsondb () method via Alias
return value: Jsondb
3. Query (SQL)
Overview:
Executes an SQL statement that supports SELECT, delete, update operations, where select supports the selection of the specified field, from, where, order by, limit words, and delete supports the From, where, limit words, Update supports the From, set, where, limit words, which implements the basic SQL statement operations of the database. The query () method does not support INSERT statements, as the reason for JSON data format is that the Insert method is used to implement an efficient and feasible insert operation method alone.
Parameters:
SQL (required)
The SQL statement to execute must be one of the Select, delete, and update.
return value:
The SELECT statement returns the results of the query, and the delete and UPDATE statements return the number of bars that perform the impact operation
4. Insert (Data[,dbname])
Overview:
Insert a piece of data into the end of the data table or insert a data table into the database
Parameters:
Data (required)
The data to insert
DbName (optional)
The name of the database or data table, which defaults to json_db
return value: Jsondb
5. FINDALL ([DbName])
Overview:
Fast access to all data in the specified database/table, mainly used for data processing after completion, such as deleting the specified data after the call method to get all the deleted data and stored in the local storage, of course, different applications may implement different functions.
Parameters:
DbName (optional)
The database or data table name. Default is json_db
Return value: Specifies the data for the database or data table
First, JSONDB basic application Example
//create a User data table and define a DB jsondb alias var data = [{username: ' Zhang San ', Sex: ' Male ', birthday:{year:2000,month:6,day:18}},{username: ' Li Hong ', Sex: ' Female ', Birthday:{year:1986,month:9,day:22}}];//The following methods can be used to obtain aliases in two ways, one is obtained by the Init method (recommended), and the other is to get the return value of the JSONDB () method//In subsequent examples will use Init ( ) method to create the alias operation data var JDB = jsondb (data, ' user '). Init (' DB ');//Insert a new data = {username: ' Li think ', Sex: ' Male ', birthday:{year:1990, MONTH:2,DAY:15}};D B.insert (data, ' user ');//Query the gender of the name Li Hong, where condition must plus () No person will appear error var result =db.query (' Select sex from user Where (username= "Li Hong");//Result: [{"Sex": "Female"}]//query 2000 years ago and sorted by birth year var result =db.query (' select * from user where ( birthday.year<2000) Order by birthday.year ASC ');//Result: [{"username": "Li Hong", "sex": "female", "birthday": {"Year": 1986, " Month ": 9," Day ": 22}},{" username ":" Li Want "," sex ":" Male "," birthday ": {" Year ": 1990," Month ": 2," Day ": []}]
Query for the minimum age of two people var result =db.query (' SELECT * from user order by birthday.year desc limit 2 ');//Query result: [{"username": "Zhang San", "Sex" : "Male", "birthday": {"Year": $, "month": 6, "Day": 18}},{"username": "Li Want", "sex": "Male", "birthday": {"Year": 1990, "Month": 2 , "Day": 15}}]//modified Li Hong's birth date var result =db.query (' Update user set birthday.year=1991 where (username= "Li Hong") limit 1 ');// The number of affected bars is one, you can get all the data through Db.findall (' user ') to see if it has been modified successfully//delete the data named Li think var result =db.query (' Delete from user where (username= "Li think" Limit 1 ');//affect the number of bars is one, you can get all the data through Db.findall (' user ') to see if it was deleted successfully//database all data//[{"username": "Zhang San", "Sex": "Male", "birthday": {" Year ": $," month ": 6," Day ": 18}},{" username ":" Li Hong "," sex ":" female "," birthday ": {" Year ": 1991," month ": 9," Day ": 22}}]
Third, JSONDB expansion
Brief introduction:
Based on the JSONDB core module, the database coherent operation model is written to simplify the writing of SQL statements, data manipulation, and so on, which realizes jsondb more powerful operation mode and function in the extended model.
1. Table (table)
Overview:
JSONDB query default table name after definition
Parameters:
Table (required)
The default table name that is jsondb to be manipulated after it is defined does not affect the default table name of the SQL statement, only affects all subsequent consecutive operations.
return value: Jsondb
2. Field (Fileds)
Overview:
Define the fields returned by the query
Parameters:
Fields (required)
can accept arrays or strings separated by a number
return value: Jsondb
3. where (where)
Overview:
Define a query condition that supports all if acceptable conditions, a double equal sign can be written as a single equal sign, and more concise than an SQL statement without having to write a pair of parentheses at both ends of the where condition.
Parameters:
where (required)
Query criteria
return value: Jsondb
4. Order (Order)
Overview:
Define sorting methods, currently only support single field sorting
Parameters:
Order (required)
Defining sorting methods
return value: Jsondb
5. Limit (limit)
Overview:
Limit the number of query bars or ranges
Parameters:
Limit (required)
Limit the number of query bars or ranges, a single number limit the number of query bars, separated by commas two numbers to represent the scope of the query.
return value: Jsondb
6. Add (data)
Overview:
You can insert data into the specified data table in conjunction with the table () method, which is inserted into the JSON_DB data table by default if the table () method was not previously set.
Parameters:
Data (required)
The data to insert into the data table or database.
return value: Jsondb
7. Select ()
Overview:
You can match a defined field (), table (), where (), order (), limit () method to a realistic complex query
Parameters: None
return value: Result
8. Update (data)
Overview:
You can modify the specified data with the table (), where (), limit () methods
Parameters:
Data (required)
Set sentence string for UPDATE statement
return value:
Operation affects number of bars
9. Delete ()
Overview:
The specified data can be deleted with the table (), where, limit () method
Parameters: None
return value:
The number of bars affected by the operation.
Drop ()
Overview:
To delete a specified database or table
Parameters: None
return value: Jsondb
Iv. Advanced Applications of JSONDB
Insert a data db.table (' user ') into the data table. Add ({username: ' Wang Shuai ', Sex: ' Male ', birthday:{year:1995,month:10,day:23}});//Query everyone's date of birth, and sorted by year of birth, because previously defined as the User table using table (' user '), it can be omitted, the table method definition is always valid unless the var result = Db.field (' username,birthday ') is reset. Order (' Birthday.year '). Select ();//query result [{"username": "Li Hong", "birthday": {"Year": 1991, "month": 9, "Day": 22}},{" Username ":" Wang Shuai "," Birthday ": {" Year ": 1995," month ": Ten," Day ": 23}},{" username ":" Zhang San "," Birthday ": {" Year ": $," month " : 6, "Day": 18}}]//first insert some data to facilitate the subsequent retrieval operation db.table (' user '). Add ({username: ' Li Hen ', Sex: ' Male ', birthday:{year:2008,month:8,day:8 }}). Add ({username: ' Zhang Qi ', Sex: ' Male ', birthday:{year:1990,month:10,day:23}}). Add ({username: ' Li Yuafang ', Sex: ' Female ', birthday:{ YEAR:1985,MONTH:2,DAY:28}). Add ({username: ' Ligougo ', Sex: ' Female ', birthday:{year:2002,month:3,day:15}}). Add ({username: ' Zhang Yuan ', sex: ' Male ', Birthday:{year:2005,month:12,day:5}}). Add ({username: ' Wang Lina ', Sex: ' Female ', birthday:{year:1992,month:5,day : 12});//fuzzy search for advanced queries//query All Li members (regular lookup method) var result = Db.field (["username"]). WHERE (' Username.match (/^ Lee/) '). Select ();// Query result [{"username": "Li Hong"},{"username": "Li Hen"},{"username": "Li Yuafang"},{"username": "Ligougo"}]//query all Li members (string search method) var result = Db.field (["username"]). Where (' Username.indexof ("li") =0 '). Select ();//query result [{"username": "Li Hong"},{"username": "Li Hen"},{"username": "Li Yuafang"},{" Username ":" Ligougo "}]//query all people born from 1990 to 1995 var result = Db.field ([" username "," Birthday "]). WHERE (' birthday.year>= 1990 and birthday.year<=1995 '). Order (' Birthday.year '). Select ();//Query result: [{"username": "Zhang Qi", "Birthday": {"Year" : 1990, "month": Ten, "Day": 23}},{"username": "Li Hong", "birthday": {"Year": 1991, "month": 9, "Day": 22}},{"username": "Wang Lina", " Birthday ": {" Year ": 1992, ' Month ': 5, ' Day ': 12}},{" username ":" Wang Shuai "," Birthday ": {" Year ": 1995," month ": Ten," Day ": 23}]
source code and sample download: Jsondb.rar