downstairs
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -13,15 +13,25 @@ var Downstairs = {}; | ||
if (!name) { | ||
name = "default"; | ||
name = 'default'; | ||
} | ||
Downstairs.connections[name] = connection; | ||
if (connection && connection.connectionString) { | ||
Downstairs.connections[name] = connection; | ||
} else { | ||
throw new Error('You cannot add a connection without a connection string.'); | ||
} | ||
}; | ||
Downstairs.get = function(connectionName){ | ||
if (!connectionName) { | ||
connectionName = 'default'; | ||
} | ||
return Downstairs.connections[connectionName]; | ||
}; | ||
Downstairs.clear = function() { | ||
Downstairs.connections = {}; | ||
} | ||
Downstairs.Table = Table; | ||
Downstairs.Connection = Connection; | ||
module.exports = Downstairs; | ||
@@ -48,4 +48,6 @@ var Table = {} | ||
if (conditions){ | ||
var conditions = jsonConditionsToSQL(this, conditions); | ||
sqlBaseQuery = sqlBaseQuery.where(conditions); | ||
var sqlConditions = jsonConditionsToSQL(this, conditions); | ||
if (sqlConditions) { | ||
sqlBaseQuery = sqlBaseQuery.where(sqlConditions); | ||
} | ||
} | ||
@@ -71,2 +73,7 @@ | ||
// DEBUG | ||
if (!this.connection.connectionString) { | ||
this.connection.connectionString = this.Downstairs.connectionString; | ||
} | ||
this.connection.query(sqlStr, finderAllCb); | ||
@@ -119,4 +126,6 @@ }; | ||
if (conditions){ | ||
var conditions = jsonConditionsToSQL(this, conditions); | ||
sqlBaseQuery = sqlBaseQuery.where(conditions); | ||
var sqlConditions = jsonConditionsToSQL(this, conditions); | ||
if (sqlConditions) { | ||
sqlBaseQuery = sqlBaseQuery.where(sqlConditions); | ||
} | ||
} | ||
@@ -231,3 +240,2 @@ | ||
for (var validation in this.validations){ | ||
@@ -267,3 +275,8 @@ this[validation] = this.validations[validation]; | ||
dbConnection.register(name, Model); | ||
if (!dbConnection){ | ||
throw new Error('There is no connection defined. Make sure you have called Downstairs.add(connection)'); | ||
} | ||
else { | ||
dbConnection.register(name, Model); | ||
} | ||
return Model; | ||
@@ -270,0 +283,0 @@ } |
{ | ||
"name": "downstairs", | ||
"description": "A light ORM wrapped about brianc's node-sql and node-pg", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"homepage": "https://github.com/moneytribeaustralia/downstairs.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -10,2 +10,3 @@ var Connection = require('./../../lib/connections/connection') | ||
var connection = new Connection(); | ||
connection.connectionString = 'connection string'; | ||
Downstairs.add(connection); | ||
@@ -12,0 +13,0 @@ var User = Table.model('User', helper.userSQL); |
@@ -9,5 +9,9 @@ var Downstairs = require('../lib/downstairs.js') | ||
beforeEach(function() { | ||
Downstairs.clear(); | ||
}); | ||
describe('storing connections', function(){ | ||
it('stores a default connection', function(){ | ||
var dummyConnection = {}; | ||
var dummyConnection = {connectionString: 1}; | ||
Downstairs.add(dummyConnection); | ||
@@ -18,10 +22,10 @@ Downstairs.get('default').should.equal(dummyConnection); | ||
it('stores a named connection', function(){ | ||
var dummyConnection = {}; | ||
var dummyConnection = {connectionString: 1}; | ||
Downstairs.add(dummyConnection, 'primaryDb'); | ||
Downstairs.get('primaryDb').should.equal(dummyConnection); | ||
}) | ||
}); | ||
it('stores a named connection *and* a default connection', function(){ | ||
var dummyConnection = {id: 1}; | ||
var dummyConnection2 = {id: 2}; | ||
var dummyConnection = {connectionString: 2}; | ||
var dummyConnection2 = {connectionString: 3}; | ||
Downstairs.add(dummyConnection, 'primaryDb'); | ||
@@ -31,5 +35,21 @@ Downstairs.add(dummyConnection2); | ||
Downstairs.get('default').should.equal(dummyConnection2); | ||
}) | ||
}); | ||
it('does not store a connection which is null', function() { | ||
var dummyConnection = {connectionString: 4}; | ||
try { | ||
Downstairs.add(null); | ||
} catch (exception) { | ||
// Do nothing | ||
} | ||
should.not.exist(Downstairs.get('default')); | ||
}); | ||
it('fetches a default connection without a name', function() { | ||
var dummyConnection = {connectionString: 5}; | ||
Downstairs.add(dummyConnection); | ||
Downstairs.get().should.equal(dummyConnection); | ||
}); | ||
}); | ||
}); |
@@ -39,2 +39,3 @@ var pg = require('pg') | ||
, quote: true | ||
, schema: 'public' | ||
, columns: ['id' | ||
@@ -52,3 +53,3 @@ , 'username' | ||
(\ | ||
id bigserial NOT NULL,\ | ||
id serial NOT NULL,\ | ||
username character varying(100) unique NOT NULL,\ | ||
@@ -55,0 +56,0 @@ created_at timestamp with time zone NOT NULL DEFAULT now(),\ |
@@ -14,2 +14,3 @@ var Downstairs = require('../lib/downstairs') | ||
, quote: true | ||
, schema: 'public' | ||
, columns: ['id' | ||
@@ -16,0 +17,0 @@ , 'username' |
@@ -10,4 +10,2 @@ var Downstairs = require('../lib/downstairs') | ||
//Table.use(Downstairs); | ||
var pgConnection = new Downstairs.Connection.PostgreSQL(env.connectionString); | ||
@@ -19,2 +17,3 @@ Downstairs.add(pgConnection); | ||
, quote: true | ||
, schema: 'public' | ||
, columns: ['id' | ||
@@ -82,2 +81,26 @@ , 'username' | ||
it('finds all records with an empty object JSON condition', 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.findAll({} , function(err, user){ | ||
should.exist(user); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('finds all records with a null JSON condition', 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.findAll(null , function(err, user){ | ||
should.exist(user); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('updates a record with JSON condition', function(done){ | ||
@@ -95,2 +118,28 @@ var User = Table.model('User', userSQL); | ||
}) | ||
it('updates a record with an empty object JSON condition', 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.update({username: 'fredUpdated'}, {}, function(err, result){ | ||
should.not.exist(err); | ||
result.should.be.ok; | ||
done(); | ||
}); | ||
}) | ||
}) | ||
it('updates a record with an empty object JSON condition', 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.update({username: 'fredUpdated'}, null, function(err, result){ | ||
should.not.exist(err); | ||
result.should.be.ok; | ||
done(); | ||
}); | ||
}) | ||
}) | ||
}); |
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
44102
992