Lua on Beans

ArangoDB Wrapper

This page provides an overview of the ArangoDB wrapper functions available in Lua on Beans. These functions allow you to interact with ArangoDB, a multi-model database, using Lua.

For more detailed information about ArangoDB's HTTP API, which these wrapper functions are based on, please refer to the official ArangoDB HTTP API documentation.

Aql

The Aql function is used to execute ArangoDB Query Language (AQL) queries. It allows you to perform complex data retrieval and manipulation operations on your ArangoDB database.

Usage:

Adb.Aql(query, [bindVars])

Parameters:

  • query: A string containing the AQL query to be executed.
  • bindVars (optional): A table containing bind variables for the query.

Returns:

The function returns the result of the AQL query execution.

Example:

local result = Adb.Aql("FOR doc IN users LIMIT @limit RETURN doc", { limit = 10 })

Auth

The Auth function is used to authenticate with the ArangoDB server. It establishes a connection and sets up the necessary credentials for subsequent database operations.

Usage:

Adb.Auth(db_config)

Parameters:

  • db_config: A table containing the database configuration details, including server address, username, and password.

Returns:

The function returns the result of the authentication process.

Example:

local db_config = {
  server = "http://localhost:8529",
  username = "root",
  password = "password"
}
local result = Adb.Auth(db_config)

GetDocument

The GetDocument function is used to retrieve a document from ArangoDB using its unique identifier.

Usage:

Adb.GetDocument(id)

Parameters:

  • id: A string representing the unique identifier of the document. This typically includes both the collection name and the document key, separated by a forward slash.

Returns:

The function returns the requested document if found, or an error if the document doesn't exist.

Example:

local document = Adb.GetDocument("users/12345")

UpdateDocument

The UpdateDocument function is used to update an existing document in ArangoDB.

Usage:

Adb.UpdateDocument(handle, params)

Parameters:

  • handle: A string representing the document handle (collection name and document key).
  • params: A table containing the updated document data.

Returns:

The function returns the result of the update operation, typically including the updated document.

Example:

local handle = "users/12345"
local params = {
  name = "John Doe",
  age = 30
}
local result = Adb.UpdateDocument(handle, params)

CreateDocument

The CreateDocument function is used to create a new document in ArangoDB.

Usage:

Adb.CreateDocument(collection, document)

Parameters:

  • collection: A string representing the name of the collection where the document will be created.
  • document: A table containing the data for the new document.

Returns:

The function returns the result of the create operation, typically including the newly created document with its assigned _key and _id.

Example:

local collection = "users"
local document = {
  name = "Jane Doe",
  age = 28,
  email = "jane.doe@example.com"
}
local result = Adb.CreateDocument(collection, document)

DeleteDocument

The DeleteDocument function is used to remove an existing document from ArangoDB.

Usage:

Adb.DeleteDocument(handle)

Parameters:

  • handle: A string representing the document handle (collection name and document key).

Returns:

The function returns the result of the delete operation, typically including a confirmation of the deletion.

Example:

local handle = "users/12345"
local result = Adb.DeleteDocument(handle)

PatchDocument

The PatchDocument function is used to partially update an existing document in ArangoDB.

Usage:

Adb.PatchDocument(handle, params)

Parameters:

  • handle: A string representing the document handle (collection name and document key).
  • params: A table containing the fields to be updated in the document.

Returns:

The function returns the result of the patch operation, typically including the updated document.

Example:

local handle = "users/12345"
local params = {
  age = 31,
  email = "john.doe@newemail.com"
}
local result = Adb.PatchDocument(handle, params)

GetCollection

The GetCollection function is used to retrieve information about a specific collection in ArangoDB.

Usage:

Adb.GetCollection(collection_name)

Parameters:

  • collection_name: A string representing the name of the collection to retrieve.

Returns:

The function returns information about the specified collection, including its properties and statistics.

Example:

local collection_info = Adb.GetCollection("users")

UpdateCollection

The UpdateCollection function is used to modify the properties of an existing collection in ArangoDB.

Usage:

Adb.UpdateCollection(collection_name, properties)

Parameters:

  • collection_name: A string representing the name of the collection to update.
  • properties: A table containing the properties to be updated for the collection.

