Home >Web Front-end >JS Tutorial >javascript lib_javascript techniques to operate json like a database

javascript lib_javascript techniques to operate json like a database

WBOY
WBOYOriginal
2016-05-16 17:18:42961browse

I used Json data at work some time ago, and I hope to put some simple additions, deletions, modifications and queries on the client side, which can also reduce the pressure on the server side. I searched for several javascript libraries that can operate on Json, and finally selected TAFFY DB. The reasons are as follows:
1. It uses the BSD open source license, so there is no need to worry about commercial use restrictions.
2. Someone has been updating it, and it does not conflict with other frameworks (such as Jquery, YUI, Dojo, etc.)
3. It has complete functions - addition, deletion, modification, search, sorting, etc.
4. Small, only 17k in size after compression.
So, after finishing the work, I will write this article and recommend this class library to everyone. I hope it can help some Coders who are looking for Json query modification, and communicate and learn with everyone.

TAFFY DB basic information
Project homepage: http://taffydb.com/
Hosting address: https://github.com/typicaljoe/taffydb

Usage:

Copy code The code is as follows:



Copy code The code is as follows:

//Create a blank database
var db = TAFFY();
//Create a database containing a piece of data
var db = TAFFY({record:1,text:"example"})
//Create a database through an array. The elements of the array are objects in json format (can be multiple)
var db = TAFFY([{ record:1,text:"example"}])
// Create data through json format string (can be multiple)
var db = TAFFY('[{"record":1,"text": "example"}]')

Query data:
First build a database. All subsequent examples will be based on this database.
Copy code The code is as follows:

var db = TAFFY([//Build a city Database, fields represent province, city name, postal code, sorting
{ province:"Beijing", cityName:"Beijing", zipCode:"10001", orderNum:1},
{ province:"Hebei" , cityName:"Shijiazhuang", zipCode:"10002", orderNum:2},
{ province:"Hebei", cityName:"Baoding", zipCode:"10003", orderNum:3},
{ province :"Hebei", cityName:"Chengde", zipCode:"10004", orderNum:4},
]);

1. Query based on field value
Copy code The code is as follows:

var cities = db({province:"Hebei"}); //Query all The province value is "Hebei" data, and returns the object in TAFFY format
//console.log This function can be used in any browser with a console, such as Firefox's Firebug and Google Chrome Developer tools (press F12 to call out)
for(var i = 0; i< cities().count(); i ){
//get() function of TAFFY can be used to convert TAFFY Convert object data to json format
console.log("city name", cities().get()[i].cityName);
}
console.log("The first piece of data is: ", cities().first()); // first() function can return the first piece of data in json format

2. Query based on conditions (for specific conditions, please refer to the "Comparison Operators" section of http://www.taffydb.com/writingqueries

Copy code The code is as follows:

//Single condition query
//Query all data with order greater than 2
db({ orderNum:{'>':2 }});
//Range query
//Query all data whose order is greater than 2 and less than 4
db({ orderNum:{'>':2, '<':4}} ; <':4}, province:"Hebei"});
//Multiple conditions" or "Query
// Query sorting data greater than 2 or less than 4
db({ orderNum:{' >':2}}, { orderNum : {'<':4}});
//Query within specified data (where in)
//Query data for cities such as Baoding and Shijiazhuang
db({ cityName:['Baoding','Shijiazhuang']});



3. Sorting


Copy code The code is as follows:

//Single condition sorting
db().order("orderNum desc"); // Reverse order according to orderNum
db().order("orderNum"); //Forward order
//Multi-field sorting
db().order("orderNum desc, zipCode asc"); //First in reverse order by orderNum, then in positive order by zipCode

4. Calculate

Copy code The code is as follows:

//Find the maximum value
db().max("orderNum"); //Get the maximum value of orderNum and return
//Find the minimum value
db().min("orderNum"); //Get the minimum orderNum
//Sum
db().sum("orderNum"); //Get the sum of all orderNums
//Get the first piece of data
db().first(); //Get the first piece of data and return it in json format
// Get the last piece of data
db().last(); // Get the last piece of data and return it in json format
// This can be used Let’s paginate haha
db().start(15).limit(20); //Start from the 15th piece of data and take the next 20 pieces

5. Built-in function query. Some data need to be calculated when querying. It is a little complicated. You can query it through built-in function

Copy code The code is as follows:

db().filter(function(){
return this.cityName.length > 2;
});

Add data

Copy code The code is as follows:

//Add a piece of data
db.insert({province:"Hunan", cityName:"Changsha", zipCode:"10005", orderNum:5});

Delete data

Copy code The code is as follows:

//Delete all data
db().remove();
//Delete all data with orderNum greater than 5
db({orderNum:{'>':5}}).remove();

Modify data

Copy code The code is as follows:

//Change the orderNum of all data Modify to 1
db().update({orderNum:1});

//Change the postal code of the city named Beijing to 100000
db({cityName:"Beijing"}).update({zipCode:"100000"})

//Increase all orderNum by 1
db().update(function(){
this.orderNum = this.orderNum 1;
return this;
});
/*
Special note: Sometimes, there is no problem with adding, deleting or checking, but when modifying, you will encounter an error: Field not found
This should be a small bug in TAFFY, encountered In this case, just re-initialize it. You need to use a function
stringify(), which turns all the data in TAFFY DB into strings. So when you encounter this situation, you can do this:
*/
db = TAFFY(db().stringify()); //Reinitialize the content
db().update({column:value});

Writing this, I think this simple tutorial is enough, and it is already convenient for beginners to quickly get started with this class library.
Of course, there are many more complex operations that I have not mentioned and need to be queried by yourself, such as fuzzy queries and so on. You can go to the official homepage of the project to see it. (English)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn