Persistencejs: Asynchronous JavaScript Database Mapping Library

Source: Internet
Author: User
Tags sqlite sqlite database

Persistence.js is an asynchronous JavaScript Object database Mapping (ORM) framework. A separate, database-independent abstraction layer that can easily support new databases. The software was first designed for the browser, using HTML5 's Websql database. The persistence.js is then extended to support a variety of JavaScript environments, including server-side JavaScript environments such as node. js.

Supported Browsers

    • Modern WebKit browsers (Google Chrome and Safari)
    • Firefox (through Google Gears)
    • Opera
    • Android Browser (tested on 1.6 and 2.x)
    • iphone browser (iphone OS)
    • Palm WebOS (tested on 1.4.0)
    • Other browsers supporting localStorage (e.g. Firefox)

Use

Introduction of Persistence.js

<script type= "Text/javascript" src= "js/persistence.js" ></script>  <script type= "Text/javascript "Src=" Js/persistence.store.sql.js "></script>  <script type=" Text/javascript "src=" js/ Persistence.store.websql.js "></script>  <script type=" Text/javascript "src=" js/ Persistence.store.memory.js "></script>

  

Persistence.js divides its core modules into smaller JavaScript files, effectively guaranteeing high-performance loading, and users only need to introduce the appropriate JavaScript files when they use a function module. Here we use Persistence.js,persistence.store.sql.js and persistence.store.websql.js, these three files are required to use WEB SQL. Persistence.store.memory.js can also be introduced if the user wants to temporarily save the data in memory as an alternative to the Web SQL API that the browser does not support.

Configuration

Before writing the ORM JavaScript code, we need to tell persistence.js what database we will be creating or which database we are connecting to. This is shown in Listing 7. This code corresponds to the code that is connected to a database as described in Listing 1. This shows that persistence.js simplifies a lot of JavaScript code.

Configuring Persistence Global Variables
if (window.opendatabase) {  persistence.store.websql.config (persistence, ' TestDB ',         ' My SQLite database ', 5 * * 1024x768);  } else {  persistence.store.memory.config (persistence);  }

  

Persistence is a global variable after the introduction of Persistence.js. The current browser If there is a opendatabase API exists, then the expression is support for Web SQL, we can directly specify the database information will be created, if not, then use another way to do the storage.

Create a table

Once the database has been created, let's look at how to create a table in an object-oriented way, and the functionality of listing 8 will be the SQL statement shown in Listing 2.

Persistence.js creating objects
var User = persistence.define (' User ', {  name: "TEXT", Age  : "INTEGER"});  Persistence.schemasync ();

  

A very concise definition of a User object, and then through the Schemasync () method, the corresponding table in the database is created. Later, if users want to do something about the data in the user table, they can call the relevant API directly from the user object.

Persistence.js is fully supported for basic SQLite data types, such as Null,integer,real,text,blob, and there are some extension types that can be supported, such as Int,bool,date,json.

Inserting and deleting data

Inserting a record into the user table is simply saving a user object and calling the flush () method in persistence, as shown in Listing 9.

Persistence.js Inserting data
var mark = new User ({name: "Mark", age:60});  Persistence.add (Mark);  Persistence.flush ();

  

Deleting a record deletes a User object directly, as shown in Listing 10.

Persistence.js Deleting data
Persistence.remove (Mark);  Persistence.flush ();

GitHub homepage: Https://github.com/zefhemel/persistencejs, official website http://persistencejs.org.

Persistencejs: Asynchronous JavaScript Database Mapping Library

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.