To add a database adapter to orm
, call its addAdapter
method:
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:
var orm = require('orm2');
orm.connect('cassandra://username:password@localhost/test', function (err, db) { });
The second argument is the constructor for your adapter object.
Your adapter should provide the following members.
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
ofhref
- 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 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)
andset(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
- properties.association_key - A function taking a
name
andfield
parameter that returns the name of the column that establishes the association
This should be set to true
if your database is a SQL database.
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.
Establishes your database connection.
Establishes/re-establishes a connection. The optional prior connection is passed in the connection
parameter.
Tests whether your connection is still alive.
Closes your database connection.
Maps an object property to the correlated value to use for the database.
Maps a database value to the property value to use for the mapped object.
Implement this to select and return data stored in the database. See the documentation for Model.find.
Inserts an object into a database table.
Updates an object in the appropriate database row.
Implement this to support the removal of an object from a table.
Implement this to support Model.count.
Implement this to support Model.clear
, which deletes all objects from a given table.
Implement this to support eager loading of associated objects.
For SQL databases, the Query
object from the sql-query
package used to generate ad-hoc queries.
For SQL databases, returns a Query
object from the sql-query
package.
For SQL databases, this executes a Query
object from the sql-query
package.
If your adapter supports SQL aggregate functions, this should be an array of supported function names.
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
- get(Instance, conditions, options, createInstance, cb) - retrieves associated objects
- add(Instance, Association, data, cb) - inserts an associated object
- del(Instance, Associations, cb) - deletes an object from a set of associations
If your adapter supports creating a table from a model, implement this method. The following options are passed:
- extension
- id
- table
- properties
- allProperties
- indexes
- customTypes
- one_associations
- many_associations
- extend_associations
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
- one_associations
- many_associations
Your adapter should be an EventEmitter
, and should emit the error
event when applicable.