auto incremented int?
Is there a auto incremented int capability that is tracked internally by the database i could use? I don’t want to use a custom v.id, I need a int that increments for every new document added as well as having the standard v.id. Is this possible?
5 Replies
1. Add a field for the incremented number.
2. Add an index on that field.
3. When inserting to the table, fetch the first record on that index (ordered descending) to get the last number
4. Set the new record to that number plus 1
If you need this auto incrementing number to be immutable, eg., if the newest record gets deleted, the next record should not reuse it's number, then you'll need to do a bit more to make that happen. But this is your basis.
This will suffice! Thanks.
Is this something the team cares enough about to implement in the future?
this is something we want to support, although it's more likely we will support it with a library like Convex Ents (https://stack.convex.dev/ents) or relationship helpers (https://stack.convex.dev/functional-relationships-helpers) . The library will do what @erquhart describes, just with a cleaner interface.
Convex Ents: Manage your document relationships
Convex Ents is a library that provides: simpler ways to model and query related documents, ability to easily map and filter documents, enforcing uniqu...
Database Relationship Helpers
Traverse database relationships in a readable, predictable, and debuggable way. Support for one-to-one, one-to-many, and many-to-many via utility func...
Note there is a downside to having an auto-incrementing integer: it serializes all inserts into the table, so throughput may go down.
Thanks!