Skip to content

db model types #200

Answered by kettanaito
jmaldon1 asked this question in Q&A
Apr 5, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

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 db client, you can do that from the db[model].findFirst return type:

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 a…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jmaldon1
Comment options

@kettanaito
Comment options

Answer selected by jmaldon1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants