Oriento
Official orientdb driver for node.js. Fast, lightweight, uses the binary protocol.
Supported Versions
Oriento aims to work with version 1.7.1 of orientdb and later. While it may work with earlier versions, they are not currently supported, pull requests are welcome!
IMPORTANT: Oriento does not currently support OrientDB's Tree Based RIDBag feature because it relies on making additional network requests. This means that by default, the result of e.g.
JSON.stringify(record)
for a record with up to 119 edges will be very different from a record with 120+ edges. This can lead to very nasty surprises which may not manifest themselves during development but could appear at any time in production. There is an open issue for this in OrientDB, until that gets fixed, it is strongly recommended that you setRID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD
to a very large value, e.g. 2147483647. Please see the relevant section in the OrientDB manual for more information.
Installation
Install via npm.
npm install oriento
Running Tests
To run the test suite, first invoke the following command within the repo, installing the development dependencies:
npm install
Then run the tests:
npm test
Features
- Tested with latest orientdb (1.7).
- Intuitive API, based on bluebird promises.
- Fast binary protocol parser.
- Access multiple databases via the same socket.
- Migration support.
- Simple CLI.
- Connection Pooling
Usage
Configuring the client.
var Oriento = ; var server = ;
Listing the databases on the server
server;
Creating a new database
server;
Using an existing database
var db = server;console;
Using an existing database with credentials
var db = server;console;
Execute an Insert Query
db;
Execute a Select Query with Params
db;
Raw Execution of a Query String with Params
db;
Query Builder: Insert Record
db;
Query Builder: Update Record
db;
Query Builder: Delete Record
db;
Query Builder: Select Records
dball;
Query Builder: Text Search
dball;
Query Builder: Select Records with Fetch Plan
dball;
Query Builder: Select an expression
db;
Query Builder: Traverse Records
dball;
Query Builder: Return a specific column
dball;
Query Builder: Transform a field
db;
Query Builder: Transform a record
db;
Query Builder: Specify default values
db;
Query Builder: Put a map entry into a map
db;
Loading a record by RID.
dbrecord;
Deleting a record
dbrecord;
Listing all the classes in the database
dbclass;
Creating a new class
dbclass;
Creating a new class that extends another
dbclass;
Getting an existing class
dbclass;
Updating an existing class
dbclass;
Listing properties in a class
MyClassproperty;
Adding a property to a class
MyClassproperty;
Deleting a property from a class
MyClassproperty;
Renaming a property on a class
MyClassproperty;;
Creating a record for a class
MyClass;
Listing records in a class
MyClass;
Create a new index for a class property
dbindex;
Get entry from class property index
dbindex;
Creating a new, empty vertex
db;
Creating a new vertex with some properties
db;
Deleting a vertex
db;
Creating a simple edge between vertices
db;
Creating an edge with properties
db;
Deleting an edge between vertices
db;
Creating a function
You can create a function by supplying a plain javascript function. Please note that the method stringifies the function
passed so you can't use any varaibles outside the function closure.
db;
You can also omit the name and it'll default to the Function#name
db;
CLI
An extremely minimalist command line interface is provided to allow databases to created and migrations to be applied via the terminal.
To be useful, oriento requires some arguments to authenticate against the server. All operations require the password
argument unless the user is configured with an empty password. For operations that involve a specific db, include the dbname
argument (with dbuser
and dbpassword
if they are set to something other than the default).
You can get a list of the supported arguments using oriento --help
.
-d, --cwd The working directory to use. -h, --host The server hostname or IP address. -p, --port The server port. -u, --user The server username. -s, --password The server password. -n, --dbname The name of the database to use. -U, --dbuser The database username. -P, --dbpassword The database password. -?, --help Show the help screen.
If it's too tedious to type these options in every time, you can also create an oriento.opts
file containing them. Oriento will search for this file in the working directory and apply any arguments it contains.
For an example of such a file, see test/fixtures/oriento.opts.
Note: For brevity, all these examples assume you've installed oriento globally (
npm install -g oriento
) and have set up an oriento.opts file with your server and database credentials.
Database CLI Commands.
Listing all the databases on the server.
oriento db list
Creating a new database
oriento db create mydb graph plocal
Destroying an existing database
oriento db drop mydb
Migrations
Oriento supports a simple database migration system. This makes it easy to keep track of changes to your orientdb database structure between multiple environments and distributed teams.
When you run a migration command, oriento first looks for an orient class called Migration
. If this class doesn't exist it will be created.
This class is used to keep track of the migrations that have been applied.
Oriento then looks for migrations that have not yet been applied in a folder called migrations
. Each migration consists of a simple node.js module which exports two methods - up()
and down()
. Each method receives the currently selected database instance as an argument.
The up()
method should perform the migration and the down()
method should undo it.
Note: Migrations can incur data loss! Make sure you back up your database before migrating up and down.
In addition to the command line options outlined below, it's also possible to use the migration API programatically:
var db = server; var manager = db: db dir: __dirname + '/migrations'; manager;
Listing the available migrations
To list all the unapplied migrations:
oriento migrate list
Creating a new migration
oriento migrate create my new migration
creates a file called something like m20140318_200948_my_new_migration
which you should edit to specify the migration up and down methods.
Migrating up fully
To apply all the migrations:
oriento migrate up
Migrating up by 1
To apply only the first migration:
oriento migrate up 1
Migrating down fully
To revert all migrations:
oriento migrate down
Migrating down by 1
oriento migrate down 1
Events
You can also bind to the following events
beginQuery
Given the query
db.select('name, status').from('OUser').where({"status": "active"}).limit(1).fetch({"role": 1}).one();
The following event will be triggered
db.on("beginQuery", function(obj) {
// => {
// query: 'SELECT name, status FROM OUser WHERE status = :paramstatus0 LIMIT 1',
// mode: 'a',
// fetchPlan: 'role:1',
// limit: -1,
// params: { params: { paramstatus0: 'active' } }
// }
});
endQuery
After a query has been run, you'll get the the following event emitted
db.on("endQuery", function(obj) {
// => {
// "err": errObj,
// "result": resultObj,
// "perf": {
// "query": timeInMs
// }
// }
});
History
In 2012, Gabriel Petrovay created the original node-orientdb library, with a straightforward callback based API.
In early 2014, Giraldo Rosales made a whole host of improvements, including support for orientdb 1.7 and switched to a promise based API.
Later in 2014, codemix refactored the library to make it easier to extend and maintain, and introduced an API similar to nano. The result is so different from the original codebase that it warranted its own name and npm package. This also gave us the opportunity to switch to semantic versioning.
Notes for contributors
Please see CONTRIBUTING.
Changes
See CHANGELOG
License
Apache 2.0 License, see LICENSE