Abstract layer for Node.js database Abstract layer for Node.js database mongodb mongodb

Abstract layer for Node.js database


There are a few solutions, available via NPM :

  • Node-DBI : "Node-DBI is a SQL database abstraction layer library, strongly inspired by the PHP Zend Framework Zend_Db API. It provides unified functions to work with multiple database engines, through Adapters classes. At this time, supported engines are mysql, mysql-libmysqlclient and sqlite3". Looks like the developpment has been paused.
  • Accessor : "A database wrapper, provide easy access to databases." Supports only MySQL and MongoDB at the moment.
  • Activerecord : "An ORM written in Coffeescript that supports multiple database systems (SQL, NoSQL, and even REST), as well as ID generation middleware. It is fully extendable to add new database systems and plugins."


Update:

Since I've posted this answer, I have abandoned mongoose for official MongoDB NodeJS Drivers as it is fairely intuitive and more loyal to the concept of NoSQL databases.

Original Answer:

I though it might be time to update the answer of an old question:

If you want to use MongoDB as your document-oriented database, mongoose is a good choice and easy to use (example from official site):

var mongoose = require('mongoose');mongoose.connect('mongodb://localhost/test');var Cat = mongoose.model('Cat', { name: String });var kitty = new Cat({ name: 'Zildjian' });kitty.save(function (err) {  if (err) // ...  console.log('meow');});

For a rather modern approach, Mongorito is a good ODM which uses ES6 generators instead of callbacks.

As of 06.2015 I reckon that the best ORM for SQL databases with Node.js/io.js is Sequelize supporting the following databases:

  • PostgreSQL
  • MySQL
  • MariaDB
  • SQLite
  • MSSQL

The setup is fairly easy:

var sequelize = new Sequelize('database', 'username', 'password', {  host: 'localhost',  dialect: 'mysql'});// Or you can simply use a connection urivar sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');

It also provides transactions, migrations and many other goodies.