Libraries
    Preparing search index...

    Function createQuery

    • Bind a Knex instance once and get back a query(schema) function that doesn't require repeating the knex argument on every call.

      Parameters

      • knexInstance: Knex

      Returns BoundQuery

      A bound query factory: (schema, baseQuery?) => SchemaQueryBuilder.

      import Knex from 'knex';
      import { createQuery } from '@cleverbrush/knex-schema';

      const knex = Knex({ client: 'pg', connection: process.env.DB_URL });
      const query = createQuery(knex);

      // No knex argument needed from here on
      const users = await query(UserSchema).where(t => t.role, '=', 'admin');
      const post = await query(PostSchema).where(t => t.id, '=', 42).first();

      // Optional base query (e.g. soft-delete scope applied globally)
      const active = query(UserSchema, knex('users').where('deleted_at', null));