Quick Start Guide
This guide walks you through setting up a complete type-safe app stack incorporating authentication and database transaction connections in under five minutes.
1. Initialize your Application Stack
Create an entrypoint file src/lib/forgekit.ts to initialize your modular application layer using @forgekit/core:
import { ForgeApp } from '@forgekit/core';
import { db } from '@forgekit/db';
import { auth } from '@forgekit/auth';
const app = new ForgeApp();
// Bind database transaction connection pools
app.use(
db({
url: process.env.DATABASE_URL,
poolSize: 10
})
);
// Bind JWT/OAuth session identity
app.use(
auth({
session: { maxAge: '7d' },
providers: ['github', 'google']
})
);
export default app; 2. Execute Queries and Transactions
To run database queries inside your route endpoints (e.g. SvelteKit +page.server.ts), import your initialized connection pool instance:
import app from '$lib/forgekit';
export const load = async () => {
// Database transactions are checked at compile time
const activeUsers = await app.db.transaction(async (tx) => {
return await tx.select('id', 'email').from('users').where('active', '=', true).limit(5);
});
return { activeUsers };
}; 3. Verify Session Identity in Middleware
Intercept incoming requests to secure your server API endpoint routes using @forgekit/auth sessions:
import app from '$lib/forgekit';
export const POST = async ({ request }) => {
const session = await app.auth.getSession(request);
if (!session) {
return new Response('Unauthorized Access Restricted', { status: 401 });
}
// The session user object is strictly typed
const userEmail = session.user.email;
return new Response(
JSON.stringify({
message: `Hello ${userEmail}, authorization granted.`
})
);
};