Database
VentureKit provides database support through @venturekit/data with RDS configuration, Drizzle Kit migrations, query utilities, and transaction management.
1. Install the Package
Section titled “1. Install the Package”npm install @venturekit/data@dev2. Declare a Database Intent
Section titled “2. Declare a Database Intent”export default defineVenture({ base, security, envs: { dev, prod }, routesDir: 'src/routes', infrastructure: { databases: [{ id: 'main', type: 'postgres', size: 'small', name: 'mydb', backups: true, encrypted: true, }], },});3. Define Your Schema
Section titled “3. Define Your Schema”Create src/db/schema.ts using Drizzle ORM:
import { pgTable, serial, text, boolean, timestamp } from 'drizzle-orm/pg-core';
export const tasks = pgTable('tasks', { id: serial('id').primaryKey(), title: text('title').notNull(), completed: boolean('completed').default(false), createdAt: timestamp('created_at').defaultNow(),});4. Run Migrations
Section titled “4. Run Migrations”# Generate migration files from schema changesvk migrate generate
# Apply migrationsvk migrate up
# Check statusvk migrate statusQuerying
Section titled “Querying”Simple Queries
Section titled “Simple Queries”import { query, mapResults } from '@venturekit/data';
const result = await query('SELECT * FROM tasks WHERE completed = $1', [false]);
const tasks = mapResults(result, row => ({ id: row.id, title: row.title, completed: row.completed,}));Connection Pool
Section titled “Connection Pool”import { getPool } from '@venturekit/data';
const pool = getPool();const client = await pool.connect();try { const result = await client.query('SELECT NOW()'); return result.rows[0];} finally { client.release();}Transactions
Section titled “Transactions”Automatic Transactions
Section titled “Automatic Transactions”Use withTransaction for automatic commit/rollback:
import { withTransaction } from '@venturekit/data';
const result = await withTransaction(async (tx) => { await tx.query('INSERT INTO tasks (title) VALUES ($1)', ['New task']); await tx.query('UPDATE counters SET count = count + 1 WHERE name = $1', ['tasks']); return { created: true };});// Auto-committed on success, rolled back on errorTransactional Handlers
Section titled “Transactional Handlers”Enable per-request transactions in your handler:
import { handler } from '@venturekit/runtime';
export const main = handler(async (body, ctx, logger) => { // ctx.tx is a transaction — available because transactional: true await ctx.tx.query('INSERT INTO tasks (title) VALUES ($1)', [body.title]); await ctx.tx.query('INSERT INTO audit_log (action) VALUES ($1)', ['task_created']); return { created: true };}, { scopes: ['tasks.write'], transactional: true });Manual Transactions
Section titled “Manual Transactions”import { beginTransaction } from '@venturekit/data';
const tx = await beginTransaction();try { await tx.query('INSERT INTO tasks (title) VALUES ($1)', ['Task 1']); await tx.query('INSERT INTO tasks (title) VALUES ($1)', ['Task 2']); await tx.commit();} catch (error) { await tx.rollback(); throw error;}Database Sizes
Section titled “Database Sizes”| Size | Instance | Use Case |
|---|---|---|
small | db.t3.small | Development, low traffic |
medium | db.r6g.large | Moderate traffic |
large | db.r6g.xlarge | High traffic |
xlarge | db.r6g.2xlarge | Enterprise |
Migration Commands
Section titled “Migration Commands”| Command | Description |
|---|---|
vk migrate generate | Generate SQL from schema changes |
vk migrate up | Apply pending migrations |
vk migrate drop | Drop a migration file |
vk migrate status | Check applied migrations |
vk migrate push | Push schema directly (dev only) |
vk migrate studio | Open Drizzle Studio GUI |