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

Support for BigInt field types #21

Merged
merged 7 commits into from
Sep 1, 2023
Merged

Conversation

jonbarrow
Copy link
Contributor

Mongoose supports BigInts as a schema field type, and this library already works with them as-is. The only thing preventing this was a check for only Number of the field type

Copy link
Member

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test actually verifying BigInt works~~, also, you may wanna check utils.isNumber too~~

Edit: nevermind, i thought this was a PR for typegoose (not auto-increment)

@jonbarrow
Copy link
Contributor Author

I tested this in an actual project I'm working on and it worked just fine, but I'll add a proper test here later today

@jonbarrow
Copy link
Contributor Author

jonbarrow commented Aug 24, 2023

After looking into this further, it looks like Mongoose doesn't expose BigInt in their type defs (see here for reference), which is why these tests are failing. However Mongoose does support this, as seen here as well as the documentation

The way I had tested this originally in my personal project was, admittedly, quick and hacky. I just modified the transpiled JavaScript to add these checks, and it works just fine

In fact you can verify that this type does exist and is accessible by simply logging it
Screenshot from 2023-08-24 14-02-28

The issue is only that TypeScript thinks the property BigInt doesn't exist, when it does

Not entirely sure how to proceed from here. This is a type defs issue now, not a functionality issue. I suppose I'll file an issue on the Mongoose side and then check back later

Copy link
Member

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks better now, but the snapshots would need to be updated (yarn run test -u, and verify it is what it should be)

also it looks like there are only tests for BigInt and AutoIncrementID and not AutoIncrementSimple?

@jonbarrow
Copy link
Contributor Author

Ah my apologies. I have never used yarn or jest before, and the test which failed was named with should Error, so I assumed it failing was expected. Thank you

@jonbarrow
Copy link
Contributor Author

There were some errors which cropped up while adding the AutoIncrementSimple when count would become negative and newCount was a BigInt. To fix this I had to change count to be a BigInt internally and cast from Number if it wasn't already

All tests pass now, though

@codecov
Copy link

codecov bot commented Aug 25, 2023

Codecov Report

Patch coverage is 90.90% of modified lines.

Files Changed Coverage
src/autoIncrement.ts 90.90%

📢 Thoughts on this report? Let us know!.

Copy link
Member

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, code looks relatively good to me, though there are still some things i noticed:

  • type AutoIncrementIDOptions options startAt and incrementBy do not list bigint as supported
  • type AutoIncrementOptionsSimple options incrementBy do not list bigint as supported
  • i would rather not change the IDSchema's type, but i guess it should not be a problem (old stuff should get automatically get converted to bigint from number)

src/autoIncrement.ts Outdated Show resolved Hide resolved
@hasezoey
Copy link
Member

There were some errors which cropped up while adding the AutoIncrementSimple when count would become negative and newCount was a BigInt.

also could you explain more on what the problem with negative was?

@jonbarrow
Copy link
Contributor Author

My apologies for the late reply as well

type AutoIncrementIDOptions options startAt and incrementBy do not list bigint as supported
type AutoIncrementOptionsSimple options incrementBy do not list bigint as supported

I'm not sure what you mean here? Where should this be listed at? The types themselves are already updated to number | bigint for each field you listed here

i would rather not change the IDSchema's type, but i guess it should not be a problem (old stuff should get automatically get converted to bigint from number)

In my testing yes, everything does get converted correctly

also could you explain more on what the problem with negative was?

I do apologize but I've forgotten the specifics now, I've been prepping for the hurricane about to hit my state so that was the last thing on my mind 😅

@hasezoey
Copy link
Member

I'm not sure what you mean here? Where should this be listed at? The types themselves are already updated to number | bigint for each field you listed here

i am sorry, it seems i look at some other versions of the code and didnt notice, all is good here, aside from AutoIncrementIDTrackerSpec missing the switch to bigint

@@ -39,13 +39,18 @@ export function AutoIncrementSimple(
throw new Error(`Field "${field.field}" does not exists on the Schema!`);
}
// check if the field is an number
if (!(schemaField instanceof mongoose.Schema.Types.Number)) {
throw new Error(`Field "${field.field}" is not an SchemaNumber!`);
if (schemaField.instance !== 'Number' && schemaField.instance !== 'BigInt') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this still be used once upgraded to mongoose 7.5, which has typescript types for BigInt?

@hasezoey hasezoey merged commit 2941446 into typegoose:master Sep 1, 2023
hasezoey added a commit that referenced this pull request Sep 1, 2023
hasezoey pushed a commit that referenced this pull request Sep 1, 2023
* Support for BigInt field types

* refactor: switch from instanceof to field.instance for type checks to support BigInts

* test: added BigInt tests

* refactor: allow incrementBy and startAt to be BigInt, change count to BigInt and cast

* test: added Simple tests and update snapshots

* refactor: move incrementBy and startAt BigInt conversions to pre save callback

* fix: type AutoIncrementIDTrackerSpec count is now bigint
@hasezoey
Copy link
Member

hasezoey commented Sep 1, 2023

i have merged this PR for now, but did a mistake and had to force-push it (bad commit message).

i would still like a answer to #21 (comment)

@hasezoey
Copy link
Member

hasezoey commented Sep 1, 2023

Released in 3.5.0

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

Successfully merging this pull request may close these issues.

2 participants