Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Schema<T>

The Schema describes a collection in Cosmos DB. It's a fluent-style builder where you define all the fields, which are passed into the Model. For a fully-type TypeScript consumer, you could provide:

import { createSchema, asType } from 'cosmonaut';

const schema = createSchema('users')
  // note that IDs are defined implicity:
  .partitionKey('/id')
  // the "asType" helper provides type information to the type system.
  .field('username', asType<string>())
  .field('favoriteColors', asType<string[]>())
  .field('favoriteCities', asType<{ name: string; country: string }[]>())
  .field('address', asType<{ street: string; postal: number }>())

Type parameters

  • T = { id: string }

Hierarchy

  • BasicSchema<T>
    • Schema

Index

Constructors

constructor

  • new Schema<T>(schemaMap: SchemaMap<T>, definition: ContainerRequest & { partitionKey?: PartitionKeyDefinition }): Schema<T>
  • Type parameters

    • T = { id: string }

    Parameters

    • schemaMap: SchemaMap<T>
    • definition: ContainerRequest & { partitionKey?: PartitionKeyDefinition }

    Returns Schema<T>

Properties

Readonly definition

definition: ContainerRequest & { partitionKey?: PartitionKeyDefinition }

Readonly schemaMap

schemaMap: SchemaMap<T>

Accessors

id

  • get id(): string
  • Gets the ID of the Cosmos DB container.

    Returns string

jsonSchema

  • get jsonSchema(): JSONSchema7
  • Gets a JSON schema representing the configured Cosmos schema.

    Returns JSONSchema7

Methods

addToIndex

  • addToIndex(path: CosmosIndexPath<T>, ...indexes: Index[]): Schema<T>

enableTtlWithoutDefault

  • enableTtlWithoutDefault(): Schema<T>
  • Enables the TTL without setting a default TTL, equivalent to .ttl(-1). But I have to look that up literally every time, so this is more friendly.

    Returns Schema<T>

field

  • field<K, TField>(name: K, asType: AsType<OptionalType<TField>>, fieldConfig?: ISchemaField<TField>): Schema<T & {[ K_ in string]: TField }>
  • field<K, TField>(name: K, asType: AsType<TField>, fieldConfig?: ISchemaField<TField>): Schema<T & {[ K_ in string]: TField }>
  • field<K>(name: K, fieldConfig?: ISchemaField<unknown>): Schema<T & {[ K_ in string]: unknown }>
  • Adds a new field to the schema.

    Type parameters

    • K: string

    • TField

    Parameters

    • name: K

      the name of the field

    • asType: AsType<OptionalType<TField>>
    • Optional fieldConfig: ISchemaField<TField>

      optional configuration for the field

    Returns Schema<T & {[ K_ in string]: TField }>

    the modified schema

  • Adds a new field to the schema.

    Type parameters

    • K: string

    • TField

    Parameters

    • name: K

      the name of the field

    • asType: AsType<TField>
    • Optional fieldConfig: ISchemaField<TField>

      optional configuration for the field

    Returns Schema<T & {[ K_ in string]: TField }>

    the modified schema

  • Adds a new field to the schema.

    Type parameters

    • K: string

    Parameters

    • name: K

      the name of the field

    • Optional fieldConfig: ISchemaField<unknown>

      optional configuration for the field

    Returns Schema<T & {[ K_ in string]: unknown }>

    the modified schema

partitionKey

  • partitionKey(path: CosmosSimplePathImpl<Required<T>, keyof T>, partitionKeyCanBeLong?: boolean): Schema<T>

removeAllFromIndex

  • removeAllFromIndex(): Schema<T>
  • Removes all paths from indexing, unless explicitly included. This reduces write charges and would be a sensible behavior for most use cases. Note that unless this is called, all fields are indexed.

    Returns Schema<T>

removeFromIndex

  • removeFromIndex(...paths: CosmosIndexPath<T>[]): Schema<T>

setConflictResolution

  • setConflictResolution(policy: ConflictResolutionPolicy & { conflictResolutionPath?: CosmosSimplePathImpl<Required<T>, keyof T> }): Schema<T>

setGeospatialConfig

  • setGeospatialConfig(type: GeospatialType): Schema<T>

setThroughput

  • setThroughput(throughput: number | Pick<ContainerRequest, "throughput" | "maxThroughput" | "autoUpgradePolicy">): Schema<T>

ttl

  • ttl(duration: undefined | number): Schema<T>
  • Updates the default TTL in seconds for the container.

    • If undefined, items in the container are not expired (default)
    • If set to a positive value, the items expire after the given time
    • If set to -1, items will expire but won't have a default ttl.

    Parameters

    • duration: undefined | number

    Returns Schema<T>

unique

  • unique(...paths: CosmosSimplePathImpl<Required<T>, keyof T>[]): Schema<T>

Generated using TypeDoc