code-docs/plugins/connections/knex.md
SQL database connection for Lowdefy using Knex.js. Supports PostgreSQL, MySQL, SQLite, and more.
| Type | Purpose |
|---|---|
Knex | Connect to SQL databases |
connections:
- id: postgres
type: Knex
properties:
client: pg
connection:
host:
_secret: PG_HOST
port: 5432
database: myapp
user:
_secret: PG_USER
password:
_secret: PG_PASSWORD
connections:
- id: mysql
type: Knex
properties:
client: mysql2
connection:
host: localhost
database: myapp
user:
_secret: MYSQL_USER
password:
_secret: MYSQL_PASSWORD
connections:
- id: db
type: Knex
properties:
client: pg
connection:
_secret: DATABASE_URL
| Type | Purpose |
|---|---|
KnexRaw | Raw SQL query |
KnexBuilder | Knex query builder |
Execute raw SQL:
requests:
- id: getUsers
type: KnexRaw
connectionId: postgres
properties:
query: |
SELECT * FROM users
WHERE active = ?
ORDER BY created_at DESC
LIMIT ?
bindings:
- true
- 100
Use Knex query builder:
requests:
- id: getUsers
type: KnexBuilder
connectionId: postgres
properties:
query:
- select:
- id
- name
- email
- from: users
- where:
active: true
- orderBy:
- column: created_at
order: desc
- limit: 100
properties:
query:
- select: '*'
- from: users
properties:
query:
- select: '*'
- from: users
- where:
department:
_state: departmentId
- andWhere:
active: true
properties:
query:
- insert:
name:
_state: name
email:
_state: email
- into: users
- returning: '*'
properties:
query:
- update:
name:
_state: name
- table: users
- where:
id:
_state: userId
- returning: '*'
properties:
query:
- delete: null
- from: users
- where:
id:
_state: userId
properties:
query:
- select:
- 'users.*'
- 'departments.name as department_name'
- from: users
- leftJoin:
- departments
- 'users.department_id'
- 'departments.id'
Prevent SQL injection with bindings:
properties:
query: |
SELECT * FROM users
WHERE name LIKE ?
AND department = ?
bindings:
_array:
- _string:
- '%'
- _state: search
- '%'
- _state: department
Knex manages connection pools automatically. Configure pool size in connection properties.
Always use bindings for user input. The query builder automatically parameterizes values.