In some situations you want to enumerate your resources with an increasing counter or use global sequences for your app needs. Aidbox provides handy access to PostgreSQL sequences through REST API. Values generated by this API are transactional and safe to use in concurrent environment.
You can create named sequence by posting PGSequence resource. We use explicit naming for sequences, so the id element is required!
create-sequence.yamlPOST /PGSequenceid: pt_seq
You can specify other PGSequence attributes:
sequence name; only lower case, digits and _ are allowed in name
initial sequence value (default 1)
cycle after reaching sequence max/min value
sequence step (default 1)
max value to cycle
min value to cycle
Now you can move sequence forward transactionally and reserve the next value:
POST /PGSequence/pt_seq# responseid: pt_seqvalue: 1
Each call will increment the sequence.
You can get a range of values by providing the count parameter in the body.
POST /PGSequence/pt_seqcount: 5# response 200id: pt_seqvalues: [2,3,4,5,6]
You can read the current state of sequence without incrementing it with
GET /PGSequence/pt_seq# response 200id: pt_seqvalue: 1
You can set the sequence to a specific value by
PUT /PGSequence/pt_seqvalue: 30# response 200value: 30
This will reset the current sequence value to 30, so next get value operation will return 31:
POST /PGSequence/pt_seq## response 200value: 31
You can use this endpoint if you want to update or create sequence:
PUT /PGSequence/pt_seqstart: 300# response 201 - created---PUT /PGSequence/pt_seqstart: 300# response 200 - untouched
You can drop the sequence with
DELETE /PGSequence/pt_seq# response 200id: pt_seqvalue: 1