indurate
v0.2.0
Published
Indurate.js is an asynchronous sqlite3 wrapper for the openDatabase module.
Downloads
20
Readme
Indurate Database
A wrapper to WebSql and node openDatabase
Author: Robert Edward Steckroth II
Digital Persona: Surgemcgee, Bustout [email protected]
Licence: GNU GENERAL PUBLIC LICENSE Version 3
Description:
Indurate.js is a user friendly API for server and mobile applications. Indurate uses WebSql node openDatabase with a strong object oriented structure. Indurate can provide database driven applications or supplement existing mobile applications.
Features:
- Efficient design
- Simple table management
- Pretty database and table printing
- Asynchronous or synchronous code structure
###Example usage
var path = require('path'),
Indurate = require('indurate')
function log(message) {
console.log('[Indurate Test Server] '+message)
}
var server_db = function() {
this.appcwd = path.dirname(__filename)
this.database_dir = path.join(this.appcwd, 'Indurate_test_database.sqlite')
this.db = new Indurate({name: this.database_dir, version: "1.0", description: "Example database for indurate.js", size: 3})
}
server_db.prototype = {
get_set_test: function(t_name) {
var newThis = this // Gots to have the scope cheat to be functional
var table_object = {}
// First array value will be used as the primary key/object identifier
// If the table was created outside of Indurate, the first column in the table will be used as the primary key
this[t_name] = this.db.initTable(t_name, ['key', 'name', 'last_updated', 'has_failed'], function() {
log('Initializing table '+this.name)
this.getTable(function(table, out) {
log('Fetching table '+this.name+' as js object')
log('Retrieved '+out.rows.length+' from '+this.name) // This.name is the table name passed into initTable
log('Table has columns: '+this.columns)
log('Table colums is stored in results as a array '+out.rows.info().toString())
log('The second paramater is optional and WebSql compliant./nout.rows.length: '+out.rows.length+'\nout.rows.item(0): ')
console.dir(out.rows.item(0))
log('Table columns are stored in results.rows.info() as an array '+out.rows.info().toString())
table_object = table
table_object['my_key'] = {}
table_object['my_key'].name = 'Cool yo'
table_object['my_key'].last_updated = new Date()
table_object['my_key'].has_failed = false
this.set(table_object, function(){
// Set it the table with the values we built in the getTable() call
this.describe(function(output) { log(output) }) // Provide us with pretty printing of the table
})
})
})
},
change_table_test: function(t_name, key, name, value) {
// If the table exists, the column fields are ignored so we will leave it empty here for convenience
this[t_name].getTable(function(t_obj) { // Second paramater (WebSql) is ommited here which uses less memory
console.dir(t_obj)
log('Columns in table '+this.name+' are '+this.columns.toString())
if ( t_obj[key] ) // Does the rows/key exists? We can also check this.columns or out.row.info() in here
t_obj[key][name] = value
this.set(t_obj, function() {
this.describe() // All callbacks are optional and will log to console if not provided
})
})
},
}
var tests = new server_db()
tests.get_set_test('my_table1')
setTimeout(function(){
tests.change_table_test('my_table1', 'my_key', 'name', 'Surgemcgee') // DON'T SO THIS TOO QUICK. Remember that Indurate is asynchronous
}, 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) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
|_____|___________________________|___________________________|________|_____________________________________________________________________________________________________________________________________________|