I have a node application where I am using rethinkdb as database. The id rethinkdb generates for each document is not human readable and not suitable for real world reference purpose.
For example, each user I create I want to have a short integer id which I can use to refer that user in real world. The id I get from database is not suitable for that.
What would be a concurrent safe way to generate a short unique integer id ?
Building off of @Suvitruf 's answer you can accomplish this in pure reql using the following where "last" is a table that contains the last id. Modifications made here are specifically for parallel updates. Instead of individual id updates for each update, the number block is reserved in 1 insert that is part of the query. Please award any points to his answer.
r([{ foo: 'a' }, { foo: 'b'}])
.do(records => {
return r.table('lastId').get('last')('current')
.do(current => {
return r.table('lastId').get('last').update({ current: current.add(records.count()) })
.do(() => {
return r.range(0, records.count()).map(idx => {
return records.nth(idx).merge(rec => {
return { id: current.add(idx) }
})
})
})
})
})
.coerceTo('array')
.do(records => {
return r.table('ids').insert(records)
})