Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
docs: updating drizzle quick start
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Oct 17, 2024
1 parent 2d8770d commit 2be5837
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 105 deletions.
Binary file removed examples/aws-drizzle/bun.lockb
Binary file not shown.
15 changes: 8 additions & 7 deletions examples/aws-drizzle/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Resource } from "sst";
import { defineConfig } from "drizzle-kit";

const cfg = Resource.MyPostgres;

export default defineConfig({
dialect: "postgresql",
// Pick up all our schema files
schema: ["./src/**/*.sql.ts"],
out: "./migrations",
dbCredentials: {
host: cfg.host,
port: cfg.port,
user: cfg.username,
password: cfg.password,
database: cfg.database,
ssl: {
rejectUnauthorized: false,
},
host: Resource.MyPostgres.host,
port: Resource.MyPostgres.port,
user: Resource.MyPostgres.username,
password: Resource.MyPostgres.password,
database: Resource.MyPostgres.database,
},
});
5 changes: 3 additions & 2 deletions examples/aws-drizzle/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "97498b16-7727-4cbb-aede-eeb61e37722d",
"id": "89cd0e0e-59a6-42ec-a876-c44671efc130",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "6",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.todo": {
Expand Down Expand Up @@ -35,6 +35,7 @@
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
Expand Down
10 changes: 5 additions & 5 deletions examples/aws-drizzle/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version": "5",
"dialect": "pg",
"version": "7",
"dialect": "postgresql",
"entries": [
{
"idx": 0,
"version": "6",
"when": 1714417253232,
"tag": "0000_fearless_sersi",
"version": "7",
"when": 1729181504948,
"tag": "0000_lethal_justin_hammer",
"breakpoints": true
}
]
Expand Down
12 changes: 6 additions & 6 deletions examples/aws-drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"db": "sst shell drizzle-kit",
"db:studio": "sst shell drizzle-kit studio"
"db": "sst shell drizzle-kit"
},
"dependencies": {
"@types/aws-lambda": "^8.10.142",
"drizzle-kit": "0.23.0",
"drizzle-orm": "^0.32.1",
"sst": "latest",
"pg": "^8.13.0"
"@types/pg": "^8.11.10",
"drizzle-kit": "^0.26.2",
"drizzle-orm": "^0.35.1",
"pg": "^8.13.0",
"sst": "latest"
}
}
6 changes: 3 additions & 3 deletions examples/aws-drizzle/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { db } from "./drizzle";
import { todo } from "./todo.sql";
import { APIGatewayProxyHandlerV2 } from "aws-lambda";
import { APIGatewayProxyEventV2 } from "aws-lambda";

export const handler: APIGatewayProxyHandlerV2 = async (evt) => {
export const handler = async (evt: APIGatewayProxyEventV2) => {
if (evt.requestContext.http.method === "GET") {
const result = await db.select().from(todo).execute();

Expand All @@ -15,7 +15,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (evt) => {
if (evt.requestContext.http.method === "POST") {
const result = await db
.insert(todo)
.values({ title: "Todo", id: crypto.randomUUID() })
.values({ title: "Todo", description: crypto.randomUUID() })
.returning()
.execute();

Expand Down
11 changes: 5 additions & 6 deletions examples/aws-drizzle/src/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { Pool } from "pg";
import { Resource } from "sst";
import * as schema from "./todo.sql";

const cfg = Resource.MyPostgres;
const pool = new Pool({
host: cfg.host,
port: cfg.port,
user: cfg.username,
password: cfg.password,
database: cfg.database,
host: Resource.MyPostgres.host,
port: Resource.MyPostgres.port,
user: Resource.MyPostgres.username,
password: Resource.MyPostgres.password,
database: Resource.MyPostgres.database,
});

export const db = drizzle(pool, { schema });
34 changes: 2 additions & 32 deletions examples/aws-drizzle/src/todo.sql.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
import { relations } from "drizzle-orm";
import { text, pgTable, uuid } from "drizzle-orm/pg-core";
import { text, serial, pgTable } from "drizzle-orm/pg-core";

export const todo = pgTable("todo", {
id: uuid("id").primaryKey(),
id: serial("id").primaryKey(),
title: text("title").notNull(),
description: text("description"),
});

export const todoRelations = relations(todo, (ctx) => ({
user: ctx.many(todoUser),
}));

export const user = pgTable("user", {
id: uuid("id").primaryKey(),
email: text("email").notNull(),
});

export const userRelations = relations(user, (ctx) => ({
todos: ctx.many(todoUser),
}));

export const todoUser = pgTable("todo_user", {
todoId: uuid("todo_id").references(() => todo.id),
userId: uuid("user_id").references(() => user.id),
});

export const todoToGroupRelations = relations(todoUser, (ctx) => ({
todo: ctx.one(todo, {
fields: [todoUser.todoId],
references: [todo.id],
}),
user: ctx.one(user, {
fields: [todoUser.userId],
references: [user.id],
}),
}));
24 changes: 18 additions & 6 deletions examples/aws-drizzle/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
MyPostgres: {
clusterArn: string
database: string
secretArn: string
type: "sst.aws.Postgres"
"MyApi": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"MyPostgres": {
"database": string
"host": string
"password": string
"port": number
"type": "sst.aws.Postgres"
"username": string
}
"MyVpc": {
"bastion": string
"type": "sst.aws.Vpc"
}
}
}
export {}
13 changes: 11 additions & 2 deletions examples/aws-drizzle/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ export default $config({
};
},
async run() {
const vpc = new sst.aws.Vpc("MyVpc");
const rds = new sst.aws.Postgres("MyPostgres", { vpc });
const vpc = new sst.aws.Vpc("MyVpc", { bastion: true, nat: "ec2" });
const rds = new sst.aws.Postgres("MyPostgres", { vpc, proxy: true });

const api = new sst.aws.Function("MyApi", {
vpc,
url: true,
link: [rds],
handler: "src/api.handler",
});

new sst.x.DevCommand("Studio", {
link: [rds],
dev: {
autostart: true,
command: "npx drizzle-kit studio",
},
});

return {
api: api.url,
};
Expand Down
6 changes: 5 additions & 1 deletion examples/aws-drizzle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{}
{
"compilerOptions": {
"strict": true
}
}
Loading

0 comments on commit 2be5837

Please sign in to comment.