Comparing version 0.1.7 to 0.2.0
@@ -6,3 +6,4 @@ !function(){ | ||
, log = require('ee-log') | ||
, Model = require('./Model'); | ||
, Model = require('./Model') | ||
, Transaction = require('./Transaction'); | ||
@@ -21,2 +22,3 @@ | ||
this._setProrperty('_orm', options.orm); | ||
this._setProrperty('_database', options.database); | ||
this._setProrperty('_queryBuilders', {}); | ||
@@ -27,12 +29,16 @@ | ||
this.emit('load'); | ||
// emit load not before the next main loop execution | ||
process.nextTick(function(){ | ||
this.emit('load'); | ||
}.bind(this)); | ||
} | ||
, _setOrm: function(orm) { | ||
this._orm = orm; | ||
, createTransaction: function() { | ||
return new Transaction(this); | ||
} | ||
, _getOrm: function() { | ||
return this._orm; | ||
, executeQuery: function(mode, query, callback) { | ||
this._database.query(mode, query, callback); | ||
} | ||
@@ -46,2 +52,7 @@ | ||
, _getDatabase: function(){ | ||
return this; | ||
} | ||
, _initialize: function(definition){ | ||
@@ -55,3 +66,3 @@ Object.keys(definition).forEach(function(tablename){ | ||
, queryBuilders : this._queryBuilders | ||
, getOrm : this._getOrm.bind(this) | ||
, getDatabase : this._getDatabase.bind(this) | ||
}); | ||
@@ -58,0 +69,0 @@ }.bind(this)); |
@@ -24,4 +24,2 @@ !function(){ | ||
// remove deprecated parent property | ||
delete this.parent; | ||
@@ -170,28 +168,24 @@ this._setProrperty('_values', {}); | ||
, delete: function(callback) { | ||
var callback = arg(arguments, 'function', function(){}) | ||
, connection = arg(arguments, 'object'); | ||
var callback = arg(arguments, 'function', function(){}) | ||
, transaction = arg(arguments, 'object'); | ||
if (connection) { | ||
this._delete(connection, callback); | ||
if (transaction) { | ||
this._delete(transaction, callback); | ||
} | ||
else { | ||
this.getOrm().transaction().getDatabase().getConnection(function(err, connection){ | ||
if (err) callback(err); | ||
transaction = this.getDatabase().createTransaction(); | ||
this._delete(transaction, function(err){ | ||
if (err) { | ||
transaction.rollback(function(transactionErr){ | ||
if (transactionErr) callback(transactionErr); | ||
else callback(err); | ||
}.bind(this)); | ||
} | ||
else { | ||
this._delete(connection, function(err){ | ||
if (err) { | ||
connection.rollback(function(transactionErr){ | ||
if (transactionErr) callback(transactionErr); | ||
else callback(err); | ||
}.bind(this)); | ||
} | ||
transaction.commit(function(err){ | ||
if (err) callback(err); | ||
else { | ||
connection.commit(function(err){ | ||
if (err) callback(err); | ||
else { | ||
this._fromDb = false; | ||
callback(null, this); | ||
} | ||
}.bind(this)); | ||
this._fromDb = false; | ||
callback(null, this); | ||
} | ||
@@ -208,7 +202,6 @@ }.bind(this)); | ||
, _delete: function(connection, callback) { | ||
var i = 0 | ||
, query; | ||
query = { | ||
, _delete: function(transaction, callback) { | ||
var query = { | ||
from : this._defintion.getTableName() | ||
@@ -220,20 +213,15 @@ , database : this._defintion.getDatabaseName() | ||
// insert or update? | ||
// icannot delete a model not loaded from the database | ||
if (this._fromDb){ | ||
this._defintion.primaryKeys.forEach(function(key){ | ||
query.filter[key] = this[key]; | ||
i++; | ||
}.bind(this)); | ||
if (i === 0) { | ||
if (!Object.keys(query.filter).length) { | ||
log.dir(query); | ||
throw new Error('Failed to create proper delete query, no filter was created (see query definition above)'); | ||
} | ||
else { | ||
connection.query('delete', query, callback); | ||
} | ||
else transaction.executeQuery('delete', query, callback); | ||
} | ||
else callback(); | ||
else callback(new Error('Cannot delete model, it wasn\'t loaded from the database!')); | ||
} | ||
@@ -246,27 +234,24 @@ | ||
, save: function() { | ||
var callback = arg(arguments, 'function', function(){}) | ||
, connection = arg(arguments, 'object') | ||
, noReload = arg(arguments, 'boolean', false); | ||
var callback = arg(arguments, 'function', function(){}) | ||
, noReload = arg(arguments, 'boolean', false) | ||
, transaction = arg(arguments, 'object'); | ||
// transactio management | ||
if (connection) { | ||
this._save(connection, noReload, callback); | ||
if (transaction) { | ||
this._save(transaction, noReload, callback); | ||
} | ||
else { | ||
this.getOrm().transaction().getDatabase().getConnection(function(err, connection){ | ||
if (err) callback(err); | ||
transaction = this.getDatabase().createTransaction(); | ||
this._save(transaction, noReload, function(err){ | ||
if (err) { | ||
transaction.rollback(function(transactionErr){ | ||
if (transactionErr) callback(transactionErr); | ||
else callback(err); | ||
}.bind(this)); | ||
} | ||
else { | ||
this._save(connection, noReload, function(err){ | ||
if (err) { | ||
connection.rollback(function(transactionErr){ | ||
if (transactionErr) callback(transactionErr); | ||
else callback(err); | ||
}.bind(this)); | ||
} | ||
else { | ||
connection.commit(function(err){ | ||
if (err) callback(err); | ||
else callback(null, this); | ||
}.bind(this)); | ||
} | ||
transaction.commit(function(err){ | ||
if (err) callback(err); | ||
else callback(null, this); | ||
}.bind(this)); | ||
@@ -283,3 +268,3 @@ } | ||
, _save: function(connection, noReload, callback) { | ||
, _save: function(transaction, noReload, callback) { | ||
var query = { | ||
@@ -309,3 +294,3 @@ from : this._defintion.getTableName() | ||
connection.query('update', query, callback); | ||
transaction.executeQuery('update', query, callback); | ||
} | ||
@@ -315,7 +300,7 @@ else callback(); | ||
else { | ||
connection.query('insert', query, function(err, result){ | ||
transaction.executeQuery('insert', query, function(err, result){ | ||
if (err) callback(err); | ||
else { | ||
if (result.type === 'id'){ | ||
// reload | ||
if(result.id) { | ||
@@ -325,3 +310,3 @@ if (this._defintion.primaryKeys.length === 1){ | ||
} | ||
else throw new Error('Cannot load record with more than one primarykey!'); | ||
else throw new Error('Cannot load record with more than one primarykey when at least on of the primary keys has an autoincermented value!'); | ||
} | ||
@@ -340,3 +325,3 @@ | ||
, _saveChildren: function(connection, noReload, callback) { | ||
, _saveChildren: function(transaction, noReload, callback) { | ||
@@ -370,3 +355,3 @@ // save references | ||
} | ||
else value._save(connection, noReload, next); | ||
else value._save(transaction, noReload, next); | ||
} | ||
@@ -373,0 +358,0 @@ }.bind(this), function(err, results){ |
@@ -31,3 +31,3 @@ !function(){ | ||
, definition : _options.definition | ||
, getOrm : _options.getOrm | ||
, getDatabase : _options.getDatabase | ||
}); | ||
@@ -51,3 +51,3 @@ | ||
, relatingSets : relatingSets | ||
, getOrm : _options.getOrm | ||
, getDatabase : _options.getDatabase | ||
}); | ||
@@ -83,3 +83,3 @@ | ||
// the events are global listeners for all model instances | ||
Constructor.__proto__ = new EventEmitter(); | ||
//Constructor.__proto__ = new EventEmitter(); | ||
@@ -164,3 +164,3 @@ | ||
// make sure the instantiated model can get the correct orm instance (support for transactions) | ||
CustomModel.getOrm = options.getOrm; | ||
CustomModel.getDatabase = options.getDatabase; | ||
@@ -167,0 +167,0 @@ |
153
lib/ORM.js
@@ -14,3 +14,2 @@ !function(){ | ||
var Database = require('./Database') | ||
, Functions = require('./Functions') | ||
, StaticORM = require('./StaticORM') | ||
@@ -26,11 +25,8 @@ , staticORM; | ||
, init: function(options) { | ||
delete this.parent; | ||
this._setProrperty('_options', options); | ||
this._setProrperty('_dbNames', []); | ||
this._setProrperty('_databases', {}); | ||
this.fn = new Functions(); | ||
// db connectivity | ||
this._initializeDatabase(options); | ||
this._initializeDatabases(options); | ||
@@ -43,103 +39,44 @@ this._initializeOrm(function(err){ | ||
, transaction: function(){ | ||
if (this.isTransaction) return this; | ||
else { | ||
var instance = { | ||
__proto__ : this | ||
, isTransaction : true | ||
, _db: { | ||
__proto__ : this._db | ||
, getConnection : function(callback){ | ||
this._getTransactionConnection(callback, instance); | ||
}.bind(this) | ||
} | ||
}; | ||
this._dbNames.forEach(function(databaseName){ | ||
instance[databaseName]._setOrm(instance); | ||
}.bind(this)); | ||
return instance; | ||
} | ||
, _setProrperty: function(name, value){ | ||
Object.defineProperty(this, name, {value: value}); | ||
} | ||
, commit: function(callback) { | ||
if (this._transactionConnection) { | ||
this._transactionConnection.commit(callback); | ||
delete this._transactionConnection; | ||
} | ||
else callback(new Error('Cannot commit transaction. no active transaction!')); | ||
} | ||
, rollback: function(callback) { | ||
if (this._transactionConnection) { | ||
this._transactionConnection.rollback(callback); | ||
delete this._transactionConnection; | ||
} | ||
else callback(new Error('Cannot rollback transaction. no active transaction!')); | ||
} | ||
, _getTransactionConnection: function(callback, instance) { | ||
if (instance._transactionConnection) { | ||
callback(null, instance._transactionConnection); | ||
} | ||
else { | ||
this._db.getConnection(false, function(err, connection){ | ||
if (err) callback(err); | ||
, _initializeOrm: function(callback) { | ||
async.each(this._dbNames | ||
// get definition from database | ||
, function(databaseName, next){ | ||
this._databases[databaseName].describe([databaseName], function(err, databases){ | ||
if (err) next(err); | ||
else { | ||
instance._transactionConnection = connection; | ||
instance._transactionConnection.startTransaction(); | ||
callback(null, instance._transactionConnection); | ||
// push config to next step | ||
next(null, databaseName, databases[databaseName]); | ||
} | ||
}.bind(this)); | ||
} | ||
} | ||
}.bind(this) | ||
, _setProrperty: function(name, value){ | ||
Object.defineProperty(this, name, {value: value}); | ||
} | ||
, _initializeOrm: function(callback) { | ||
this._db.describe(function(err, databases){ | ||
if (err) callback(err); | ||
// initialize orm per databse | ||
, function(databaseName, definition, next){ | ||
if (this[databaseName]) next(new Error('Failed to load ORM for database «'+databaseName+'», the name is reserved for the orm.').setName('ORMException')); | ||
else { | ||
// initialize orm for each database | ||
async.each(Object.keys(databases), function(databaseName, next){ | ||
if (this[databaseName]) next(new Error('Failed to load ORM for database «'+databaseName+'», the name is reserved for the orm.').setName('ORMException')); | ||
else { | ||
this._dbNames.push(databaseName); | ||
// create names for mapping / reference accessor, handle duplicates | ||
this._manageAccessorNames(databases[databaseName]); | ||
// create names for mapping / reference accessor, handle duplicates | ||
this._manageAccessorNames(definition); | ||
//log(databases[databaseName]); | ||
this[databaseName] = new Database({ | ||
orm: this | ||
, definition: databases[databaseName] | ||
, on: { | ||
load: function(){ | ||
setTimeout(next, 0); | ||
} | ||
} | ||
}); | ||
this[databaseName] = new Database({ | ||
orm: this | ||
, definition: definition | ||
, database: this._databases[databaseName] | ||
, on: { | ||
load: next | ||
} | ||
}.bind(this), function(err, results){ | ||
if (err) callback(err); | ||
else callback(); | ||
}.bind(this)); | ||
}); | ||
} | ||
}.bind(this)); | ||
}.bind(this) | ||
// check for errors | ||
, function(err, results){ | ||
if (err) callback(err); | ||
else callback(); | ||
}.bind(this)); | ||
} | ||
@@ -221,13 +158,25 @@ | ||
, getDatabase: function(){ | ||
return this._db; | ||
, getDatabase: function(id){ | ||
if (!type.string(id) || !id.length) throw new Error('cannot return a db without knowing which on to return (argument 0 must be the db id!)'); | ||
return this._databases[id]; | ||
} | ||
, _initializeDatabase: function(options){ | ||
this._setProrperty('_db', new DBCluster({type: options.db.type})); | ||
, _initializeDatabases: function(options){ | ||
if (type.object(options) && type.object(options.db)) { | ||
Object.keys(options.db).forEach(function(databaseName){ | ||
if (!type.string(options.db[databaseName].type)) throw new Error('['+databaseName+'] > Database type not in config specified (type: \'mysql\' / \'postgres\')!'); | ||
if (!type.array(options.db[databaseName].hosts) || !options.db[databaseName].hosts.length) throw new Error('['+databaseName+'] > Please add at least one host per db in the config!'); | ||
this._dbNames.push(databaseName); | ||
options.db.hosts.forEach(function(config){ | ||
this._db.addNode(config); | ||
}.bind(this)); | ||
this._databases[databaseName] = new DBCluster({type: options.db[databaseName].type}); | ||
options.db[databaseName].hosts.forEach(function(config){ | ||
config.database = databaseName; | ||
this._databases[databaseName].addNode(config); | ||
}.bind(this)); | ||
}.bind(this)); | ||
} | ||
else throw new Error('no database configuration present!'); | ||
} | ||
@@ -234,0 +183,0 @@ |
@@ -420,3 +420,3 @@ !function(){ | ||
orm : this._orm | ||
, getOrm : this._getOrm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
@@ -430,3 +430,3 @@ }).find(callback); | ||
orm : this._orm | ||
, getOrm : this._getOrm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
@@ -440,3 +440,3 @@ }).findOne(callback); | ||
orm : this._orm | ||
, getOrm : this._getOrm | ||
, getDatabase : this._getDatabase | ||
, resource : this._rootResource | ||
@@ -478,3 +478,3 @@ }).delete(callback); | ||
this._setProperty('_orm', options.orm); | ||
this._setProperty('_getOrm', options.getOrm); | ||
this._setProperty('_getDatabase', options.getDatabase); | ||
} | ||
@@ -481,0 +481,0 @@ |
@@ -16,3 +16,3 @@ !function(){ | ||
this._orm = options.orm; | ||
this.getOrm = options.getOrm; | ||
this.getDatabase = options.getDatabase; | ||
} | ||
@@ -215,11 +215,3 @@ | ||
, _executeQuery: function(mode, query, callback){ | ||
if (this.getOrm().isTransaction) { | ||
this.getOrm().getDatabase().getConnection(function(err, connection){ | ||
if (err) callback(err); | ||
else connection.query(mode, query, callback); | ||
}.bind(this)); | ||
} | ||
else { | ||
this.getOrm().getDatabase().query(mode, query, callback); | ||
} | ||
this.getDatabase().executeQuery(mode, query, callback); | ||
} | ||
@@ -226,0 +218,0 @@ |
@@ -26,4 +26,4 @@ !function(){ | ||
value: options.related | ||
} | ||
} | ||
, _column: { | ||
@@ -30,0 +30,0 @@ value: options.column |
{ | ||
"name" : "ee-orm" | ||
, "description" : "a simple yet powerful javascript orm for node.js" | ||
, "version" : "0.1.7" | ||
, "description" : "a simple yet powerful javascript orm for node.js supporting mysql & postgres" | ||
, "version" : "0.2.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-orm" | ||
@@ -36,3 +36,3 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
, "optionalDependencies": {} | ||
, "keywords" : [] | ||
, "keywords" : ["orm", "mysql", "postgres"] | ||
, "scripts": { | ||
@@ -39,0 +39,0 @@ "test" : "./node_modules/mocha/bin/mocha --reporter spec" |
16
test.js
@@ -21,3 +21,3 @@ | ||
while(i--) arr.push(1); | ||
//log(orm); | ||
log(orm); | ||
@@ -42,7 +42,7 @@ log.wtf('hui'); | ||
*/ | ||
/* | ||
orm.eventbox.venue.setMappingAccessorName('venue_media', 'media'); | ||
orm.eventbox.venue.setReferenceAccessorName('id_media', 'logo'); | ||
*/ | ||
//log(orm.eventbox.venue.getDefinition()); | ||
@@ -82,10 +82,10 @@ | ||
orm.eventbox.event().describeMethods(); | ||
//orm.eventbox.event().describeMethods(); | ||
var transaction = orm.transaction() | ||
, query = transaction.eventbox.event(['*']).limit(10).offset(100); | ||
var transaction = orm.eventbooster.createTransaction() | ||
, query = transaction.user(['*']).limit(10).offset(0); | ||
query.getMapping('event_media'); | ||
/*query.getMapping('event_media'); | ||
@@ -97,3 +97,3 @@ query.getEventLocale(['subtitle', 'description']).getLanguage().filter({language: 'de'}); | ||
query.getSale(['*']); | ||
query.fetchMedia(['*']); | ||
query.fetchMedia(['*']);*/ | ||
@@ -100,0 +100,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78823
2175