Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Creating Type-Safe Models #282

Open
kjrocker opened this issue Aug 14, 2023 · 3 comments
Open

Help Creating Type-Safe Models #282

kjrocker opened this issue Aug 14, 2023 · 3 comments

Comments

@kjrocker
Copy link

kjrocker commented Aug 14, 2023

I'm working on an application where all the BE types are compiled into Typescript for me. I would love to be able to enforce type-safety on the models in factory to some extent.

I've got the following code

type User = {
  id: string;
  firstName: string;
  lastName: string;
};

export const db = factory({
  user: {
    id: primaryKey(() => 'abc-123'),
    firstName: () => 'John',
    lastName: () => 'Maverick',
  }
})

Obviously because of the getters I can't just do user: { .... } as User

I also tried defining type Factory<T> = { [P in keyof T]-?: () => T[P] };

With that, I can do user: { .... } as Factory<User>, which works for simple fields, but breaks for anything using primaryKey, manyOf, oneOf, etc.

Any thoughts? I'd really just like to make sure I'm not missing keys when I define new models.

@kettanaito
Copy link
Member

Hi, @kjrocker. This is a valid concern. I'm working on the updated version of this library that covers improves type-safety as well. I can't promise to implement this into the existing version due to the lack of availability. But I do hope to get the new version out sometime soon (~this year).

In the next version, I tended to give the Schema generic to the consumer to specify the model on the type level:

type Models = {
  user: {
    id: Number
  }
  book: {
    id: number
    author: Relationship<'oneOf', 'user'>
  }
}

const db = factory<Models>({
  user: {
    id: id(Number)
  },
  book: {
    id: id(Number),
    author: oneOf('user')
  }
})

This is a bit tricky but I think I'm slowly getting it right internally. If you have some use cases to share please do, they can help a lot!

@pfe-nazaries
Copy link

Same problem here

@yslpn
Copy link

yslpn commented Nov 10, 2024

+++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants