Cosmoose
API Reference

Types

API reference for Type enum, Document, PatchExpression, and other types

Enum: Type

Field type identifiers used in schema definitions.

index.ts
import { Type } from '@cosmoose/core';
ValueDescription
Type.STRINGString values
Type.NUMBERNumeric values
Type.BOOLEANBoolean values
Type.DATEDate values (stored as ISO strings)
Type.EMAILEmail with auto trim, lowercase, and validation
Type.OBJECTNested object (requires schema property)
Type.ARRAYArray (requires items property)
Type.MAPKey-value map (requires of property)
Type.ANYAny value (no validation)

Type: Document<T>

Represents a document returned from Cosmos DB. Extends T with auto-added fields.

types.ts
type Document<T> = T & {
  id: string;
  _etag?: string;
  _ts?: number;
  _rid?: string;
  _self?: string;
  _attachments?: string;
};

When timestamps are enabled on the schema, documents also include:

  • createdAt?: Date
  • updatedAt?: Date

Interface: PatchExpression<T>

Operators for partial document updates via patchById().

types.ts
interface PatchExpression<T> {
  $set?: Partial<T>;
  $add?: Partial<Record<keyof T, unknown>>;
  $incr?: Partial<Record<keyof T, number>>;
  $unset?: Partial<Record<keyof T, true>>;
}
OperatorDescription
$setSet fields to new values
$addAdd values to fields
$incrIncrement numeric fields (use negative values to decrement)
$unsetRemove fields from the document

Type: SchemaDefinition<T>

Maps field names to their descriptors.

types.ts
type SchemaDefinition<T> = {
  [K in keyof T]: FieldDescriptor;
};

Type: FieldDescriptor

Union of all field descriptor types. See Schema API for details on each variant.


Interface: ContainerConfig

Container-level configuration used by schema options and syncContainers().

types.ts
interface ContainerConfig {
  partitionKey?: PartitionKeyDefinition;
  uniqueKeys?: string[][];
  compositeIndexes?: CompositeIndexEntry[];
  ttl?: number;
}

Type: PartitionKeyDefinition

types.ts
type PartitionKeyDefinition =
  | string                                    // simple: '/email'
  | { paths: string[]; kind?: 'Hash' }        // hash
  | { paths: string[]; kind: 'MultiHash' };   // hierarchical

Interface: CompositeIndexEntry

types.ts
interface CompositeIndexEntry {
  [path: string]: 1 | -1;  // 1 = ascending, -1 = descending
}

Migration Types

ContainerSyncResult

types.ts
interface ContainerSyncResult {
  name: string;
  status: ContainerSyncStatus;
  driftDetails?: DriftDetail[];
  updatedProperties?: string[];
}

ContainerSyncStatus

types.ts
type ContainerSyncStatus = 'created' | 'updated' | 'drift' | 'unchanged';

DriftDetail

types.ts
interface DriftDetail {
  property: string;
  expected: unknown;
  actual: unknown;
  mutable: boolean;
  reason?: string;
}

SyncReport

types.ts
type SyncReport = ContainerSyncResult[];

Exceptions

SchemaValidationFailedException

Thrown when document data fails schema validation. Contains the underlying Zod validation error.

InvalidIndexKeyException

Thrown when an invalid index key is specified in the container config.

InvalidUniqueKeyException

Thrown when an invalid unique key is specified in the container config.


Utility Functions

generateId()

Signature
function generateId(): string

Generates a UUID v7 string (no hyphens). Used automatically by Model.create() but can be called directly:

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

const id = generateId(); // e.g. '019756a0b5947d3a8e4c1f2b3d4e5f6a'

On this page