Help with a request
Hey, guys, I'm new here. Here's my schema.
export const Anime = pgTable('Anime', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
img: text('img').notNull(),
imgHeader: text('imgHeader').notNull(),
describe: text('describe').notNull(),
genres: text('genres').array().notNull(),
author: text('author').notNull(),
country: text('country').notNull(),
published: integer('published'),
averageRating: doublePrecision('averageRating').default(0),
ratingCount: integer('ratingCount').default(0),
status: text('status').notNull(),
popularity: integer('popularity').default(0),
})
export const Users = pgTable('User', {
id: serial('id').primaryKey().notNull(),
name: text('name').notNull(),
email: text('email').notNull(),
image: text('image').notNull(),
favorite: text('favorite').array(),
createdAt: timestamp('createdAt').defaultNow().notNull(),
})
Here's a function with prisma that I can't rewrite myself to drizzle
async getUserFavoriteAnime(email: string) {
const user = await getUserFavorite(email);
if (!user?.favorite || user?.favorite.length === 0) {
return [];
}
return prisma.anime.findMany({
where: { name: { in: user?.favorite } },
});
}
In user.favorite I have an array of names and I want to find all the Anime that are in that array of names.
3 Replies
db.select().from(Anime).where(inArray(Anime.name, user?.favorite))
Thank you so much. I'd like to take this opportunity to ask you another question. return prisma.user.update({
where: { email: email },
data: {
favorite: {
set: user.favorite.filter((anime: string) => anime !== name),
},
},
}) and return prisma.user.update({
where: { email: email },
data: {
favorite: {
push: name,
},
},
})
db.update(Users).set({ favorite: user.favorite.filter((anime: string) => anime !== name) }).where(eq(Users.email, email))
don't think there's a push, so this would be a get and then update, prob best as a transaction,
db.transaction(async (tx) => { /** await tx.select...then await tx.update... */ })