Documentation
    Preparing search index...

    Class SQLiteTransaction<TResultType, TRunResult, TFullSchema, TSchema>Abstract

    Type Parameters

    • TResultType extends "sync" | "async"
    • TRunResult
    • TFullSchema extends Record<string, unknown>
    • TSchema extends TablesRelationalConfig

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _: {
        fullSchema: TFullSchema;
        schema: TSchema | undefined;
        tableNamesMap: Record<string, string>;
    }
    $cache: { invalidate: (params: MutationOption) => Promise<void> }

    Type Declaration

    • invalidate: (params: MutationOption) => Promise<void>

    Creates a subquery that defines a temporary named result set as a CTE.

    It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.

    See docs: https://orm.drizzle.team/docs/select#with-clause

    The alias for the subquery.

    Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.

    // Create a subquery with alias 'sq' and use it in the select query
    const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));

    const result = await db.with(sq).select().from(sq);

    To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:

    // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
    const sq = db.$with('sq').as(db.select({
    name: sql<string>`upper(${users.name})`.as('name'),
    })
    .from(users));

    const result = await db.with(sq).select({ name: sq.name }).from(sq);
    nestedIndex: number
    query: TFullSchema extends Record<string, never>
        ? DrizzleTypeError<
            "Seems like the schema generic is missing - did you forget to add it to your DB type?",
        >
        : {
            [K in string
            | number
            | symbol]: RelationalQueryBuilder<
                TResultType,
                TFullSchema,
                TSchema,
                TSchema[K],
            >
        }
    schema:
        | {
            fullSchema: Record<string, unknown>;
            schema: TSchema;
            tableNamesMap: Record<string, string>;
        }
        | undefined
    "[entityKind]": string

    Methods

    • Type Parameters

      • T = unknown

      Parameters

      • query: string | SQLWrapper

      Returns DBResult<TResultType, T[]>

    • Type Parameters

      • T = unknown

      Parameters

      • query: string | SQLWrapper

      Returns DBResult<TResultType, T>

    • Returns never

    • Creates a select query.

      Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.

      Use .from() method to specify which table to select from.

      See docs: https://orm.drizzle.team/docs/select

      Returns SQLiteSelectBuilder<undefined, TResultType, TRunResult>

      // Select all columns and all rows from the 'cars' table
      const allCars: Car[] = await db.select().from(cars);

      // Select specific columns and all rows from the 'cars' table
      const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({
      id: cars.id,
      brand: cars.brand
      })
      .from(cars);

      Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:

      // Select specific columns along with expression and all rows from the 'cars' table
      const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({
      id: cars.id,
      lowerBrand: sql<string>`lower(${cars.brand})`,
      })
      .from(cars);
    • Creates a select query.

      Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.

      Use .from() method to specify which table to select from.

      See docs: https://orm.drizzle.team/docs/select

      Type Parameters

      Parameters

      Returns SQLiteSelectBuilder<TSelection, TResultType, TRunResult>

      // Select all columns and all rows from the 'cars' table
      const allCars: Car[] = await db.select().from(cars);

      // Select specific columns and all rows from the 'cars' table
      const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({
      id: cars.id,
      brand: cars.brand
      })
      .from(cars);

      Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:

      // Select specific columns along with expression and all rows from the 'cars' table
      const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({
      id: cars.id,
      lowerBrand: sql<string>`lower(${cars.brand})`,
      })
      .from(cars);
    • Adds distinct expression to the select query.

      Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.

      Use .from() method to specify which table to select from.

      See docs: https://orm.drizzle.team/docs/select#distinct

      Returns SQLiteSelectBuilder<undefined, TResultType, TRunResult>

      // Select all unique rows from the 'cars' table
      await db.selectDistinct()
      .from(cars)
      .orderBy(cars.id, cars.brand, cars.color);

      // Select all unique brands from the 'cars' table
      await db.selectDistinct({ brand: cars.brand })
      .from(cars)
      .orderBy(cars.brand);
    • Adds distinct expression to the select query.

      Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.

      Use .from() method to specify which table to select from.

      See docs: https://orm.drizzle.team/docs/select#distinct

      Type Parameters

      Parameters

      Returns SQLiteSelectBuilder<TSelection, TResultType, TRunResult>

      // Select all unique rows from the 'cars' table
      await db.selectDistinct()
      .from(cars)
      .orderBy(cars.id, cars.brand, cars.color);

      // Select all unique brands from the 'cars' table
      await db.selectDistinct({ brand: cars.brand })
      .from(cars)
      .orderBy(cars.brand);
    • Creates an update query.

      Calling this method without .where() clause will update all rows in a table. The .where() clause specifies which rows should be updated.

      Use .set() method to specify which values to update.

      See docs: https://orm.drizzle.team/docs/update

      Type Parameters

      Parameters

      • table: TTable

        The table to update.

      Returns SQLiteUpdateBuilder<TTable, TResultType, TRunResult>

      // Update all rows in the 'cars' table
      await db.update(cars).set({ color: 'red' });

      // Update rows with filters and conditions
      await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));

      // Update with returning clause
      const updatedCar: Car[] = await db.update(cars)
      .set({ color: 'red' })
      .where(eq(cars.id, 1))
      .returning();
    • Type Parameters

      • T extends unknown[] = unknown[]

      Parameters

      • query: string | SQLWrapper

      Returns DBResult<TResultType, T[]>