DEV Community

DeVoresyah ArEst
DeVoresyah ArEst

Posted on

Using UUID in AdonisJS v5

Just want to share simple tips how to using UUID in AdonisJS v5. This will help you to define a migration file when using uuid data type and give the default value for it. So, you will no need to use @beforeCreate() on model file.

Before creating the column, you need to create extension of uuid-ossp first by using this query:

this.db.rawQuery('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').knexQuery
Enter fullscreen mode Exit fullscreen mode

Next, define the uuid column and give default value:

table.uuid('id', { primaryKey: true }).defaultTo(this.db.rawQuery('uuid_generate_v4()').knexQuery)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)