Created
February 13, 2012 18:15
-
-
Save janjongboom/1818774 to your computer and use it in GitHub Desktop.
Inheritance
This file contains hidden or 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
var sys = require("sys"); | |
var pg = require("pg"); | |
var connStr = "tcp://postgres:admin@localhost:5432/c9_businessrules"; // @todo: to config | |
var entities = { | |
product: require("./entities/product"), | |
contract: require("./entities/contract"), | |
delivery: require("./entities/delivery"), | |
contractType: require("./entities/contracttype"), | |
log_contract: require("./entities/log_contract"), | |
log_transaction: require("./entities/log_transaction"), | |
transaction: require("./entities/transaction") | |
}; | |
var client = new pg.Client(connStr); | |
client.on("drain", client.end.bind(client)); | |
client.connect(); | |
module.exports = function (username) { | |
// username should be specified to tell us what user you are | |
if (!username || typeof username !== "string") { | |
throw new Error("Username is required as the first parameter"); | |
} | |
var exp = (function () { | |
/** | |
* Get all current versions of a certain type | |
*/ | |
var getAll = function (type, callback) { | |
getAllWithClauses(type, "", [], callback); | |
}; | |
/** | |
* Get all current versions from a certain type, but specify some fancy info | |
*/ | |
var getAllWithClauses = function (type, preparedClause, preparedValues, callback) { | |
var query = "SELECT * FROM " + type.prototype.metadata.view + " " + preparedClause; | |
client.query(query, preparedValues, function (err, res) { | |
if (typeof callback === "function") { | |
if (res && res.rows) { | |
res.rows.forEach(function (r) { | |
r.prototype = type.prototype; | |
r.constructor = type.constructor; | |
}); | |
// now i want | |
// res.rows[0] instanceof type to equal true | |
} | |
callback(err, res && res.rows); | |
} | |
}); | |
}; | |
/** | |
* Get the most recent version of a specific type and id | |
*/ | |
var getById = function (type, id, callback) { | |
client.query("SELECT * FROM " + type.prototype.metadata.view + " WHERE id = $1", [ id ], function (err, res) { | |
if (typeof callback === "function") { | |
callback(err, res && res.rows && res.rows[0]); | |
} | |
}); | |
}; | |
/** | |
* Get a specific version of a type | |
*/ | |
var getByAutonumber = function (type, autonumber, callback) { | |
client.query("SELECT * FROM " + type.prototype.metadata.table + " WHERE autonumber = $1", [ autonumber ], function (err, res) { | |
if (typeof callback === "function") { | |
callback(err, res && res.rows && res.rows[0]); | |
} | |
}); | |
}; | |
/** | |
* Store an object | |
*/ | |
var saveNewVersion = function (type, obj, callback) { | |
if (!(obj instanceof type)) { | |
return callback("The object is not of the provided type"); | |
} | |
// find required fields | |
var keys = Object.keys(type.prototype.fields); | |
var mandatory = keys.filter(function (f) { | |
return f.required; | |
}); | |
// object map for the query | |
var data = {}; | |
// add id if versioned | |
if (type.prototype.metadata.versioned) { | |
mandatory.push("id"); | |
data.id = obj.id; | |
data.insertuser = username; | |
} | |
// do validation | |
for (var ix = 0; ix < mandatory.length; ix++) { | |
if (!obj[mandatory[ix]]) { | |
return callback("Field " + mandatory[ix] + " is mandatory but has no value"); | |
} | |
} | |
// create an object map with data required to do the query | |
keys.forEach(function (key) { | |
if (obj[key]) { | |
data[key] = obj[key]; | |
} | |
}); | |
// postgres-node wants a list of $1, $2, $3 etc. | |
var valueList = []; | |
for (var ix = 0; ix < Object.keys(data).length; ix++) { | |
valueList.push("$" + (ix+1)); | |
} | |
var query = "INSERT INTO " + type.prototype.metadata.table + " (" + Object.keys(data).join(", ") + ") "; | |
query += "VALUES (" + valueList.join(", ") + ")"; | |
var values = Object.keys(data).map(function (key) { return data[key]; }); | |
// now query it | |
client.query(query, values, function (err, res) { | |
callback(err, res && res[0]); | |
}); | |
}; | |
/** | |
* Retrieve a list of all products | |
*/ | |
var getProducts = function (callback) { | |
getAll(entities.product, callback); | |
}; | |
/** | |
* Save a product | |
*/ | |
var insertProduct = function (product, callback) { | |
saveNewVersion(entities.product, product, callback); | |
}; | |
/** | |
* Retrieve a list of all contract types per product | |
*/ | |
var getContractTypesByProductId = function (productId, callback) { | |
getAllWithClauses(entities.contractType, "WHERE productId = $1", [ productId ], callback); | |
}; | |
/** | |
* Save a contract type | |
*/ | |
var insertContractType = function (contractType, callback) { | |
saveNewVersion(entities.contractType, contractType, callback); | |
}; | |
return { | |
getProducts: getProducts, | |
insertProduct: insertProduct, | |
getContractTypesByProductId: getContractTypesByProductId, | |
insertContractType: insertContractType | |
}; | |
}()); | |
var _self = this; | |
Object.keys(exp).forEach(function (key) { | |
_self[key] = exp[key]; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment