new queries project structure

This commit is contained in:
Oliver Bryan
2025-12-08 00:52:08 +00:00
parent 3cecc0e5a0
commit 01e32a8177
8 changed files with 50 additions and 45 deletions

3
src/db/queries/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export * from "./users";
export * from "./projects";
export * from "./issues";

View File

@@ -1,46 +1,7 @@
import { eq, sql, and } from "drizzle-orm"; import { eq, sql, and } from "drizzle-orm";
import { db } from "./client"; import { db } from "../client";
import { Issue, Project, User } from "./schema"; import { Issue } from "../schema";
// user related
export async function createUser(name: string, username: string) {
const [user] = await db.insert(User).values({ name, username }).returning();
return user;
}
export async function getUserByUsername(username: string) {
const [user] = await db.select().from(User).where(eq(User.username, username));
return user;
}
// project related
export async function createProject(blob: string, name: string, owner: typeof User.$inferSelect) {
const [project] = await db
.insert(Project)
.values({
blob,
name,
ownerId: owner.id,
})
.returning();
if (!project) {
throw new Error(`failed to create project ${name} with blob ${blob} for owner ${owner.username}`);
}
return project;
}
export async function getProjectByID(projectId: number) {
const [project] = await db.select().from(Project).where(eq(Project.id, projectId));
return project;
}
export async function getProjectByBlob(projectBlob: string) {
const [project] = await db.select().from(Project).where(eq(Project.blob, projectBlob));
return project;
}
// issue related
export async function createIssue(projectId: number, title: string, description: string) { export async function createIssue(projectId: number, title: string, description: string) {
// prevents two issues with the same unique number // prevents two issues with the same unique number
return await db.transaction(async (tx) => { return await db.transaction(async (tx) => {

View File

@@ -0,0 +1,28 @@
import { eq } from "drizzle-orm";
import { db } from "../client";
import { Project, User } from "../schema";
export async function createProject(blob: string, name: string, owner: typeof User.$inferSelect) {
const [project] = await db
.insert(Project)
.values({
blob,
name,
ownerId: owner.id,
})
.returning();
if (!project) {
throw new Error(`failed to create project ${name} with blob ${blob} for owner ${owner.username}`);
}
return project;
}
export async function getProjectByID(projectId: number) {
const [project] = await db.select().from(Project).where(eq(Project.id, projectId));
return project;
}
export async function getProjectByBlob(projectBlob: string) {
const [project] = await db.select().from(Project).where(eq(Project.blob, projectBlob));
return project;
}

13
src/db/queries/users.ts Normal file
View File

@@ -0,0 +1,13 @@
import { eq } from "drizzle-orm";
import { db } from "../client";
import { User } from "../schema";
export async function createUser(name: string, username: string) {
const [user] = await db.insert(User).values({ name, username }).returning();
return user;
}
export async function getUserByUsername(username: string) {
const [user] = await db.select().from(User).where(eq(User.username, username));
return user;
}

View File

@@ -1,5 +1,5 @@
import type { BunRequest } from "bun"; import type { BunRequest } from "bun";
import { createIssue, getProjectByID, getProjectByBlob } from "../db/queries.js"; import { createIssue, getProjectByID, getProjectByBlob } from "../db/queries";
// /issue/create?projectId=1&title=Testing&description=Description // /issue/create?projectId=1&title=Testing&description=Description
// OR // OR

View File

@@ -1,5 +1,5 @@
import type { BunRequest } from "bun"; import type { BunRequest } from "bun";
import { deleteIssue } from "../db/queries.js"; import { deleteIssue } from "../db/queries";
// /issue/delete?id=1 // /issue/delete?id=1
export default async function issueDelete(req: BunRequest) { export default async function issueDelete(req: BunRequest) {

View File

@@ -1,5 +1,5 @@
import type { BunRequest } from "bun"; import type { BunRequest } from "bun";
import { updateIssue } from "../db/queries.js"; import { updateIssue } from "../db/queries";
// /issue/update?id=1&title=Testing&description=Description // /issue/update?id=1&title=Testing&description=Description
export default async function issueUpdate(req: BunRequest) { export default async function issueUpdate(req: BunRequest) {

View File

@@ -1,5 +1,5 @@
import type { BunRequest } from "bun"; import type { BunRequest } from "bun";
import { getIssuesByProject, getProjectByBlob } from "../db/queries.js"; import { getIssuesByProject, getProjectByBlob } from "../db/queries";
export default async function issues(req: BunRequest<"/issues/:projectId">) { export default async function issues(req: BunRequest<"/issues/:projectId">) {
const { projectId } = req.params; const { projectId } = req.params;