From e22c8c1fa67a0b832164a9774e4bf0dbddd85b13 Mon Sep 17 00:00:00 2001 From: Lukas Reichart Date: Wed, 13 May 2015 12:00:06 +0200 Subject: [PATCH] Added react-native store ( using ios native sqlite3 ). --- lib/persistence.store.config.js | 3 + lib/persistence.store.react-native.js | 129 ++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 lib/persistence.store.react-native.js diff --git a/lib/persistence.store.config.js b/lib/persistence.store.config.js index 6eecb1e..d5f8cca 100644 --- a/lib/persistence.store.config.js +++ b/lib/persistence.store.config.js @@ -10,6 +10,9 @@ exports.init = function(persistence, config) { case 'sqlite3': persistenceStore = require('./persistence.store.sqlite3'); break; + case 'react-native': + persistenceStore = require('./persistence.store.react-native'); + break; default: persistenceStore = require('./persistence.store.mysql'); break; diff --git a/lib/persistence.store.react-native.js b/lib/persistence.store.react-native.js new file mode 100644 index 0000000..7fc4b11 --- /dev/null +++ b/lib/persistence.store.react-native.js @@ -0,0 +1,129 @@ +/** + * This module depends on the react-native asynchronous SQLite3 driver as found on: + * https://github.com/almost/react-native-sqlite + * Easy install using npm: + * npm install react-native-sqlite + * and follow the instructions provided in the README + * @author Lukas Reichart + */ +var sys = {}; +sys.print = console.log; +var sql = require('./persistence.store.sql'); +var sqlite = require('react-native-sqlite'); + +var db, username, password; + +function log(o) { + sys.print(o + "\n"); +} + + +exports.config = function(persistence, dbPath) { + exports.getSession = function(cb) { + var that = {}; + cb = cb || function() { }; + var conn = new sqlite.Database(dbPath, cb); + + var session = new persistence.Session(that); + session.transaction = function (explicitCommit, fn) { + if (typeof arguments[0] === "function") { + fn = arguments[0]; + explicitCommit = false; + } + var tx = transaction(conn); + if (explicitCommit) { + tx.executeSql("START TRANSACTION", null, function(){ + fn(tx) + }); + } + else + fn(tx); + }; + + session.close = function(cb) { + cb = cb || function() {}; + conn.close(cb); + }; + return session; + }; + + function transaction(conn){ + var that = {}; + // TODO: add check for db opened or closed + that.executeSql = function(query, args, successFn, errorFn){ + var queryResult = []; + function cb(err){ + if (err) { + log(err.message); + that.errorHandler && that.errorHandler(err); + errorFn && errorFn(null, err); + return; + } + if (successFn) { + if( !queryResult ) { + queryResult = []; + } + successFn(queryResult); + } + } + function rowCallback(row) { + queryResult.push(row); + } + if (persistence.debug) { + console.log(query + "\n"); + //args && args.length > 0 && sys.print(args.join(",") + "\n") + } + if (!args) { + conn.executeSQL(query, [], rowCallback, cb ); + } + else { + conn.executeSQL(query, args, rowCallback, cb ); + } + } + + that.commit = function(session, callback){ + session.flush(that, function(){ + that.executeSQL("COMMIT", [], function(){}, callback); + }) + } + + that.rollback = function(session, callback){ + that.executeSQL("ROLLBACK", [], function() {}, function() { + session.clean(); + callback(); + }); + } + return that; + } + + ///////////////////////// SQLite dialect + + persistence.sqliteDialect = { + // columns is an array of arrays, e.g. + // [["id", "VARCHAR(32)", "PRIMARY KEY"], ["name", "TEXT"]] + createTable: function(tableName, columns) { + var tm = persistence.typeMapper; + var sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` ("; + var defs = []; + for(var i = 0; i < columns.length; i++) { + var column = columns[i]; + defs.push("`" + column[0] + "` " + tm.columnType(column[1]) + (column[2] ? " " + column[2] : "")); + } + sql += defs.join(", "); + sql += ')'; + return sql; + }, + + // columns is array of column names, e.g. + // ["id"] + createIndex: function(tableName, columns, options) { + options = options || {}; + return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX IF NOT EXISTS `" + tableName + "__" + columns.join("_") + + "` ON `" + tableName + "` (" + + columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")"; + } + }; + + sql.config(persistence, persistence.sqliteDialect); +}; +