Sequel-ish db utility for Node
Massive is a data access tool for Node JS that works with relational data systems - currently Postgres and MySQL.
Massive's goal is to help you get data from your database. This is not an ORM, it's a bit more than a query tool - our goal is to do just enough, then get out of your way. I happen to love Sequel (the Ruby data access tool) and I've emulated that tool ... sort of... with Massive.
Installation is the usual:
npm install massive
To work with Massive, create a connection do something interesting:
var db = require"massive";dbconnect"postgres://postgres@localhost/mydatabase"dbmyTableeachconsole.logthing;;;
You open a connection against a database (currently Postgres or MySQL) and Massive will snoop your tables using Information.Schema, and will magically "bind" those tables as properties to your db for querying:
dbmyTablefindid : 1executeconsole.logthing;;
Sometimes having all your columns returned to you isn't optimal:
dbmyTablefindid : 1 columns : "name"executeconsole.logthingname;;
If you pass a column list in to find, as an array, it works too:
Massive is highly-evented, which means you can work with events rather than callbacks:
var allMyStuff = dbmyTablefind"id > " : 0;allMyStuffon"row"console.logrow;;
By adding the "row" event, the query is triggered and you can then iterate over it. You could also use "each()" as above, and it would do the same thing. You can also listen for when the iteration is completed:
var allMyStuff = dbmyTablefind"id > " : 0;allMyStuffeachconsole.logthing;;allMyStuffon"end"console.log"Guess that's all!";
One other event that is very handy is "executed" - this tells you when a query has completed. This works pretty nicely with the way inserts work:
var newProduct = dbproductsinsertname : "vanilla soda" price : 100;newProducton"executed" dbfeaturedProductsinsert;newProductexecute;
Queries in Massive are independent of their execution and are little Event Vehicles (a word I made up). In this example I created a query and wired another query to go off when execution is completed. Massive "forwards" the data to any listeners on the event (in this case it's a new product record). Node then uses that bit of data as an argument to pass into the db.featuredProducts table.
You can add, edit, and delete records - which I'll show in a second - but the ultimate thing to remember is you can always use SQL when you need to:
dbrun"select message from freakytable where id = $1" "la la la"console.log"Freaky! " + result;
I like SQL and I find that by staying true to SQL rather than muscling an ORM abstraction makes life much happier. Now, on to the rest of the stuff...
Inserting data into your database is pretty straightforward:
dbmyTableinsertname:"rubber ducky" message : "You're the one"execute//if you're using Postgres, the new record is returnedconsole.log"The new id is " + resultid;;
Many times you need to insert a whole bunch of stuff:
var items =name:"stuffy stuff" price: 12.00name:"poofy poof" price: 24.00;dbmyFluffyAnimalsinsertitemsexecute_eachnewGuysconsole.log"Hello there " + newGuyname;;;
Here I'm using Underscore.js's "each" method to roll out an array of results - all of the critters are inserted as part of a single statement, rather than one at a time.
Updates follow the same pattern:
dbmyFluffyAnimalsupdatename : "crunchy crunch" 2executeconsole.log"Price updated!";;
This example used a single record, but you can also update more than one record:
dbmyFluffyAnimalsupdatename : "crunchy crunch" "id <>" : 100executeconsole.log"Price updated!";;
You can order and limit your query using a bit of a fluent interface:
var moreThanAHundo = dbmyFluffyAnimalsfind"price < ": 100order"name"limit10;moreThanAHundoeachconsole.log"Ahoy!";
This example uses a few new things: the
each method on the query and the order/limit stuff in a fluent fashion. It also uses a nice readable criteria set where the operator is a string key that gets sent in.
Deletes are a sad thing, but often are required:
dbmyFluffyAnimalsdestroyexecuteconsole.log"All gone :(";
Of course, that's a bit drastic. Let's just delete a few:
dbmyFluffyAnimalsdestroy"price > " : 1000executeconsole.log"Expensive stuff - OUTTA HERE";;
Or maybe just one?
dbmyFluffyAnimalsdestroy1executeconsole.log"The first critter is toast";;
No self-respecting data tool would leave you without the ability to create a table!
//a query, just like the other stuffvar sparklyCritters = dbcreateTable"sparkly_stuff"name : 'string not null'price : 'money'birthday : 'date'ip : 'inet not null default '127.00.1'});sparklyCritters.execute();
No, "string", "money" and "date" aren't valid Postgres types (well date sorta is). These are transformed by Massive into "varchar(255)", "decimal(8,2)", and "timestamptz" for you.
The last entry is an IP address - and notice you can send in whatever you like. Massive will try and digest it.
There's an examples directory that shows more goodies, and our tests also give a nice indication of what's possible.
If you see a bug, please be so kind as to show how it's failing, and I'll do my best to get it fixed quickly.