Libraries
    Preparing search index...

    Bound query function returned by createQuery.

    interface BoundQuery {
        transaction<T>(callback: (db: BoundQuery) => Promise<T>): Promise<T>;
        withTransaction(trx: Transaction): BoundQuery;
        <
            TLocalSchema extends
                ObjectSchemaBuilder<any, any, any, any, any, any, any>,
        >(
            schema: TLocalSchema,
        ): SchemaQueryBuilder<TLocalSchema, QueryResultType<TLocalSchema>>;
        <
            TLocalSchema extends
                ObjectSchemaBuilder<any, any, any, any, any, any, any>,
        >(
            schema: TLocalSchema,
            baseQuery: QueryBuilder,
        ): SchemaQueryBuilder<TLocalSchema, QueryResultType<TLocalSchema>>;
    }
    Index

    Methods

    • Start a Knex transaction and run callback inside it, passing a transaction-bound BoundQuery factory as the argument. The transaction is committed when the callback resolves and rolled back if it rejects.

      This is the callback-style counterpart to withTransaction — you don't need to obtain a Knex.Transaction object yourself.

      Type Parameters

      • T

      Parameters

      • callback: (db: BoundQuery) => Promise<T>

        An async function that receives a transaction-bound BoundQuery and returns a value. The returned value is forwarded as the resolved value of the outer Promise.

      Returns Promise<T>

      A Promise that resolves with the value returned by callback.

      const db = createQuery(knex);

      const user = await db.transaction(async dbTrx => {
      const newUser = await dbTrx(UserSchema).insert({ name: 'Alice' });
      await dbTrx(PostSchema).insert({ authorId: newUser.id, title: 'Hello' });
      return newUser;
      });
    • Return a version of this bound factory whose queries all run within the given Knex transaction. Equivalent to calling .transacting(trx) on each individual builder, but more convenient when every query in a block must share the same transaction.

      Parameters

      • trx: Transaction

      Returns BoundQuery

      const db = createQuery(knex);

      await knex.transaction(async trx => {
      const dbTrx = db.withTransaction(trx);
      const user = await dbTrx(UserSchema).insert({ name: 'Alice' });
      await dbTrx(PostSchema).insert({ authorId: user.id, title: 'Hello' });
      });