taylor.presto
taylor.presto
Explore posts from servers
DTDrizzle Team
Created by taylor.presto on 11/5/2024 in #help
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 } : {})
});
}
2 replies
TtRPC
Created by taylor.presto on 11/18/2023 in #❓-help
I am getting a errors after starting a TRPC project with T3. "Unsafe return of an `any` typed value"
No description
13 replies
TtRPC
Created by taylor.presto on 12/20/2022 in #❓-help
useQuery enabled not working???
Even setting the enabled to false --> trpc.order.get({id: "123"}, {enabled: false}) still makes the request
2 replies
TtRPC
Created by taylor.presto on 10/29/2022 in #❓-help
Bi directional infinite query example with prisma
I'm working on a project that is using this pattern, but my hasPreviousPages gets blown away when I go to the next page. Anyone have an idea what might be happening?
1 replies
TtRPC
Created by taylor.presto on 10/21/2022 in #❓-help
How to infer the output of a mutation? The current methods appear to be depreciated.
4 replies