Skip to content
Browse files

AMD support

  • Loading branch information...
1 parent ca38084 commit b0bcfc3181ebfe12799c4b5768d4be8819a74cbf Egil Moeller committed
View
6 index.js
@@ -1 +1,5 @@
-module.exports = require('./lib/');
+if (define != undefined) {
+ define(['./lib/'], function (lib) { return lib; });
+} else {
+ module.exports = require('./lib/');
+}
View
11 lib/index.js
@@ -1,2 +1,9 @@
-module.exports = require('./persistence').persistence;
-module.exports.StoreConfig = require('./persistence.store.config');
+if (define != undefined) {
+ define(['./persistence', './persistence.store.config'], function (persistence, StoreConfig) {
+ persistence.StoreConfig = StoreConfig;
+ return persistence;
+ });
+} else {
+ module.exports = require('./persistence').persistence;
+ module.exports.StoreConfig = require('./persistence.store.config');
+}
View
17 lib/persistence.js
@@ -33,8 +33,21 @@ if (typeof exports !== 'undefined') {
singleton = exports.createPersistence();
return singleton;
});
-}
-else {
+} else if (define != undefined) {
+ define([], function () {
+ mod = {};
+ mod.createPersistence = function() {
+ return initPersistence({})
+ }
+ var singleton;
+ mod.__defineGetter__("persistence", function () {
+ if (!singleton)
+ singleton = mod.createPersistence();
+ return singleton;
+ });
+ return mod;
+ });
+} else {
window = window || {};
window.persistence = initPersistence(window.persistence || {});
}
View
56 lib/persistence.store.config.js
@@ -1,27 +1,35 @@
-exports.init = function(persistence, config) {
- var persistenceStore;
- switch (config.adaptor) {
- case 'memory':
- persistenceStore = require('./persistence.store.memory');
- break;
- case 'mysql':
- persistenceStore = require('./persistence.store.mysql');
- break;
- case 'sqlite3':
- persistenceStore = require('./persistence.store.sqlite3');
- break;
- default:
- persistenceStore = require('./persistence.store.mysql');
- break;
+var stores = {
+ memory: './persistence.store.memory',
+ mysql: './persistence.store.mysql',
+ sqlite3: './persistence.store.sqlite3'
+};
+
+var init = function(persistence, config, cb) {
+ var setupStore = function (persistenceStore) {
+ if (config.username) config.user = config.username;
+ if (config.hostname) config.host = config.hostname;
+ persistenceStore.config(persistence,
+ config.host,
+ config.port,
+ config.database,
+ config.user,
+ config.password);
+ cb(null, persistenceStore);
}
- if (config.username) config.user = config.username;
- if (config.hostname) config.host = config.hostname;
- persistenceStore.config(persistence,
- config.host,
- config.port,
- config.database,
- config.user,
- config.password);
- return persistenceStore;
+ var adaptorName = config.adaptor[config.adaptor] || config.adaptor.mysql;
+
+ if (define != undefined) {
+ require([adaptorName], setupStore);
+ } else {
+ setupStore(require(adaptorName));
+ }
};
+
+if (define != undefined) {
+ define([], function () {
+ return {init:init};
+ });
+} else {
+ exports.init = init;
+}
View
212 lib/persistence.store.mysql.js
@@ -4,122 +4,134 @@
* Easy install using npm:
* npm install mysql
*/
-var sys = require('sys');
-var sql = require('./persistence.store.sql');
-var mysql = require('mysql');
-var db, username, password;
-function log(o) {
- sys.print(sys.inspect(o) + "\n");
-}
+var definemod = function (exports, sys, sql, mysql) {
+ var db, username, password;
+ function log(o) {
+ sys.print(sys.inspect(o) + "\n");
+ }
-exports.config = function(persistence, hostname, port, db, username, password) {
- exports.getSession = function(cb) {
- var that = {};
- var client = mysql.createClient({
- host: hostname,
- port: port,
- database: db,
- user: username,
- password: password
- });
- var session = new persistence.Session(that);
- session.transaction = function (explicitCommit, fn) {
- if (typeof arguments[0] === "function") {
- fn = arguments[0];
- explicitCommit = false;
- }
- var tx = transaction(client);
- if (explicitCommit) {
- tx.executeSql("START TRANSACTION", null, function(){
- fn(tx)
- });
- }
- else
- fn(tx);
- };
+ exports.config = function(persistence, hostname, port, db, username, password) {
+ exports.getSession = function(cb) {
+ var that = {};
+ var client = mysql.createClient({
+ host: hostname,
+ port: port,
+ database: db,
+ user: username,
+ password: password
+ });
- session.close = function() {
- client.end();
- //conn._connection.destroy();
+ var session = new persistence.Session(that);
+ session.transaction = function (explicitCommit, fn) {
+ if (typeof arguments[0] === "function") {
+ fn = arguments[0];
+ explicitCommit = false;
+ }
+ var tx = transaction(client);
+ if (explicitCommit) {
+ tx.executeSql("START TRANSACTION", null, function(){
+ fn(tx)
+ });
+ }
+ else
+ fn(tx);
+ };
+
+ session.close = function() {
+ client.end();
+ //conn._connection.destroy();
+ };
+ session.client = client;
+ return session;
};
- session.client = client;
- return session;
- };
- function transaction(conn){
- var that = {};
- if(conn.ending) {
- throw new Error("Connection has been closed, cannot execute query.");
- }
- that.executeSql = function(query, args, successFn, errorFn){
- function cb(err, result){
- if (err) {
- log(err.message);
- that.errorHandler && that.errorHandler(err);
- errorFn && errorFn(null, err);
- return;
+ function transaction(conn){
+ var that = {};
+ if(conn.ending) {
+ throw new Error("Connection has been closed, cannot execute query.");
+ }
+ that.executeSql = function(query, args, successFn, errorFn){
+ function cb(err, result){
+ if (err) {
+ log(err.message);
+ that.errorHandler && that.errorHandler(err);
+ errorFn && errorFn(null, err);
+ return;
+ }
+ if (successFn) {
+ successFn(result);
+ }
}
- if (successFn) {
- successFn(result);
+ if (persistence.debug) {
+ sys.print(query + "\n");
+ args && args.length > 0 && sys.print(args.join(",") + "\n")
+ }
+ if (!args) {
+ conn.query(query, cb);
+ }
+ else {
+ conn.query(query, args, cb);
}
}
- if (persistence.debug) {
- sys.print(query + "\n");
- args && args.length > 0 && sys.print(args.join(",") + "\n")
- }
- if (!args) {
- conn.query(query, cb);
+
+ that.commit = function(session, callback){
+ session.flush(that, function(){
+ that.executeSql("COMMIT", null, callback);
+ })
}
- else {
- conn.query(query, args, cb);
+
+ that.rollback = function(session, callback){
+ that.executeSql("ROLLBACK", null, function() {
+ session.clean();
+ callback && callback();
+ });
}
+ return that;
}
-
- that.commit = function(session, callback){
- session.flush(that, function(){
- that.executeSql("COMMIT", null, callback);
- })
- }
-
- that.rollback = function(session, callback){
- that.executeSql("ROLLBACK", null, function() {
- session.clean();
- callback && callback();
- });
- }
- return that;
- }
-
- exports.mysqlDialect = {
- // 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] : ""));
+
+ exports.mysqlDialect = {
+ // 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 += ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
+ return sql;
+ },
+
+ // columns is array of column names, e.g.
+ // ["id"]
+ createIndex: function(tableName, columns, options) {
+ options = options || {};
+ return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX `" + tableName + "__" + columns.join("_") +
+ "` ON `" + tableName + "` (" +
+ columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
}
- sql += defs.join(", ");
- sql += ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
- return sql;
- },
+ };
- // columns is array of column names, e.g.
- // ["id"]
- createIndex: function(tableName, columns, options) {
- options = options || {};
- return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX `" + tableName + "__" + columns.join("_") +
- "` ON `" + tableName + "` (" +
- columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
- }
+ sql.config(persistence, exports.mysqlDialect);
};
-
- sql.config(persistence, exports.mysqlDialect);
};
+if (define != undefined) {
+ define(["sys", './persistence.store.sql', 'mysql'], function(sys, sql, mysql) {
+ var mod = {};
+ definemod(mod, sys, sql, mysql);
+ return mod;
+ });
+} else {
+ var sys = require('sys');
+ var sql = require('./persistence.store.sql');
+ var mysql = require('mysql');
+ definemod(exports, sys, sql, mysql);
+}
View
10 lib/persistence.store.sql.js
@@ -886,8 +886,14 @@ function config(persistence, dialect) {
if (typeof exports !== 'undefined') {
exports.defaultTypeMapper = defaultTypeMapper;
exports.config = config;
-}
-else {
+} else if (define != undefined) {
+ define([], function () {
+ return {
+ defaultTypeMapper: defaultTypeMapper,
+ config: config
+ };
+ });
+}else {
window = window || {};
window.persistence = window.persistence || {};
window.persistence.store = window.persistence.store || {};
View
199 lib/persistence.store.sqlite.js
@@ -5,117 +5,128 @@
* npm install sqlite
* @author Eugene Ware
*/
-var sys = require('sys');
-var sql = require('./persistence.store.sql');
-var sqlite = require('sqlite');
-var db, username, password;
+var definemod = function (exports, sys, sql, sqlite) {
+ var db, username, password;
-function log(o) {
- sys.print(sys.inspect(o) + "\n");
-}
+ function log(o) {
+ sys.print(sys.inspect(o) + "\n");
+ }
-exports.config = function(persistence, dbPath) {
- exports.getSession = function(cb) {
- var that = {};
- cb = cb || function() { };
- var conn = new sqlite.Database();
- conn.open(dbPath, cb);
+ exports.config = function(persistence, dbPath) {
+ exports.getSession = function(cb) {
+ var that = {};
+ cb = cb || function() { };
+ var conn = new sqlite.Database();
+ conn.open(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);
- };
+ 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);
+ session.close = function(cb) {
+ cb = cb || function() {};
+ conn.close(cb);
+ };
+ return session;
};
- return session;
- };
- function transaction(conn){
- var that = {};
- // TODO: add check for db opened or closed
- that.executeSql = function(query, args, successFn, errorFn){
- function cb(err, result){
- if (err) {
- log(err.message);
- that.errorHandler && that.errorHandler(err);
- errorFn && errorFn(null, err);
- return;
+ function transaction(conn){
+ var that = {};
+ // TODO: add check for db opened or closed
+ that.executeSql = function(query, args, successFn, errorFn){
+ function cb(err, result){
+ if (err) {
+ log(err.message);
+ that.errorHandler && that.errorHandler(err);
+ errorFn && errorFn(null, err);
+ return;
+ }
+ if (successFn) {
+ successFn(result);
+ }
}
- if (successFn) {
- successFn(result);
+ if (persistence.debug) {
+ sys.print(query + "\n");
+ args && args.length > 0 && sys.print(args.join(",") + "\n")
+ }
+ if (!args) {
+ conn.execute(query, cb);
+ }
+ else {
+ conn.execute(query, args, cb);
}
}
- if (persistence.debug) {
- sys.print(query + "\n");
- args && args.length > 0 && sys.print(args.join(",") + "\n")
- }
- if (!args) {
- conn.execute(query, cb);
+
+ that.commit = function(session, callback){
+ session.flush(that, function(){
+ that.executeSql("COMMIT", null, callback);
+ })
}
- else {
- conn.execute(query, args, cb);
+
+ that.rollback = function(session, callback){
+ that.executeSql("ROLLBACK", null, function() {
+ session.clean();
+ callback();
+ });
}
+ return that;
}
-
- that.commit = function(session, callback){
- session.flush(that, function(){
- that.executeSql("COMMIT", null, callback);
- })
- }
-
- that.rollback = function(session, callback){
- that.executeSql("ROLLBACK", null, 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] : ""));
+ ///////////////////////// 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 += 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);
};
-
- sql.config(persistence, persistence.sqliteDialect);
};
+if (define != undefined) {
+ define(["sys", './persistence.store.sql', 'sqlite'], function(sys, sql, sqlite) {
+ var mod = {};
+ definemod(mod, sys, sql, sqlite);
+ return mod;
+ });
+} else {
+ var sys = require('sys');
+ var sql = require('./persistence.store.sql');
+ var mysql = require('sqlite');
+ definemod(exports, sys, sql, sqlite);
+}
View
193 lib/persistence.store.sqlite3.js
@@ -7,116 +7,127 @@
* @author Jeff Kunkle
* @author Joe Ferner
*/
-var sys = require('sys');
-var sql = require('./persistence.store.sql');
-var sqlite = require('sqlite3');
-var db, username, password;
+var definemod = function (exports, sys, sql, sqlite) {
+ var db, username, password;
-function log(o) {
- sys.print(sys.inspect(o) + "\n");
-}
+ function log(o) {
+ sys.print(sys.inspect(o) + "\n");
+ }
-exports.config = function(persistence, dbPath) {
- exports.getSession = function(cb) {
- var that = {};
- cb = cb || function() { };
- var conn = new sqlite.Database(dbPath, cb);
+ 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);
- };
+ 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);
+ session.close = function(cb) {
+ cb = cb || function() {};
+ conn.close(cb);
+ };
+ return session;
};
- return session;
- };
- function transaction(conn){
- var that = {};
- // TODO: add check for db opened or closed
- that.executeSql = function(query, args, successFn, errorFn){
- function cb(err, result){
- if (err) {
- log(err.message);
- that.errorHandler && that.errorHandler(err);
- errorFn && errorFn(null, err);
- return;
+ function transaction(conn){
+ var that = {};
+ // TODO: add check for db opened or closed
+ that.executeSql = function(query, args, successFn, errorFn){
+ function cb(err, result){
+ if (err) {
+ log(err.message);
+ that.errorHandler && that.errorHandler(err);
+ errorFn && errorFn(null, err);
+ return;
+ }
+ if (successFn) {
+ successFn(result);
+ }
}
- if (successFn) {
- successFn(result);
+ if (persistence.debug) {
+ sys.print(query + "\n");
+ args && args.length > 0 && sys.print(args.join(",") + "\n")
+ }
+ if (!args) {
+ conn.all(query, cb);
+ }
+ else {
+ conn.all(query, args, cb);
}
}
- if (persistence.debug) {
- sys.print(query + "\n");
- args && args.length > 0 && sys.print(args.join(",") + "\n")
- }
- if (!args) {
- conn.all(query, cb);
- }
- else {
- conn.all(query, args, cb);
+
+ that.commit = function(session, callback){
+ session.flush(that, function(){
+ that.executeSql("COMMIT", null, callback);
+ })
}
- }
- that.commit = function(session, callback){
- session.flush(that, function(){
- that.executeSql("COMMIT", null, callback);
- })
+ that.rollback = function(session, callback){
+ that.executeSql("ROLLBACK", null, function() {
+ session.clean();
+ callback();
+ });
+ }
+ return that;
}
- that.rollback = function(session, callback){
- that.executeSql("ROLLBACK", null, function() {
- session.clean();
- callback();
- });
- }
- return that;
- }
+ ///////////////////////// SQLite dialect
- ///////////////////////// 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;
+ },
- 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] : ""));
+ // 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 += 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);
};
-
- sql.config(persistence, persistence.sqliteDialect);
};
+if (define != undefined) {
+ define(["sys", './persistence.store.sql', 'sqlite3'], function(sys, sql, sqlite) {
+ var mod = {};
+ definemod(mod, sys, sql, sqlite);
+ return mod;
+ });
+} else {
+ var sys = require('sys');
+ var sql = require('./persistence.store.sql');
+ var sqlite = require('sqlite3');
+ definemod(exports, sys, sql, sqlite);
+}

0 comments on commit b0bcfc3

Please sign in to comment.
Something went wrong with that request. Please try again.