Javascript lib that operates json as a database


TAFFY DB basic information Project home page: http://taffydb.com/ Host address: https://github.com/typicaljoe/taffydb

Usage:

<script type="text/javascript" charset="utf-8" src="taffy-min.js"></script>
//Create a blank database
var db = TAFFY();
//Create a database that contains one piece of data
var db = TAFFY({record:1,text:"example"})
 //Create database by array, the elements of the array are json-formatted objects (can be multiple)
var db = TAFFY([{record:1,text:"example"}])     
//Create data (more than one) from a json-formatted string & NBSP;    
var db = TAFFY('[{"record":1,"text":"example"}]')

Query data: Start by building a database, and follow that database for future examples.

var db = TAFFY([//Build a database of cities with fields representing province, city name, zip code, and sort
    { 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 according to the field value

var cities = db({province:" hebei "});  //Query all data with a province value of "hebei" and return an object in TAFFY format
//Console.log is a function that can be used in any browser with a console, such as firebug in firefox and developer tools in Google (press F12 to exhale).
for(var i = 0; i< cities().count(); i++){
    //Get (), a function of TAFFY, converts TAFFY's object data into json
    console.log(" City name ", cities().get()[i].cityName);
}
console.log(" The first data is :", cities().first()); //First () returns the first data in json format

2, according to the conditions query (specific conditions, please refer to http://www.taffydb.com/writingqueries “Comparison Operators” section

//Single conditional query
//Query all data with a sort greater than 2
db({ orderNum:{'>':2}});
//Range queries
//All data whose query sort is greater than 2 and less than 4
db({ orderNum:{'>':2, '<':4}});
//Multi - condition and query
//The query is greater than 2 and less than 4, and the province is the data of hebei province
db({ orderNum:{'>':2, '<':4}, province:" hebei "});
//Multi-condition or query
//Query sort & NBSP; Greater than 2 or less than 4
db({ orderNum:{'>':2}}, { orderNum : {'<':4}});
//Specify the data in query (where in)
//Inquiring the city is & NBSP; Data from baoding and shijiazhuang
db({ cityName:[' baoding ',' shijiazhuang ']});

3, sorting,

//Single conditional sort
db().order("orderNum desc");    //In reverse order according to orderNum
db().order("orderNum"); // Positive sequence
//Multifield sort
db().order("orderNum desc, zipCode asc"); //First in reverse order in orderNum, then in positive order in zipCode

4, calculation

//For maximum
db().max("orderNum"); //Get the maximum value of orderNum and return
//For the minimum
db().min("orderNum"); //Get the smallest orderNum
// sum
db().sum("orderNum"); //I get the sum of all orderNum
//Get the first data
db().first();  //Take the first data and return it in json format
//I get the last piece of data
db().last(); //Take the last one and return it in json format
//This can be used for pagination
db().start(15).limit(20); //Start at number 15 and then take 20

5, built-in function query, some data, need to query, do some calculations, slightly more complex, you can query through the built-in function

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

Add data

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

Delete the data

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

Modify the data

//Change the orderNum of all the data to 1
db().update({orderNum:1});
//Change the postcode of the city named Beijing to 100000
db({cityName:" Beijing "}).update({zipCode:"100000"})
//Add all orderNum to 1
db().update(function(){
    this.orderNum = this.orderNum + 1;
    return this;
});

db = TAFFY(db().stringify()); //Reinitialize the contents
db().update({column:value});

Write here, I think this simple tutorial is also can, has been convenient for the quick use of this class library. Of course, there are a lot of things that I didn’t mention that need to be looked up by myself, such as fuzzy queries and so on, more complex operations. You can go to the official website of the project. (English)