Skip to content

Database

VentureKit provides database support through @venturekit/data with RDS configuration, Drizzle Kit migrations, query utilities, and transaction management.

Terminal window
npm install @venturekit/data@dev
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,
}],
},
});

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(),
});
Terminal window
# Generate migration files from schema changes
vk migrate generate
# Apply migrations
vk migrate up
# Check status
vk migrate status
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,
}));
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();
}

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 error

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 });
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;
}
SizeInstanceUse Case
smalldb.t3.smallDevelopment, low traffic
mediumdb.r6g.largeModerate traffic
largedb.r6g.xlargeHigh traffic
xlargedb.r6g.2xlargeEnterprise
CommandDescription
vk migrate generateGenerate SQL from schema changes
vk migrate upApply pending migrations
vk migrate dropDrop a migration file
vk migrate statusCheck applied migrations
vk migrate pushPush schema directly (dev only)
vk migrate studioOpen Drizzle Studio GUI