mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
new queries project structure
This commit is contained in:
3
src/db/queries/index.ts
Normal file
3
src/db/queries/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./users";
|
||||
export * from "./projects";
|
||||
export * from "./issues";
|
||||
@@ -1,46 +1,7 @@
|
||||
import { eq, sql, and } from "drizzle-orm";
|
||||
import { db } from "./client";
|
||||
import { Issue, Project, User } from "./schema";
|
||||
import { db } from "../client";
|
||||
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) {
|
||||
// prevents two issues with the same unique number
|
||||
return await db.transaction(async (tx) => {
|
||||
28
src/db/queries/projects.ts
Normal file
28
src/db/queries/projects.ts
Normal 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
13
src/db/queries/users.ts
Normal 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;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
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
|
||||
// OR
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { deleteIssue } from "../db/queries.js";
|
||||
import { deleteIssue } from "../db/queries";
|
||||
|
||||
// /issue/delete?id=1
|
||||
export default async function issueDelete(req: BunRequest) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { updateIssue } from "../db/queries.js";
|
||||
import { updateIssue } from "../db/queries";
|
||||
|
||||
// /issue/update?id=1&title=Testing&description=Description
|
||||
export default async function issueUpdate(req: BunRequest) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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">) {
|
||||
const { projectId } = req.params;
|
||||
|
||||
Reference in New Issue
Block a user