Sequence API
Aidbox provides access to PostgreSQL sequences through REST API.
In some cases 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.

Create Sequence

You can create named sequence by posting PGSequence resource. We use explicit naming for sequences, so the id element is required!
create-sequence.yaml
1
POST /PGSequence
2
Accept: text/yaml
3
Content-Type: text/yaml
4
​
5
id: pt_seq
Copied!
You can specify other PGSequence attributes:
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 move sequence forward transactionally and reserve the next value:
1
POST /PGSequence/pt_seq
2
Accept: text/yaml
3
​
4
# response
5
id: pt_seq
6
value: 1
Copied!
Each call will increment the sequence.
You can get a range of values by providing the count parameter in the body.
1
POST /PGSequence/pt_seq
2
Accept: text/yaml
3
Content-Type: text/yaml
4
​
5
count: 5
6
​
7
# response 200
8
id: pt_seq
9
values: [2,3,4,5,6]
Copied!

Read current value

You can read the current state of sequence without incrementing it with GET /PGSequence/[id]
1
GET /PGSequence/pt_seq
2
Accept: text/yaml
3
​
4
# response 200
5
id: pt_seq
6
value: 1
Copied!

Reset value

You can set the sequence to a specific value by PUT /PGSequence/[id]
1
PUT /PGSequence/pt_seq
2
Accept: text/yaml
3
Content-Type: text/yaml
4
​
5
value: 30
6
​
7
# response 200
8
value: 30
Copied!
This will reset the current sequence value to 30, so next get value operation will return 31:
1
POST /PGSequence/pt_seq
2
Accept: text/yaml
3
​
4
## response 200
5
value: 31
Copied!
You can use this endpoint if you want to update or create sequence:
1
PUT /PGSequence/pt_seq
2
Accept: text/yaml
3
Content-Type: text/yaml
4
​
5
start: 300
6
​
7
# response 201 - created
8
​
9
---
10
PUT /PGSequence/pt_seq
11
Accept: text/yaml
12
Content-Type: text/yaml
13
​
14
start: 300
15
​
16
# response 200 - untouched
Copied!

Destroy Sequence

You can drop the sequence with DELETE /PGSequence/[id]
1
DELETE /PGSequence/pt_seq
2
Accept: text/yaml
3
​
4
# response 200
5
id: pt_seq
6
value: 1
Copied!