-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add db package with drizzle orm * update lock file * switch to regular postgres * adding db to trpc context * add dotenv * copy env before building * set db adapter to vercel pg * adding health check * add db health check * fixed eslint * fix env vars * fix false positive linting errors * make sheld nonnullable in UI component
- Loading branch information
1 parent
8e55f60
commit ccd3d1b
Showing
29 changed files
with
1,368 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Since .env is gitignored, you can use .env.example to build a new `.env` file when you clone the repo. | ||
# Keep this file up-to-date when you add new variables to \`.env\`. | ||
|
||
# This file will be committed to version control, so make sure not to have any secrets in it. | ||
# If you are cloning this repo, create a copy of this file named `.env` and populate it with your secrets. | ||
|
||
# The database URL is used to connect to your Supabase database. | ||
POSTGRES_URL="postgresql://postgres:postgres@localhost:5432/sovoli" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Next.js: debug full stack", | ||
"type": "node-terminal", | ||
"request": "launch", | ||
"command": "pnpm dev", | ||
"cwd": "${workspaceFolder}/apps/sovoli.com", | ||
"serverReadyAction": { | ||
"pattern": "- Local:.+(https?://.+)", | ||
"uriFormat": "%s", | ||
"action": "debugWithChrome" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
apps/sovoli.com/src/app/(profile)/[username]/_components/User.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use client"; | ||
|
||
import { api } from "~/trpc/react"; | ||
import { UserScreen } from "@sovoli/ui/screens/user"; | ||
|
||
export function User({ username }: { username: string }) { | ||
const [user] = api.user.byUsername.useSuspenseQuery({ username }); | ||
|
||
if (!user) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<h1> | ||
{user.username} - {user.id} - {user.name} | ||
</h1> | ||
<UserScreen username={user.username} /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
/** @jsxImportSource react */ | ||
|
||
import { UserScreen } from "@sovoli/ui/screens/user"; | ||
import { api, HydrateClient } from "~/trpc/server"; | ||
import { User } from "./_components/User"; | ||
import { Suspense } from "react"; | ||
|
||
export default function UserPage({ params }: { params: { username: string } }) { | ||
void api.user.byUsername.prefetch({ username: params.username }); | ||
|
||
export default function Profile({ params }: { params: { username: string } }) { | ||
return ( | ||
<div className="min-h-screen sm:pl-60 dark:bg-black"> | ||
<h1>Profile: {params.username}</h1> | ||
<UserScreen username={params.username} /> | ||
</div> | ||
<HydrateClient> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<User username={params.username} /> | ||
</Suspense> | ||
</HydrateClient> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { api } from "~/trpc/server"; | ||
import { db } from "@sovoli/db/client"; | ||
import { sql } from "drizzle-orm"; | ||
|
||
export async function GET() { | ||
// health check for all systems | ||
// Systems: | ||
// - db | ||
// - auth | ||
// - storage | ||
// - analytics | ||
// - database | ||
// - AI Gateway (OpenAI, etc) | ||
let trpcHealth, dbHealth; | ||
|
||
try { | ||
trpcHealth = await api.health.check(); | ||
} catch (e) { | ||
trpcHealth = { status: "error", error: e }; | ||
} | ||
try { | ||
// check round trip time to db health check | ||
const startTime = Date.now(); | ||
|
||
const dbResponse = await db.execute(sql`SELECT NOW()`); | ||
|
||
const endTime = Date.now(); | ||
const roundTripTime = endTime - startTime; | ||
|
||
dbHealth = { | ||
status: "ok", | ||
roundTripTime: `${roundTripTime}ms`, | ||
dbTime: dbResponse.rows[0]?.now, | ||
}; | ||
} catch (e) { | ||
dbHealth = { | ||
status: "error", | ||
error: e instanceof Error ? e.message : String(e), | ||
}; | ||
} | ||
|
||
const data = { status: "ok", trpc: trpcHealth, db: dbHealth }; | ||
|
||
return Response.json(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { createEnv } from "@t3-oss/env-nextjs"; | ||
import { vercel } from "@t3-oss/env-nextjs/presets"; | ||
import { z } from "zod"; | ||
|
||
// import { env as authEnv } from "@acme/auth/env"; | ||
|
||
export const env = createEnv({ | ||
extends: [vercel()], | ||
shared: { | ||
NODE_ENV: z | ||
.enum(["development", "production", "test"]) | ||
.default("development"), | ||
}, | ||
/** | ||
* Specify your server-side environment variables schema here. | ||
* This way you can ensure the app isn't built with invalid env vars. | ||
*/ | ||
server: { | ||
POSTGRES_URL: z.string().url(), | ||
}, | ||
|
||
/** | ||
* Specify your client-side environment variables schema here. | ||
* For them to be exposed to the client, prefix them with `NEXT_PUBLIC_`. | ||
*/ | ||
client: { | ||
// NEXT_PUBLIC_CLIENTVAR: z.string(), | ||
}, | ||
/** | ||
* Destructure all variables from `process.env` to make sure they aren't tree-shaken away. | ||
*/ | ||
experimental__runtimeEnv: { | ||
NODE_ENV: process.env.NODE_ENV, | ||
|
||
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR, | ||
}, | ||
skipValidation: | ||
!!process.env.CI || process.env.npm_lifecycle_event === "lint", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { TRPCRouterRecord } from "@trpc/server"; | ||
// import { z } from "zod"; | ||
// import { eq } from "@sovoli/db"; | ||
// import { User } from "@sovoli/db/schema"; | ||
|
||
import { publicProcedure } from "../trpc"; | ||
|
||
export const healthRouter = { | ||
check: publicProcedure.query(() => { | ||
return { status: "ok" }; | ||
}), | ||
} satisfies TRPCRouterRecord; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { TRPCRouterRecord } from "@trpc/server"; | ||
import { z } from "zod"; | ||
import { eq } from "@sovoli/db"; | ||
import { User } from "@sovoli/db/schema"; | ||
|
||
import { publicProcedure } from "../trpc"; | ||
|
||
export const userRouter = { | ||
byUsername: publicProcedure | ||
.input(z.object({ username: z.string() })) | ||
.query(({ ctx, input }) => { | ||
return ctx.db.query.User.findFirst({ | ||
where: eq(User.username, input.username), | ||
}); | ||
}), | ||
} satisfies TRPCRouterRecord; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig } from "drizzle-kit"; | ||
|
||
if (!process.env.POSTGRES_URL) { | ||
throw new Error("Missing POSTGRES_URL"); | ||
} | ||
|
||
export default defineConfig({ | ||
schema: "./src/schema.ts", | ||
out: "./drizzle", | ||
dialect: "postgresql", | ||
dbCredentials: { url: process.env.POSTGRES_URL }, | ||
}); |
Oops, something went wrong.