init. moved lil website for madeline into fuse project so I can write apis in here
This commit is contained in:
18
src/server/db/index.ts
Executable file
18
src/server/db/index.ts
Executable file
@@ -0,0 +1,18 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
|
||||
import { env } from "~/env";
|
||||
import * as schema from "./schema";
|
||||
|
||||
/**
|
||||
* Cache the database connection in development. This avoids creating a new connection on every HMR
|
||||
* update.
|
||||
*/
|
||||
const globalForDb = globalThis as unknown as {
|
||||
conn: postgres.Sql | undefined;
|
||||
};
|
||||
|
||||
const conn = globalForDb.conn ?? postgres(env.DATABASE_URL);
|
||||
if (env.NODE_ENV !== "production") globalForDb.conn = conn;
|
||||
|
||||
export const db = drizzle(conn, { schema });
|
29
src/server/db/schema.ts
Executable file
29
src/server/db/schema.ts
Executable file
@@ -0,0 +1,29 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import {
|
||||
pgTableCreator,
|
||||
serial,
|
||||
timestamp,
|
||||
varchar,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
export const createTable = pgTableCreator((name) => `${name}`);
|
||||
|
||||
export const users = createTable(
|
||||
"user",
|
||||
{
|
||||
id: serial("id").primaryKey(),
|
||||
name: varchar("name", { length: 256 }),
|
||||
message: varchar("message", { length: 256 }),
|
||||
createdAt: timestamp("created_at", { withTimezone: true })
|
||||
.default(sql`CURRENT_TIMESTAMP`)
|
||||
.notNull(),
|
||||
},
|
||||
);
|
||||
|
||||
export const countdown = createTable(
|
||||
"countdown",
|
||||
{
|
||||
id: serial("id").primaryKey(),
|
||||
date: timestamp("date", { withTimezone: true }),
|
||||
},
|
||||
);
|
55
src/server/functions.ts
Executable file
55
src/server/functions.ts
Executable file
@@ -0,0 +1,55 @@
|
||||
import 'server-only';
|
||||
import { db } from '~/server/db';
|
||||
import * as schema from '~/server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
export const getMessage = async (userId: number) => {
|
||||
try {
|
||||
let message = 1;
|
||||
if (userId === 1)
|
||||
message = 2;
|
||||
const result = await db.select({
|
||||
receivedMessage: schema.users.message,
|
||||
}).from(schema.users)
|
||||
.where(eq(schema.users.id, message))
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("Error fetching message", error);
|
||||
throw new Error("Failed to fetch message");
|
||||
}
|
||||
};
|
||||
|
||||
export const setMessage = async (userId: number, message: string) => {
|
||||
try {
|
||||
await db.update(schema.users)
|
||||
.set({ message: message })
|
||||
.where(eq(schema.users.id, userId));
|
||||
} catch (error) {
|
||||
console.error("Error setting message", error);
|
||||
throw new Error("Failed to set message");
|
||||
}
|
||||
};
|
||||
|
||||
export const getCountdown = async () => {
|
||||
try {
|
||||
const result = await db.select({
|
||||
countdown: schema.countdown.date,
|
||||
}).from(schema.countdown)
|
||||
.where(eq(schema.countdown.id, 1))
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("Error fetching countdown", error);
|
||||
throw new Error("Failed to fetch countdown");
|
||||
}
|
||||
};
|
||||
|
||||
export const setCountdown = async (date: Date) => {
|
||||
try {
|
||||
await db.update(schema.countdown)
|
||||
.set({ date: date })
|
||||
.where(eq(schema.countdown.id, 1));
|
||||
} catch (error) {
|
||||
console.error("Error setting countdown", error);
|
||||
throw new Error("Failed to set countdown");
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user