Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added react-native store ( using ios native sqlite3 ). #169

Merged
merged 1 commit into from
May 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/persistence.store.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
129 changes: 129 additions & 0 deletions lib/persistence.store.react-native.js
Original file line number Diff line number Diff line change
@@ -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);
};