db model types #200
-
I am not seeing a clear answer anywhere so I thought I'd just ask. How do you add types for a model in typescript. For example: export const db = factory({
muscle: {
id: primaryKey(Number),
muscle: String,
}
}); What should the type of this muscle array be? const muscles = []; Do I need to create my own type: export type muscle = {
id: number;
muscle: string;
}; and then do this: const muscles: muscle[] = [] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey, @jmaldon1. The types are inferred from the value getter you give to your model. This is done automatically by the library, and the result you get is already typed according to your model: const db = factory({
muscle: {
id: primaryKey(String),
muscle: string
}
})
const muscles = db.muscle.findMany()
// Array<{ id: string, muscle: string }> If you wish to extract the type from the generated type ExtractModelType<F extends Function> = ReturnType<F>
type Muscle = ExtractModelType<typeof db.muscle.findFirst>
// { id: strig, muscle: string } I wouldn't advise doing this though. The library does not support accepting types as a generic to the |
Beta Was this translation helpful? Give feedback.
Hey, @jmaldon1.
The types are inferred from the value getter you give to your model. This is done automatically by the library, and the result you get is already typed according to your model:
If you wish to extract the type from the generated
db
client, you can do that from thedb[model].findFirst
return type:I wouldn't advise doing this though.
The library does not support a…