Cosmoose
API Reference

Schema

API reference for the Schema class and SchemaOptions

Class: Schema<T>

Defines the shape, validation rules, and container configuration for documents.

index.ts
import { Schema, Type } from '@cosmoose/core';

Constructor

Signature
new Schema<T>(definition: SchemaDefinition<T>, options?: SchemaOptions)

SchemaDefinition<T>

A record mapping each field name in T to a FieldDescriptor:

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

SchemaOptions

PropertyTypeDefaultDescription
timestampsbooleanfalseAuto-manage createdAt / updatedAt
containerContainerConfig{}Container-level configuration

Methods

getDefinition()

Signature
getDefinition(): SchemaDefinition<T>

Returns the field definitions.


getOptions()

Signature
getOptions(): SchemaOptions

Returns the schema options.


getContainerConfig()

Signature
getContainerConfig(): ContainerConfig

Returns the container configuration (or {} if not set).


FieldDescriptor

A union type describing a single field. All descriptors share:

PropertyTypeDefaultDescription
typeTypeField type (required)
optionalbooleanfalseWhether the field is optional
defaultvariesDefault value if not provided

StringFieldDescriptor

types.ts
{ type: Type.STRING, trim?: boolean, lowercase?: boolean, uppercase?: boolean }

NumberFieldDescriptor

types.ts
{ type: Type.NUMBER }

BooleanFieldDescriptor

types.ts
{ type: Type.BOOLEAN }

DateFieldDescriptor

types.ts
{ type: Type.DATE }

EmailFieldDescriptor

types.ts
{ type: Type.EMAIL }

Auto-applies trim and lowercase. Validates email format.

ObjectFieldDescriptor

types.ts
{ type: Type.OBJECT, schema: Schema<Record<string, unknown>> }

ArrayFieldDescriptor

types.ts
{ type: Type.ARRAY, items: FieldDescriptor }

MapFieldDescriptor

types.ts
{ type: Type.MAP, of: Type }

AnyFieldDescriptor

types.ts
{ type: Type.ANY }

ContainerConfig

Configuration for the Cosmos DB container, used by syncContainers().

types.ts
interface ContainerConfig {
  partitionKey?: PartitionKeyDefinition;
  uniqueKeys?: string[][];
  compositeIndexes?: CompositeIndexEntry[];
  ttl?: number;
}
PropertyTypeDescription
partitionKeystring | { paths: string[]; kind?: 'Hash' | 'MultiHash' }Partition key definition
uniqueKeysstring[][]Unique key constraints
compositeIndexes{ [path]: 1 | -1 }[]Composite index definitions
ttlnumberDefault time-to-live in seconds

PartitionKeyDefinition

types.ts
// Simple string shorthand
partitionKey: '/email'

// Hash partition key
partitionKey: { paths: ['/email'], kind: 'Hash' }

// Hierarchical partition key
partitionKey: { paths: ['/tenantId', '/userId'], kind: 'MultiHash' }

CompositeIndexEntry

types.ts
// Ascending name, descending createdAt
compositeIndexes: [{ name: 1, createdAt: -1 }]

On this page