API Reference
Schema
API reference for the Schema class and SchemaOptions
Class: Schema<T>
Defines the shape, validation rules, and container configuration for documents.
import { Schema, Type } from '@cosmoose/core';Constructor
new Schema<T>(definition: SchemaDefinition<T>, options?: SchemaOptions)SchemaDefinition<T>
A record mapping each field name in T to a FieldDescriptor:
type SchemaDefinition<T> = {
[K in keyof T]: FieldDescriptor;
};SchemaOptions
| Property | Type | Default | Description |
|---|---|---|---|
timestamps | boolean | false | Auto-manage createdAt / updatedAt |
container | ContainerConfig | {} | Container-level configuration |
Methods
getDefinition()
getDefinition(): SchemaDefinition<T>Returns the field definitions.
getOptions()
getOptions(): SchemaOptionsReturns the schema options.
getContainerConfig()
getContainerConfig(): ContainerConfigReturns the container configuration (or {} if not set).
FieldDescriptor
A union type describing a single field. All descriptors share:
| Property | Type | Default | Description |
|---|---|---|---|
type | Type | — | Field type (required) |
optional | boolean | false | Whether the field is optional |
default | varies | — | Default value if not provided |
StringFieldDescriptor
{ type: Type.STRING, trim?: boolean, lowercase?: boolean, uppercase?: boolean }NumberFieldDescriptor
{ type: Type.NUMBER }BooleanFieldDescriptor
{ type: Type.BOOLEAN }DateFieldDescriptor
{ type: Type.DATE }EmailFieldDescriptor
{ type: Type.EMAIL }Auto-applies trim and lowercase. Validates email format.
ObjectFieldDescriptor
{ type: Type.OBJECT, schema: Schema<Record<string, unknown>> }ArrayFieldDescriptor
{ type: Type.ARRAY, items: FieldDescriptor }MapFieldDescriptor
{ type: Type.MAP, of: Type }AnyFieldDescriptor
{ type: Type.ANY }ContainerConfig
Configuration for the Cosmos DB container, used by syncContainers().
interface ContainerConfig {
partitionKey?: PartitionKeyDefinition;
uniqueKeys?: string[][];
compositeIndexes?: CompositeIndexEntry[];
ttl?: number;
}| Property | Type | Description |
|---|---|---|
partitionKey | string | { paths: string[]; kind?: 'Hash' | 'MultiHash' } | Partition key definition |
uniqueKeys | string[][] | Unique key constraints |
compositeIndexes | { [path]: 1 | -1 }[] | Composite index definitions |
ttl | number | Default time-to-live in seconds |
PartitionKeyDefinition
// Simple string shorthand
partitionKey: '/email'
// Hash partition key
partitionKey: { paths: ['/email'], kind: 'Hash' }
// Hierarchical partition key
partitionKey: { paths: ['/tenantId', '/userId'], kind: 'MultiHash' }CompositeIndexEntry
// Ascending name, descending createdAt
compositeIndexes: [{ name: 1, createdAt: -1 }]