From CommonJS Spec Wiki
Jump to: navigation, search

Relational Database Interface

Though there are many kinds of persistence engines available, relational databases are the most commonly deployed. Relational databases have a consistent data model and a reasonably consistent set of operations. This has allowed for the creation of a standard interface in many different languages.

If it's possible to interface with other types of databases using the same kind of interface, that is a win. However, the primary goal with this API is to provide access to relational databases.

Prior Art

Other notes

I second that proposal.

I particuarly like the model CodeIgniter uses for the dbms interface: A common interface or abstract class that is then subclassed for each database implementation. This would allow some RDBMS implementations to be done by this project, but should allow for me (a third party) to use my own driver.

What are we gonna call our interface for connecting to a database? Options: RDBMS, DB, Connection, Database? 'DB' is short and rather to the point.

   DB : Class {
   DB : function (driver, host, username, password, database) // or the following constructor
   DB : function (driver, connectionString) // mcoquet: I rather like better passing in an object with the arguments instead of a string like so:
   DB : function (driver, connectionConfigObj) // ie: method ("mysql", {host:"bla.com",port:"3306",user:"username",pass:"mypass"})
   query : function (sql) // returns a JS object
   execute : function (sql)
   version : function ()
   close : function()

I haven't defined any sort of ResultSet object because I'm left to understand that a ResultSet class merely (or often) provides a pretty interface for looping through the results and getting some other minor meta data. Most of this functionality can be achieved by providing a simple structured Javascript object. For example:

   myResultSet: {
   meta: ['id', 'name', 'age'],
   data: [
   { id: 1, name: 'Yunero', age: 22 },
   { id: 2, name: 'Abbadon', age: 45 },
   { id: 3, name: 'Luna', age: 31 }

Is there much else a recordset needs?

The DB interface is slim to say the least; Often drivers and classes have all sorts of helper methods; In the case of an RDBMS we could add all sorts of methods for update, select, delete, creating/manipulating database schema,.. but often is the case that the more that gets added - the more politics and ideals are gonna slow it down. Should there be more to this design? Perhaps the DB interface above should be more function overloaded?

The prepared statement style of querying (passing a string with "?" placeholders, and additional arguments filling in the placeholders) is a good way of preventing SQL injection. Should it support that syntax?


Object relational mappers would use the interface defined here, but are still an area of active exploration and not one to standardize at this time.