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

Infer types does require id to be set despite it being set as Primary Key and AutoIncrement? #65

Open
chrisspiegl opened this issue Mar 20, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@chrisspiegl
Copy link

I am trying to use d1-orm and the model setup.

I created a model:

new Model(
      {
        D1Orm: orm,
        tableName: 'user_chats',
        primaryKeys: 'id',
        autoIncrement: 'id',
      // uniqueKeys: [['email']],
      },
      {
        id: {
          type: DataTypes.INTEGER,
          notNull: true,
        },
      }
)

My expectation is that then, I can infer the type and it'd know that the id field is kind of optional for inserts and stuff like that?

But doing type UserChats = Infer<typeof models.user_chats>

Followed by a definition if said object:

const userChats: UserChats = {
        }

Shows the type error:

Property 'id' is missing in type '{ }' but required in type 'InferFromColumns<{ id: { type: DataTypes.INTEGER; notNull: true; }; 

I'd hoped that this would just work? Am I missing something? Do I have to update my code somehow to be able to use types with that?

@chrisspiegl chrisspiegl added the bug Something isn't working label Mar 20, 2023
@chrisspiegl
Copy link
Author

Adding to this:

in a similar way, even when a value has a defaultValue set, it still forces me to define the property when creating a new object.

@Skye-31
Copy link
Contributor

Skye-31 commented Mar 20, 2023

The library will make any column that has notNull: true be required. I had played around with ignoring it when a default value is set too, however that wasn't working out. If you're able to improve the experience of Infer, please do open a pull 🙂

For now, if you want to ignore a particular key, I'd use Omit<UserChats, 'id'> to remove it from the type.

@chrisspiegl
Copy link
Author

Thanks, for the update. I myself am kind of new to TypeScript and thus a pull request from me is unlikely any time soon from me on this topic.

But a thought would be this:

Isn't it possible to mark types as optional on a type/interface?

export interface Env {
  title?: string
}

Maybe that could be done with the Infer method?

@Skye-31
Copy link
Contributor

Skye-31 commented Mar 21, 2023

It is possible, and that's what happens when notNull is falsey. It's rather difficult to extend that type to pick up default values, which is the only reason it doesn't currently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants