Endtable, an ORM for CouchDB on Node.JS
Two important facts about me: I hate relational-databases, and I absolutely love Node.JS (I would have its JavaScript babies).
Why do I hate relational-databases? it comes down to the way they’re usually used:
If I am modeling an automobile, I really don’t care that it can be stored in and retrieved from my garage. Furthermore, my garage really shouldn’t care about the attributes of the cars being stored within it.
The ORMs that I have worked with have generally felt too tightly coupled to the persistence mechanism underneath them.
Back when I was developing the MMO HackWars, I was a fairly green developer:
- Having grown up developing games in C, I thought of objects as things that stuck around for a while, e.g., a player shooting at alien objects as they darted around the screen.
- Coming from a strong OOP background, I had an object-centric view of software development, not a data-centric view.
I made the following design decisions for HackWars based on these facts:
- MySQL was used purely as a key/value store. I just wanted a place to serialize my models to.
- Saving was abstracted away from the domain model and overseen by a central thread. I think this decision related to my not thinking of an application in terms of a request/response paradigm.
It was a bit of a shift for me when I started doing professional web-development. The design approaches prevalent in the space (and encouraged by frameworks like Rails) did not really jive with my way of thinking about software design. I think this might be why I took to doing a lot of front-end JavaScript work. The type of design you do in browser-side JavaScript is actually shockingly close to what I was used to from game development.
Enter Node.JS. I recently saw a talk by Ryan Dahl, the creator of Node.JS. In a nutshell, he said it works “the way you feel things should”. In a similar vein, I’ve been working on an Object Mapper for CouchDB/Node.js that works the way I feel persisting data should. It’s modeled after the approach I used for my MMO:
- Models are just dumped directly to CouchDB.
- Relationships, validations, schemas, etc., are all implicit in the domain model, not imposed by the database.
- Saving is performed asynchronously and automatically. You’re always interacting with a cached version of an object, with the understanding that it will save occasionally — this is freaking fast, and works well for domains like games.
Without further adieu, I present to you, Endtable:
You can install it either from github or NPM:
git clone https://github.com/bcoe/endtable.git
or,
npm install endtable
First you create an engine for connecting to CouchDB:
You then define the Plain-Old-JavaScript-Objects that you’ll be using:
type is a reserved word in endtable that is used when permanent views are being auto-generated.
You can now create some objects:
As changes are made to the endtable objects, they will be automatically persisted.
You can retrieve objects from CouchDB using the following syntax, views will be created on the fly:
There you have it, Endtable is in its primordial stages, but I’m pretty happy with how it’s looking so far. Feedback is greatly appreciated.