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

downstairs

Package Overview
Dependencies
Maintainers
3
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.3.0a to 0.3.1

207

lib/collection.js

@@ -52,89 +52,2 @@ var Collection = {}

var jsonConditionsToSQL = function(Model, conditions){
var clauses = [];
for (var key in conditions){
if (Model.sql[key]) {
if (typeof conditions[key] === 'null') {
var clause = Model.sql[key].isNull();
clauses.push(clause);
}
else if (conditions[key]) {
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;
}
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
*/
Collection.getConnection = function(){
if (!this.connection.connectionString) {
this.connection.connectionString = this.Downstairs.connectionString;
}
return this.connection;
}
select = function(model, conditions, cb){
var sqlBaseQuery = model.sql.select(model.sql.star()).from(model.sql);
sqlBaseQuery = parseConditions(conditions, model, sqlBaseQuery);
var sqlStr = sqlBaseQuery.toQuery();
if (conditions && conditions.queryParameters){
if (conditions.queryParameters.orderBy){
sqlStr.text = sqlStr.text + " ORDER BY " + conditions.queryParameters['orderBy'];
}
if (conditions.queryParameters.limit){
sqlStr.text = sqlStr.text + " LIMIT " + conditions.queryParameters['limit'];
}
// if (conditions.queryParameters.offset){
// sqlStr.text = sqlStr.text + " OFFSET " + conditions.queryParameters['offset'];
// }
}
model.getConnection().query(sqlStr, cb);
}
Collection.findAll = function(conditions, cb){

@@ -196,19 +109,2 @@ var modelCallbacks, queryEvents;

var endQuery = function(model, queryEvents, modelCallbacks, data, cb, err){
if (err){
return cb(err, null);
}
if (queryEvents){
model.emitQueryEvents(queryEvents, data);
}
if (modelCallbacks){
model.runModelCallbacks(modelCallbacks, data, cb);
}
else {
cb(null, data);
}
}
Collection.count = function(conditions, cb){

@@ -306,4 +202,107 @@ var results = [];

Collection.getConnection = function(){
if (!this.connection.connectionString) {
this.connection.connectionString = this.Downstairs.connectionString;
}
return this.connection;
}
/*
* Called to finish a query lifecycle - currently only by find and findAll.
*/
var endQuery = function(model, queryEvents, modelCallbacks, data, cb, err){
if (err){
return cb(err, null);
}
if (queryEvents){
model.emitQueryEvents(queryEvents, data);
}
if (modelCallbacks){
model.runModelCallbacks(modelCallbacks, data, cb);
}
else {
cb(null, data);
}
}
var jsonConditionsToSQL = function(Model, conditions){
var clauses = [];
for (var key in conditions){
if (Model.sql[key]) {
if (typeof conditions[key] === 'null' || typeof conditions[key] === 'undefined' ) {
var clause = Model.sql[key].isNull();
clauses.push(clause);
}
else if (conditions[key]) {
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;
}
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;
}
select = function(model, conditions, cb){
var sqlBaseQuery = model.sql.select(model.sql.star()).from(model.sql);
sqlBaseQuery = parseConditions(conditions, model, sqlBaseQuery);
var sqlStr = sqlBaseQuery.toQuery();
if (conditions && conditions.queryParameters){
if (conditions.queryParameters.orderBy){
sqlStr.text = sqlStr.text + " ORDER BY " + conditions.queryParameters['orderBy'];
}
if (conditions.queryParameters.limit){
sqlStr.text = sqlStr.text + " LIMIT " + conditions.queryParameters['limit'];
}
// if (conditions.queryParameters.offset){
// sqlStr.text = sqlStr.text + " OFFSET " + conditions.queryParameters['offset'];
// }
}
model.getConnection().query(sqlStr, cb);
}
/*
* Associations.

@@ -310,0 +309,0 @@ */

{
"name": "downstairs",
"description": "A light ORM wrapped about brianc's node-sql and node-pg",
"version": "0.3.0a",
"version": "0.3.1",
"homepage": "https://github.com/moneytribeaustralia/downstairs.js",

@@ -6,0 +6,0 @@ "author": {

@@ -68,2 +68,14 @@ var Downstairs = require('../lib/downstairs')

it('returns nothing if finding with an undefined id', function(done){
var User = Collection.model('User', helper.userConfig);
var data = {password: '5f4dcc3b5aa765d61d8327deb882cf99', username: 'fred', email: 'fred@moneytribe.com.au' };
ectypes.User.create(data, function(err, results) {
var id;
User.find({ id: id } , function(err, user){
should.not.exist(user);
done();
});
});
});
it('finds all records with an empty object JSON condition', function(done) {

@@ -70,0 +82,0 @@ var User = Collection.model('User', helper.userConfig);

@@ -8,3 +8,4 @@ var Downstairs = require('../lib/downstairs')

, helper = require('./helper')
, Validator = require('validator').Validator;
, Validator = require('validator').Validator
, async = require('async');

@@ -92,3 +93,3 @@ var pgConnection = new Downstairs.Connection.PostgreSQL(env.connectionString);

describe('defining callbacks on the Model that are run on a Record', function(done){
describe('defining callbacks on the Model that are run on the Record', function(done){
beforeEach(function(done) {

@@ -98,3 +99,3 @@ helper.resetDb(helper.userSQL + helper.roleSQL, done);

it("a callback for eagerly loading the user's role", function(done) {
it("after find", function(done) {
var pgConnection = new Connection.PostgreSQL(env.connectionString);

@@ -122,2 +123,32 @@ Downstairs.add(pgConnection);

});
it("after findAll", function(done) {
var pgConnection = new Connection.PostgreSQL(env.connectionString);
Downstairs.add(pgConnection);
var User = Collection.model('User', helper.userConfig);
var Role = Collection.model('Role', helper.roleConfig);
Role.hasMany(User);
User.belongsTo(Role);
var loadRole = function(users, cb){
async.forEach(users
, function(user, cb2){user.get('role', cb2)}
, cb)
};
User.when('securityDisplay', loadRole);
Role.create({name: 'admin'}, function(err, role){
User.create({role_id: role.id, username: 'donald'}, function(err, user) {
User.create({role_id: role.id, username: 'mary'}, function(err, user) {
User.findAll({callbacks: ['securityDisplay']}, function(err, users){
users[0].role.name.should.equal('admin');
users[1].role.name.should.equal('admin');
done();
});
});
});
});
});
})

@@ -124,0 +155,0 @@

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