Using UUID in AdonisJS v5
DeVoresyah ArEst

DeVoresyah ArEst @devoresyah

About: Nothing impossible with Javascript~!

Location:
Indonesia
Joined:
Nov 20, 2018

Using UUID in AdonisJS v5

Publish Date: Jun 11 '23
3 0

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

Comments 0 total

    Add comment