Permalink
Browse files

added makefile and make PG database connection string overridable by ENV

  • Loading branch information...
1 parent d33fc1c commit 790e2d142449ffdcb1611cd0f6f37683aec65e69 Karl Seguin committed May 5, 2012
Showing with 57 additions and 18 deletions.
  1. +13 −9 lib/postgres.js
  2. +17 −0 makefile
  3. +1 −0 package.json
  4. +16 −0 test/pg_helper.coffee
  5. +5 −4 test/pg_query_spec.coffee
  6. +3 −3 test/pg_schema_spec.coffee
  7. +2 −2 test/pg_spec.coffee
View
@@ -85,7 +85,7 @@ var Query = function(sql, params, table) {
self.sql = self.sql.replace("*", columns);
}else if(args.length > 1 && args[1].columns) {
self.sql = self.sql.replace("*", args[1].columns);
-
+
}else if (args.length > 0 && _.isNumber(args[0])){
var criteria = {};
criteria[self.table.pk] = args[0];
@@ -101,9 +101,9 @@ var Query = function(sql, params, table) {
return self;
}
self.execute = function(callback) {
- self.db.execute(self.sql, self.params, function(err,result){
- if(callback) callback(err,result);
- self.emit("executed");
+ self.db.execute(self.sql, self.params, function(err,result,client){
+ if(callback) callback(err,result,client);
+ self.emit("executed",client);
})
}
@@ -206,13 +206,13 @@ var Table = function(tableName, pk, _db) {
sql+= util.format(" \nWHERE \"%s\" = %d", this.pk, where);
}else if (_.isString(where)){
sql+= util.format(" \nWHERE \"%s\" = $%d", this.pk, parameters.length+1);
- parameters.push(where);
+ parameters.push(where);
}else{
where = parseWhere(where);
sql += where.sql;
if(where.params.length > 0) parameters.push(where.params);
}
-
+
}
var query = new Query(sql, parameters, this);
return query;
@@ -243,20 +243,24 @@ var Postgres = function(connection){
from information_schema.tables ist \
where table_schema NOT IN ('pg_catalog', 'information_schema')";
+ this.end = function() {
+ pg.end();
+ }
+
this.execute = function(sql, params, callback) {
pg.connect(self.connectionString, function(err,db){
self.emit("beforeExecute", self);
db.query(sql, params,function(err, results){
if(err && callback) {
- callback(err,null)
+ callback(err,null,db)
self.emit("error", err);
}else {
if(results && results.rows.length > 0){
- if(callback)callback(null,results.rows);
+ if(callback)callback(null,results.rows,db);
}else {
- if(callback)callback(null,results);
+ if(callback)callback(null,results,db);
}
self.emit("executed");
}
View
@@ -0,0 +1,17 @@
+PG_TESTS = test/pg*_spec.coffee
+MYSQL_TESTS = test/mysql*_spec.coffee
+
+test: test-pg test-mysql
+
+test-pg:
+ @./node_modules/.bin/mocha $(PG_TESTS)
+
+test-mysql:
+ @./node_modules/.bin/mocha $(MYSQL_TESTS)
+
+setup-pg:
+ @coffee test/pg_helper.coffee setup
+
+
+
+.PHONY: test pg mysql
View
@@ -9,6 +9,7 @@
, "pg" : "*"
, "mysql" : "*"
, "underscore" : "*"
+ , "coffee-script" : "*"
}
, "keywords": ["database", "query", "postgres", "sequel", "mysql"]
, "main": "./lib/index.js"
View
@@ -0,0 +1,16 @@
+Path = require('path')
+
+class Helper
+ @connectionString: ->
+ process.env.MASSIVE_PG_TEST || "postgres://postgres@localhost/test"
+
+ @setup: ->
+ massive = require("../index");
+ massive.connect @connectionString(), (err, db) ->
+ db.createTable('products', {name: 'string'}).execute () -> db.end()
+
+module.exports = Helper
+
+
+if Path.basename(process.argv[1]) == 'pg_helper.coffee' && process.argv[2] == 'setup'
+ Helper.setup()
@@ -1,17 +1,18 @@
massive = require("../index");
should = require("should");
util = require("util");
+helper = require('./pg_helper')
describe "Postgres Queries", ->
db = null
before (done) ->
- massive.connect "postgres://postgres@localhost/test", (err,_db) ->
+ massive.connect helper.connectionString(), (err,_db) ->
db = _db
done()
describe "initialization", ->
- it "sets the table name", ->
+ it "sets the table name", ->
db.products.name.should.equal("products")
it "defaults the pk to id", ->
@@ -128,7 +129,7 @@ describe "Postgres Queries", ->
query = db.products.update({name:"pumpkin", price:1000}, 12)
query.sql.should.equal("UPDATE products SET name = $1, price = $2 \nWHERE \"id\" = 12")
query.params.length.should.equal 2
-
+
it "creates a basic update with a string key", ->
query = db.products.update({name:"pumpkin", price:1000}, "12")
query.sql.should.equal("UPDATE products SET name = $1, price = $2 \nWHERE \"id\" = $3")
@@ -165,7 +166,7 @@ describe "Postgres Queries", ->
it "has an each method", ->
query = db.products.find()
should.exist query.each
-
+
it "should iterate", ->
query = db.products.find()
query.each (err,result) ->
@@ -1,15 +1,15 @@
massive = require("../index");
should = require("should");
util = require("util");
-
+helper = require('./pg_helper')
describe "schema queries", ->
db = null
before (done) ->
- massive.connect "postgres://postgres@localhost/test", (err,_db) ->
+ massive.connect helper.connectionString(), (err,_db) ->
db = _db
done()
-
+
it "should exist", ->
should.exist db
View
@@ -1,13 +1,13 @@
massive = require("../index");
should = require("should");
util = require("util");
+helper = require('./pg_helper')
describe "Connections", ->
db = null
before (done) ->
- massive.connect "postgres://postgres@localhost/test", (err,_db) ->
+ massive.connect helper.connectionString(), (err,_db) ->
db = _db
- #console.log(db)
done()
it "returns a new postgres db", ->

0 comments on commit 790e2d1

Please sign in to comment.