Don't enforce type restrictions on mysqlEnum and pgEnum to be non-empty arrays #2429
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of changes
mysqlEnum
pgEnum
values
parameter forpgEnum
is not an empty array, if it is, throw an error.Related discussion: #1914
Problem:
mysqlEnum
andpgEnum
both expect the 2nd argument to be of type[string, ...string[]]
which means it ensures that we are not passing an empty array to it. However, this would only work smoothly for arrays but not for enums. To get around the limitation, we always have to use theas
keyword like so:Reason being is because
Object.values(eCategoryType)
becomes a type ofeCategoryType[]
which doesn't fit what we are explicitly checking for[eCategoryType, ...eCategoryType[]]
.I understand that this restriction was put in place to ensure that no one passes an empty array in there. In this case, I think we can add that kind of restriction in-code rather than in-type -- which looking at the
mysqlEnum
code, we are already doing by checkingvalues.length === 0
.Note
I saw on some other files that enums are generally expected as
[string, ...string[]]
, these other places are not covered in this PR, this only aims to fix the issue onmysqlEnum
andpgEnum
.What will work
enums will work -- now without needing to use
as
keyword.Regular arrays will still work
Empty arrays will now work -- previously there will be a TS error. However, in-code validation will prevent this from happening and throw an error.
Patch Package
You can use patch-package to apply the fix right now.
Step 1
yarn add -D patch-package
Step 2
2.1.
cd /to/root/of/your/project
2.2.
touch patches/drizzle-orm+0.31.0.patch
Step 3
Copy the contents below to the file you just created
patches/drizzle-orm+0.31.0.patch
Step 4
yarn patch-package
Step 5
Update your
package.json
file to the following: