UNPKG

indurate

Version:

Indurate.js is an asynchronous sqlite3 wrapper for the openDatabase module.

123 lines (87 loc) 6.25 kB
# Indurate Database ## A wrapper for WebSql and [openDatabase](https://npmjs.org/package/opendatabase/) [![NPM](https://nodei.co/npm/indurate.png?downloads=true)](https://nodei.co/npm/indurate/) **Author:** Robert Edward Steckroth II **Digital Persona:** Surgemcgee, Bustout <RobertSteckroth@gmail.com> **Licence:** GNU GENERAL PUBLIC LICENSE Version 3 **Description:** Indurate.js is a user friendly API for server and mobile applications. Indurate uses WebSql openDatabase with a strong object oriented structure. Indurate can provide database driven applications or supplement existing mobile applications. **Features:** * Works with Ubuntu Touch, Phonegap, Nodejs and Gecko based browsers * SUper simple table management * Pretty database and table printing * Asynchronous or synchronous code structure * Impossible to break or use corrupted data ###_Example usage_ ``` if ( console && typeof console.dir === "undefined" ) console.dir = function(obj) { console.log(JSON.stringify(obj, null, '\t')) } var db = new IndurateDatabase({name: "Example Application", version: 1.0, description: "A cool database for everyone (except some people)", size: 4*1024*1024}) var table = db.$("Indurate Table", ["MyColumn", "Second column"], function() { console.log("Here we go..") this['first'] = {} // Make some initial empty rows this['second'] = {} console.dir(this) this.$("getTable", function() { console.log(this.indurate.plugin_sql) // The current command processed by indurate E.g. SELECT * FROM "My Indurate Tabledd"; console.log(this.indurate.plugin) // The current indurate plugin run E.g. getTable console.log(this.indurate.name) // The current table name in use E.g. My Indurate Table console.dir(this) // this will be empty the first tiome this script is be run // after that the "getTable" command will populate this with the entire table data this['first']["MyColumn"] = "Initial data in first column" this['first']["Second column"] = "Initial Data in second column" console.log(this['first']["MyColumn"]) // === "Initial data in first column" this['second'] = {"MyColumn": "Column data in here", "Second column": "Some data for this"} this.$("describe", function(out) { console.log(out) this.$("set", function() { this.$("describe", function(out) { console.log(out) this['first']["MyColumn"] = "Random number "+(Math.round(Math.random() * 99999999)) this['first']["Second column"] = "more data for this column" this['third'] = {"MyColumn": "A another column! What?"} // will be set to a empty string if column data is not set this.$("set", function () { this.$("describe", function() { }) }) }) }) })//.$("set").$("describe") // This will be called before the internal stuff so it will show the initial table data })//.$("describe", function(out) { console.log("This is called after the first getTable command callback is fired\n"+out)} ) // Append pretty printing to log message }, function(tx, err) { console.log('I am in db global still!\n'+err.message) this.$("describe") // <- !THIS IS THE ONLY TIME WE HAVE ACCESS TO DATABASE COMMAND // This will not happen in the sucess callback either. }) // Close out the database scope. // The database needs to be at rest before we can call it in a gloabal setting. // Rember that the asynchronous format will run everything all at once. setTimeout(function() { table['first']["MyColumn"] = "And we change this one last time" table.$("set").$("describe")//.$("getTable") <-- adding this to the end of $("set") commands will ensure that only successfull database entries will remain in the table object console.dir(table) db.$("describe", function(out) { // specifying a success callback here will disable defualt logging and provide the pretty table text into an argument (out) console.log("\n---- I WILL CONTROL PRETTY PRINTING ---- \n\n"+out) }) }, 1000) ``` _example output table describe_ ```` ___________________________________________________________________________________________ |key |name |last_updated |has_failed| |___________|__________________________|_______________________________________|__________| |my_key . . |Surgemcgee . . . . . . . .|Sat Oct 26 2013 10:58:54 GMT-0400 (EDT)|false . . | |another_key|Robert Edward Steckroth II|Sat Oct 26 2013 10:52:52 GMT-0400 (EDT)|No way! . | |___________|__________________________|_______________________________________|__________| ```` _example output show database_ ```` | Show Database: example Version: 1.0 Size(bytes): 3145728 Description: Example database for indurate.js | Rows: 5 Columns: 5 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ |type |name |tbl_name |rootpage|sql | |_____|___________________________|___________________________|________|_____________________________________________________________________________________________________________________________________________| |table|__WebKitDatabaseInfoTable__|__WebKitDatabaseInfoTable__|3 . . . |CREATE TABLE __WebKitDatabaseInfoTable__ (key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,value TEXT NOT NULL ON CONFLICT FAIL)| |table|userTable . . . . . . . . .|userTable . . . . . . . . .|5 . . . |CREATE TABLE userTable(name TEXT UNIQUE, value TEXT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |table|peopleTable . . . . . . . .|peopleTable . . . . . . . .|7 . . . |CREATE TABLE peopleTable(name TEXT UNIQUE, value TEXT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |_____|___________________________|___________________________|________|_____________________________________________________________________________________________________________________________________________| ````