[Adonis 5] Using UUID in the tables #1416
-
How to tell Model to use uuid instead of incrementing it. In 4.* we do class User extends Model {
static get incrementing () {
return false
}
} But what can be done for 5 ? i'm using beforeCreate hook to set UUID @beforeCreate()
public static async addUUID (user: User) {
user.id = uuid()
} |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 35 replies
-
I'm not start with Adonis 5 yet, but i think u don't need the export default class User extends BaseModel {
@column({
prepare: (value: string) => uuid()
})
public id: string
} Edited: Sorry my bad... this is to transform data... for example. Encryption when creating AND updating with a new value. I think your code for |
Beta Was this translation helpful? Give feedback.
-
Prepare executes every time, Create / Update, i wouldn't wanna generate a new id every time, by following that example @column({
prepare: (value: string) => value ? value : uuid()
}) But that still will be called every time. Now my code |
Beta Was this translation helpful? Give feedback.
-
What database are you using? If you are using Postgres then you can let PG generate UUIDs for you, like it would with auto incrementing IDs and there's no need to mess with hooks UUIDv4 collision rate is so low you won't have to worry about it most likely. If I remember correctly you had to generate 1mil UUID / sec for 7 mil years to get first collision. UUIDs are little bit slower. To search by ID 100 milion times it takes about 2 sec, while with UUIDs it takes about 4 sec in PG (on stupid small synthetic test) UUIDs are bigger, and that's the main problem imo. If you are dealing with a lot of writes, like storing bilions of rows in DB you want to save every possible byte you can. You can save considerable amount of space when not using UUIDs In most applications UUID upsides overweight the downsides by quite a lot. Like not leaking business stats with IDs (how many users you have in system, how many clients, how many invoices you have sent etc), protects from some ID based attacks etc. |
Beta Was this translation helpful? Give feedback.
-
Full example with UUID using Lucid
import { BaseModel, beforeCreate, column } from '@ioc:Adonis/Lucid/Orm'
import { v4 as uuid } from 'uuid'
export default class Author extends BaseModel {
public static selfAssignPrimaryKey = true
@column({ isPrimary: true })
public id: string
@beforeCreate()
public static async createUUID (model: Author) {
model.id = uuid()
}
} |
Beta Was this translation helpful? Give feedback.
-
This is how to use uuid as primary keyFirst i create a migration to run berfore all others export default class Extensions extends BaseSchema {
public async up () {
await this.db.rawQuery('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";')
.knexQuery
}
public async down () {}
} Then my migrations can run uuids as primary key export default class UsersSchema extends BaseSchema {
protected tableName = 'users'
public async up () {
this.schema.createTable(this.tableName, (table) => {
table
.uuid('id')
.primary()
.defaultTo(this.db.rawQuery('uuid_generate_v4()').knexQuery)
table.string('name', 255).notNullable()
})
}
public async down () {
this.schema.dropTable(this.tableName)
}
} My Model import { DateTime } from 'luxon'
import Hash from '@ioc:Adonis/Core/Hash'
import {
column,
BaseModel,
} from '@ioc:Adonis/Lucid/Orm'
export default class User extends BaseModel {
@column({ isPrimary: true })
public id: string
@column()
public name: string
} |
Beta Was this translation helpful? Give feedback.
-
I did this to make sure the uuid is also created when adding an entry to the db outside of adonis
|
Beta Was this translation helpful? Give feedback.
Prepare executes every time, Create / Update, i wouldn't wanna generate a new id every time, by following that example
i can do
But that still will be called every time.
Now my code
beforeCreate
works, it does create a record with UUID, but when we do user.create the return response gives me an incrementing ID instead of thar UUID, in database UUID is stored only, so like earlier it may be a local variable there.