Querying and typing a many to many relationship?

So I'm enjoying using drizzle, but querying a many to many relationship seems a bit verbose, which makes me think I'm doing it wrong. It's also making using drizzle-zod a bit confusing: So I've set up my tables, user and profile
// Drizzle relationships:
export const userRelations = relations(users, ({ many }) => ({
usersToProfiles: many(userToProfiles),
}));

export const profileRelations = relations(profiles, ({ many }) => ({
usersToProfiles: many(usersToProfiles),
}));

export const usersToProfilesRelations = relations(
usersToProfiles,
({ one }) => ({
user: one(users, {
fields: [usersToProfiles.userId],
references: [users.id],
}),
profiles: one(profiles, {
fields: [usersToProfiles.profileId],
references: [profiles.id],
}),
}),
);

// Querying all users and their profiles
const listings = await db.query.users.findMany({
with: {
usersToProfiles: {
with: {
profiles: true,
},
},
},
});
// Drizzle relationships:
export const userRelations = relations(users, ({ many }) => ({
usersToProfiles: many(userToProfiles),
}));

export const profileRelations = relations(profiles, ({ many }) => ({
usersToProfiles: many(usersToProfiles),
}));

export const usersToProfilesRelations = relations(
usersToProfiles,
({ one }) => ({
user: one(users, {
fields: [usersToProfiles.userId],
references: [users.id],
}),
profiles: one(profiles, {
fields: [usersToProfiles.profileId],
references: [profiles.id],
}),
}),
);

// Querying all users and their profiles
const listings = await db.query.users.findMany({
with: {
usersToProfiles: {
with: {
profiles: true,
},
},
},
});
I'm then not sure how to extend the User schema with its usersToProfiles and its Profiles:
export const userSchema = createInsertSchema(users);
export const UserWithProfilesSchema = userSchema.extend() // not sure how to extend here
export type UserWithProfileType = z.infer<typeof UserWithProfileSchema>;
export const userSchema = createInsertSchema(users);
export const UserWithProfilesSchema = userSchema.extend() // not sure how to extend here
export type UserWithProfileType = z.infer<typeof UserWithProfileSchema>;
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server