Sequence API

Aidbox provides access to PostgreSQL sequences through REST API.

In some situations you want to enumerate your resources with 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.

Create Sequence

You can create named sequence by posting PGSequence resource. We use explicit naming for sequences, so id element is required!

create-sequence.yaml
POST /PGSequence
id: pt_seq

Other PGSequence attributes you can specify:

attr

type

desc

id

ident

sequence name; only lower case, digits and _ are allowed in name

start

integer

initial sequence value (default 1)

cycle

boolean

cycle after reaching sequence max/min value

increment

integer

sequence step (default 1)

maxval

integer

max value to cycle

minval

integer

min value to cycle

Get next value

Now you can transactionally move sequence forward and reserve next value:

POST /PGSequence/pt_seq
# response
id: pt_seq
value: 1

Each call will increment sequence.

You can get range of values by providing count parameter in body.

POST /PGSequence/pt_seq
count: 5
# response 200
id: pt_seq
values: [2,3,4,5,6]

Read current value

You can read current state of sequence without incrementing it with GET /PGSequence/[id]

GET /PGSequence/pt_seq
# response 200
id: pt_seq
value: 1

Reset value

You can set sequence to specific value by PUT /PGSequence/[id]

PUT /PGSequence/pt_seq
value: 30
# response 200
value: 30

This will reset current sequence value to 30, so next get value operation will return 31:

POST /PGSequence/pt_seq
## response 200
value: 31

You can use this endpoint If you want to "update or create" sequence:

PUT /PGSequence/pt_seq
start: 300
# response 201 - created
---
PUT /PGSequence/pt_seq
start: 300
# response 200 - untouched

Destroy Sequence

You can drop sequence with DELETE /PGSequence/[id]

DELETE /PGSequence/pt_seq
# response 200
id: pt_seq
value: 1