const mixinafMorphia::IntSequences
afMorphia::IntSequences
(Service) - Creates sequential sequences of Ints
as an alternative to using BSON ObjectIds
.
It works by creating MongoDB collection (called IntSequences
by default) where it stores the last ID for an entity to be used. When a new ID is requested an atomic findAndUpdate operation is run on the collection to both increment and return the relevant document.
By keeping the last IDs stored in the database it ensures the IDs are persisted between system restarts and that multiple clients can generate unique IDs.
While there is an overhead in generating new IDs, Int
IDs have the advantage of using less space in indexes, being easier to work with in web applications and they're generally nicer to look at!
- collectionName
abstract Str collectionName()
The name of MongoDB collection used to store the Int sequence data.
- drop
abstract Void drop()
Delete the
IntSequences
collection. Safe operation, does not throw Err if the collection does not exist.- nextId
abstract Int nextId(Type entityType)
Returns the next
Int
for the given entity. The sequence name is derived from the class name and@Entity
facet.Throws
ArgErr
if the given type does not have the@Entity
facet.- nextInt
abstract Int nextInt(Str seqName)
Returns the next
Int
for the given sequence name.- reset
abstract Void reset(Str seqName)
Resets the last ID to zero.
- resetAll
abstract Void resetAll()
Resets all the last IDs to zero.