How do I get types for relationship queries?

// Users table
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
name: text('name').notNull(),
createdAt: timestamp('created_at').defaultNow().notNull()
});

// Todos table
export const todos = pgTable('todos', {
id: serial('id').primaryKey(),
title: varchar('title', { length: 255 }).notNull(),
completed: boolean('completed').default(false).notNull(),
userId: integer('user_id').references(() => users.id).notNull(),
createdAt: timestamp('created_at').defaultNow().notNull()
});

// Define the relationships
export const usersRelations = relations(users, ({ many }) => ({
todos: many(todos)
}));

export const todosRelations = relations(todos, ({ one }) => ({
user: one(users, {
fields: [todos.userId],
references: [users.id],
})
}));

// Example usage:
const db = drizzle(client);

// Query user with their todos
const userWithTodos = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.id, newUser[0].id),
with: { // HOW DO I GET THESE TYPES?
todos: true
}
});


I want to do something like this, but I want to be able to pass a type for my relationships so I know what can be passed in.
getUserById = (id, relationships?) => {
await db.query.users.findFirst({
where: (users, { eq }) => eq(users.id, newUser[0].id),
...(relationships ? { with: relationships } : {})
});
}
// Users table
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
name: text('name').notNull(),
createdAt: timestamp('created_at').defaultNow().notNull()
});

// Todos table
export const todos = pgTable('todos', {
id: serial('id').primaryKey(),
title: varchar('title', { length: 255 }).notNull(),
completed: boolean('completed').default(false).notNull(),
userId: integer('user_id').references(() => users.id).notNull(),
createdAt: timestamp('created_at').defaultNow().notNull()
});

// Define the relationships
export const usersRelations = relations(users, ({ many }) => ({
todos: many(todos)
}));

export const todosRelations = relations(todos, ({ one }) => ({
user: one(users, {
fields: [todos.userId],
references: [users.id],
})
}));

// Example usage:
const db = drizzle(client);

// Query user with their todos
const userWithTodos = await db.query.users.findFirst({
where: (users, { eq }) => eq(users.id, newUser[0].id),
with: { // HOW DO I GET THESE TYPES?
todos: true
}
});


I want to do something like this, but I want to be able to pass a type for my relationships so I know what can be passed in.
getUserById = (id, relationships?) => {
await db.query.users.findFirst({
where: (users, { eq }) => eq(users.id, newUser[0].id),
...(relationships ? { with: relationships } : {})
});
}
1 Reply
Angelelz
Angelelz3w ago
I answered this question a long time ago in this discussion: https://github.com/drizzle-team/drizzle-orm/discussions/1483
GitHub
Relations input · drizzle-team drizzle-orm · Discussion #1483
I have a schema that looks like somewhat like this: export const employeesSchema = pgTable('employees', { firstName: varchar('first_name', { length: 256 }), lastName: varchar('l...
Want results from more Discord servers?
Add your server