InferModel including relations?

Hi, How do you use InferModel to obtain a type that includes relations? The results for an executed query has the inferred types but I'm not sure how to infer the type more generally? Thanks
13 Replies
gerty1
gerty117mo ago
Bumping this, I'd love to know as well
ChronicStone
ChronicStone16mo ago
Hi @danielsharvey @gerty1 Did one of you happen to have found a workaround for this ?
Ꚃimon
Ꚃimon16mo ago
@chronicstone You can get the returntype of the function where you fetch the data and do the following
export type InitialPostsType = NonNullable<
Awaited<ReturnType<typeof getDataFunction>>
>
export type InitialPostsType = NonNullable<
Awaited<ReturnType<typeof getDataFunction>>
>
ChronicStone
ChronicStone16mo ago
@intelligently Good point, thanks
daniel
danielOP16mo ago
@gerty1 @chronicstone Sorry for the slow response! Similar to @intelligently 's answer:
export type TypeIncludingWith = Awaited<ReturnType<MyController['getItems']>>;
class MyController {
public async getItems() {
const rows = await this.db.query.inventoryItems.findMany({
with: {
designInfo: true,
},
where: ...
offset: offset,
limit: limit,
});
return rows;
}
}
export type TypeIncludingWith = Awaited<ReturnType<MyController['getItems']>>;
class MyController {
public async getItems() {
const rows = await this.db.query.inventoryItems.findMany({
with: {
designInfo: true,
},
where: ...
offset: offset,
limit: limit,
});
return rows;
}
}
fermentfan
fermentfan16mo ago
That's not really a good fitting solution everywhere right? I don't want to write code just so I can define my types correctly... Is there another way? In my case I want to define a function parameter that is the type of a single relation in my table.
Angelelz
Angelelz16mo ago
The Relations API can output too many types. You can have drizzle give you whatever you want. Depending on the shape you give it, the type will be different. How could you get a type if you don't write it? Now, what we've done where we need the relational API and we need to export the types, we just create them like so:
export const vehicles = mysqlTable(
...
);
export const inspections = mysqlTable(
...
);
export type Vehicle = InferModel<typeof vehicles>;
export type Inspection = InferModel<typeof inspections>;
export type VehicleWithInspections = Vehicle & { inspections: Inspection[] };
export const vehicles = mysqlTable(
...
);
export const inspections = mysqlTable(
...
);
export type Vehicle = InferModel<typeof vehicles>;
export type Inspection = InferModel<typeof inspections>;
export type VehicleWithInspections = Vehicle & { inspections: Inspection[] };
InferModel is a type helper imported from drizzle-orm.
daniel
danielOP16mo ago
👍🏻 I've also used this mechanism in places.
Ꚃimon
Ꚃimon16mo ago
Issue is that this will give you the wrong typehints if you're only selecting certain columns instead of all of them
Andrii Sherman
Andrii Sherman16mo ago
I guess @Dan Kochetov can help you with a proper type for it
daniel
danielOP15mo ago
@Andrew Sherman @Dan Kochetov Related question: Is there an official way to get the relation metadata? I'm currently doing the following:
import { createTableRelationsHelpers, extractTablesRelationalConfig, Table } from 'drizzle-orm';

/// Drizzle relations metadata
export const { tables: t } = extractTablesRelationalConfig(
s,
createTableRelationsHelpers,
);
import { createTableRelationsHelpers, extractTablesRelationalConfig, Table } from 'drizzle-orm';

/// Drizzle relations metadata
export const { tables: t } = extractTablesRelationalConfig(
s,
createTableRelationsHelpers,
);
Dan
Dan15mo ago
no official way for now, but this should work fine
Want results from more Discord servers?
Add your server