Turkish
Explore posts from serversDTDrizzle Team
•Created by Turkish on 10/13/2023 in #help
Querying based on record in another table
I have this schema:
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:
Any help is appreciated, many thanks ❤️
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;
};
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);
},
},
},
});
1 replies