← Docs

SvelteKit

SvelteKit's src/lib/server/ directory ensures server-only modules never leak to the client. Gold Lapel fits right in.

Install

npm install goldlapel pg
# or: npm install goldlapel postgres
# or: npm install goldlapel @vercel/postgres

Gold Lapel is a driver-agnostic wrapper — install whichever Postgres driver your app uses alongside it. pg is the most common; postgres (postgres.js) and @vercel/postgres also work.

Setup

Create a connection module at src/lib/server/db.js:

import * as goldlapel from 'goldlapel';
import pg from 'pg';

const gl = await goldlapel.start(process.env.DATABASE_URL);
const pool = new pg.Pool({ connectionString: gl.url });

export { gl, pool };

Load Function

Use the connection in a +page.server.js load function:

import { pool } from '$lib/server/db';

export async function load() {
  const result = await pool.query('SELECT * FROM users');
  return { users: result.rows };
}

API Endpoint

Use the connection in a +server.js endpoint:

import { pool } from '$lib/server/db';
import { json } from '@sveltejs/kit';

export async function GET() {
  const result = await pool.query('SELECT * FROM users');
  return json(result.rows);
}

ORMs

Using Prisma? See @goldlapel/prisma. Using Drizzle? See @goldlapel/drizzle.