Skip to content

Commit

Permalink
Added verifications for correct object instantiation for both databas…
Browse files Browse the repository at this point in the history
…e and transaction objects.
  • Loading branch information
Vitaly Tomilov committed Mar 6, 2015
1 parent 0ddbe51 commit e5a8723
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ queryResult = {
module.exports = function (options) {

var lib = function (cn) {
if(Object.keys(this).length){
if(!$isEmptyObject(this)){
// This makes it easy to locate the most common mistake -
// skipping keyword 'new' when calling: var db = new pgp(cn);
throw new Error("Invalid database object instantiation.");
}
if (!cn) {
Expand Down Expand Up @@ -189,6 +191,12 @@ function dbInit(dbInst, cn, options) {

var tx = this;

if(!$isEmptyObject(tx)){
// This makes it easy to locate the most common mistake -
// skipping keyword 'new' when calling: var tx = new db.tx(cn);
throw new Error("Invalid transaction object instantiation.");
}

var local = {
db: null,
start: function (db) {
Expand Down Expand Up @@ -312,6 +320,11 @@ function $isNull(val) {
return typeof(val) === 'undefined' || val === null;
}

// Checks if the object is empty (has no properties);
function $isEmptyObject(obj){
return Object.keys(obj).length === 0;
}

// Fixes single-quote symbols in text fields;
function $fixQuotes(val) {
return val.replace("'", "''");
Expand Down

0 comments on commit e5a8723

Please sign in to comment.