Using 'where' inside 'with'
Hello! I'm using schema like this:
Next step, I wanna use this query:
But I got TS-error:
As I understand it, for some reason it is not possible to use
export const user = pgTable('user', {
id: serial('id').primaryKey(),
username: text('username').notNull(),
email: text('email').notNull(),
password: text('password').notNull(),
bio: text('bio').default(null),
image: text('image').default(null),
});
export const userRelations = relations(user, ({ many }) => ({
article: many(article),
}));
export const article = pgTable('article', {
id: serial('id').primaryKey(),
slug: text('slug').notNull(),
title: text('title').notNull(),
description: text('description').default(null),
body: text('body').default(null),
createdAt: timestamp('createdAt').defaultNow(),
updatedAt: timestamp('updatedAt'),
tagList: text('tagList').notNull().array(),
favoritesCount: integer('favoritesCount').default(0),
authorId: integer('authorId').references(() => user.id),
});
export const articleRelations = relations(article, ({ one }) => ({
authorId: one(user, {
fields: [article.authorId],
references: [user.id],
}),
}));
Next step, I wanna use this query:
const articlesQuery = await this.db.query.article.findMany({
with: {
authorId: {
where: (authorId) => eq(authorId.username, queryParam.author),
},
},
But I got TS-error:
error TS2322: Type '{ where: (authorId: any) => SQL<unknown>; }' is not assignable to type 'true | { columns?: { id?: boolean; username?: boolean; email?: boolean; password?: boolean; bio?: boolean; image?: boolean; }; with?: { article?: true | DBQueryConfig<"many", false, ExtractTablesWithRelations<typeof import("D:/Site/mediumclone_nestjs/src/schema")>, { ...; }>; }; extras?: Record<...> | ((fields: { .....'.
As I understand it, for some reason it is not possible to use
where inside with, however in the example on the site it works fine. Could you please tell me what my mistake is?