How to derive type from relational query?

Hi, I was wondering if there was an easy way to generate types from relational queries that I can then use in TS. Don't seem to see anything in the docs but I could be blind.
2 Replies
MAST
MAST17mo ago
I could be wrong but I don't think there's anyway to do this currently.
Artifex
Artifex17mo ago
i did it this way, i dont know if its what you are looking for
export const address = sqliteTable("address", {
id: integer("id").primaryKey(),
street: text("street"),
city: text("city"),
province: text("province"),
});

export const users = sqliteTable(
"users",
{
id: integer("id").primaryKey(),
email: text("email").notNull(),
firstName: text("firstName").notNull(),
lastName: text("lastName").notNull(),
address: integer("address_id").notNull(),
},
(users) => {
return {
emailIndex: uniqueIndex("email_index").on(users.email),
};
}
);

export const userRelations = relations(users, ({ one }) => ({
address: one(address, {
fields: [users.address],
references: [address.id],
}),
}));

export type User = InferModel<typeof users, "select"> & {
address: InferModel<typeof address, "select">;
};

export const getUsers: User[] = db.query.users.findMany({
with: {
address: true,
},
});
export const address = sqliteTable("address", {
id: integer("id").primaryKey(),
street: text("street"),
city: text("city"),
province: text("province"),
});

export const users = sqliteTable(
"users",
{
id: integer("id").primaryKey(),
email: text("email").notNull(),
firstName: text("firstName").notNull(),
lastName: text("lastName").notNull(),
address: integer("address_id").notNull(),
},
(users) => {
return {
emailIndex: uniqueIndex("email_index").on(users.email),
};
}
);

export const userRelations = relations(users, ({ one }) => ({
address: one(address, {
fields: [users.address],
references: [address.id],
}),
}));

export type User = InferModel<typeof users, "select"> & {
address: InferModel<typeof address, "select">;
};

export const getUsers: User[] = db.query.users.findMany({
with: {
address: true,
},
});
Want results from more Discord servers?
Add your server