Options
All
  • Public
  • Public/Protected
  • All
Menu

cosmonaut-odm

Index

Type aliases

InterfaceForModel

InterfaceForModel<T>: T extends BaseModel<infer V> ? V : never

Helper type to get the data interface for a Model.

Type parameters

  • T

InterfaceForSchema

InterfaceForSchema<T>: T extends BasicSchema<infer I> ? I : never

Helper that extracts the interface a given schema.

type User = InterfaceForSchema<typeof userCollection>;

const user: User = {
  username: 'Connor'
};

Type parameters

  • T

QueryIterator

QueryIterator<T>: {[ K in keyof Cosmos.QueryIterator<T>]: Cosmos.QueryIterator<T>[K] }

Type describing the Cosmos DB QueryIterator. A mapped type here is used so that we can implement the interface with our custom mapped type.

Type parameters

  • T

Thenable

Thenable<T>: PromiseLike<T> | T

Type parameters

  • T

Functions

Const Model

  • Model<T>(schema: BasicSchema<T>): typeof __class
  • Creates a class that represents the provided schema. You can use class standalone if you don't need any other methods:

    const User = Model(userSchema);
    const user = new User();
    user.username = 'connor4312';
    await user.save();
    

    ...or extend it to add custom functionality:

    class User extends Model(userSchema) {
      // e.g. overriding a lifecycle hook
      beforePersist(password: string) {
        if (this.isDirty('password')) {
          this.password = hash(password);
        }
      }
    }
    

    The returned class extends the BaseModel and has the following static properties, which TypeDoc does not represent well:

    • Model.partition(partitionKey: stirng | number) returns a Partition to run operation in a single partition.
    • Model.crossPartitionQuery() returns a Query.
    • Model.container() returns the associated Container.
    • Model.schema is the source Schema object.

    Type parameters

    • T: { id: string }

    Parameters

    • schema: BasicSchema<T>

    Returns typeof __class

asType

  • asType<T>(): AsType<T>
  • Type parameters

    • T

    Returns AsType<T>

Const connectModels

  • connectModels(connection: Database): void
  • Associates the database connection with all models.

    Parameters

    • connection: Database

    Returns void

Const createSchema

  • createSchema(containerName: string): Schema<{ id: string }>
  • Parameters

    • containerName: string

    Returns Schema<{ id: string }>

Const sql

  • sql(parts: TemplateStringsArray, ...params: JSONValue[]): SqlQuerySpec
  • Template literal tag function that produces a Cosmos DB query. For example:

    User.query(sql`SELECT users.* FROM users WHERE users.username = ${username}`)
    

    In this case, ${username} is correctly extracted to a parameter. Useful for making security auditors' hearts skip a beat.

    Parameters

    • parts: TemplateStringsArray
    • Rest ...params: JSONValue[]

    Returns SqlQuerySpec

Generated using TypeDoc