How to express "children" or "parent" relation with typescript?

Let's say I have a country and region tables. A region belongs to a country, so on the regiontable, there's a foreign key country_id to the country table. The question is if it is possible to have a Country type generated like this:
type Country {
id: number;
}

type Region {
id: number;
country_id: number;
country: Country; // This one is the difficult/impossible one to get?
}
type Country {
id: number;
}

type Region {
id: number;
country_id: number;
country: Country; // This one is the difficult/impossible one to get?
}
The goal is to be able to query a region and join it with the country table as well. Thank you!
9 Replies
nelsonprsousa
nelsonprsousa•11mo ago
Current schema attempt:
export const countries = pgTable("country", {
id: serial("id").primaryKey().notNull(),
defaultName: varchar("default_name", { length: 50 }).notNull(),
});

export const regions = pgTable("region", {
id: serial("id").primaryKey().notNull(),
defaultName: varchar("default_name", { length: 50 }).notNull(),
countryId: integer("country_id").references(() => countries.id),
});
export const countries = pgTable("country", {
id: serial("id").primaryKey().notNull(),
defaultName: varchar("default_name", { length: 50 }).notNull(),
});

export const regions = pgTable("region", {
id: serial("id").primaryKey().notNull(),
defaultName: varchar("default_name", { length: 50 }).notNull(),
countryId: integer("country_id").references(() => countries.id),
});
Angelelz
Angelelz•11mo ago
You could do that, but it's usually all the way around. I don't know what are you app requirements but this what people would typically do:
type Country = {
id: number,
defaultName: string,
regions: Region[]
}
type Region = {
id: number,
defaultName: string,
countryId: number
}
type Country = {
id: number,
defaultName: string,
regions: Region[]
}
type Region = {
id: number,
defaultName: string,
countryId: number
}
But you can make it however you want
nelsonprsousa
nelsonprsousa•11mo ago
Sure. But how can I do it, either way? It is using the relations()?
nelsonprsousa
nelsonprsousa•11mo ago
No description
Angelelz
Angelelz•11mo ago
The types you are looking for are not autimatically exported for you You either have to construct them manually or get them from the query you'd do for example
const countries = await db.query.countries.findFirst({
with: { regions: true }
})
const countries = await db.query.countries.findFirst({
with: { regions: true }
})
Will be the type you're looking for Or you can construct it like:
type Country = typeof countries.$inferSelect
type Region = typeof regions.$inferSelect
type RegionWithCountry = Region & { country: Country }
type Country = typeof countries.$inferSelect
type Region = typeof regions.$inferSelect
type RegionWithCountry = Region & { country: Country }
nelsonprsousa
nelsonprsousa•11mo ago
I tried the first approach without success. Looks like I got a never type
No description
nelsonprsousa
nelsonprsousa•11mo ago
Is this expected?
Angelelz
Angelelz•11mo ago
Nope, are you defining your relations the drizzle way? https://orm.drizzle.team/docs/rqb#declaring-relations
nelsonprsousa
nelsonprsousa•11mo ago
Checking 🔃 That was it, thank you!!
Want results from more Discord servers?
Add your server