Cosmoose
API Reference

Cosmoose

API reference for the Cosmoose connection class

Class: Cosmoose

The main entry point for connecting to Azure Cosmos DB and registering models. Extends EventEmitter.

index.ts
import { Cosmoose } from '@cosmoose/core';

Constructor

Signature
new Cosmoose(options: CosmooseOptions)

CosmooseOptions

PropertyTypeDescription
endpointstringCosmos DB account endpoint URL
keystringPrimary or secondary access key
databaseNamestringDatabase name (created if it doesn't exist)

Methods

connect()

Signature
async connect(): Promise<void>

Connects to the database and creates it if it doesn't exist.

Safe to call multiple times — subsequent calls are no-ops.


model(name, schema)

Signature
model<T>(name: string, schema: Schema<T>): Model<T>

Registers a model for the given container name. Must be called after connect().

ParameterTypeDescription
namestringContainer name in Cosmos DB
schemaSchema<T>Schema defining document shape

Returns: Model<T>

Throws: Error if not connected or if model name is already registered.


syncContainers()

Signature
async syncContainers(): Promise<SyncReport>

Syncs all registered model containers. Creates missing containers, updates mutable properties, and reports drift for immutable properties.

Returns: SyncReport (array of ContainerSyncResult)


syncContainer(name)

Signature
async syncContainer(name: string): Promise<ContainerSyncResult>

Syncs a single container by name.


isConnected()

Signature
isConnected(): boolean

Returns true if connected to the database.


getDatabase()

Signature
getDatabase(): Database | undefined

Returns the underlying @azure/cosmos Database instance, or undefined if not connected.


getRegisteredModels()

Signature
getRegisteredModels(): Map<string, RegisteredModel>

Returns all registered models.


Events

EventPayloadWhen
initiateBefore connection starts
initiateddatabaseName: stringAfter successful connection

On this page