Cloesce - v0.0.5-unstable.27
    Preparing search index...

    Class Orm

    Index

    Methods

    • Fetches a model by its primary key ID or key parameters.

      • If the model does not have a primary key, key parameters must be provided.
      • If the model has a primary key, the ID must be provided.

      Type Parameters

      • T extends object

      Parameters

      • ctor: new () => T

        Constructor of the model to retrieve

      • args: {
            id?: any;
            includeTree?: IncludeTree<T> | null;
            keyParams?: Record<string, string>;
        } = ...

        Arguments for retrieval

      Returns Promise<T | null>

      The retrieved model instance, or null if not found

    • Given a base object representing a Model, hydrates its D1, R2 and KV properties. Fetches all KV and R2 data concurrently.

      Type Parameters

      • T extends object

      Parameters

      • ctor: new () => T

        Constructor of the model to hydrate

      • args: {
            base?: any;
            includeTree?: IncludeTree<T> | null;
            keyParams?: Record<string, string>;
        } = ...

        Arguments for hydration

      Returns Promise<T>

      The hydrated model instance

    • Lists all instances of a given Model from D1.

      Type Parameters

      • T extends object

      Parameters

      • ctor: new () => T

        Constructor of the model to list

      • includeTree: IncludeTree<T> | null = null

        Include tree specifying which navigation properties to include

      Returns Promise<T[]>

      Array of listed model instances

    • Given a new Model object, performs an upsert operation for D1 and KV.

      Concurrently performs all D1 and KV operations.

      Some KV results depend on a successful D1 upsert to resolve their keys, and will be uploaded only after the D1 upsert completes.

      If a Model is missing a primary key, and that primary key is of Integer type, it will be auto-incremented by D1. Else, upsert will fail if the primary key is missing.

      Type Parameters

      • T extends object

      Parameters

      • ctor: new () => T

        Constructor of the model to upsert

      • newModel: DeepPartial<T>

        The new model object to upsert

      • includeTree: IncludeTree<T> | null = null

        Include tree specifying which navigation properties to include

      Returns Promise<T | null>

      The upserted model instance, or null if upsert failed

    • Creates an instance of an Orm

      Parameters

      • env: unknown

        The Wrangler environment containing Cloudflare bindings.

      Returns Orm

    • Maps D1 results into model instances. Capable of mapping a flat result set (ie, SELECT * FROM Model) or a joined result granted it is aliased as select_model would produce.

      Does not hydrate into an instance of the model; for that, use hydrate after mapping.

      Type Parameters

      • T extends object

      Parameters

      • ctor: new () => T

        Constructor of the model to map to

      • d1Results: D1Result

        Results from a D1 query

      • includeTree: IncludeTree<T> | null = null

        Include tree specifying which navigation properties to include

      Returns T[]

      Array of mapped model instances

      const d1Result = await db.prepare("SELECT * FROM User").all();
      const users: User[] = Orm.map(User, d1Result.results);
      const d1Result = await db.prepare(`
      ${Orm.select(User, null, { posts: {} })}
      WHERE User.id = ?
      `).bind(1).all();

      const users: User[] = Orm.map(User, d1Result.results, { posts: {} });
    • Generates a SELECT query string for a given Model, retrieving the model and its relations aliased as JSON.

      Type Parameters

      • T extends object

      Parameters

      • ctor: new () => T

        Constructor of the model to select

      • args: { from?: string | null; includeTree?: IncludeTree<T> | null } = ...

        Arguments specifying which relations/fields to select

      Returns string

      The generated SELECT query string

      Orm.select(Boss, Boss.withAll);

      // Example result:
      const result = `
      SELECT
      "Boss"."id" AS "id",
      "Person_1"."id" AS "persons.id",
      "Person_1"."bossId" AS "persons.bossId",
      "Dog_2"."id" AS "persons.dogs.id",
      "Dog_2"."personId" AS "persons.dogs.personId",
      "Cat_3"."id" AS "persons.cats.id",
      "Cat_3"."personId" AS "persons.cats.personId"
      FROM "Boss"
      LEFT JOIN "Person" AS "Person_1"
      ON "Boss"."id" = "Person_1"."bossId"
      LEFT JOIN "Dog" AS "Dog_2"
      ON "Person_1"."id" = "Dog_2"."personId"
      LEFT JOIN "Cat" AS "Cat_3"
      ON "Person_1"."id" = "Cat_3"."personId"
      `;