Permalink
Browse files

Readying up examples, more tests

  • Loading branch information...
1 parent 0c075d6 commit 15ca7e2dd93ffa6d5b4d1e4ed3bc5a0fc1ad7ec1 @subsonic subsonic committed May 1, 2012
View
12 .gitignore
@@ -1,3 +1,9 @@
-*.DS_Store
-*.swp
-
+.DS_Store
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+node_modules
View
3 .npmignore
@@ -0,0 +1,3 @@
+support
+test
+examples
View
0 lib/massive/LICENSE → LICENSE
File renamed without changes.
View
0 lib/massive/README.md → README.md
File renamed without changes.
View
54 examples/batch_insert.js
@@ -0,0 +1,54 @@
+var massive = require("../lib/massive");
+massive.connect("postgresql://postgres@localhost/test");
+
+//drop the table
+console.log("dropping...")
+massive.dropTable("products");
+
+
+//create the table
+console.log("creating products table")
+massive.createTable("products", {
+ name : "string",
+ price : "money",
+ timestamps : true
+});
+
+//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?")
+var query = products.select();
+query.on("row", function(row){
+ console.log("Hi, I'm product " + row.title);
+})
+
+//we charge too little
+console.log("let's update our prices")
+query = products.update({price : 100.00}, {"id >" : 0}).execute(function(err,result){
+ if(err) console.log("Ohhhh mann! " + err);
+ console.log("All set!")
+});
+
+console.log("Updated pricing...")
+var query = products.select();
+query.on("row", function(row){
+ console.log(row.title + " is now priced at " + row.price);
+})
+
+
+//drop it all
+console.log("all done...")
+products.delete().execute(function(err,result){
+ if(err) console.log("Ohhhh mann! " + err);
+ console.log("See ya!")
+})
View
40 lib/massive/lib/massive.model.js
@@ -1,5 +1,6 @@
var _query = require('./massive.query');
-
+var _ = require("underscore")._;
+var util = require("util");
var Model = function(tableName, pKField) {
this.tableName = tableName;
@@ -38,10 +39,15 @@ var Model = function(tableName, pKField) {
this.insert = function(){
if(arguments.length == 0 ) throw "Have to pass in some kind of value yo..."
- var item = arguments[0];
+ var parsed = _parseInserts(arguments[0]);
var _sql = "INSERT INTO " + this.tableName;
+ _sql += "("+parsed.cols.join(", ")+") VALUES ("+parsed.values.join(", ")+") RETURNING *";
+ return new _query(_sql,parsed.params);
+
+ };
+ var _parseInserts = function(item, counterSeed) {
var _args = [];
- var _counter=1;
+ var _counter=counterSeed || 1;
var _cols = [];
var _vals= [];
for(var prop in item){
@@ -50,10 +56,32 @@ var Model = function(tableName, pKField) {
_args.push(item[prop]);
_counter++;
};
- _sql = _sql + "("+_cols.join(", ")+") VALUES ("+_vals.join(", ")+")";
- return new _query(_sql,_args);
+ return {cols : _cols, values : _vals, params : _args, seedStop : _counter};
+ }
+ this.insertBatch = function(){
+ //this handles an array
+ var _sql = "INSERT INTO " + this.tableName;
+ var _columnList;
+ var _statements = [];
+ var _params = [];
+ var _counter = 1;
- };
+ for(var i = 0;i < arguments[0].length; i ++){
+ var item = arguments[0][i];
+ var _parsed = _parseInserts(item, _counter);
+ _counter = _parsed.seedStop;
+ if(!_columnList){
+ _columnList = "("+_parsed.cols.join(", ")+") \n VALUES \n";
+ }
+
+ var _valueList = "\n("+_parsed.values.join(", ")+")";
+ _statements.push(_valueList);
+ _.each(_parsed.params, function(p){
+ _params.push(p);
+ });
+ }
+ return new _query(_sql + _columnList + _statements.join(",\n"), _params);
+ }
this.update = function(){
if(arguments.length < 2 ) throw "Have to pass in the update criteria and a key";
View
7 lib/massive/lib/massive.pg.js
@@ -1,5 +1,6 @@
var pg = require("pg");
var _ = require("underscore")._;
+var util = require("util");
//default it, cause we're nice
var _connectionString = "tcp://postgres@localhost/postgres";
@@ -32,10 +33,10 @@ exports.getRecords = function(query, callback){
};
exports.execute = function(query,callback) {
- var _that = this;
-
pg.connect(_connectionString, function(err,client){
- var query = client.query(query.sql, query.params , callback);
+ console.log(query.sql)
+ console.log(query.params)
+ client.query(query.sql, query.params , callback);
});
};
View
4 sandbox.js
@@ -1,4 +0,0 @@
-var massive = require ('massive');
-
-console.log(massive);
-
View
11 test/massive_spec.coffee
@@ -93,10 +93,15 @@ describe "queries", ->
describe "insert", ->
- it "creates a basic insert", ->
+ it "creates a basic insert with returning", ->
query = m.insert({name : "steve", price : 12.00})
- query.sql.should.equal "INSERT INTO products(name, price) VALUES ($1, $2)"
+ query.sql.should.equal "INSERT INTO products(name, price) VALUES ($1, $2) RETURNING *"
+ query.params.length.should.equal(2)
+ it "creates a batch for item arrays", ->
+ items = [{title:"stuffy stuff", price: 12.00, desc : "bubble"},{title:"poofy poof", price: 24.00, desc : "glurp"}];
+ query = m.insertBatch(items)
+ console.log(query.sql)
describe "updates", ->
it "creates a basic update", ->
@@ -116,7 +121,7 @@ describe "queries", ->
beforeEach (done)->
done()
- it "fires something when new events are added", ->
+ it "fires iterator when new events are added", ->
query = m.select();
query.on "row", (row)->
should.exist(row.name)
View
40 test/schemeSpec.coffee
@@ -0,0 +1,40 @@
+massive = require "../lib/massive"
+should = require "should"
+
+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)
+
+ it "adds an ID of serial by default", ->
+ sql = massive.createTable "cheese",{title:"string"}
+ 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)
+
+ 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)
+
+ it "adds a decimal(8,2) for money", ->
+ sql = massive.createTable "cheese",{price:"money"}
+ 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)
+
+ it "adds a bool for... bool", ->
+ sql = massive.createTable "cheese",{stuff:"bool"}
+ 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)
+
+
+

0 comments on commit 15ca7e2

Please sign in to comment.