Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3740032
Showing
9 changed files
with
1,123 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/fast_legs'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
var Statements = require("./statements") | ||
, async = require("../../support/async"); | ||
|
||
function Base(client) { | ||
this.client = client; | ||
}; | ||
|
||
Base.prototype.extend = function(obj) { | ||
return _.extend(obj, FastLegS.Base); | ||
}; | ||
|
||
Base.prototype.truncate = function(opts, callback) { | ||
if (typeof opts === 'function') { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
|
||
var truncateStatement = Statements.truncate(this, opts); | ||
this.client.emit('query', truncateStatement, function(err, result) { | ||
callback(err, !result.rowCount); | ||
}); | ||
}; | ||
|
||
Base.prototype.create = function(obj, callback) { | ||
var self = this; | ||
|
||
var createQuery = function() { | ||
var insertStatement = Statements.insert(self, obj); | ||
self.client.emit('query', insertStatement, function(err, result) { | ||
callback(err, result.rowCount); | ||
}); | ||
}; | ||
|
||
loadSchema(self, createQuery); | ||
}; | ||
|
||
Base.prototype.find = function(selector, opts, callback) { | ||
var self = this; | ||
|
||
var findQuery = function() { | ||
if (_.isFunction(opts)) { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
|
||
var selectStatement = Statements.select(self, selector, opts); | ||
self.client.emit('query', selectStatement, function(err, result) { | ||
var results = null; | ||
|
||
if (_(opts.include).isUndefined()) { | ||
if (_(selector).isArray()) | ||
results = _.isUndefined(result) ? [] : result.rows; | ||
else if (_(selector).isNumber() || _(selector).isString()) | ||
results = _.isUndefined(result.rows[0]) ? null : result.rows[0]; | ||
else | ||
results = _.isUndefined(result) ? [] : result.rows; | ||
|
||
callback(err, results); | ||
} else { | ||
results = _.isUndefined(result) ? [] : result.rows; | ||
handleIncludes(self, results, selector, opts.include, callback); | ||
} | ||
}); | ||
}; | ||
|
||
loadSchema(self, findQuery); | ||
}; | ||
|
||
Base.prototype.destroy = function(selector, callback) { | ||
var self = this; | ||
|
||
var destroyQuery = function() { | ||
if (_.isFunction(selector)) { | ||
callback = selector; | ||
selector = {}; | ||
} | ||
|
||
var destroyStatement = Statements.destroy(self, selector); | ||
self.client.emit('query', destroyStatement, function(err, results) { | ||
var deletedRows = _(results).isUndefined() ? 0 : results.rowCount; | ||
callback(err, deletedRows); | ||
}); | ||
} | ||
|
||
loadSchema(self, destroyQuery); | ||
}; | ||
|
||
var handleIncludes = function(self, models, selector, includes, callback) { | ||
var includedModels = _(includes).keys(); | ||
|
||
var findIncludes = function(model, callback) { | ||
var finders = {}; | ||
_(includedModels).each(function(includeFinder) { | ||
finders[includeFinder] = function(cb) { | ||
var includedModel = _(self.many).select(function(m) { | ||
return _(_(m).keys()).include(includeFinder); | ||
})[0]; | ||
|
||
var where = _(includes[includeFinder].where).isUndefined() ? | ||
{} : includes[includeFinder].where; | ||
|
||
if (!_(includedModel).isUndefined()) { | ||
var primaryKeySelector = {}; | ||
primaryKeySelector[includedModel.joinOn] = model[self.primaryKey]; | ||
var selector = _.extend(primaryKeySelector, where); | ||
|
||
delete includes[includeFinder].where; | ||
|
||
includedModel[includeFinder].find(selector, | ||
includes[includeFinder], | ||
function(err, results) { | ||
cb(err, results); | ||
}); | ||
} else { | ||
cb(null, []); | ||
} | ||
} | ||
}); | ||
|
||
async.parallel(finders, function(err, results) { | ||
var models = _.extend(model, results); | ||
callback(err, models); | ||
}); | ||
}; | ||
|
||
async.map(models, findIncludes, function(err, results) { | ||
var formattedResults = null; | ||
|
||
if (_(selector).isArray()) | ||
formattedResults = _.isUndefined(results) ? [] : results; | ||
else if (_(selector).isNumber() || _(selector).isString()) | ||
formattedResults = _.isUndefined(results) ? null : results[0]; | ||
else | ||
formattedResults = _.isUndefined(results) ? [] : results; | ||
|
||
callback(err, formattedResults); | ||
}); | ||
}; | ||
|
||
var loadSchema = function(model, query) { | ||
var statement = Statements.information(model); | ||
|
||
if (_(model._fields).isUndefined()) { | ||
model.client.emit('query', statement, function(err, result) { | ||
model._fields = result.rows; | ||
query(); | ||
}); | ||
} else { | ||
query(); | ||
} | ||
}; | ||
|
||
module.exports = Base; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var pg = require(__dirname + "/../../support/node-postgres/lib") | ||
, EventEmitter = require('events').EventEmitter; | ||
|
||
function Client(connParams) { | ||
this.connParams = connParams; | ||
this.connected = false; | ||
}; | ||
|
||
Client.prototype.__proto__ = EventEmitter.prototype; | ||
|
||
Client.prototype.connect = function() { | ||
this.client = new pg.Client(this.connParams); | ||
this.on('query', function(query, callback) { | ||
this.connected || this.doConnect(); | ||
this.client.query(query, function(err, result) { | ||
callback(err, result); | ||
}); | ||
}); | ||
} | ||
|
||
Client.prototype.disconnect = function() { | ||
if (this.client.queryQueue.length === 0) { | ||
this.client.end(); | ||
} else { | ||
this.client.on('drain', this.client.end.bind(this.client)); | ||
} | ||
} | ||
|
||
Client.prototype.doConnect = function() { | ||
this.client.connect(); | ||
return this.connected = true; | ||
} | ||
|
||
module.exports = Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
exports.version = '0.0.1'; | ||
|
||
var Base = require('./base') | ||
, Client = require('./client'); | ||
|
||
var FastLegS = function() { | ||
|
||
}; | ||
|
||
FastLegS.prototype.connect = function(connParams) { | ||
var client = new Client(connParams); | ||
client.connect(); | ||
this.Base = new Base(client); | ||
this.client = client; | ||
return this; | ||
}; | ||
|
||
module.exports = exports = new FastLegS(); |
Oops, something went wrong.