Libraries
    Preparing search index...
    • Execute a raw SQL query or Knex query builder and map the result rows through the schema's column→property name mapping.

      This is the escape hatch for complex queries that can't be expressed with the typed SchemaQueryBuilder API. The schema is used only for result mapping — column names in the result are converted back to property names. Extra columns (not in the schema) are passed through unchanged.

      Type Parameters

      • TSchema extends ObjectSchemaBuilder<any, any, any, any, any, any, any>

      Parameters

      • knex: Knex

        A configured Knex instance.

      • schema: TSchema

        The ObjectSchemaBuilder for result mapping.

      • queryOrSql: string | QueryBuilder<any, any>

        A raw SQL string or a Knex.QueryBuilder.

      • Optionalbindings: any[]

        Optional bindings for parameterised SQL queries.

      Returns Promise<(InferType<TSchema> & Record<string, any>)[]>

      Mapped result rows.

      // Raw SQL with schema result mapping
      const results = await rawQuery(knex, PostSchema, `
      SELECT p.*, COUNT(c.id) AS comment_count
      FROM posts p
      LEFT JOIN comments c ON c.post_id = p.id
      GROUP BY p.id
      ORDER BY comment_count DESC
      LIMIT ?
      `, [10]);

      // Knex query builder as the source
      const subQuery = knex('posts').select('author_id', knex.raw('COUNT(*) as post_count')).groupBy('author_id');
      const results = await rawQuery(knex, UserSchema, subQuery);