Skip to the content.

Database API

new Database

new Database([config]): Database

Creates a new Database instance.

If config is a string, it will be interpreted as config.url.

Arguments

database.acquireHostList

async database.acquireHostList(): this

Updates the URL list by requesting a list of all coordinators in the cluster and adding any endpoints not initially specified in the url configuration.

For long-running processes communicating with an ArangoDB cluster it is recommended to run this method repeatedly (e.g. once per hour) to make sure new coordinators are picked up correctly and can be used for fail-over or load balancing.

Note: This method can not be used when the arangojs instance was created with isAbsolute: true.

database.useDatabase

database.useDatabase(databaseName): this

Updates the Database instance and its connection string to use the given databaseName, then returns itself.

Note: This method can not be used when the arangojs instance was created with isAbsolute: true.

Arguments

Examples

const db = new Database();
db.useDatabase("test");
// The database instance now uses the database "test".

database.useBasicAuth

database.useBasicAuth([username, [password]]): this

Updates the Database instance’s authorization header to use Basic authentication with the given username and password, then returns itself.

Arguments

Examples

const db = new Database();
db.useDatabase("test");
db.useBasicAuth("admin", "hunter2");
// The database instance now uses the database "test"
// with the username "admin" and password "hunter2".

database.useBearerAuth

database.useBearerAuth(token): this

Updates the Database instance’s authorization header to use Bearer authentication with the given authentication token, then returns itself.

Arguments

Examples

const db = new Database();
db.useBearerAuth("keyboardcat");
// The database instance now uses Bearer authentication.

database.login

async database.login([username, [password]]): string

Validates the given database credentials and exchanges them for an authentication token, then uses the authentication token for future requests and returns it.

Arguments

Examples

const db = new Database();
db.useDatabase("test");
await db.login("admin", "hunter2");
// The database instance now uses the database "test"
// with an authentication token for the "admin" user.

database.version

async database.version(): Object

Fetches the ArangoDB version information for the active database from the server.

Examples

const db = new Database();
const version = await db.version();
// the version object contains the ArangoDB version information.

database.close

database.close(): void

Closes all active connections of the database instance. Can be used to clean up idling connections during longer periods of inactivity.

Note: This method currently has no effect in the browser version of arangojs.

Examples

const db = new Database();
const sessions = db.collection("sessions");
// Clean up expired sessions once per hour
setInterval(async () => {
  await db.query(aql`
    FOR session IN ${sessions}
    FILTER session.expires < DATE_NOW()
    REMOVE session IN ${sessions}
  `);
  // Make sure to close the connections because they're no longer used
  db.close();
}, 1000 * 60 * 60);