Skip to content

Commit

Permalink
Terminology change from Driver to Adapter
Browse files Browse the repository at this point in the history
Updated terminology, where third-party database adapters are now called Adapters instead of Drivers.
  • Loading branch information
jpage-godaddy committed Jun 4, 2014
1 parent 44ea464 commit 33a09e9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
38 changes: 18 additions & 20 deletions Drivers.md → Adapters.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Creating drivers for orm2
# Creating database adapters for orm2

To add a driver to `orm2`, call its `addDriver` method:
To add a database adapter to `orm`, call its `addAdapter` method:

```js
require('orm2').addDriver('cassandra', CassandraDriver);
require('orm2').addAdapter('cassandra', CassandraAdapter);
```

The first argument is the alias to register for connection URLs. For example, the above will allow you to do this:
Expand All @@ -13,29 +13,29 @@ var orm = require('orm2');
orm.connect('cassandra://username:password@localhost/test', function (err, db) { });
```

The second argument is the constructor for your driver object.
The second argument is the constructor for your adapter object.

## Defining driver objects
## Defining adapters

Your driver should provide the following members.
Your adapter should provide the following members.

### Constructor(config, connection, opts)

The driver object constructor should have three parameters:
The adapter object constructor should have three parameters:

* config - optional configuration for the database connection. It contains the following properties:
* timezone - optional timezone
* href - URL to use for connecting to the database if the connection argument is null
* host - The hostname of `href`
* pathname - The `path` of `href`
* ssl - Boolean indicating whether the driver should use SSL when connecting to the database
* query - Optional configuration for how the driver should perform queries
* ssl - Boolean indicating whether the adapter should use SSL when connecting to the database
* query - Optional configuration for how the adapter should perform queries
* ssl - Boolean indicating whether queries should be sent using SSL
* strdates - Boolean indicating whether strings should be used for dates
* connection - optionally passed if reusing an existing connection
* opts - optional options configuring the driver's behavior. It contains the following properties:
* pool - A boolean indicating whether the driver should use connection pooling
* debug - If true, whether the driver should operate in debug mode
* opts - optional options configuring the adapter's behavior. It contains the following properties:
* pool - A boolean indicating whether the adapter should use connection pooling
* debug - If true, whether the adapter should operate in debug mode
* settings - A key/value object store. Use `get(key)` and `set(key, value)` methods to manipulate the settings. The
following settings are defined:
* properties.primary_key - The column/field name to use for object primary keys
Expand All @@ -48,7 +48,7 @@ This should be set to `true` if your database is a SQL database.

### customTypes property

Your driver should have a `customTypes` object, with the property names being the names of the custom types, and each
Your adapter should have a `customTypes` object, with the property names being the names of the custom types, and each
value being the options relating to the type.

### connect(cb) method (required)
Expand Down Expand Up @@ -118,11 +118,11 @@ For SQL databases, this executes a `Query` object from the `sql-query` package.

### aggregate_functions[] property (optional)

If your driver supports SQL aggregate functions, this should be an array of supported function names.
If your adapter supports SQL aggregate functions, this should be an array of supported function names.

### hasMany(Model, association) method (optional)

If your driver maintains associations in a unique (non-SQL-like) manner, return an object from this method to implement
If your adapter maintains associations in a unique (non-SQL-like) manner, return an object from this method to implement
a one-to-many association. The return value should have the following methods:

* has(Instance, Associations, conditions, cb) - tests if the associations have any objects matching the conditions
Expand All @@ -132,7 +132,7 @@ a one-to-many association. The return value should have the following methods:

### sync(opts, cb) method (optional)

If your driver supports creating a table from a model, implement this method. The following options are passed:
If your adapter supports creating a table from a model, implement this method. The following options are passed:

* extension
* id
Expand All @@ -147,7 +147,7 @@ If your driver supports creating a table from a model, implement this method. Th

### drop(opts, cb) method (optional)

If your driver supports dropping a table, implement this method. The following options are passed to this method:
If your adapter supports dropping a table, implement this method. The following options are passed to this method:

* table - The name of the table
* properties
Expand All @@ -156,6 +156,4 @@ If your driver supports dropping a table, implement this method. The following o

### on(event, cb) method (required)

Your driver should be an `EventEmitter`, and should emit the following types of events, when applicable:

* error
Your adapter should be an `EventEmitter`, and should emit the `error` event when applicable.
10 changes: 5 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,13 @@ Person(1).getPets(...);
Pet(2).getOwners(...);
```

## Adding external drivers
## Adding external database adapters

To add an external driver to `orm2`, call the `addDriver` method, passing in the alias to use for connecting with this
driver, along with the constructor for the driver object:
To add an external database adapter to `orm`, call the `addAdapter` method, passing in the alias to use for connecting
with this adapter, along with the constructor for the adapter:

```js
require('orm2').addDriver('cassandra', CassandraDriver);
require('orm').addAdapter('cassandra', CassandraAdapter);
```

See [the documentation for creating drivers](./Drivers.md) for more details.
See [the documentation for creating adapters](./Adapters.md) for more details.
21 changes: 21 additions & 0 deletions lib/Adapters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var aliases = require('./Drivers/aliases');

module.exports.add = addAdapter;
module.exports.get = getAdapter;


var adapters = {};

function addAdapter(name, constructor) {
adapters[name] = constructor;
}

function getAdapter(name) {
if (name in aliases) {
return getAdapter(aliases[name]);
} else if (!(name in adapters)) {
adapters[name] = require("./Drivers/DML/" + name).Driver;
}

return adapters[name];
}
21 changes: 0 additions & 21 deletions lib/Drivers/drivers.js

This file was deleted.

8 changes: 4 additions & 4 deletions lib/ORM.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var _ = require("lodash");

var Model = require("./Model").Model;
var DriverAliases = require("./Drivers/aliases");
var drivers = require("./Drivers/drivers");
var adapters = require("./Adapters");
var Settings = require("./Settings");
var Singleton = require("./Singleton");
var ORMError = require("./Error");
Expand Down Expand Up @@ -49,7 +49,7 @@ exports.use = function (connection, proto, opts, cb) {
}

try {
var Driver = drivers.get(proto);
var Driver = adapters.get(proto);
var settings = new Settings.Container(exports.settings.get('*'));
var driver = new Driver(null, connection, {
debug : (opts.query && opts.query.debug === 'true'),
Expand Down Expand Up @@ -110,7 +110,7 @@ exports.connect = function (opts, cb) {
}

try {
var Driver = drivers.get(proto);
var Driver = adapters.get(proto);
var settings = new Settings.Container(exports.settings.get('*'));
var debug = extractOption(opts, "debug");
var pool = extractOption(opts, "pool");
Expand Down Expand Up @@ -143,7 +143,7 @@ exports.connect = function (opts, cb) {
return db;
};

exports.addDriver = drivers.add;
exports.addAdapter = adapters.add;

function ORM(driver_name, driver, settings) {
this.validators = exports.validators;
Expand Down

0 comments on commit 33a09e9

Please sign in to comment.