A configured Knex instance.
The ObjectSchemaBuilder for result mapping.
A raw SQL string or a Knex.QueryBuilder.
Optionalbindings: any[]Optional bindings for parameterised SQL queries.
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);
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
SchemaQueryBuilderAPI. 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.