downstairs
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -7,29 +7,11 @@ var pg = require('pg'); | ||
PGConnection.prototype.query = function(query, cb) { | ||
pg.connect(this.connectionString, function(err, client) { | ||
//log connection errors | ||
client.query(query, function(err, result) { | ||
//log query errors | ||
cb(err, result); | ||
}); | ||
pg.connect(this.connectionString, function(err, client) { | ||
//log connection errors | ||
client.query(query, function(err, result) { | ||
//log query errors | ||
cb(err, result); | ||
}); | ||
}; | ||
}); | ||
}; | ||
module.exports = PGConnection; | ||
// module.exports = function(connectionString) { | ||
// this.connectionString = connectionString; | ||
// var _self = this; | ||
// this.query = function(query, cb) { | ||
// pg.connect(_self.connectionString, function(err, client) { | ||
// //log connection errors | ||
// client.query(query, function(err, result) { | ||
// //log query errors | ||
// cb(err, result); | ||
// }); | ||
// }); | ||
// }; | ||
// } | ||
var Table = {} | ||
, async = require('async') | ||
, _ = require('underscore') | ||
, Model = require('./model'); | ||
, Record = require('./record'); | ||
//Table.registry = {}; | ||
Table.Downstairs; | ||
@@ -13,2 +12,24 @@ | ||
var jsonConditionsToSQL = function(Model, conditions){ | ||
var clauses = []; | ||
for (var key in conditions){ | ||
var clause = Model.sql[key].equals(conditions[key]); | ||
clauses.push(clause); | ||
} | ||
var anded = ander(clauses); | ||
return anded; | ||
} | ||
var ander = function(clauses){ | ||
var base = clauses.shift(); | ||
var chainer = function(clause){ | ||
base = base.and(clause); | ||
}; | ||
clauses.forEach(chainer, base); | ||
return base; | ||
} | ||
/* | ||
@@ -28,2 +49,3 @@ * mixin behaviours for all models go here | ||
if (conditions){ | ||
var conditions = jsonConditionsToSQL(this, conditions); | ||
sqlBaseQuery = sqlBaseQuery.where(conditions); | ||
@@ -54,2 +76,3 @@ } | ||
Table.find = function(conditions, cb){ | ||
if (typeof conditions === 'function') { | ||
@@ -133,17 +156,9 @@ cb = conditions; | ||
var createCb = function(err, results){ | ||
var result = false; | ||
if (results.rowCount > 0 && results.command === 'INSERT') { | ||
return _self.connection.query("SELECT LASTVAL();", fetchCb); | ||
if (results) { | ||
return _self.find(data, cb); | ||
} else { | ||
return _cb(err, null); | ||
} | ||
return _cb(err, result); | ||
} | ||
var fetchCb = function(err, results) { | ||
if (results.rowCount > 0 /*&& results.command === 'SELECT'*/) { | ||
var id = results.rows[0].lastval; | ||
_self.find(_self.sql.id.equals(id), _cb); | ||
} | ||
} | ||
this.connection.query(sqlStr, createCb); | ||
@@ -207,3 +222,3 @@ } | ||
var ModelImpl = function(properties){ | ||
var Model = function(properties){ | ||
this.properties = properties; | ||
@@ -240,18 +255,18 @@ var validationCycle = []; | ||
ModelImpl.prototype = new Model(); | ||
ModelImpl.prototype.constructor = Model; | ||
Model.prototype = new Record(); | ||
Model.prototype.constructor = Record; | ||
ModelImpl.connection = dbConnection; | ||
ModelImpl.sql = sql; | ||
ModelImpl.prototype.sql = sql; | ||
ModelImpl.Downstairs = Table.Downstairs; | ||
ModelImpl.name = name; | ||
ModelImpl.prototype.validations = validations; | ||
Model.connection = dbConnection; | ||
Model.sql = sql; | ||
Model.prototype.sql = sql; | ||
Model.Downstairs = Table.Downstairs; | ||
Model.name = name; | ||
Model.prototype.validations = validations; | ||
mixinTableFunctions(ModelImpl); | ||
mixinTableFunctions(Model); | ||
dbConnection.register(name, ModelImpl); | ||
return ModelImpl; | ||
dbConnection.register(name, Model); | ||
return Model; | ||
} | ||
module.exports = Table; |
{ | ||
"name": "downstairs", | ||
"description": "A light ORM wrapped about brianc's node-sql and node-pg", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"homepage": "https://github.com/moneytribeaustralia/downstairs.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -45,2 +45,3 @@ var Downstairs = require('../lib/downstairs') | ||
Downstairs.add(myDefaultPGConnection); | ||
var User = Table.model('User', userSQL); | ||
@@ -51,2 +52,3 @@ User.create({username: 'fred2', password: 'nottelling', email: 'test2@test.com'}, function(err, user) { | ||
should.exist(user.id); | ||
user.username.should.equal('fred2'); | ||
done(); | ||
@@ -53,0 +55,0 @@ }); |
@@ -5,7 +5,11 @@ var Downstairs = require('../lib/downstairs') | ||
, sql = require('sql') | ||
, Connection = require('../lib/connections/connection'); | ||
, Connection = require('../lib/connections/connection') | ||
, helper = require('./helper') | ||
, ectypes = helper.ectypes | ||
, env = require('./../config/env'); | ||
//Table.use(Downstairs); | ||
Downstairs.add(new Connection()); //a dummy connection | ||
var pgConnection = new Downstairs.Connection.PostgreSQL(env.connectionString); | ||
Downstairs.add(pgConnection); | ||
@@ -56,3 +60,22 @@ var userSQL = sql.Table.define({ | ||
Role.sql.should.equal(roleSQL); | ||
}); | ||
}); | ||
describe('Table level behaviours', function() { | ||
beforeEach(function(done){ | ||
helper.resetDb(helper.userTableSQL, done); | ||
}) | ||
}) | ||
it('finds a record with a where JSON data structure clause', function(done) { | ||
var User = Table.model('User', userSQL); | ||
var data = {password: '5f4dcc3b5aa765d61d8327deb882cf99', username: 'fred', email: 'fred@moneytribe.com.au'}; | ||
ectypes.User.create(data, function(err, results) { | ||
User.find({username: 'fred', email: 'fred@moneytribe.com.au'} , function(err, user){ | ||
should.exist(user); | ||
user.username.should.equal('fred'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -63,3 +63,3 @@ var Downstairs = require('../lib/downstairs.js') | ||
uniqueUsername: function(cb){ | ||
User.find(this.sql.username.equals(this.username), function(errs, user){ | ||
User.find({username: user.username}, function(errs, user){ | ||
if (user){ | ||
@@ -66,0 +66,0 @@ cb(null, "User already exists with username, id: ", user.id); |
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
40497
895