Returns:

The function returns the result of the update operation, typically including the updated collection properties.

Example:

local properties = {
  waitForSync = true
}
local result = Adb.UpdateCollection("users", properties)

RenameCollection

The RenameCollection function is used to change the name of an existing collection in ArangoDB.

Usage:

Adb.RenameCollection(old_name, new_name)

Parameters:

  • old_name: A string representing the current name of the collection.
  • new_name: A string representing the new name for the collection.

Returns:

The function returns the result of the rename operation, typically including confirmation of the name change.

Example:

local result = Adb.RenameCollection("old_users", "new_users")

CreateCollection

The CreateCollection function is used to create a new collection in ArangoDB.

Usage:

Adb.CreateCollection(collection_name, [options])

Parameters:

  • collection_name: A string representing the name of the new collection to create.
  • options (optional): A table containing additional options for the collection creation.

Returns:

The function returns the result of the create operation, typically including details of the newly created collection.

Example:

local options = {
  type = 2,  -- document collection
  waitForSync = true
}
local result = Adb.CreateCollection("new_users", options)

CreateCollectionWithTimestamps

The CreateCollectionWithTimestamps function creates a new collection in ArangoDB with automatic timestamp fields.

Usage:

Adb.CreateCollectionWithTimestamps(collection_name, [options])

Parameters:

  • collection_name: A string representing the name of the new collection to create.
  • options (optional): A table containing additional options for the collection creation.

Returns:

The function returns the result of the create operation, including details of the newly created collection with timestamp fields.

Example:

local options = {
  waitForSync = true
}
local result = Adb.CreateCollectionWithTimestamps("timestamped_users", options)

DeleteCollection

The DeleteCollection function is used to remove an existing collection from ArangoDB.

Usage:

Adb.DeleteCollection(collection_name)

Parameters:

  • collection_name: A string representing the name of the collection to delete.

Returns:

The function returns the result of the delete operation, typically including confirmation of the collection deletion.

Example:

local result = Adb.DeleteCollection("old_users")

PatchCollection

The PatchCollection function is used to partially update the properties of an existing collection in ArangoDB.

Usage:

Adb.PatchCollection(collection_name, properties)

Parameters:

  • collection_name: A string representing the name of the collection to patch.
  • properties: A table containing the properties to be updated for the collection.

Returns:

The function returns the result of the patch operation, typically including the updated collection properties.

Example:

local properties = {
  waitForSync = false
}
local result = Adb.PatchCollection("users", properties)

TruncateCollection

The TruncateCollection function is used to remove all documents from a collection in ArangoDB while keeping the collection itself.

Usage:

Adb.TruncateCollection(collection_name)

Parameters:

  • collection_name: A string representing the name of the collection to truncate.

Returns:

The function returns the result of the truncate operation, typically including confirmation that the collection has been emptied.

Example:

local result = Adb.TruncateCollection("users")

GetAllIndexes

The GetAllIndexes function is used to retrieve information about all indexes in a specific collection in ArangoDB.

Usage:

Adb.GetAllIndexes(collection_name)

Parameters:

  • collection_name: A string representing the name of the collection to get indexes from.

Returns:

The function returns a list of all indexes in the specified collection, including their properties and types.

Example:

local indexes = Adb.GetAllIndexes("users")

CreateIndex

The CreateIndex function is used to create a new index on a collection in ArangoDB.

Usage:

Adb.CreateIndex(collection_name, index_definition)

Parameters:

  • collection_name: A string representing the name of the collection to create the index on.
  • index_definition: A table containing the definition of the index to create.

Returns:

The function returns the result of the index creation, typically including details of the newly created index.

Example:

local index_definition = {
  type = "hash",
  fields = {"email"},
  unique = true
}
local result = Adb.CreateIndex("users", index_definition)

DeleteIndex

The DeleteIndex function is used to remove an existing index from a collection in ArangoDB.

Usage:

Adb.DeleteIndex(index_handle)

Parameters:

  • index_handle: A string representing the handle of the index to delete.

Returns:

The function returns the result of the delete operation, typically including confirmation of the index deletion.

Example:

local index_handle = "users/12345"
local result = Adb.DeleteIndex(index_handle)

CreateDatabase

The CreateDatabase function is used to create a new database in ArangoDB.

Usage:

Adb.CreateDatabase(database_name, [options])

Parameters:

  • database_name: A string representing the name of the new database to create.
  • options (optional): A table containing additional options for the database creation.

Returns:

The function returns the result of the create operation, typically including details of the newly created database.

Example:

local options = {
  users = {
    {username = "admin", passwd = "secret", active = true}
  }
}
local result = Adb.CreateDatabase("new_database", options)

DeleteDatabase

The DeleteDatabase function is used to remove an existing database from ArangoDB.

Usage:

Adb.DeleteDatabase(database_name)

Parameters:

  • database_name: A string representing the name of the database to delete.

Returns:

The function returns the result of the delete operation, typically including confirmation of the database deletion.

Example:

local result = Adb.DeleteDatabase("old_database")

BeginTransaction

The BeginTransaction function is used to start a new transaction in ArangoDB.

Usage:

Adb.BeginTransaction([options])

Parameters:

  • options (optional): A table containing additional options for the transaction.

Returns:

The function returns the result of the transaction initiation, typically including a transaction ID.

Example:

local options = {
  collections = {
    write = {"users", "orders"}
  }
}
local result = Adb.BeginTransaction(options)

CommitTransaction

The CommitTransaction function is used to commit an ongoing transaction in ArangoDB.

Usage:

Adb.CommitTransaction(transaction_id)

Parameters:

  • transaction_id: A string representing the ID of the transaction to commit.

Returns:

The function returns the result of the commit operation, typically including confirmation of the transaction commit.

Example:

local result = Adb.CommitTransaction("12345")

AbortTransaction

The AbortTransaction function is used to abort an ongoing transaction in ArangoDB.

Usage:

Adb.AbortTransaction(transaction_id)

Parameters:

  • transaction_id: A string representing the ID of the transaction to abort.

Returns:

The function returns the result of the abort operation, typically including confirmation of the transaction abort.

Example:

local result = Adb.AbortTransaction("12345")

GetQueryCacheEntries

The GetQueryCacheEntries function is used to retrieve information about the entries in the query cache of ArangoDB.

Usage:

Adb.GetQueryCacheEntries()

Parameters:

This function does not take any parameters.

Returns:

The function returns a list of entries in the query cache, including details about each cached query.

Example:

local cache_entries = Adb.GetQueryCacheEntries()

GetQueryCacheConfiguration

The GetQueryCacheConfiguration function is used to retrieve the current configuration of the query cache in ArangoDB.

Usage:

Adb.GetQueryCacheConfiguration()

Parameters:

This function does not take any parameters.

Returns:

The function returns the current configuration settings of the query cache.

Example:

local cache_config = Adb.GetQueryCacheConfiguration()

UpdateCacheConfiguration

The UpdateCacheConfiguration function is used to modify the configuration of the query cache in ArangoDB.

Usage:

Adb.UpdateCacheConfiguration(config)

Parameters:

  • config: A table containing the new configuration settings for the query cache.

Returns:

The function returns the result of the update operation, typically including the new configuration settings.

Example:

local new_config = {
  mode = "on",
  maxResults = 1000
}
local result = Adb.UpdateCacheConfiguration(new_config)

DeleteQueryCache

The DeleteQueryCache function is used to clear the query cache in ArangoDB.

Usage:

Adb.DeleteQueryCache()

Parameters:

This function does not take any parameters.

Returns:

The function returns the result of the delete operation, typically indicating whether the cache was successfully cleared.

Example:

local result = Adb.DeleteQueryCache()

RefreshToken

The RefreshToken function is used to refresh the authentication token for the ArangoDB connection.

Usage:

Adb.RefreshToken(db_config)

Parameters:

  • db_config: A table containing the database configuration settings.

Returns:

This function does not return a value. It refreshes the token if necessary.

Description:

RefreshToken checks if the current token is older than 10 minutes (600 seconds). If so, it calls the Auth function to get a new token and updates the LastDBConnect time.

Example:

local db_config = {
  -- database configuration details
}
Adb.RefreshToken(db_config)