Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

downstairs

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

downstairs - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

13

config/defaults.json

@@ -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"
}
}
}

83

lib/table.js

@@ -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();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc