knex-schema-and-transactions
Use when creating tables, altering schema, or running atomic write flows with knex_dart driver wrappers.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when creating tables, altering schema, or running atomic write flows with knex_dart driver wrappers.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when configuring connection pools, executing raw SQL, or tapping observability streams on knex_dart driver clients.
Use when choosing between SQL-only knex_dart usage and a live driver package, or when writing the first connected query.
Use when writing INSERT, UPDATE, DELETE, or conflict-handling queries with knex_dart.
Use when instrumenting knex_dart live driver wrappers with OpenTelemetry spans, DB client duration metrics, hooks, transactions, or stream/query interceptor behavior.
Use when generating SQL with knex_dart query builders: filtering, joins, grouping, CTEs, unions, and SQL inspection.
Use when defining database migrations with knex_dart's Migrator — code-first, SQL-directory, or schema-input styles.
| name | knex-schema-and-transactions |
| description | Use when creating tables, altering schema, or running atomic write flows with knex_dart driver wrappers. |
| metadata | {"knex_dart_version":"1.2.1"} |
This skill assumes a live driver wrapper such as KnexSQLite, KnexPostgres, or KnexMySQL.
Use executeSchema((schema) { ... }) on the driver wrapper:
await db.executeSchema((schema) {
schema.createTable('users', (table) {
table.increments('id');
table.string('name').notNullable();
table.string('email').unique();
table.boolean('active').defaultTo(true);
table.timestamps();
});
});
Add follow-up schema operations in the same callback when needed:
await db.executeSchema((schema) {
schema.alterTable('users', (table) {
table.index(['email']);
});
});
Use trx(...) on the driver wrapper. Inside the callback, run everything through the transaction-scoped client:
await db.trx((trx) async {
await trx.insert(
trx.queryBuilder().table('accounts').insert({
'owner': 'Alice',
'balance': 1000,
}),
);
await trx.insert(
trx.queryBuilder().table('ledger').insert({
'action': 'deposit',
'amount': 1000,
}),
);
});
await db.trx((trx) async {
final rows = await trx.select(
trx.queryBuilder()
.table('accounts')
.select(['id', 'balance'])
.where('id', '=', 1)
.limit(1),
);
if ((rows.first['balance'] as num) < 100) {
throw Exception('Insufficient balance');
}
});
Nested trx(...) calls create savepoints on the drivers that support them:
await db.trx((outer) async {
await outer.insert(
outer.queryBuilder().table('accounts').insert({'owner': 'Alice'}),
);
try {
await outer.trx((inner) async {
await inner.insert(
inner.queryBuilder().table('accounts').insert({'owner': 'Bob'}),
);
throw Exception('rollback inner only');
});
} catch (_) {
// outer transaction is still open here
}
});
executeSchema(...) for DDL on the driver wrappertrx(...) for atomic sequences of live queriesdb.toSQL(); use driver execution helpers only when you actually need to run the queryhttps://docs.knex.mahawarkartikey.in/raw/query-building/schema-builder.mdhttps://docs.knex.mahawarkartikey.in/raw/query-building/transactions.mdhttps://docs.knex.mahawarkartikey.in/raw/migration/migrations.md