Some Light Abstraction for Accessing Data
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.
The 5-Second Hi How Are Ya
Installation is the usual:
npm install massive
To work with Massive, create a connection do something interesting:
var db = ;db;
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:
Sometimes having all your columns returned to you isn't optimal:
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 = dbmyTable;allMyStuff;
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 = dbmyTable;allMyStuff;allMyStuff;
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 = dbproducts;newProduct;newProduct;
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:
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:
Many times you need to insert a whole bunch of stuff:
var items =name:"stuffy stuff" price: 1200name:"poofy poof" price: 2400;dbmyFluffyAnimals;
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:
This example used a single record, but you can also update more than one record:
Limits, Orders, etc
You can order and limit your query using a bit of a fluent interface:
var moreThanAHundo = dbmyFluffyAnimals;moreThanAHundo;
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:
Of course, that's a bit drastic. Let's just delete a few:
Or maybe just one?
No self-respecting data tool would leave you without the ability to create a table!
//a query, just like the other stuffvar sparklyCritters = db
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.
Want to help?
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.