Lab 10: Database Patterns
Overview
Step 1: Setup Knex with SQLite
cd /tmp && npm init -y --quiet
npm install knex better-sqlite3const knex = require('knex')({
client: 'better-sqlite3',
connection: { filename: ':memory:' }, // In-memory for testing
useNullAsDefault: true,
pool: {
min: 1,
max: 5,
afterCreate: (conn, done) => {
// Enable WAL mode for better concurrent reads
conn.pragma('journal_mode = WAL');
conn.pragma('foreign_keys = ON');
done(null, conn);
}
},
debug: false
});
module.exports = knex;Step 2: Migrations
Step 3: Seeds
Step 4: Query Builder Patterns
Step 5: Transactions
Step 6: Repository Pattern
Step 7: Connection Pooling
Step 8: Capstone — Full Demo
Summary
Pattern
Knex API
Use Case
Last updated
