Waterline adapter for OrientDB. Waterline is an adapter-based ORM for Node.js with support for mysql, mongo, postgres, redis, orientdb and more.
waterline-schema v0.10.19, a dependency of waterline, breaks sails-orientdb tests. As such we recommend using sails-orientdb with waterline <= 0.10.27 / waterline-schema <= 0.1.18. More about this on issue #152.
sails-orientdbmaps the logical
idattribute to the required
@ridphysical-layer OrientDB Record ID.
We don't recommend using
migrate: 'alter'as it has the nasty effect of deleting the data of all edges on a graphDB, leaving only data on the vertexes. Either use
'safe'and migrate manually, or use
'drop'to completely reset the data and collections or use
'create'to only create new collections/attributes.
From the waterline adapter interfaces sails-orientdb supports
SQL interfaces. Sails-orientdb complies with waterline API and it's used in the same way as other waterline/sails adapters.
Sails-orientdb connects to OrientDB using OrientJS (OrientDB's official driver).
Table of Contents
- Issues or Suggestions
- About Waterline
Install stable version from NPM:
npm install sails-orientdb --save
Install edge version from GitHub (mostly for testing):
npm install appscot/sails-orientdb
For examples on how to use Waterline/sails-orientdb with frameworks such as Express look in the example folder.
Waterline v0.10.x configuration
var orientAdapter = ;var config =adapters:'default': orientAdapterorient: orientAdapterconnections:myLocalOrient:adapter: 'orient'host: 'localhost'port: 2424user: 'root'password: 'root'database: 'waterline'defaults:// The first time you run sails-orientdb `migrate` needs to be set to 'drop', 'alter' or 'create' in order to create the DB schema// More about this on: #!/documentation/concepts/ORM/model-settings.htmlmigrate: 'safe'
Connection advanced config example
myLocalOrient:adapter: 'orient'host: 'localhost'port: 2424user: 'root'password: 'root'database: 'waterline'schema : true// Additional optionsoptions:// DB/Oriento Options//// database type: graph | documentdatabaseType : 'graph'//// storage type: memory | plocalstorage : 'plocal'// Useful in REST APIs//// If `id` is URI encoded, decode it with `decodeURIComponent()` (useful when `id` comes from an URL)decodeURIComponent : true//// Replaces circular references with `id` after populate operations (useful when results will be JSONfied)removeCircularReferences : false// migrations//// Drop tables without deleting edges/vertexes hence not ensuring graph consistency// Will speed up drop operations. Only works with migration: 'alter' or 'drop'unsafeDrop : false// other//// Turn parameterized queries onparameterized : true
The values stated above represent the default values. For an up to date comprehensive list check adapter.js.
In a graph db Sails-orientdb will represent most models in OrientDB as vertexes, the exception being Many-to-Many join tables which are represented by Edges. If using a document db, all models will be represented by documents.
To learn how to create associations with Waterline/Sails.js check the Waterline Docs Associations Page. Below we go through how sails-orientdb represent each kind of association in an OrientDB database.
For One-to-One Associations sails-orientdb creates a LINK (OrientDB Types) to associate records.
One-to-Many Associations are also represented by LINKs in OrientDB.
In many-to-many associations sails-orientdb will connect vertexes using edges, hence edges act as join tables.
Custom edge names (optional)
Usually Waterline will create rather long names for join tables (e.g. driver_taxis__taxi_drivers) which are little meaningful from the perspective of a graphDB. Sails-orientdb allows you to change the name of the edge by adding a property
joinTableNames to the dominant collection. Example:
identity: 'driver'joinTableNames:taxis: 'drives'attributes:name: 'string'taxis:collection: 'taxi'via: 'drivers'dominant: true
In this example the join table name driver_taxis__taxi_drivers gets converted to drives. Complete example of the fixture can be found here.
Many-to-Many Through Associations
In a Many-to-Many Through Association (more info) the join table is represented in OrientDB by Edges. Sails-orientdb automatically creates the edges whenever an association is created. The Edge is named after the property tableName (or identity in case tableName is missing).
Populate queries (joins)
Sails-orientdb implements its own custom join function so when the user runs
.populate(some_collection) it will send a single
SELECT query with a fetchplan to OrientDB. This way join operations remain fast and performant by leveraging OrientDB's graphDB features.
For a comprehensive reference on how to use
sails-orientdb/waterline check the Waterline Documentation.
For a SailsJS/OrientDB tutorial check Chris' Creating an application with SailsJS and OrientDB on your Mac.
Below we describe how
sails-orientdb approaches and adds to the waterline core experience.
sails-orientdb uses the standard waterline model definition and extends it in order to accommodate OrientDB features.
To add an index to an attribute the keyword
index is used as per the waterline default model definition:
attributes:email:type: 'string'index: true // will default to SB-Tree NOTUNIQUE
However OrientDB supports other indexes and it's possible to use them by specifying the name of the index type as a string in the
index key. Example:
attributes:email:type: 'string'index: 'fulltext_hash_index'
List of supported index types:
It's possible to force the class of a model by adding the property
orientdbClass to the definition. Generally this is not required as
sails-orientdb can determine which is the best class to use, so it should only be used in special cases. Possible values:
undefined- the default and recommended option. The appropriate class will be determined for the model;
"document"- class will be the default OrientDB document class;
"V"- class will be Vertex;
"E"- class will be Edge.
identity : 'post'orientdbClass : 'V'attributes :name : 'string'
Note, when using a document database (through
orientdbClass class will be ignored and all classes will be documents.
This adapter supports all waterline standard query methods and extends it with the following methods:
.query (query [, options], cb)
Runs a SQL query against the database using Oriento's query method. Will attempt to convert @rid's into ids.
// Assume a model named "Friend"Friend;// Using params (and promise)Friend;
Returns a native Oriento class
// Assume a model named "Post"Postproperty;
Returns a native Oriento database object
// Assume a model named "Post"Postclass;
Returns a native Oriento connection
// Assume a model named "Post"Post;
.runFunction (functionName [, ...])
Returns a prepared Oriento statement with query and params to run an OrientDB function.
// Assume a model named "Post"Post;
.createEdge (from, to, options, callback)
Creates edge between specified two model instances by ID in the form parameters
// Assume a model named "Post"Post;// Adding param createdAt to the edge (and using promise)Post;
Note: when using many-to-many or many-to-many through associations edges will automatically be created. This method is for manual edge manipulation only and it's not required for maintaining associations.
.deleteEdges (from, to, options, callback)
Deletes edges between specified two model instances by ID in the form parameters
// Assume a model named "Post"Post;
Note: when using many-to-many or many-to-many through associations edges will automatically be deleted when using the conventional waterline methods. This method is for manual edge manipulation only and it's not required for maintaining associations.
.increment (criteria, field[, amount][, cb])
Increments the given field by amount (defaults to
1). This can be used to generate sequencial numbers, more about this in OrientDB docs.
// Given a model Counter with attributes `name` and `value`Counter;// To decrement use negative numbersCounter;
Convenience method that replaces circular references with
id when one is available, otherwise it replaces the object with string '[Circular]'.
//Assume a model named "Post"var result = Post;console; // it's safe to stringify result
For a vast set of examples on how to set up models take a look at waterline-adapter-tests fixtures, all of those are working examples and frequently tested.
The only case poorly covered is how to set up a Many-to-many through association as it is not yet officially supported. Below is an example of a Many-to-many through association that works in OrientDB.
Many-to-many through example
Venue model which will join (via edge) teams to stadiums
/*** Venue Model** Join table between teams and associations*/var Waterline = ;moduleexports = WaterlineCollection;
Team Model to be associated with Stadium model
/*** Team.js*/var Waterline = ;moduleexports = WaterlineCollection;
Stadium Model to be associated with Team model
/*** Stadium.js*/var Waterline = ;moduleexports = WaterlineCollection;
An edge named venueTable will be created from Team to Stadium model instances whenever an instance of team model is saved with a 'stadiums' attribute.
Test are written with mocha. Integration tests are handled by the waterline-adapter-tests project, which tests adapter methods against the latest Waterline API.
To run tests:
Issues or Suggestions
We are always trying to improve
sails-orientdb either by fixing issues or adding new features. If you experienced an issue or have a suggestion feel free to raise a new issue, preferably by following the guidelines.
We are always looking for the quality contributions! Please check the CONTRIBUTING.md for the contribution guidelines.
In March 2015, Srinath, Gaurav and Dário combined their efforts into maintaining a single adapter with the original name: sails-orientdb.
- Srinath Janakiraman (vjsrinath)
- Gaurav Dhiman (gauravdhiman)
- Dário Marcelino (dmarcelino)
- and our generous contributors (thank you!)
Waterline is a new kind of storage and retrieval engine.
It provides a uniform API for accessing stuff from different kinds of databases, protocols, and 3rd party APIs. That means you write the same code to get and store things like users, whether they live in OrientDB, Redis, mySQL, LDAP, MongoDB, or Postgres.
Waterline strives to inherit the best parts of ORMs like ActiveRecord, Hibernate, and Mongoose, but with a fresh perspective and emphasis on modularity, testability, and consistency across adapters.