Querying based on record in another table

I have this schema:
export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name"),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
seenPosts: many(seenPosts),
}));
export type User = InferSelectModel<typeof users>;
export type UserWithPosts = User & { posts: Post[] };

export const posts = pgTable("posts", {
id: serial("id").primaryKey(),
userId: integer("user_id").notNull(),
content: text("content"),
});
export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [posts.userId],
references: [users.id],
}),
seenPosts: many(seenPosts),
}));
export type Post = InferSelectModel<typeof posts>;
export type PostWithAuthor = Post & { author: User };

export const seenPosts = pgTable("seen_posts", {
userId: integer("user_id").references(() => users.id),
postId: integer("post_id").references(() => posts.id),
});

export const seenPostsRelations = relations(seenPosts, ({ one }) => ({
user: one(users, {
fields: [seenPosts.userId],
references: [users.id],
}),
post: one(posts, {
fields: [seenPosts.postId],
references: [posts.id],
}),
}));
export type SeenPost = InferSelectModel<typeof seenPosts>;
export type SeenPostWithUserAndPost = SeenPost & {
user: User;
post: Post;
};
export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name"),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
seenPosts: many(seenPosts),
}));
export type User = InferSelectModel<typeof users>;
export type UserWithPosts = User & { posts: Post[] };

export const posts = pgTable("posts", {
id: serial("id").primaryKey(),
userId: integer("user_id").notNull(),
content: text("content"),
});
export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [posts.userId],
references: [users.id],
}),
seenPosts: many(seenPosts),
}));
export type Post = InferSelectModel<typeof posts>;
export type PostWithAuthor = Post & { author: User };

export const seenPosts = pgTable("seen_posts", {
userId: integer("user_id").references(() => users.id),
postId: integer("post_id").references(() => posts.id),
});

export const seenPostsRelations = relations(seenPosts, ({ one }) => ({
user: one(users, {
fields: [seenPosts.userId],
references: [users.id],
}),
post: one(posts, {
fields: [seenPosts.postId],
references: [posts.id],
}),
}));
export type SeenPost = InferSelectModel<typeof seenPosts>;
export type SeenPostWithUserAndPost = SeenPost & {
user: User;
post: Post;
};
In my nextjs api route (pages router), I cant figure out how should i query only the posts that does not have a record saved in the seenPosts table . My query:
const unseenPosts = await db.query.posts.findMany({
with: {
author: true,
seenPosts: {
where({ userId }, { eq }) {
return eq(userId, LOGGED_IN_USER_ID);
},
},
},
});
const unseenPosts = await db.query.posts.findMany({
with: {
author: true,
seenPosts: {
where({ userId }, { eq }) {
return eq(userId, LOGGED_IN_USER_ID);
},
},
},
});
Any help is appreciated, many thanks ❤️
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server