Skip to content

Commit

Permalink
code refactoring. adding version.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed May 30, 2015
1 parent b023bb2 commit 1afa077
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
28 changes: 19 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Cannot declare 'use strict' here, because queryResult
// needs to be exported into the global namespace.
// is exported into the global namespace.

var npm = {
pg: require('pg'),
Expand Down Expand Up @@ -48,7 +48,7 @@ queryResult = {
//
// pgFormatting: false,
// - Redirects query formatting into node-postgres library;
// - Default is false, and all queries are formatted within 'pg-promise'.
// - Default is false, and all queries are formatted by 'pg-promise'.
//
// promiseLib: null
// - Overrides the promise library to be used.
Expand Down Expand Up @@ -104,6 +104,13 @@ module.exports = function (options) {
npm.pg.end();
};

// Library version details;
inst.version = {
major: 1,
minor: 3,
patch: 3
};

return inst;
};

Expand Down Expand Up @@ -224,8 +231,8 @@ function $extend(obj, cn, db, options) {
}, values, qrm);
};

// A procedure is expected to return either no rows
// or one row that represents a list of OUT values;
// A procedure expects either no rows,
// or one row with the return value(s);
obj.proc = function (procName, values) {
return obj.query({
funcName: procName
Expand Down Expand Up @@ -346,7 +353,13 @@ function $extend(obj, cn, db, options) {
// Generic query call;
function $query(db, query, values, qrm, options) {
return $p(function (resolve, reject) {
var isRaw = qrm === 'raw';

var errMsg,
pgFormatting = (options && options.pgFormatting),
params = pgFormatting ? values : undefined,
isFunc = query && typeof(query) === 'object' && 'funcName' in query,
isRaw = qrm === 'raw';

if (!isRaw) {
if (qrm === null || qrm === undefined) {
qrm = queryResult.any; // default query result;
Expand All @@ -356,9 +369,6 @@ function $query(db, query, values, qrm, options) {
}
}
}
var errMsg, pgFormatting = (options && options.pgFormatting);
var params = pgFormatting ? values : undefined;
var isFunc = query && typeof(query) === 'object' && 'funcName' in query;
if (isFunc) {
query = query.funcName; // query is a function name;
}
Expand Down Expand Up @@ -402,7 +412,7 @@ function $query(db, query, values, qrm, options) {
errMsg = err;
} else {
if (isRaw) {
data = result; // queryRaw was called;
data = result; // raw object requested;
} else {
data = result.rows;
var l = result.rows.length;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "1.3.2",
"version": "1.3.3",
"description": "node-postgres via Promises/A+",
"main": "lib/index.js",
"scripts": {
Expand Down
21 changes: 3 additions & 18 deletions test/dbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ var dbHeader = require('./db/header')(options);
var pgp = dbHeader.pgp;
var db = dbHeader.db;

describe("Library Initialization", function () {
it("must throw an error when invalid 'options' is passed", function () {

var err = "Invalid parameter 'options' specified.";
var lib = require('./db/header');
expect(function () {
lib('');
}).toThrow(err);

expect(function () {
lib(0);
}).toThrow(err);

});
});

describe("Database Instantiation", function () {
it("must throw an error when empty or no connection passed", function () {
var err = "Invalid parameter 'cn' specified.";
Expand Down Expand Up @@ -66,7 +50,7 @@ describe("Connection", function () {
});
});

it("must be provide functioning context for queries", function () {
it("must provide functioning context for queries", function () {
var result, sco;
db.connect()
.then(function (obj) {
Expand All @@ -92,10 +76,11 @@ describe("Connection", function () {
runs(function () {
expect(result).toBeDefined();
expect(result.count > 0).toBe(true);
expect(typeof(sco.tx)).toBe('function'); // just a protocol check;
});
});

it("must be provide functioning context for raw queries", function () {
it("must provide functioning context for raw queries", function () {
var result, sco;
db.connect()
.then(function (obj) {
Expand Down
26 changes: 21 additions & 5 deletions test/protocolSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var PG = require('pg');
var promise = require('bluebird');
var pgpLib = require('../lib/index');

Expand All @@ -6,31 +7,46 @@ var dbHeader = require('./db/header')();
var pgp = dbHeader.pgp;
var db = dbHeader.db;

describe("Library entry object", function () {
describe("Library entry function", function () {

it("must throw error on invalid promise override", function () {
it("must throw an error on invalid promise override", function () {
expect(function () {
pgpLib({
promiseLib: "test"
});
}).toThrow("Invalid or unsupported promise library override.");
});

it("must throw an error on invalid 'options' parameter", function () {
expect(function () {
pgpLib(123);
}).toThrow("Invalid parameter 'options' specified.");
});

});

describe("Library initialization object", function () {
describe("Library instance", function () {

it("must be a function", function () {
expect(typeof(pgp)).toBe('function');
});

it("must have property 'pg'", function () {
it("must have valid property 'pg'", function () {
expect(typeof(pgp.pg)).toBe('object');
expect(pgp.pg).toBe(PG); // the same library instance;
});

it("must have function 'end'", function () {
expect(typeof(pgp.end)).toBe('function');
});

it("must have property 'version'", function () {
expect(typeof(pgp.version)).toBe('object');
expect(typeof(pgp.version.major)).toBe('number');
expect(typeof(pgp.version.minor)).toBe('number');
expect(typeof(pgp.version.patch)).toBe('number');
});

it("must have valid property 'as'", function () {
expect(typeof(pgp.as)).toBe('object');
expect(typeof(pgp.as.text)).toBe('function');
Expand Down Expand Up @@ -174,7 +190,7 @@ describe("$sequence", function () {
it("must throw an error correctly", function () {
var error;
db.tx(function (t) {
return t.sequence(function(){
return t.sequence(function () {
return 'something'; // not null/undefined and not a promise;
});
}).then(function () {
Expand Down

0 comments on commit 1afa077

Please sign in to comment.