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

update to use bigint for resourceId and blobId #365

Merged
merged 1 commit into from
Jul 29, 2024

Conversation

harishv7
Copy link
Contributor

@harishv7 harishv7 commented Jul 25, 2024

TL;DR

This pull request updates the ID fields for the Resource, and Blob models to use BigInt instead of Int. Corresponding changes have been made to Prisma schema, migration files, TypeScript types, and various parts of the application code to handle this change.

This is in relation to introducing versions later!

What changed?

  • Updated Prisma schema to change ID types.
  • Created migration script for the database schema changes.
  • Modified TypeScript types to reflect new ID types.
  • Updated application code to handle BigInt IDs.

Note:

  1. We still use Number in JS throughout, so we are technically limited by the max int support in JS which is 2^53 - 1.
  2. If you look at the generatedTypes, you will see that bigints are output as String. This requires a cast to and fro number and string. For example, Zod might validate the incoming payload field as a number. But when we query or mutate DB we will need to convert this into a String.

How to test?

  1. Run the migration script to update the database schema.
  2. Ensure that the application builds without errors.
  3. Execute the application and ensure that all parts work as expected, particularly those dealing with Resource, and Blob models.
  4. Run existing tests to ensure they pass, and add new tests if necessary.

Why make this change?

To accommodate larger datasets and future-proof the application, enabling the use of BigInt for IDs in critical models like Resource, and Blob. This change reduces the risk of ID collisions and supports the scaling of the application.

As shared, a quick back-of-envelope calculation shows:

We now have a 1-many relationship for resources and versions. Estimating worst case scenario:
Max running order of integer = 5000 sites, 14k pages each site, each page has 1000 versions = 5k * 14k * 1k = 70,000,000,000
Current max supported int = 4 bytes = 32 bits = 2,147,483,647
But JS supports up to 53 bits (2^53 - 1)
Might be good to consider using BigInt for all our IDs - 8 bytes = 64 bits

But in our app, we will only use a max of 53 bits (JS's max INT value)

Copy link

vercel bot commented Jul 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
isomer-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 28, 2024 8:29am

Copy link
Contributor Author

harishv7 commented Jul 25, 2024

@harishv7 harishv7 marked this pull request as ready for review July 25, 2024 08:24
@harishv7 harishv7 requested a review from a team as a code owner July 25, 2024 08:24
Copy link
Contributor

@dcshzj dcshzj left a comment

Choose a reason for hiding this comment

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

Still not very sure what's the value of using bigint versus using strings directly (since unlikely we will be doing arithmetic operations on the ID), but there might be some other optimisations done by Postgres so I think it should be okay.

apps/studio/prisma/seed.ts Show resolved Hide resolved
@harishv7 harishv7 merged commit 6c85067 into main Jul 29, 2024
16 checks passed
@harishv7 harishv7 deleted the 07-25-update_to_use_bigint_for_resourceid_and_blobid branch July 29, 2024 05:20
@seaerchin seaerchin mentioned this pull request Jul 30, 2024
2 tasks
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

Successfully merging this pull request may close these issues.

2 participants