This post was written for Nodejitsu blog and is crossposted here. Nodejitsu loved what I was doing at and is now supporting the project.

Data access is something Node.js is very well known for. When it comes to drivers, there’s support for nearly every database under the sun. These days however, in most cases you use an ORM library instead of a plain driver. It saves you time and you don’t have to write every single thing youself.

There are database specific ORM libraries such as Mongoose for MongoDB and Sequelize for MySQL and PostgreSQL. The downside to this approach is that you have to learn a whole new API when you have to go from one database to another. I’m not suggesting it’s a good idea to switch or use different databases during a lifecycle of a single project (eg SQLite for development and MySQL for production). But going from project to project, client to client, your requirements might change and you will have to use a different database and then learn a whole new API. Fun? Yes! But if you have to delivery features quickly, this might be a setback.

JugglingDB (GitHub: 1602/jugglingdb, License: MIT) by Anatoliy Chakkaev is a young ORM library that tries to provide a single API for a vast array of databases. All adapters come in a form of separate modules and there are quite a few already available:

  • ArangoDB
  • CouchDB
  • Firebird
  • MongoDB
  • MySQL
  • PostgreSQL
  • Redis
  • RethinkDB
  • SQLite

Lets check it out!

npm install jugglingdb


var Schema = require('jugglingdb').Schema;
var schema = new Schema('redis', {port: 6379});

var Post = schema.define('Post', {
  title: { type: String, length: 255 },
  content: { type: Schema.Text },
  date: { type: Date, default: function () { return new Date; } },
  timestamp: { type: Number,  default: },
  published: { type: Boolean, default: false, index: true }

var User = schema.define('User', {
  name: String,
  bio: Schema.Text,
  approved: Boolean,
  joinedAt: Date,
  age: Number

User.hasMany(Post, { as: 'posts',  foreignKey: 'userId' });
Post.belongsTo(User, { as: 'author', foreignKey: 'userId' });

var user = new User({ name: 'Alex' }); (err) {
  var post ={ title: 'Hello world' });;

Here’s the cool part, if you want to use a different database, all you have to do is change how your schema connects:

var schema = new Schema('mongodb', {url: 'mongodb://localhost/myapp'});

What’s even cooler, is that you can have multiple schemas connecting to multiple databases at the same time. This could be very handy if for example you want to keep some cached data in Redis, which storing your loosly defined documents in MongoDB and your accounting stuff in MySQL. You could do all that with a single API.

Closing thoughts

1602/jugglingdb is not as well known as Mongoose, but it’s been in development since 2011 and has a pretty strong following on GitHub. Documentation is pretty extensive and roadmap gives you a glimpse of where the project is headed and what is currently considered missing. Finally, check out source code for this article on GitHub and an interactive example on Runnable. I’ve included MongoDB, Redis and Memory examples.