Skip to content

Commit

Permalink
chore: use functions for wheres and orderBys
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-bo-davis committed May 12, 2024
1 parent 2371def commit 7aef6d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
10 changes: 5 additions & 5 deletions remix/app/.server/models/chat/chatModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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',
);
Expand Down Expand Up @@ -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),
},
},
}),
Expand Down
17 changes: 9 additions & 8 deletions remix/app/.server/models/user/userModel.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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),
},
},
}),
Expand Down Expand Up @@ -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),
},
},
}),
Expand Down

0 comments on commit 7aef6d5

Please sign in to comment.