Skip to content

Databases

dajester2013 edited this page May 28, 2014 · 3 revisions

Basic information

The database is the entry point for all ArangoDB operations. Each operation requires a database to be specified, even if it is the ArangoDB default database, _system. Therefore, if authentication is enabled on the ArangoDB side, you must make sure the user either has access to _system or set the user's default database in the connection.

Creating a database

Normally the database would be created on the instance. However, it is possible to use CFArango to create a database instance. Note: This requires the user to have access to the _system database.

<cfscript>
// Please review Getting Connected for info on setting up the connection.

dbHelper = new org.jdsnet.arangodb.DatabaseHelper(conn);

// if the username/password is different from the current connection, a new connection is opened for that username/combo
tweetbase = dbHelper.createDatabase(
     "tweetbase"
    ,"root" // defaults to the current connection's user
    ,"password" // defaults to the current connection's password
);
</cfscript>

Dropping a database

Databases can be dropped using the same DatabaseHelper object:

<cfscript>
success = dbHelper.dropDatabase(
     "tweetbase"
);
</cfscript>

Interacting with a database

All other operations concerning a database are handled by the Database model. Below are some examples:

<cfscript>
// A database model is accessible via the connection factory method:
tweetbase = conn.getDatabase("tweetbase");

// Get database information
tweetbaseinfo = tweetbase.getInfo();

// This will fetch a list of database collections:
userCollections = tweetbase.getCollections();
systemCollections = tweetbase.getCollections("system");

// Create a document collection (similar to a RDBMS table)
// Collections are covered in greater detail in the Collections article
Person = tweetbase.createCollection("Person");
Tweets = tweetbase.createCollection("Tweets");

// Create an edge collection (specialized collection that has the ability to link any two documents, from 
// any collection.
follows = tweetbase.createEdgeCollection("follows");

// Get an existing collection
Person = tweetbase.getCollection("Person");

// Drop a collection
result = tweetbase.dropCollection("Tweets");

// Fast look-up of a document by id:
document = tweetbase.getDocumentById("Tweet/33793927293");

// transactional statements
batch = tweetbase.prepareBatch(); // batch of statements to execute together.
transaction = tweetbase.prepareTransaction(); // extension of a batch that ensures all statements succeed.

// AQL query statements
var stmt = tweetbase.prepareStatement("for tweet in Tweets return tweet");
</cfscript>

Clone this wiki locally