UNPKG

fixr-compiled

Version:

Create data fixtures for testing projects

123 lines (105 loc) 3.09 kB
// Generated by CoffeeScript 1.3.3 (function() { var async, pg, pgEngine, sql; pg = require('pg'); sql = require('sql'); async = require('async'); sql.setDialect('postgres'); pgEngine = (function() { function pgEngine(engineConfig) { this.client = new pg.Client(engineConfig); this.client.connect(); } pgEngine.prototype.beforeFix = function(data, cb) { return cb(null, data); }; pgEngine.prototype.beforeFixRecord = function(data, cb) { return cb(null, data); }; pgEngine.prototype.afterFixRecord = function(err, cb) { return cb(err); }; pgEngine.prototype.afterFix = function(err, cb) { this.client.end(); return cb(err); }; pgEngine.prototype.fix = function(data, cb) { var _this = this; return this.beforeFix(data, function(err, data) { var cbErr, q, record, records, table, _results; if (err) { return cb(err); } cbErr = null; q = async.queue(function(task, cb) { return _this._fixRecord(task.table, task.record, function(err) { if (err && !cbErr) { cbErr = err; } return cb(); }); }, 1); q.drain = function() { return _this.afterFix(cbErr, cb); }; _results = []; for (table in data) { records = data[table]; _results.push((function() { var _i, _len, _results1; _results1 = []; for (_i = 0, _len = records.length; _i < _len; _i++) { record = records[_i]; _results1.push(q.push({ table: table, record: record })); } return _results1; })()); } return _results; }); }; pgEngine.prototype._fixRecord = function(table, record, cb) { var _this = this; return this.beforeFixRecord(record, function(err, data) { var column, query, value, values; query = _this._getQuery(table, record); values = (function() { var _results; _results = []; for (column in record) { value = record[column]; _results.push(value); } return _results; })(); return _this.client.query(query, values, function(err) { return _this.afterFixRecord(err, cb); }); }); }; pgEngine.prototype._getQuery = function(table, record) { var column, columns, insert, query, value; columns = (function() { var _results; _results = []; for (column in record) { value = record[column]; _results.push(column); } return _results; })(); table = sql.define({ name: table, columns: columns }); insert = table.insert(record); query = new sql.dialect().getQuery(insert); return query.text; }; return pgEngine; })(); module.exports = pgEngine; }).call(this);