Node-tiny (a super-small node. js database)

Source: Internet
Author: User
Recently written program node. data storage is required for js programs. there are many databases that js can use, such as mongodb and MySQL. However, I still think these databases are too large to be useful, making them more troublesome. So I found node-tiny, a non-relational data... SyntaxHighlighter. all

Recently written program node. data storage is required for js programs. there are many databases that js can use, such as mongodb and MySQL. However, I still think these databases are too large to be useful, making them more troublesome. So I found node-tiny, a non-relational database, a bit similar to mongodb.
Tiny's document describes the fact that when we search for data in a database, we will search for short Attributes Based on the content such as id, name, category, tag, and date, in rare cases, search for such ultra-long fields as full-text information. Therefore, when creating a tiny database, tiny will load the attribute smaller than 128 bytes into the memory, so that the retrieval speed will be very fast, when the search ends, tiny loads fields with more than 128 bytes of records based on the search results.
Usage:

1. install npm install tiny

 

2. Create a database instance.
Var Tiny = require ('tiny ');
Tiny ('articles. tiny', function (err, db ){
// Use database instance db to operate data.
});
Note: The first parameter is the name of the file used to store data. This file will be created when it is used for the first time. The second parameter is the callback function executed after the database is created successfully.

3. Example.

Tiny ('articles. tiny', function (err, db ){
Db. set ('doc001 ',{
Title: 'The title ',
Content: 'The Content'
}, Function (err ){
Console. log (err)
Console. log ('set! ');
});
}); Note: Save the Document Object. The first parameter is the key of the stored data, the second parameter is the content to be stored, and the third parameter is the callback function.


4. functions:

Set: Add a record. The parameter is as above.
Remove: delete a record. The parameter is the same as set.
Update: update record. The first parameter is the same as set (the stored key), and the second parameter is the callback function.

Get: get a record. The first parameter is the same as set, and the second parameter is the callback function.
All: Get all data. The parameter is the callback function, but this function has been discarded.
Each: iterates every object in the database. The first parameter is the iteration function (the first parameter is the record object, and the second parameter is the record key). The second parameter is the callback function after the iteration ends. The third parameter is a Boolean value, if this parameter is set to true, more than 128 bytes will be loaded into the memory iteration. The default value is false. We recommend that you keep the default value.
Fetch: Data Retrieval. The first parameter is the restricted object, and the second parameter is the function. The former is used to filter data, and the latter is the callback function after the retrieval.
Find: mongodb-style retrieval.

Close: close the database and close open files.
Kill: delete all data.
Compact: clears useless data in database files.

5. Supplement: for tiny, adding, deleting, and modifying data are written to the file. Note that the data is directly appended to the end of the file. Therefore, the update and remove methods only Append content to the end of the file. This design is possible because the JavaScript Object Attributes are unique and the value assignment operation overwrites the previous values. Therefore, tiny only needs to parse the file in the order of the file to obtain the correct data. The disadvantage of this design is that the deleted data also occupies storage space. The compact method is to delete excess data in the file.


6. Demonstration of each method.


Tiny ('articles. tiny', function (err, db ){
/*
For (var I = 0; I <10; I ++ ){
Db. set ('Doc' + I ,{
Title: 'a document '+ I,
Content: 'Hello world' + I
}, Function (err ){
If (err ){
Console. log (err );
} Else {
Console. log ('set! ');
}
});
}
*/

/*
Db. get ("doc0", function (err, data ){
Console. log (arguments)
});
*/

/*
Db. each (function (obj, key ){
Console. log (arguments );

}, Function (){


}, False );
*/

/*
Db. fetch ({
Limit: 3
}, Function (obj, key ){
Return true;
}, Function (err, datas ){
Console. log (arguments)
});*/

/*
Db. compact (function (err ){
Console. log ('done ');
});
*/
});


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.