-
Notifications
You must be signed in to change notification settings - Fork 3
Document Database
Additional details can be found on the OrientDB wiki page. If you would like to extend the graph features, check out the graph database page.
Table of Contents
Notes on Callbacks:
The methods are BlueBird promise objects and use the then and error methods for callbacks. Take a look at the Bluebird API for more details.
db.method().then(resultMethod).error(errorMethod);
//Example
db.method().then(function(results){}).error(function(error){});
***
When calling methods in the database, a connection must be opened using the open method first. All other methods must be included in the callback.
###Open a Database
This is the first operation the client should call. It opens the default database specified in the node-orientdb configuration or you can specify another database to access. Returns the session ID to be reused for all the next calls and the list of configured clusters.
db.open();
//or
db.open(database);
-
database - (string) Database object. Required.
- database_name - (string) Database name. Optional. Default: [default database name].
- database_type - (string) Database type. Either document or graph. Optional. Default: [default database type].
- database_username - (string) Database username. Optional. Default: [default username].
- database_password - (string) Database password. Optional. Default: [default password].
-
results - (array) An array of databases on the server.
-
obj - (object) Database object.
- sessionId - (string) Database name.
-
clusters - (array) Path to database.
- id - (numeric) Id.
- name - (string) Name of cluster.
- type - (string) Cluster type.
- dataSegmentId - (numeric) Data segment id.
- config - (string) Is always null unless you're running in a server clustered configuration.
- release - (string) Version of OrientDB release deployed on server and optionally build number. Example: "1.4.0-SNAPSHOT (build 13)"
-
obj - (object) Database object.
Example
var orientdb = require("node-orientdb");
var Db = orientdb.Db;
var dbConfig = {
//Server
server_host:'localhost',
server_port:2424,
server_username:'admin',
server_password:'admin',
//Database
database_name:'social',
database_username:'admin',
database_password:'admin',
database_type: "document", //Optional. Default: document.
database_storage: "local" //Optional. Default: local.
};
var db = new Db(dbConfig);
db.open()
.then(function(results){
//Details
console.log("Database '" + db.databaseName + "' has " + db.clusters.length + " clusters");
//Queries
db.query("SELECT FROM Users")
.then(function(results){
db.close();
console.log(results);
})
.error(function(error) {
db.close();
console.log(error);
});
})
.error(function(error) {
console.log(error);
});
);
###Close Database Connection
Closes the database and the network connection to the OrientDB Server instance. No return is expected. The socket is also closed.
db.close();
###Copy a Database Copy the database to a remote server.
db.copy(databaseData);
-
databaseData - (object) Data to copy a database.
- url - (string) Database url. Required.
- username - (string) Database username if different from default. Optional.
- password - (object) Database password for user if different from default. Optional.
- server_name - (string) The remote server name.
- server_engine - (string) The remote server engine. Default: "local".
- results - (boolean) Status of action. 1 = successful. 0 = unsuccessful.
###Count Records Get the number of records in a database.
db.countRecords();
- results - (numeric) Number of records in database.
###Reload Database Reloads database information.
db.reload();
-
results - (array) An array of databases on the server.
-
obj - (object) Database object.
-
clusters - (array) Path to database.
- id - (numeric) Id.
- name - (string) Name of cluster.
- type - (string) Cluster type.
- dataSegmentId - (numeric) Data segment id.
-
clusters - (array) Path to database.
-
obj - (object) Database object.
###Get Database Size
Get the size of a database.
db.size();
- results - (numeric) Size in bytes
***
##Classes
###Create a Class
db.createClass(class);
-
class - (string|object) Data to create a class. May use a string for the name or an object for creating a class or sub-class.
- name - (string) Class name. Required.
- super - (string) Super class name. Optional. Default: "".
-
results - (object) Results.
- className - (numeric) Class name.
- clusterId - (numeric) Cluster ID class belongs to.
###Drop/Delete a Class
db.dropClass(className);
- className - (string) Class name. Required.
- results - (boolean) Status of action. 1 = successful. 0 = unsuccessful.
***
##Records
###Query
Run a query command. Some links that may help:
db.query(sql, options);
or
db.command(sql, options);
- sql - (string) Data to create a record.
-
options - (object) Options for the SQL command. Optional.
- params - (object|array) An array or object for prepared statements. Default: {}.
- mode - (string) Asynchronous mode ("a") or synchronous mode ("s"). Default: "s".
- fetchPlan - (string) Used only for select queries, otherwise empty. Default: "".
- nonTextLimit - (numeric) Default: -1.
- className - (string) Class name of the command implementation. Query as idempotent command ("q" or "com.orientechnologies.orient.core.sql.query.OSQLSynchQuery"). Command as non-idempotent command, ie. insert, update, etc. ("c" or "com.orientechnologies.orient.core.sql.OCommandSQL"). Script commands by using any supported server-side scripting like Javascript command. Since v1.0 ("s" or "com.orientechnologies.orient.core.command.script.OCommandScript"). Any other values is the class name. The command will be created via reflection using the default constructor and invoking the fromStream() method against it. Default: "c".
###Load a Record
Load a record by RecordID, according to a fetch plan.
db.recordLoad(record);
-
record - (object) Data to create a record.
-
@rid - (string|array) A record ID or a list of record IDs. Each record ID can be a formatted string (ie. "#9:5") or an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) Specific cluster to add new record.
- clusterPosition - (numeric) Cluster position.
-
@options (object)
- fetchPlan - (string) The fetch plan to use. Optional. Default: "".
- ignoreCache - (numeric) Tells if the cache must be ignored: 1 = ignore the cache, 0 = not ignore. Default: 0.
- loadTombstones - (numeric) The flag which indicates whether information about deleted record should be loaded. The flag is applied only to autosharded storage and ignored otherwise.
-
@rid - (string|array) A record ID or a list of record IDs. Each record ID can be a formatted string (ie. "#9:5") or an object with the cluster ID and cluster position. Required.
-
results - (object | array) Record object or array of multiple record objects.
- @class - (string)
- @type - (string)
- @version - (numeric)
-
@rid - (object)
- recordId - (string) Formatted record ID.
- clusterId - (numeric) Cluster ID
- clusterPosition - (numeric) Cluster position
- [custom] - (*) custom fields within record.
Examples:
//Example of loading a single record using a formatted record id
var record = {
'@rid':'#9:5',
'@options':{
fetchPlan:'',
ignoreCache:0
}
};
db.recordLoad(record)
.then(function(results){
console.log(results);
})
.error(function(error) {
console.log(error);
});
//Example of loading multiple records using cluster id and position
var record = {
'@rid':[{clusterID:9, clusterPosition:0}, {clusterID:9, clusterPosition:1}],
'@options':{
fetchPlan:'',
ignoreCache:0
}
};
db.recordLoad(record)
.then(function(results){
console.log(results);
})
.error(function(error) {
console.log(error);
});
###Create a New Record Create a new record. Returns the position in the cluster of the new record. New records can have version > 0 (since v1.0) in case the RID has been recycled.
db.recordCreate(record);
-
record - (object) Data to create a record.
-
@rid - (string | array) The first part of a record ID may be used (ie. "#9") or an object with the cluster ID. Required (unless class is specified).
- clusterId - (numeric) Specific cluster to add new record.
- @class - (string) Class to create new record. Required (unless clusterId is specified).
-
@options (object)
- dataSegmentId - (string) The segment id to store the data. Optional. Default: -1;
- mode - (numeric) Whether or not to wait for an answer. Valid values are : 0 (synchronous) and 1 (asynchronous). Optional. Default: 0.
- [custom] - (*) custom fields within record.
-
@rid - (string | array) The first part of a record ID may be used (ie. "#9") or an object with the cluster ID. Required (unless class is specified).
-
results - (object) Record object.
- @rid - (string) Record ID.
- @class - (string) Class record belongs to.
- @version - (numeric) Version of the record.
- @type - (string) Record type.
- [custom] - (*) custom fields within record.
Example
//Example of creating a record
var record = {
'@class':'Users',
'id':'123', //Custom field
'name':'Giraldo' //Custom field
};
db.recordCreate(record)
.then(function(results){
console.log(results);
})
.error(function(error) {
console.log(error);
});
###Update a Record Update a record. Returns the new record's version. The updated document will replace the previous. This means that any variables not included in the content will not be included in the updated document.
db.recordUpdate(record);
-
record - (object) Data to create a record.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
- @version - (numeric) Record version policy. (-1) Document update , version increment, no version control. (-2) Document update, no version control nor increment. (-3) Used internal in transaction rollback (version decrement). (>-1) Standard document update (version control).
-
@options (object)
- mode - (boolean) Whether or not to wait for an answer. Valid values are : 0 (synchronous) and 1 (asynchronous). Optional. Default: 0.
- preserve - (boolean) If true, will keep the existing data and overwrite with the updated data. "in" and "out" links will not be saved. Use an UPDATE query as an alternative. If false, will clear old data and replace with new. Valid values are: 1 (preserve old data), 0 (do not).
- [custom] - (*) custom fields within record.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
-
results - (object) Record object.
- @rid - (string) Record ID.
- @class - (string) Class record belongs to.
- @version - (numeric) Version of the record.
- @type - (string) Record type.
- [custom] - (*) custom fields within record.
Examples
//Example of updating a record
var record = {
'@rid':'#9:5',
'name':'Tester Updated' //Custom field
};
db.recordCreate(record)
.then(function(results){
console.log(results);
})
.error(function(error) {
console.log(error);
});
//Example of updating a record with version control and preserving existing data. Version control is not required to preserve.
var record = {
'@rid':'#9:5',
'@version':5,
'@options':{
preserve:true
},
'name':'Tester Updated'
};
db.recordCreate(record)
.then(function(results){
console.log(results);
})
.error(function(error) {
console.log(error);
});
###Delete a Record Delete a record by its RecordID. During the optimistic transaction the record will be deleted only if the versions match. Returns true if has been deleted otherwise false.
db.recordDelete(record);
-
record - (string|object) Record to delete or record ID.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
- @version - (numeric) A specific version to delete. Optional. Default: -1.
-
@options (object)
- mode - (boolean) Whether or not to wait for an answer. Valid values are : 0 (synchronous) and 1 (asynchronous). Optional. Default: 0.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- results - (boolean) True if record was deleted successfully.
###Clean Out Record Clean out record.
db.recordCleanOut(record);
-
record - (object) Data of record to clean out.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
- @version - (numeric) A specific version to clean out. Optional. Default: -1.
-
@options (object)
- mode - (numeric) Whether or not to wait for an answer. Valid values are : 0 (synchronous) and 1 (asynchronous). Optional. Default: 0.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- results - (boolean) True if record was cleaned successfully.
###Get Record Metadata
db.recordMetadata(record);
-
record - (object) Data of record to clean out.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
-
results - (object) Record object.
- @rid - (string) Record ID.
- @class - (string) Class record belongs to.
- @version - (numeric) Current version.
- @type - (string) Record type.
###Get Ceiling Positions
db.positionsCeiling(record);
-
record - (object) Data of record to start.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
-
results - (object) Position object.
- clusterPosition - (numeric) Cluster position.
- dataSegmentId - (numeric) Data segment ID.
- dataSegmentPos - (numeric) Data segment position.
- recordSize - (numeric) Size of record in bytes.
- recordVersion - (numeric) Current version of the record.
###Get Higher Positions Get higher positioned records
db.positionsHigher(record);
-
record - (object) Data of record to start.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
-
results - (object) Position object.
- clusterPosition - (numeric) Cluster position.
- dataSegmentId - (numeric) Data segment ID.
- dataSegmentPos - (numeric) Data segment position.
- recordSize - (numeric) Size of record in bytes.
- recordVersion - (numeric) Current version of the record.
###Get Lower Positions Get lower positioned records
db.positionsLower(record);
-
record - (object) Data of record to start.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
-
results - (object) Position object.
- clusterPosition - (numeric) Cluster position.
- dataSegmentId - (numeric) Data segment ID.
- dataSegmentPos - (numeric) Data segment position.
- recordSize - (numeric) Size of record in bytes.
- recordVersion - (numeric) Current version of the record.
###Get Floor Positions
db.positionsFloor(record);
-
record - (object) Data of record to start.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
- recordId - (string) Formatted record ID.
- clusterId - (numeric) The cluster the record belongs to.
- clusterPosition - (numeric) Cluster position.depending on record type.
-
@rid - (string | object) The Record ID to update. May pass the record ID as a string ("#9:5") or as an object with the cluster ID and cluster position. Required.
-
results - (object) Position object.
- clusterPosition - (numeric) Cluster position.
- dataSegmentId - (numeric) Data segment ID.
- dataSegmentPos - (numeric) Data segment position.
- recordSize - (numeric) Size of record in bytes.
- recordVersion - (numeric) Current version of the record.
***
##Data Cluster
###Add a New Data Cluster Add a new data cluster.
db.dataClusterAdd(clusterData);
-
clusterData - (object) Cluster object
- name - (string) Cluster name.
- clusterId - (numeric) Cluster ID. Set to -1 to create a new cluster. Optional. Default: -1.
- type - (string) Type of cluster. Valid types are: physical or memory. Optional. Default: "physical".
- location - (string) Location of physical cluster. Optional.
- dataSegmentName - (string) Data segment name if using physical. Optional.
-
results - (object)
- clusterId - (numeric)
###Drop/Delete a Data Cluster Remove a cluster.
db.dataClusterDrop(clusterId);
- clusterId - (numeric) Cluster ID
- results - (boolean) 1 if the cluster has been successfully removed and the client has to remove too, otherwise 0.
###Get Data Cluster Count Returns the number of records in one or more clusters.
db.dataClusterCount(clusterIds, tombstones);
- clusterIds - (numeric | array) Cluster ID of each single cluster. May use a numeric value for a single cluster or an array of numeric values for multiple clusters.
- tombstones - (boolean) Flag which indicates whether deleted records should be taken in account. It is applicable for autosharded storage only, otherwise it is ignored. Optional. Default: 0.
- results - (numeric) Total number of records found in the requested clusters.
###Get Data Cluster Data Range Returns the range of record ids for a cluster.
db.dataClusterDataRange(clusterId, function(error, result){});
- clusterId - (numeric) Cluster ID.
-
results - (object)
- begin - (numeric) The beginning of the cluster range.
- end - (numeric) The end of the cluster range.
***
###Add a New Data Segment Add a new data segment.
db.dataSegmentAdd(segmentName, location, function(error, result){});
- segmentName - (string) Segment name
- location - (string) Location of segment.
-
results - (object)
- segmentId - (numeric) New segment ID.
###Drop/Delete a Data Segment Drop a data segment.
db.dataSegmentDrop(segmentName);
- segmentName - (string) Segment name
- results - (boolean) 1 if the segment has been successfully removed, otherwise 0.