Database
API reference for the Database class
Constructor
new Database(config?: DatabaseConfig)
Properties
db.name: string
db.isConnected: boolean
db.adapterName: string
Methods
connect()
connect(): Promise<void>
Initializes the storage adapter. Idempotent — safe to call multiple times.
disconnect()
disconnect(): Promise<void>
Closes the adapter and clears collection cache. Idempotent.
collection()
collection<T>(name: string): Collection<T>
Returns a cached or new collection for the given name. Throws ConnectionError
if not connected.
transaction()
transaction<T>(fn: (ctx: TransactionContext) => Promise<T>): Promise<T>
Runs fn inside an adapter-level transaction. Rolled back on throw.
on()
on(callback: (event: ChangeEvent) => void): () => void
Subscribes to all change events. Returns an unsubscribe function.
Internal methods
db._getSchema(): Schema | null
db._getAdapter(): StorageAdapter
Underscore-prefixed methods exist for plugin use. Not part of the public API contract.
DatabaseConfig
interface DatabaseConfig {
name?: string
adapter?: "indexeddb" | "memory" | StorageAdapter
schema?: SchemaConfig
plugins?: CtroDBPlugin[]
logLevel?: "debug" | "info" | "warn" | "error" | "silent"
}How is this guide?
Last updated on Jun 20, 2026