From 7aef6d50b4f9c7652559f524412ba4a3ebd0feae Mon Sep 17 00:00:00 2001 From: Robert Bo Davis Date: Sun, 12 May 2024 11:41:40 -0400 Subject: [PATCH] chore: use functions for wheres and orderBys --- remix/app/.server/models/chat/chatModel.ts | 10 +++++----- remix/app/.server/models/user/userModel.ts | 17 +++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/remix/app/.server/models/chat/chatModel.ts b/remix/app/.server/models/chat/chatModel.ts index a995b1f..cc5a98e 100644 --- a/remix/app/.server/models/chat/chatModel.ts +++ b/remix/app/.server/models/chat/chatModel.ts @@ -6,7 +6,7 @@ import type { AgentHistoryUserItem, } from '@codellm/core'; -import { desc, eq } from 'drizzle-orm'; +import { eq } from 'drizzle-orm'; import { db, chatSchema, messageSchema } from '@remix/.server/db'; import { isError, newError, promiseMayFail } from '@remix/.server/errors'; @@ -57,8 +57,8 @@ export const addMessage = (chat: Chat) => (newMessage: AgentHistoryItem) => export const getMessages = (chat: Chat) => () => promiseMayFail( db.query.messageSchema.findMany({ - where: eq(messageSchema.chatId, chat.id), - orderBy: desc(messageSchema.createdAt), + where: (model, { eq }) => eq(model.chatId, chat.id), + orderBy: (model, { desc }) => desc(model.createdAt), }), 'chatModel:getMessages', ); @@ -118,10 +118,10 @@ export const dbToModel = (chat: Chat) => export const getById = async (id: Chat['id']) => { const chat = await promiseMayFail( db.query.chatSchema.findFirst({ - where: eq(chatSchema.id, id), + where: (model, { eq }) => eq(model.id, id), with: { messages: { - orderBy: desc(messageSchema.createdAt), + orderBy: (model, { desc }) => desc(model.createdAt), }, }, }), diff --git a/remix/app/.server/models/user/userModel.ts b/remix/app/.server/models/user/userModel.ts index 6fbee71..16554a0 100644 --- a/remix/app/.server/models/user/userModel.ts +++ b/remix/app/.server/models/user/userModel.ts @@ -1,5 +1,4 @@ import type { ChatInsert, User, UserInsert } from '@remix/.server/db'; -import { desc, eq } from 'drizzle-orm'; import { isError, newError, promiseMayFail } from '@remix/.server/errors'; import { db, chatSchema, userSchema, messageSchema } from '@remix/.server/db'; import * as chatModel from '@remix/.server/models/chat/chatModel'; @@ -37,10 +36,12 @@ export const getChats = } = {}) => promiseMayFail( db.query.chatSchema.findMany({ - where: eq(chatSchema.userId, user.id), - orderBy: desc(chatSchema.createdAt), + where: (model, { eq }) => eq(model.userId, user.id), + orderBy: (model, { desc }) => desc(model.createdAt), with: withMessages - ? { messages: { orderBy: desc(messageSchema.createdAt) } } + ? { + messages: { orderBy: (model, { desc }) => desc(model.createdAt) }, + } : {}, }), 'userModel:getChats', @@ -55,10 +56,10 @@ export const dbToModel = (user: User) => ({ export const getByEmail = async (email: string) => { const user = await promiseMayFail( db.query.userSchema.findFirst({ - where: eq(userSchema.email, email), + where: (model, { eq }) => eq(model.email, email), with: { chats: { - orderBy: desc(chatSchema.createdAt), + orderBy: (model, { desc }) => desc(model.createdAt), }, }, }), @@ -87,10 +88,10 @@ export const create = async (data: UserInsert) => { export const getByAuth0Id = async (auth0Id: string) => { const user = await promiseMayFail( db.query.userSchema.findFirst({ - where: eq(userSchema.auth0Id, auth0Id), + where: (model, { eq }) => eq(model.auth0Id, auth0Id), with: { chats: { - orderBy: desc(chatSchema.createdAt), + orderBy: (model, { desc }) => desc(model.createdAt), }, }, }),