From 3742b68ede5b98aead7032f5fc50a3c5deb3a5f4 Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 11 Nov 2023 16:33:18 +0000 Subject: [PATCH] feat: add index and enable ordering --- src/app/boards/boards.tsx | 2 +- src/server/api/routers/board.ts | 37 ++++++++++++++++++++++++++------- src/server/db/schema.ts | 13 +++++++++--- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/app/boards/boards.tsx b/src/app/boards/boards.tsx index 57faa72e..414f6043 100644 --- a/src/app/boards/boards.tsx +++ b/src/app/boards/boards.tsx @@ -11,7 +11,7 @@ export function Boards() {
{data?.map((board) => { return ( - +

{board.name} diff --git a/src/server/api/routers/board.ts b/src/server/api/routers/board.ts index af180f4f..0458620d 100644 --- a/src/server/api/routers/board.ts +++ b/src/server/api/routers/board.ts @@ -1,7 +1,7 @@ import { z } from "zod"; -import { eq } from "drizzle-orm"; +import { eq, asc } from "drizzle-orm"; -import { boards } from "~/server/db/schema"; +import { boards, cards, lists } from "~/server/db/schema"; import { generateUID } from "~/utils/generateUID"; import { @@ -10,24 +10,47 @@ import { } from "~/server/api/trpc"; export const boardRouter = createTRPCRouter({ - all: publicProcedure.query(async ({ ctx }) => { + all: publicProcedure.query(({ ctx }) => { const userId = ctx.session?.user.id; if (!userId) return; - const boards = await ctx.db.query.boards.findMany(); - - return boards.map((board) => ({ ...board, id: board.publicId })) + return ctx.db.query.boards.findMany({ + columns: { + publicId: true, + name: true, + }, + }); }), byId: publicProcedure .input(z.object({ id: z.string().min(12) })) .query(({ ctx, input }) => ctx.db.query.boards.findFirst({ where: eq(boards.publicId, input.id), + columns: { + publicId: true, + name: true, + }, with: { lists: { + orderBy: [asc(lists.index)], + columns: { + publicId: true, + name: true, + boardId: true, + index: true, + }, with: { - cards: true, + cards: { + orderBy: [asc(cards.index)], + columns: { + publicId: true, + title: true, + description: true, + listId: true, + index: true, + } + } }, }, }, diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts index f62aef1e..41a93ed0 100644 --- a/src/server/db/schema.ts +++ b/src/server/db/schema.ts @@ -7,6 +7,7 @@ import { primaryKey, text, timestamp, + unique, varchar, } from "drizzle-orm/mysql-core"; import { type AdapterAccount } from "next-auth/adapters"; @@ -17,7 +18,7 @@ import { type AdapterAccount } from "next-auth/adapters"; * * @see https://orm.drizzle.team/docs/goodies#multi-project-schema */ -export const mySqlTable = mysqlTableCreator((name) => `kan.${name}`); +export const mySqlTable = mysqlTableCreator((name) => `${name}`); export const boards = mySqlTable( "board", @@ -53,7 +54,10 @@ export const lists = mySqlTable( .notNull(), updatedAt: timestamp("updatedAt").onUpdateNow(), boardId: varchar("boardId", { length: 256 }).notNull(), - } + index: int("index").notNull() + }, (t) => ({ + unq: unique().on(t.boardId, t.index), + }) ); export const listsRelations = relations(lists, ({ one, many }) => ({ @@ -81,7 +85,10 @@ export const cards = mySqlTable( .notNull(), updatedAt: timestamp("updatedAt").onUpdateNow(), listId: varchar("listId", { length: 256 }).notNull(), - } + index: int("index").notNull() + }, (t) => ({ + unq: unique().on(t.listId, t.index), + }) ); export const cardsRelations = relations(cards, ({ one }) => ({