"Go" Lokijs: A lightweight database implemented with pure JavaScript

Source: Internet
Author: User

Translated from: http://www.html5cn.org/article-7091-1.html

Lokijs a lightweight document-oriented database, implemented by JavaScript, performs better than anything else. The goal is to use JavaScript objects as documents, then store them, and retrieve the data in the same way. Lokijs can run on node. JS and all JavaScript-enabled browsers, mobile applications.


Lokijs supports field indexing for faster file access and is also fairly well-performing (nearly 500,000 ops/s). Its built-in Dynamicview class can also be used for indexing subsets of data to achieve faster performance. Lokijs supports collections, much like MongoDB, and saves data in JSON format to the hard disk (depending on the session to restore state), so your data is portable.
The ideal scenario for using Lokijs:

    • Mobile apps--especially based on HTML5 (e.g. CORDOVA,PHONEGAP, etc.)
    • Data storage for small-to medium-sized applications based on node. js
    • Desktop applications with Node-webkit
    • Lokijs is still in the alpha phase. The source is hosted on GitHub.

Quick installation:

[JS]

    1. NPM Install Lokijs
    2. Or
    3. Bower Install Lokijs
Copy Code
Create a database:

    1. var db = new Loki (' Loki.json ')
    2. Pass the filename where to persist data
Copy Code
Create a collection:

    1. var children = db.addcollection (' Children ')
Copy Code
Insert a document:

    1. Children.insert ({name: ' Sleipnir ', legs:8})
    2. Children.insert ({name: ' Jormungandr ', legs:0})
    3. Children.insert ({name: ' Hel ', legs:2})
Copy Code
Retrieving/Obtaining Documents:

    1. Children.get (1); Returns Sleipnir
    2. Children.find ({' name ': ' Sleipnir '})
    3. Children.find ({legs: {' $gt ': 2}})
Copy Code
Create a dynamic view:

    1. var legs = children.adddynamicview (' legs ');
    2. Legs.applyfind ({legs: {' $gt ': 2})
    3. Legs.applysimplesort (' legs ');
    4. Legs.data ();
Copy Code
Mapreduce:

    1. Children.mapreduce (
    2. function (obj) {return obj.legs;},
    3. Function (array) {
    4. var sum = 0;
    5. for (var i=0; i < array.length; i++) {
    6. Sum + = Array[i];
    7. }
    8. Return (sum/array.length). toFixed (2);
    9. });
Copy Code
Lokijs Related information:

Official website: http://lokijs.org/

managed Address: Https://github.com/techfort/LokiJS

Source Address: http://www.csdn.net/article/2014-11-05/2822493-LokiJS?reload=1

"Go" Lokijs: A lightweight database implemented with pure JavaScript

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.