Skip to main content
The Knex adapter provides a service interface for SQL databases using the Knex.js query builder. Supports PostgreSQL, MySQL, SQLite, MSSQL, and Oracle.

Installation

KnexService

The main service class for SQL operations via Knex.

Constructor

KnexAdapterOptions
required
Configuration options for the Knex adapter
Knex
required
The Knex instance
string
required
The table name
string
default:"id"
Name of the id field property
string
Database schema name (for PostgreSQL, MSSQL)
PaginationParams
Pagination settings with default and max page size
boolean | string[]
Allow multiple updates. Can be true, false, or an array of method names
object
boolean
Use ONLY keyword for table queries (PostgreSQL)
Record<string, string>
Custom query operators mapping. Example: { '$regexp': '~*' }

Service Methods

find

Retrieve multiple records from the table.
KnexAdapterParams
AdapterQuery
Query filters including $limit, $skip, $sort, $select, and SQL operators
PaginationOptions | false
Override pagination settings
Knex.QueryBuilder
Pre-configured Knex query builder to extend
KnexAdapterTransaction
Transaction object for transactional operations
Paginated<Result> | Result[]
Paginated results or array depending on settings

get

Retrieve a single record by id.
Id
required
The record id to retrieve
Result
The found record

create

Create one or more new records.
Data | Data[]
required
Record data to create. Arrays are processed sequentially
Result | Result[]
The created record(s)

update

Completely replace a record.
Id
required
The record id to update
Data
required
Complete record data. Fields not provided will be set to null
Result
The updated record

patch

Partially update one or multiple records.
Id | null
required
The record id to patch, or null to patch multiple records matching the query
PatchData
required
Partial data to merge with existing record(s)
Result | Result[]
The patched record(s)

remove

Remove one or multiple records.
Id | null
required
The record id to remove, or null to remove multiple
Result | Result[]
The removed record(s)

KnexAdapter

The base adapter class that KnexService extends.

Properties

Model

Access the Knex instance.

fullName

Get the full table name including schema.

Methods

db

Get a Knex query builder for the table, with transaction support.
KnexAdapterParams
Parameters with optional transaction
Knex.QueryBuilder
Query builder for the configured table

createQuery

Create a Knex query from Feathers parameters.
Knex.QueryBuilder
Configured query builder with all filters applied

knexify

Convert a Feathers query object to Knex where clauses.
Knex.QueryBuilder
required
The Knex query builder to modify
Query
required
Feathers query object

Query Syntax

Standard Operators

Logical Operators

Special Query Parameters

Extended Operators

Add custom SQL operators:

Transactions

The Knex adapter supports database transactions via hooks.

Transaction Hooks

Manual Transactions

Nested Transactions

Transaction hooks support nesting automatically:

Example