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

[Feature Request] Tenant-based/partitioned autoincrement #1976

Open
sajadmh opened this issue Feb 5, 2025 · 0 comments
Open

[Feature Request] Tenant-based/partitioned autoincrement #1976

sajadmh opened this issue Feb 5, 2025 · 0 comments

Comments

@sajadmh
Copy link

sajadmh commented Feb 5, 2025

Is your feature request related to a problem? Please describe.

You can define @unique() @default(autoincrement()) on a field, but you cannot autoincrement with a constraint like tenantId or organizationId.

  • This is useful for things like accounting invoices that need a unique id (database) and a human-readable referenceId (UI) for users to reference.
  • It's also useful for jobs, e.g. Workday's "job requisition ID" (JR-ID) that candidates and recruiters can reference in a readable way, e.g. "J-012345".

Companies often require an internal increment to know which invoice, job, opening they might be on and what the next increment will be.

Describe the solution you'd like
Create a Zenstack feature that handles autoincrement on a field, taking a partition to autoincrement against. Proposed syntax:

model Invoice {
  id          String  @id @default(uuid())
  tenantId    String

  // proposal 1:
  referenceId Int     @autoincrement(partition: tenantId)
  // or proposal 2:
  referenceId Int     @autoincrement(by: tenantId)
  
  @@unique([referenceId, tenantId])
}

Simply increment to the last number, even if records are deleted in between (e.g. 1-2-[deleted]-4-5 increment to 6).

Describe alternatives you've considered

  • Originally I created a postgres sequence + nextval() but this requires setup for each tenant.
  • Database triggers
  • Dedicated sequencing table
  • Application-level sequence management (hacky findFirst... orderBy referenceId desc... return last result, increment by +1...)

References

https://stackoverflow.com/questions/41336686/auto-increment-considering-tenant-id
https://stackoverflow.com/questions/51825936/auto-increment-ids-starting-at-1-per-tenant-in-a-single-database

@sajadmh sajadmh changed the title [Feature Request] Tenant-based autoincrement (unique int autoincrement) [Feature Request] Tenant-based autoincrement Feb 5, 2025
@sajadmh sajadmh changed the title [Feature Request] Tenant-based autoincrement [Feature Request] Tenant-based/partitioned autoincrement Feb 5, 2025
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

No branches or pull requests

1 participant