downstairs
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -6,3 +6,3 @@ { | ||
"port": "5432", | ||
"user": "nicholas", | ||
"user": "damien", | ||
"password": null, | ||
@@ -16,7 +16,16 @@ "name": "downstairs_development" | ||
"port": "5432", | ||
"user": "nicholas", | ||
"user": "damien", | ||
"password": null, | ||
"name": "downstairs_test" | ||
} | ||
}, | ||
"production": { | ||
"database": { | ||
"host": "localhost", | ||
"port": "5432", | ||
"user": "damien", | ||
"password": null, | ||
"name": "downstairs_production" | ||
} | ||
} | ||
} |
@@ -34,4 +34,25 @@ var Table = {} | ||
var parseConditions = function (conditions, _self, sqlBaseQuery) { | ||
if (conditions){ | ||
var sqlConditions = jsonConditionsToSQL(_self, conditions); | ||
if (sqlConditions) { | ||
return sqlBaseQuery.where(sqlConditions); | ||
} | ||
} | ||
return sqlBaseQuery; | ||
} | ||
var cleanData = function(sql, data){ | ||
var objectKeys = _.keys(data); | ||
var differences = _.difference(objectKeys, sql._initialConfig.columns); | ||
differences.forEach(function(diff){ | ||
delete data[diff]; | ||
}) | ||
return data; | ||
} | ||
/* | ||
* mixin behaviours for all models go here | ||
* Mixin behaviours for all models go here | ||
*/ | ||
@@ -48,9 +69,3 @@ Table.findAll = function(conditions, cb){ | ||
var sqlBaseQuery = this.sql.select(this.sql.star()).from(this.sql); | ||
if (conditions){ | ||
var sqlConditions = jsonConditionsToSQL(this, conditions); | ||
if (sqlConditions) { | ||
sqlBaseQuery = sqlBaseQuery.where(sqlConditions); | ||
} | ||
} | ||
sqlBaseQuery = parseConditions(conditions, this, sqlBaseQuery); | ||
sqlStr = sqlBaseQuery.toQuery(); | ||
@@ -74,3 +89,2 @@ | ||
// DEBUG | ||
if (!this.connection.connectionString) { | ||
@@ -91,3 +105,8 @@ this.connection.connectionString = this.Downstairs.connectionString; | ||
var findCb = function(err, models){ | ||
cb(err, models[0]); | ||
if (models && models[0]) { | ||
cb(err, models[0]); | ||
} | ||
else { | ||
cb(err, null); | ||
} | ||
} | ||
@@ -98,14 +117,34 @@ | ||
Table.count = function(conditions, cb){ | ||
var results = []; | ||
var cleanData = function(sql, data){ | ||
var objectKeys = _.keys(data); | ||
var differences = _.difference(objectKeys, sql._initialConfig.columns); | ||
if (typeof conditions === 'function') { | ||
cb = conditions; | ||
conditions = null; | ||
} | ||
differences.forEach(function(diff){ | ||
delete data[diff]; | ||
}) | ||
var sqlStr; | ||
var sqlBaseQuery = this.sql.select('COUNT(*)').from(this.sql); | ||
sqlBaseQuery = parseConditions(conditions, this, sqlBaseQuery); | ||
sqlStr = sqlBaseQuery.toQuery(); | ||
return data; | ||
} | ||
var _self = this; | ||
var _cb = cb; | ||
var countCb = function(err, results){ | ||
if (results && results.rows && results.rows[0] && results.rows[0].count) { | ||
_cb(err, results.rows[0].count); | ||
} | ||
else { | ||
_cb(err, 0); | ||
} | ||
} | ||
if (!this.connection.connectionString) { | ||
this.connection.connectionString = this.Downstairs.connectionString; | ||
} | ||
this.connection.query(sqlStr, countCb); | ||
}; | ||
Table.update = function(data, conditions, cb){ | ||
@@ -141,10 +180,4 @@ if (typeof data === 'function'){ | ||
var sqlBaseQuery = this.sql.update(data); | ||
sqlBaseQuery = parseConditions(conditions, this, sqlBaseQuery); | ||
if (conditions){ | ||
var sqlConditions = jsonConditionsToSQL(this, conditions); | ||
if (sqlConditions) { | ||
sqlBaseQuery = sqlBaseQuery.where(sqlConditions); | ||
} | ||
} | ||
sqlStr = sqlBaseQuery.toQuery(); | ||
@@ -151,0 +184,0 @@ |
{ | ||
"name": "downstairs", | ||
"description": "A light ORM wrapped about brianc's node-sql and node-pg", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/moneytribeaustralia/downstairs.js", | ||
@@ -41,3 +41,3 @@ "author": { | ||
"dependencies": { | ||
"pg": "~0.8.4", | ||
"pg": ">=0.8.0", | ||
"lingo": "0.0.5", | ||
@@ -44,0 +44,0 @@ "nconf": "~0.6.4", |
@@ -103,2 +103,14 @@ var Downstairs = require('../lib/downstairs') | ||
it('finds all records with a populated 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({username: 'fred'} , function(err, user){ | ||
should.exist(user); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('updates a record with JSON condition', function(done){ | ||
@@ -182,2 +194,53 @@ var User = Table.model('User', userSQL); | ||
// | ||
it('counts 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.count({} , function(err, count){ | ||
should.exist(count); | ||
count.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('counts 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.count(null , function(err, count){ | ||
should.exist(count); | ||
count.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('counts records with a populated 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.count({ username: 'fred'} , function(err, count){ | ||
should.exist(count); | ||
count.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('returns zero count if it cannot find a match', function(done) { | ||
var User = Table.model('User', userSQL); | ||
User.count({ username: 'fred'} , function(err, count){ | ||
should.exist(count); | ||
count.should.equal(0); | ||
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
0
41503
27
938
1
+ Addedpg@8.13.1(transitive)
+ Addedpg-cloudflare@1.1.1(transitive)
+ Addedpg-connection-string@2.7.0(transitive)
+ Addedpg-int8@1.0.1(transitive)
+ Addedpg-pool@3.7.0(transitive)
+ Addedpg-protocol@1.7.0(transitive)
+ Addedpg-types@2.2.0(transitive)
+ Addedpgpass@1.0.5(transitive)
+ Addedpostgres-array@2.0.0(transitive)
+ Addedpostgres-bytea@1.0.0(transitive)
+ Addedpostgres-date@1.0.7(transitive)
+ Addedpostgres-interval@1.2.0(transitive)
+ Addedsplit2@4.2.0(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedgeneric-pool@1.0.12(transitive)
- Removedpg@0.8.8(transitive)
Updatedpg@>=0.8.0