Server - Technical Reference - v26.4.1
    Preparing search index...

    Function createPrepareStatement

    • Creates a type-safe Drizzle prepared statement for optimized, reusable database queries.

      Prepared statements are pre-compiled by the database, making them faster for repeated execution. This function dynamically builds the selection and the WHERE clause placeholders based on the provided filters map.

      Type Parameters

      • TTable extends PgTable<any>

        The Drizzle table schema type.

      • TSelectKeys extends string

        The string keys selected from the table.

      • const TFilters extends Partial<
            Record<
                keyof TTable["_"]["columns"] & string,
                | "eq"
                | "ne"
                | "gt"
                | "gte"
                | "lt"
                | "lte"
                | "like"
                | "ilike"
                | "notLike"
                | "notIlike"
                | "isNull"
                | "isNotNull"
                | "inArray"
                | "notInArray"
                | "between"
                | "notBetween",
            >,
        >

        A constant mapping of column names to Drizzle operators (e.g., 'eq', 'ilike').

      Parameters

      • table: TTable

        The Drizzle table object (e.g., users).

      • selects: TSelectKeys[]

        An array of column names to retrieve.

      • filters: TFilters

        An object mapping column names to operators. This defines the required parameters for .execute().

      • prepare_key: string

        A unique string identifier for this prepared statement in the Drizzle cache.

      • filterConditionType: FilterConditionType = "AND"

        Whether to combine filters with AND or OR. Defaults to "AND".

      Returns PgAsyncSelectPrepare<
          PgAsyncSelectBase<
              any,
              Assume<
                  Assume<
                      { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> } extends undefined
                          ? any
                          : { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> },
                      ColumnsSelection,
                  >,
                  ColumnsSelection,
              >,
              SelectMode & (
                  SelectMode & ({ [K in TSelectKeys]: PgColumn<any, PgColumnBaseConfig<any>, {}>; } extends undefined ? "single" : "partial")
              ),
              {}
              | Record<any, "not-null">,
              false,
              "where",
              SelectResult<
                  { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> } extends undefined
                      ? any
                      : { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> },
                  { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> } extends undefined
                      ? "single"
                      : "partial",
                  {} | Record<any, "not-null">,
              >[],
              Assume<
                  Assume<
                      BuildSubquerySelection<
                          { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> } extends undefined
                              ? any
                              : { [K in string]: PgColumn<any, PgColumnBaseConfig<any>, {}> },
                          {} | Record<any, "not-null">,
                      >,
                      ColumnsSelection,
                  >,
                  ColumnsSelection,
              >,
          >,
      > & {
          execute: (
              params: ExecuteParams<
                  {
                      [K in string]: {
                          [Key in string]: (...)[(...)]["_"]["notNull"] extends true
                              ? (...)[(...)]["_"]["data"]
                              : (...)[(...)]["data"] | null
                      }[K]
                  },
                  TFilters,
              >,
          ) => Promise<
              Pick<
                  {
                      [K in string]: {
                          [Key in string]: (...)[(...)] extends true
                              ? (...)[(...)]
                              : (...) | (...)
                      }[K]
                  },
                  TSelectKeys,
              >[],
          >;
      }

      A Drizzle Prepared Query object with an enhanced execute method that is strictly typed to the filters and selection provided.

      // 1. Create the prepared statement once (e.g., at the module level or in a constructor)
      const findUserByEmail = createPrepareStatement(
      users,
      ["id", "firstName", "email"],
      {
      email: "eq",
      status: "eq"
      },
      "find_user_by_email_and_status"
      );

      // 2. Execute it later with specific parameters
      // The 'params' argument is typed to { email: string; status: string }
      const results = await executePreparedStatement(findUserByEmail, {
      email: "john@doe.com",
      status: "active"
      });