Reputation: 2876
Is it possible to configure couchdb to generate ids other than the UUID way ? I am using couchdb on a single node, I understand the downside of not using UUID for a distributed environment.
I need something like an auto increment or a shorter id. I have FK references like userIds : ["16011a5704ffe78e6de2afa4b3001d10", "55464a5704ffe78e6de2afa4b3001d10", ... ] which can be too long if I use UUIDS..
Upvotes: 1
Views: 4002
Reputation: 426
You can provide your IDs yourself, even the generated UUIDs are not NO-confilct guaranteed, it just minimizes chances of conflict but since you're concerned about the length, we can shorten it to like 16. First 4 determines the user, next 8 determine the timestamp and final 4 can be random.
Using this multibase converter, you can use a char range of 0-9a-zA-Z
It will be very difficult to break this. Depending on the kind of system you're building and the max number of users you're considering, you can cut this down to as little as 10 (3 prefix, 7 time) or even add more characters (-_)
to your characterlist.
EDIT This is my own implementation On my solution, every user already has a unique prefix once he's added by the admin, and there are not morethan 10 users created by the admin. Depending on the kind of data being created, a unique character is used as a prefix. Thenafter here's my method to convert current time base62. Finally, I append a random character. `
base62 (val) {
const SYMBOLS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
let digs = ''
while (val) {
digs = SYMBOLS.charAt(val % SYMBOLS.length) + digs
val /= SYMBOLS.length
val = parseInt(val)
}
return digs
}
A single user cannot create two records under thesame category at once, and even if he does, it'll be rare to generate same random appended character.
Upvotes: 2
Reputation: 28429
The reason UUIDs are used instead of simple auto-increment values is to reduce the likelihood of conflicts when multiple nodes are in use. CouchDB does have 3 different algorithms in place that you can choose from for generating UUIDs.
If you desire some other id format, you are always able to define your own _id
field, as the uuid generator is only called upon if _id
is undefined.
Upvotes: 0