simplex2/models/Keys.moon
2018-03-16 17:00:14 -07:00

23 lines
583 B
Plaintext

import Model from require "lapis.db.model"
import autoload, locate from require "locator"
import settings from autoload "utility"
import random from locate "calc"
uuid = ->
fn = ->
r = random(16) - 1
r = (x == "x") and (r + 1) or (r % 4) + 9
return ("0123456789abcdef")\sub r, r
return ("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx")\gsub "[xy]", fn
class Keys extends Model
@primary_key: "user_id"
@timestamp: true
@create: (values, opts) =>
while true
values.uuid = uuid!
unless Keys\find uuid: values.uuid
continue
super values, opts