How to configure database with default values?

I need to configure the default fields to not insert them one by one in the database.

for example these roles used in ACL:

{
  slug: 'admin',
  name: 'admin',
  description:'unlimited access',
},
{
  slug: 'moderator',
  name: 'moderator',
  description:'Can write  post',
},
{
  slug: 'simple',
  name: 'simple',
  description:'Can read  post',
}
1 Like

That’s what seeds were created for:
https://adonisjs.com/docs/4.1/seeds-and-factories

You don’t need a factory, just create some JSON with your data and create a seed that will insert your JSON contents in the Database.

const RoleModel = use('Models/Role')

const roles = [
{
  slug: 'admin',
  name: 'admin',
  description:'unlimited access',
},
{
  slug: 'moderator',
  name: 'moderator',
  description:'Can write  post',
},
{
  slug: 'simple',
  name: 'simple',
  description:'Can read  post',
}
];

class RoleSeeder {
  async run () {
    await RoleModel.createMany(roles);
  }
}

module.exports = UserSeeder
4 Likes

It worked ,thank you very much for your attention :grinning:

1 Like