Skip to content
Browse files

moved to event emitter for execute getrecords etc. Cleaned up some

  • Loading branch information...
1 parent 20fa587 commit b5a308acd04c3e338b8b0aa1a0bf2c31bf5be65e @subsonic subsonic committed May 1, 2012
View
20 examples/batch_insert.js
@@ -14,16 +14,16 @@ massive.createTable("products", {
timestamps : true
}).execute();
-//add some data - in a batch
-console.log("Inserting 2 products...");
-var products = new massive.Model("products", "id");
-var items = [
- {name:"stuffy stuff", price: 12.00},
- {name:"poofy poof", price: 24.00}
-];
-products.insertBatch(items).execute(function(err,result){
- if(err) console.log("oops - " + err);
-});
+// //add some data - in a batch
+// console.log("Inserting 2 products...");
+// var products = new massive.Model("products", "id");
+// var items = [
+// {name:"stuffy stuff", price: 12.00},
+// {name:"poofy poof", price: 24.00}
+// ];
+// products.insertBatch(items).execute(function(err,result){
+// if(err) console.log("oops - " + err);
+// });
// //pull back out
// console.log("Wanna see em?")
View
3 lib/massive/lib/massive.js
@@ -7,8 +7,7 @@ module.exports.Model = _model;
module.exports.table = _model;
module.exports.connect = function(connection) {
- console.log("Setting connection to " + connection);
-
+ console.log("Massive connection set to " + connection);
if(connection.indexOf("postgresql://") > -1) {
_client.connect(connection);
}else{
View
14 lib/massive/lib/massive.model.js
@@ -1,8 +1,11 @@
var _query = require('./massive.query');
var _ = require("underscore")._;
var util = require("util");
-var Model = function(tableName, pKField) {
+var events = require("events");
+
+var Model = function(tableName, pKField) {
+ events.EventEmitter.call(this);
this.tableName = tableName;
this.primaryKey = pKField || "id";
@@ -18,6 +21,10 @@ var Model = function(tableName, pKField) {
}
};
+ this.count = function() {
+ return new _query("SELECT COUNT(1) FROM " + this.tableName);
+ };
+
this.select = function(){
this.parseArgs(arguments);
var _columns = this.options.columns || "*";
@@ -112,10 +119,9 @@ var Model = function(tableName, pKField) {
return query.where(key);
}
-
-
- }
+ };
};
+util.inherits(Model,events.EventEmitter);
module.exports = Model;
View
48 lib/massive/lib/massive.pg.js
@@ -1,18 +1,27 @@
var pg = require("pg");
var _ = require("underscore")._;
var util = require("util");
-var _query = require("./massive.query")
+var events = require("events");
+var massiveQuery = require("./massive.query");
+
//default it, cause we're nice
-var _connectionString = "tcp://postgres@localhost/postgres";
-module.exports.connect = function(connection) {
+var Client = function(){
+ var _connectionString = "tcp://postgres@localhost/postgres";
+ events.EventEmitter.call(this);
+};
+
+util.inherits(Client,events.EventEmitter);
+
+Client.prototype.connect = function(connection) {
console.log("Setting connection to " + connection);
_connectionString = connection;
};
-exports.stream = function(query) {
+Client.prototype.stream = function(query) {
pg.connect(_connectionString, function(err,db){
- var dbQuery = db.query(query.sql);
+ if(err) throw err;
+ var dbQuery = db.query(query.sql, query.params);
dbQuery.on("row", function(row){
query.emit("row",row);
});
@@ -25,19 +34,21 @@ exports.stream = function(query) {
});
};
-exports.getRecords = function(query, callback){
+Client.prototype.getRecords = function(query){
pg.connect(_connectionString, function(err,client){
- client.query(query.sql, query.params,callback);
+ client.query(query.sql, query.params,function(err,result){
+ query.emit("recordsReady", result.rows);
+ });
});
};
-exports.execute = function(query,callback) {
+Client.prototype.execute = function(query,callback) {
pg.connect(_connectionString, function(err,client){
- console.log(query.sql)
- console.log(query.params)
- client.query(query.sql, query.params , callback);
+ client.query(query.sql, query.params , function(err,result){
+ query.emit("executed",result);
+ });
});
};
@@ -73,11 +84,11 @@ var _containsPK = function(columns) {
return _.any(columns.values,"pk");
}
-exports.dropTable = function(tableName) {
- return new _query("DROP TABLE IF EXISTS " + tableName + ";");
+Client.prototype.dropTable = function(tableName) {
+ return new massiveQuery("DROP TABLE IF EXISTS " + tableName + ";");
}
-exports.createTable = function(tableName, columns) {
+Client.prototype.createTable = function(tableName, columns) {
var _sql ="CREATE TABLE " + tableName + "(";
var _cols = [];
@@ -108,6 +119,11 @@ exports.createTable = function(tableName, columns) {
_sql+= _cols.join(",") + ");";
//console.log(_sql);
- return new _query(_sql);
-}
+ return new massiveQuery(_sql);
+};
+
+
+
+module.exports = new Client();
+
View
8 lib/massive/lib/massive.query.js
@@ -15,12 +15,12 @@ var Query = function(_sql,_params){
massive.Client.stream(this);
}
})
- this.execute = function(callback) {
- massive.Client.execute(this,callback);
+ this.execute = function() {
+ massive.Client.execute(this);
};
- this.toArray = function(callback) {
- massive.Client.getRecords(this,callback);
+ this.toArray = function() {
+ massive.Client.getRecords(this);
}
this.limit = function (){
View
14 package.json
@@ -0,0 +1,14 @@
+{ "name": "massive"
+ , "description": "Sequel-ish db utility for Node"
+ , "version": "0.1.0"
+ , "author": "Rob Conery <rob@tekpub.com>"
+ , "devDependencies": {
+ "mocha": "*"
+ , "should": "*"
+ , "pg" : "*"
+ , "underscore" : "*"
+ }
+ , "keywords": ["database", "query", "postgres", "sequel"]
+ , "main": "./lib/massive.js"
+ , "engines": { "node": ">= 0.2.0" }
+}
View
4 test/massive_spec.coffee
@@ -115,6 +115,10 @@ describe "queries", ->
it "throws if key not passed", ->
( -> m.update({name:"pumpkin", price:1000}) ).should.throw()
+ describe "aggregates", ->
+ it "counts with SELECT COUNT", ->
+ query = m.count()
+ query.sql.should.equal("SELECT COUNT(1) FROM products");
describe "events", ->
View
32 test/schemeSpec.coffee
@@ -5,36 +5,36 @@ describe "schema", ->
describe "create table", ->
it "adds a column with NULL declaration", ->
- sql = massive.createTable "cheese", {bucket:"beef NOT NULL PLEASE"}
- sql.indexOf("bucket beef NOT NULL PLEASE").should.be.greaterThan(0)
+ query = massive.createTable "cheese", {bucket:"beef NOT NULL PLEASE"}
+ query.sql.indexOf("bucket beef NOT NULL PLEASE").should.be.greaterThan(0)
it "adds an ID of serial by default", ->
- sql = massive.createTable "cheese",{title:"string"}
- sql.indexOf("id serial PRIMARY KEY").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{title:"string"}
+ query.sql.indexOf("id serial PRIMARY KEY").should.be.greaterThan(0)
it "adds a varchar 255 for string", ->
- sql = massive.createTable "cheese",{title:"string"}
- sql.indexOf("title varchar(255)").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{title:"string"}
+ query.sql.indexOf("title varchar(255)").should.be.greaterThan(0)
it "adds a varchar 255 for string with null", ->
- sql = massive.createTable "cheese",{title:"string null"}
- sql.indexOf("title varchar(255) null").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{title:"string null"}
+ query.sql.indexOf("title varchar(255) null").should.be.greaterThan(0)
it "adds a decimal(8,2) for money", ->
- sql = massive.createTable "cheese",{price:"money"}
- sql.indexOf("price decimal(8,2)").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{price:"money"}
+ query.sql.indexOf("price decimal(8,2)").should.be.greaterThan(0)
it "adds a timestamptz for date", ->
- sql = massive.createTable "cheese",{created:"date"}
- sql.indexOf("created timestamptz").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{created:"date"}
+ query.sql.indexOf("created timestamptz").should.be.greaterThan(0)
it "adds a bool for... bool", ->
- sql = massive.createTable "cheese",{stuff:"bool"}
- sql.indexOf("stuff bool").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{stuff:"bool"}
+ query.sql.indexOf("stuff bool").should.be.greaterThan(0)
it "adds created and updated for timestamps", ->
- sql = massive.createTable "cheese",{timestamps:true}
- sql.indexOf("created_at timestamptz not null default 'now',updated_at timestamptz not null default 'now'").should.be.greaterThan(0)
+ query = massive.createTable "cheese",{timestamps:true}
+ query.sql.indexOf("created_at timestamptz not null default 'now',updated_at timestamptz not null default 'now'").should.be.greaterThan(0)

0 comments on commit b5a308a

Please sign in to comment.
Something went wrong with that request. Please try again.