Core database connection, authentication, and database lifecycle management functionality for ArangoDB.
Creates a new Database instance with connection configuration.
/**
* Creates a new Database instance with configuration options
* @param config - Configuration object, URL string, or array of URLs
* @param name - Database name (when using URL parameter)
*/
class Database {
constructor(config?: ConfigOptions | string | string[], name?: string);
}
/**
* Convenience function that wraps Database constructor
* @param config - Configuration options
*/
function arangojs(config?: ConfigOptions): Database;
/**
* Convenience function with URL and database name
* @param url - Base URL or array of URLs
* @param name - Database name
*/
function arangojs(url: string | string[], name?: string): Database;Usage Examples:
import { Database, arangojs } from "arangojs";
// Using constructor with config object
const db = new Database({
url: "http://127.0.0.1:8529",
databaseName: "myapp",
auth: { username: "root", password: "password" }
});
// Using arangojs function
const db2 = arangojs({
url: "http://localhost:8529",
databaseName: "test",
auth: { username: "admin", password: "secret" }
});
// Using URL and name parameters
const db3 = arangojs("http://127.0.0.1:8529", "production");
// Multiple URLs for failover
const db4 = new Database([
"http://server1:8529",
"http://server2:8529",
"http://server3:8529"
], "cluster_db");Manages authentication credentials for database connections.
/**
* Sets HTTP Basic authentication credentials
* @param username - Username for authentication
* @param password - Password for authentication
* @returns Database instance for method chaining
*/
useBasicAuth(username?: string, password?: string): this;
/**
* Sets HTTP Bearer token authentication
* @param token - Bearer token for authentication
* @returns Database instance for method chaining
*/
useBearerAuth(token: string): this;
/**
* Performs login and returns JWT token
* @param username - Username for login
* @param password - Password for login
* @returns Promise resolving to JWT token
*/
login(username?: string, password?: string): Promise<string>;
/**
* Renews the current authentication token
* @returns Promise resolving to new token or null
*/
renewAuthToken(): Promise<string | null>;Usage Examples:
const db = new Database("http://127.0.0.1:8529", "mydb");
// Basic authentication
db.useBasicAuth("admin", "password");
// Bearer token authentication
db.useBearerAuth("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...");
// Login to get JWT token
const token = await db.login("admin", "password");
console.log("JWT Token:", token);
// Renew existing token
const newToken = await db.renewAuthToken();
if (newToken) {
console.log("Token renewed:", newToken);
}Manages database connections and connection pooling.
/**
* Acquires host list from ArangoDB coordinators for load balancing
* @param overwrite - Whether to overwrite existing host list
*/
acquireHostList(overwrite?: boolean): Promise<void>;
/**
* Closes all active connections to the database
*/
close(): void;
/**
* Waits for changes to propagate across cluster
* @param request - Request options to check propagation
* @param timeout - Maximum time to wait in milliseconds
*/
waitForPropagation(request: RequestOptions, timeout?: number): Promise<void>;Usage Examples:
const db = new Database({
url: ["http://coordinator1:8529", "http://coordinator2:8529"],
databaseName: "mydb",
loadBalancingStrategy: "ROUND_ROBIN"
});
// Acquire updated host list from coordinators
await db.acquireHostList();
// Perform operations...
// Wait for changes to propagate in cluster
await db.waitForPropagation({ method: "GET", path: "/_api/version" }, 5000);
// Close connections when done
db.close();Manages database lifecycle operations and metadata.
/**
* Creates a reference to another database
* @param databaseName - Name of the database
* @returns New Database instance for the specified database
*/
database(databaseName: string): Database;
/**
* Retrieves information about the current database
* @returns Promise resolving to database description
*/
get(): Promise<DatabaseDescription>;
/**
* Checks if the current database exists
* @returns Promise resolving to boolean indicating existence
*/
exists(): Promise<boolean>;
/**
* Creates a new database
* @param databaseName - Name for the new database
* @param options - Database creation options
* @returns Promise resolving to Database instance for new database
*/
createDatabase(databaseName: string, options?: CreateDatabaseOptions): Promise<Database>;
/**
* Lists all databases the user has access to
* @returns Promise resolving to array of database names
*/
listDatabases(): Promise<string[]>;
/**
* Returns Database instances for all accessible databases
* @returns Promise resolving to array of Database instances
*/
databases(): Promise<Database[]>;
/**
* Drops a database
* @param databaseName - Name of database to drop
* @returns Promise resolving to boolean indicating success
*/
dropDatabase(databaseName: string): Promise<boolean>;Usage Examples:
const systemDb = new Database();
// Check if database exists
const exists = await systemDb.database("myapp").exists();
if (!exists) {
// Create new database with options
const newDb = await systemDb.createDatabase("myapp", {
users: [{ username: "appuser", password: "secret" }],
replicationFactor: 2
});
}
// Get database information
const dbInfo = await systemDb.database("myapp").get();
console.log("Database info:", dbInfo.name, dbInfo.id, dbInfo.isSystem);
// List all databases
const databases = await systemDb.listDatabases();
console.log("Available databases:", databases);
// Get Database instances for all databases
const dbInstances = await systemDb.databases();
for (const db of dbInstances) {
const info = await db.get();
console.log(`Database: ${info.name}`);
}
// Switch to specific database
const appDb = systemDb.database("myapp");interface ConfigOptions {
url?: string | string[];
databaseName?: string;
auth?: BasicAuthCredentials | BearerAuthCredentials;
loadBalancingStrategy?: LoadBalancingStrategy;
maxRetries?: number;
retryTimeout?: number;
keepAlive?: boolean;
timeout?: number;
headers?: Record<string, string>;
}
interface BasicAuthCredentials {
username: string;
password?: string;
}
interface BearerAuthCredentials {
token: string;
}
type LoadBalancingStrategy = "NONE" | "ROUND_ROBIN" | "ONE_RANDOM";
interface DatabaseDescription {
name: string;
id: string;
path: string;
isSystem: boolean;
sharding?: "" | "flexible" | "single";
replicationFactor?: "satellite" | number;
writeConcern?: number;
}
interface CreateDatabaseOptions {
users?: CreateDatabaseUserOptions[];
sharding?: "" | "flexible" | "single";
replicationFactor?: "satellite" | number;
writeConcern?: number;
}
interface CreateDatabaseUserOptions {
username: string;
password?: string;
active?: boolean;
extra?: Record<string, any>;
}
interface RequestOptions {
method?: string;
path?: string;
headers?: Record<string, string>;
query?: Record<string, any>;
body?: any;
}