UNPKG

mesa

Version:

simple elegant sql for nodejs

332 lines (315 loc) 8.8 kB
// Generated by CoffeeScript 1.10.0 var _, mesa, mohair; _ = require('underscore'); mesa = require('./mesa'); module.exports = Object.create(mesa); mohair = module.exports._mohair.escape(function(string) { return string.split(".").map(function(str) { return "\"" + str + "\""; }).join('.'); }); module.exports._mohair = mohair; module.exports._originalMohair = mohair; module.exports.getConnection = function(cb) { var connection; connection = this._connection; if (connection == null) { throw new Error("the method you are calling requires a call to connection() before it"); } if ('function' === typeof connection) { return connection(cb); } return process.nextTick(function() { return cb(null, connection); }); }; module.exports.replacePlaceholders = function(sql) { var index; index = 1; return sql.replace(/\?/g, function() { return '$' + index++; }); }; module.exports.returning = function(arg) { if ('string' !== typeof arg) { throw new Error('must be a string'); } if (arg.length === 0) { throw new Error('must not be the empty string'); } return this.set('_returning', arg); }; module.exports.insert = function(data, cb) { var cleanData, query, returning, self, sql; self = this; self.assertConnection(); self.assertTable(); self.assertAttributes(); cleanData = _.pick(data, self._attributes); if (Object.keys(cleanData).length === 0) { throw new Error('nothing to insert'); } query = self._mohair.insert(cleanData); returning = self._returning ? self._returning : self._primaryKey; sql = self.replacePlaceholders(query.sql() + (" RETURNING " + returning)); return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } return connection.query(sql, query.params(), function(err, results) { var row; if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } row = results.rows[0]; if (typeof done === "function") { done(); } return cb(null, self._returning != null ? row : row[self._primaryKey]); }); }); }; module.exports.insertMany = function(array, cb) { var query, returning, self, sql; self = this; self.assertConnection(); self.assertTable(); self.assertAttributes(); query = self._mohair.insertMany(array.map((function(_this) { return function(x) { return _.pick(x, self._attributes); }; })(this))); returning = self._returning ? self._returning : self._primaryKey; sql = self.replacePlaceholders(query.sql() + (" RETURNING " + returning)); return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } return connection.query(sql, query.params(), function(err, results) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } if (typeof done === "function") { done(); } return cb(null, self._returning != null ? results.rows : _.pluck(results.rows, self._primaryKey)); }); }); }; module.exports["delete"] = function(cb) { var query, self, sql; self = this; self.assertConnection(); self.assertTable(); query = self._mohair["delete"](); sql = self.replacePlaceholders(query.sql()); return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } return connection.query(sql, query.params(), function(err, results) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } if (typeof done === "function") { done(); } return cb(null, results); }); }); }; module.exports.update = function(updates, cb) { var cleanUpdates, query, self, sql; self = this; self.assertConnection(); self.assertTable(); self.assertAttributes(); cleanUpdates = _.pick(updates, self._attributes); if (Object.keys(cleanUpdates).length === 0) { throw new Error('nothing to update'); } query = self._mohair.update(cleanUpdates); sql = self.replacePlaceholders(query.sql()); if (self._returning != null) { sql += " RETURNING " + self._returning; } return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } return connection.query(sql, query.params(), function(err, results) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } if (typeof done === "function") { done(); } if (self._returning == null) { return cb(null, results); } return cb(null, results.rows); }); }); }; module.exports.first = function(cb) { var params, self, sql; self = this; self.assertConnection(); sql = self.replacePlaceholders(self.sql()); params = self.params(); return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } if (typeof self.hookBeforeFirst === "function") { self.hookBeforeFirst(self, connection, sql, params); } return connection.query(sql, params, function(err, results) { var record; if (typeof done === "function") { done(); } if (typeof self.hookAfterFirst === "function") { self.hookAfterFirst(self, connection, sql, params, err, results); } if (err != null) { cb(err); return; } record = results.rows[0]; if (record == null) { cb(null, null); return; } if (typeof self.hookBeforeGetIncludesForFirst === "function") { self.hookBeforeGetIncludesForFirst(self, connection, record); } return self.connection(connection)._getIncludes([record], function(err, withIncludes) { if (typeof self.hookAfterGetIncludesForFirst === "function") { self.hookAfterGetIncludesForFirst(self, connection, err, withIncludes); } if (err != null) { cb(err); return; } return cb(null, withIncludes[0]); }); }); }); }; module.exports.find = function(cb) { var params, self, sql; self = this; self.assertConnection(); sql = self.replacePlaceholders(self.sql()); params = self.params(); return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } if (typeof self.hookBeforeFind === "function") { self.hookBeforeFind(self, connection, sql, params); } return connection.query(sql, params, function(err, results) { var records; if (typeof done === "function") { done(); } if (typeof self.hookAfterFind === "function") { self.hookAfterFind(self, connection, sql, params, err, results); } if (err != null) { cb(err); return; } records = results.rows; if (records.length === 0) { cb(null, []); return; } if (typeof self.hookBeforeGetIncludesForFind === "function") { self.hookBeforeGetIncludesForFind(self, connection, records); } return self.connection(connection)._getIncludes(records, function(err, withIncludes) { if (typeof self.hookAfterGetIncludesForFind === "function") { self.hookAfterGetIncludesForFind(self, connection, err, withIncludes); } if (err != null) { cb(err); return; } return cb(null, withIncludes); }); }); }); }; module.exports.exists = function(cb) { var query, self, sql; self = this; self.assertConnection(); query = self._mohair; sql = self.replacePlaceholders(query.sql()); return self.getConnection(function(err, connection, done) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } return connection.query(sql, query.params(), function(err, results) { if (err != null) { if (typeof done === "function") { done(); } cb(err); return; } if (typeof done === "function") { done(); } return cb(null, results.rows.length !== 0); }); }); };