-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
Nullable columns in typescript interface declaration #553
Comments
I run into this problem too. I made more changes to #547. Now by default, all nullable columns will use There's no need to use the typeOverrides option, but it is in the branch already when I added the |
@kurochin143 Thanks for your reply and all your efforts on preparing the PR. I tested your PR and I can see that you have replaced the optional tag with Consider following scenario:
now assume that we have a record in DB as below:
If I want to use This will require an additional row fetch from the DB which would be redundant in most of the scenarios. What do you think if you just add |
The You should still be able to pass |
My bad! You are absolutely correct about the parameters of the However, I still believe this is a breaking change and should be avoided for this PR. For example, we are setting fields to undefined throughout our code in order to make sure that the returning object would not contain that specific data (e.g. password fields). On the other hand, we do not want to use separate types for that purpose. The
We might also need to write additional interfaces/types to overcome (1) which is again a lot of unnecessary code that would be hard to maintain. Please correct me if I am wrong. |
I assume that only a handful of fields are needed to be optional. You can override the fields that you want optional using There are also ways to make some fields optional with typescript wrappers, which is what I use. Like https://stackoverflow.com/questions/43159887/make-a-single-property-optional-in-typescript. With that wrapper, you can maintain strict typings of every function return type, rather than having optional fields that will never really be undefined for that particular function call. I think that sequelize-auto generated class should reflect the type of the database rather than the upper layer. Though, sequelize can exclude any field. But with sequelize exclusion, every field would have been optional. But if this is not good enough then I can add another option |
Ok, it is a breaking change so I added the Instruction is in #547. I still stand with my previous comment of using |
I totally agree with you that the generated classes by sequelize-auto should reflect the actual types of the DB. Now that you have added the Thanks for all your help. @steveschmitt Could you please review/merge the #547 PR? |
Problem
I can not set a nullable column to null using
.update
function after upgradingsequelize
to6.6.2
.Executing
user.update( { email: null });
, results in following error:Hence, I believe null type must be added into attributes declaration.
Example
Consider following schema as an example:
Output
The generated interface would be like below:
Expected output
Disclaimer
I might be misunderstanding something here. So if there are any solutions to this problem please let me know. Additionally, PR#547 might be a workaround to this problem.
Environment
sequelize version:
6.6.2
sequelize-auto version:
0.8.4
dialect:
mysql
node version:
16.6.1
npm version:
7.21.1
CMD:
sequelize-auto -o "./src/models" -l ts -e mysql
The text was updated successfully, but these errors were encountered: