use AnvilDb\AnvilDb;Opens the database engine. Creates the data directory if it doesn't exist. Pass a 64-character hex string to open an encrypted database.
$db = new AnvilDb('/path/to/data');
$db = new AnvilDb('/path/to/data', 'aabbccdd...64chars...');Closes the engine and frees resources. Called automatically on destruction.
Flushes all pending write buffers and closes the engine. The handle is no longer usable after this call.
Flushes all pending buffered writes to disk across all collections.
Configures the write buffer. $maxDocs is the per-collection threshold that triggers an auto-flush. $flushIntervalSecs is the background timer interval.
$db->configureBuffer(200, 10); // flush every 200 docs or 10 secondsReturns a Collection instance for the given name.
Creates a new collection. Throws AnvilDbException on failure.
Drops a collection and deletes its data file.
Returns an array of collection names.
Encrypts an unencrypted database. Rewrites all collection and index files with AES-256-GCM. $key is a 64-character hex string (32 bytes).
Decrypts an encrypted database. Rewrites all files without encryption.
Clears the internal LRU cache.
use AnvilDb\Collection\Collection;Inserts a document. Auto-generates a UUID id if not provided. Returns the inserted document with id.
$doc = $collection->insert(['name' => 'Alice', 'age' => 25]);
echo $doc['id']; // "550e8400-e29b-41d4-a716-446655440000"Inserts multiple documents. Returns array of inserted documents with IDs.
$docs = $collection->bulkInsert([
['name' => 'Alice'],
['name' => 'Bob'],
]);Finds a document by ID. Returns null if not found.
Replaces the document with the given data (preserving the ID). Returns true on success.
Deletes a document by ID. Returns true on success.
Flushes pending buffered writes for this collection to disk.
join(string $collection, string $leftField, string $rightField, string $type = 'inner', ?string $prefix = null): QueryBuilder
Starts a query chain with a join. Joined fields are prefixed (defaults to {collection}_).
$collection->join('users', 'user_id', 'id', 'inner', 'user_')->get();leftJoin(string $collection, string $leftField, string $rightField, ?string $prefix = null): QueryBuilder
Shorthand for join() with $type = 'left'.
Starts a query chain with a filter condition.
Starts a query chain with sorting.
Returns all documents in the collection.
Returns the total number of documents.
Exports the collection to a CSV file. Returns the number of exported documents. If $fields is null, infers columns from the first document.
Imports documents from a CSV file (first row = headers). Returns the number of imported documents. Inserts in batches of 1000.
Creates an index on a field. Types: hash, unique, range.
Drops an index on a field.
Sets a validation schema. Types: string, int, float, bool, array, object.
$collection->setSchema([
'name' => 'string',
'age' => 'int',
]);use AnvilDb\Query\QueryBuilder;join(string $collection, string $leftField, string $rightField, string $type = 'inner', ?string $prefix = null): self
Adds a join clause. Multiple joins can be chained. $type is inner or left. $prefix defaults to {collection}_.
$qb->join('users', 'user_id', 'id', 'inner', 'u_')
->join('products', 'product_id', 'id', 'inner', 'p_')
->where('u_status', '=', 'active')
->get();Filters, sorting, and pagination apply after all joins, so you can reference prefixed fields.
Shorthand for join() with $type = 'left'. Unmatched left rows are included without right-side fields.
Adds a filter. Chainable. Operators: =, !=, >, <, >=, <=, contains, between, in, not_in.
Shorthand for where($field, 'between', [$min, $max]). Inclusive on both ends.
Matches documents where the field value is in the given array.
Matches documents where the field value is NOT in the given array.
Adds an aggregation. When any aggregation is present, get() returns a single result object with the computed values instead of document rows.
Groups results by the given field(s) and applies aggregations per group. Each aggregation is ['function' => '...', 'field' => '...', 'alias' => '...'].
Sets sort order. Direction: asc or desc.
Limits the number of results.
Skips the first N results.
Executes the query and returns matching documents.
Updates all documents matching the current filters. Merges the given fields into each matching document (preserving id and other existing fields). Returns the number of documents updated.
Requires at least one filter — empty filters are rejected to prevent accidental mass updates.
$affected = $collection->where('status', '=', 'inactive')
->update(['status' => 'archived']);
// $affected = 15
$affected = $collection->where('role', '=', 'guest')
->where('last_login', '<', '2025-01-01')
->update(['role' => 'inactive']);Deletes all documents matching the current filters. Returns the number of documents deleted.
Requires at least one filter — empty filters are rejected to prevent accidental mass deletes.
$deleted = $collection->where('age', '<', 18)->delete();
// $deleted = 3Returns the count of matching documents.
Base exception for all database errors (validation failures, missing documents, etc.).
Thrown when the FFI bridge fails to load (missing .so, FFI disabled, unsupported platform).