Class Model<T>

Type Parameters

  • T extends Record<string, ModelColumn>

    The type of the model, which will be returned when using methods such as First() or All()

Hierarchy

  • Model

Constructors

  • Type Parameters

    Parameters

    • options: {
          D1Orm?: D1Orm;
          autoIncrement?: Extract<keyof T, string>;
          primaryKeys: Extract<keyof T, string> | Extract<keyof T, string>[];
          tableName: string;
          uniqueKeys?: Extract<keyof T, string>[][];
          withRowId?: boolean;
      }

      The options for the model. All parameters except autoIncrement, withRowId, and uniqueKeys are required.

      • Optional D1Orm?: D1Orm

        The D1Orm instance to use - optional. If not set initially, you must use SetOrm() to set before querying.

      • Optional autoIncrement?: Extract<keyof T, string>

        The column to use for auto incrementing. If specified, only one primary key is allowed, and must be of type INTEGER.

      • primaryKeys: Extract<keyof T, string> | Extract<keyof T, string>[]

        The primary key or keys of the table.

      • tableName: string

        The name of the table to use.

      • Optional uniqueKeys?: Extract<keyof T, string>[][]

        The unique keys of the table. For example [ ['id'], ['username', 'discriminator'] ] would cause ID to be unique, as well as the combination of username and discriminator.

      • Optional withRowId?: boolean

        Whether or not D1 should generate a rowid column automatically. Defaults to false.

    • columns: T

      The columns for the model. The keys are the column names, and the values are the column options. See ModelColumn

    Returns Model<T>

Properties

columns: T
primaryKeys: Extract<keyof T, string>[]
tableName: string
uniqueKeys: Extract<keyof T, string>[][]

Accessors

  • get createTableDefinition(): string
  • Returns

    A CreateTable definition for the model, which can be used in a CREATE TABLE statement.

    Returns string

Methods

  • Throws

    • Throws an error if the table already exists and the strategy is not "force".
    • Throws an error if the strategy is "alter", as this is not yet implemented

    Parameters

    • options: {
          strategy: "default" | "force";
      } = ...

      The options for creating the table. Currently only contains strategy, which is the strategy to use when creating the table.

      • "default" - The default strategy, which will attempt create the table.
      • "force" - Drops the table if it exists, then creates it
      • strategy: "default" | "force"

    Returns Promise<D1Result<unknown>>

  • Parameters

    • Optional silent: boolean

      If true, will ignore the table not existing. If false, will throw an error if the table does not exist.

    Returns Promise<D1Result<unknown>>

  • Parameters

    • data: Partial<InferFromColumns<T>>[]

      The data to insert into the table, as an array of objects with the column names as keys and the values as values.

    • orReplace: boolean = false

    Returns Promise<D1Result<InferFromColumns<T>>[]>

  • Parameters

    • data: Partial<InferFromColumns<T>>

      The data to insert into the table, as an object with the column names as keys and the values as values.

    • orReplace: boolean = false

    Returns Promise<D1Result<InferFromColumns<T>>>

  • Upserting is a way to insert a row into the table, or update it if it already exists. This is done by using SQLITE's ON CONFLICT clause. As a result, this method should control the primary key for the insert & where clauses, and should not be used with auto incrementing keys.

    Parameters

    Returns Promise<D1Result<unknown>>

Generated using TypeDoc