How to write mysql views and triggers with adonis?

my db server is mysql

i can’t see in the doc how to write with adonis:

  1. mysql views
  2. mysql triggers

advises/hints ?

I don’t think they are AdonisJs issues, they are database concerns. Can you give a concrete example of what you are trying to achieve?

Hi @student

You can write them into migrations and use raw database queries in there
Inside migration file

/** @type {import('@adonisjs/lucid/src/Database')} */
const Database = use('Database')
...
up () {
  Database.raw('do things')
}

https://adonisjs.com/docs/4.1/query-builder#_raw_queries

1 Like

Yeah @McSneaky has said, use the migration files to setup your views.

2 Likes

note that you need to use async/await when you do that. like

...
  async up () {
    await Database.raw(`CREATE VIEW v_routes AS 
                    SELECT routes.id, origin.name as origin_name, dest.name as destination_name, 
                         routes.from_location_id, routes.to_location_id
                    FROM routes
                    JOIN locations as origin 
                      ON routes.from_location_id = origin.id
                    JOIN locations as dest 
                      ON routes.to_location_id = dest.id;`)
  }

  async down () {
    await Database.raw('DROP VIEW IF EXISTS v_routes;')
  }