Но вы можете добиться этого, добавив Hook в свою модель Lucid.
Сначала создайте customer
схему следующим образом:
"use strict";
const Schema = use("Schema");
class Customer extends Schema {
up() {
this.create("customers", table => {
table.uuid("id").primary();
// Rest of your schema
});
}
down() {
this.drop("customers");
}
}
module.exports = Customer;
Давайте создадим хук с именем CustomerHook
, используяcmd adonis make:hook Customer
"use strict";
const uuidv4 = require("uuid/v4");
const CustomerHook = (exports = module.exports = {});
CustomerHook.uuid = async customer => {
customer.id = uuidv4();
};
Добавьте эти строки в вашу Customer
модель
"use strict";
const Model = use("Model");
class Customer extends Model {
static boot() {
super.boot();
this.addHook("beforeCreate", "CustomerHook.uuid");
}
static get primaryKey() {
return "id";
}
static get incrementing() {
return false;
}
// Rest of the model
}
module.exports = Customer;
При вставке данных клиента по умолчанию будет создан уникальный UUID.
Подробнее об крючке Адониса читайте здесь: https://adonisjs.com/docs/4.1/database-hooks