Engine is the main entry point for schema changes, query builders, raw SQL, and transactions.
import Engine from '@stackpress/inquire/Engine';Most applications will get an engine from a connection package:
import connect from '@stackpress/inquire-sqlite3';
const engine = connect(resource);| Property | Type | Description |
|---|---|---|
connection |
Connection<R> |
The wrapped connection implementation. |
dialect |
Dialect |
The dialect exposed by the connection. |
before |
(request: QueryObject) => Promise<R[] | void> |
Hook that runs before engine.query() calls the connection. |
Create an Alter builder.
const alter = engine.alter('users');Create a Create builder.
const create = engine.create('users');Create a Delete builder.
const remove = engine.delete('users');Create an Insert builder.
const insert = engine.insert('users');Create a Select builder.
const select = engine.select(['id', 'email']);Create an Update builder.
const update = engine.update('users');Compare two Create builders and return an Alter builder.
const alter = engine.diff(fromSchema, toSchema);
const statements = alter.query();Use this when your source of truth is a pair of schema definitions.
Drop a table.
await engine.drop('temporary_imports');Rename a table.
await engine.rename('draft_posts', 'posts');Remove all rows from a table.
await engine.truncate('logs');
await engine.truncate('events', true);Support for cascade depends on the active dialect.
Execute a raw SQL string or a QueryObject.
const rows = await engine.query<{ id: number }>(
'SELECT id FROM users WHERE id = ?',
[1]
);Execute a template-string query.
const rows = await engine.sql<{ id: number }>`
SELECT id FROM users WHERE id = ${1}
`;Backticks inside the SQL string are rewritten to the dialect quote character.
Run work inside the connection wrapper's transaction implementation.
await engine.transaction(async (tx) => {
await tx.query({ query: 'INSERT INTO users (email) VALUES (?)', values: ['ada@example.com'] });
});