Strapi V4 - how to bootstrap some data

System Information
  • Strapi Version: 4.2.3
  • Operating System: ArchLinux
  • Database: Postgres 14.4
  • Node Version: 12.17.0
  • NPM Version: 7.20.3
  • Yarn Version: not used

Hi there, I’m newbie in Strapi and I’d like to use the bootstrap function to:

  • create an admin user if it doesn’t exist
  • create some sample data

I’ve been reading the documentation, but failed to find what’s expected. I’ve been searching in forums, but it seems to have big differences between versions.

This is the snippet I’m trying to run (unsuccessfully):

//  src/index.js
'use strict';

module.exports = { 
  register(/*{ strapi }*/) {}, 

  bootstrap({ strapi }) {
    const params = { 
      username: 'admin',
      email: 'admin@email.com',
      password: 'Admin1234',
      blocked: false
    }   
    const superAdminRole = strapi.admin.services.role.getSuperAdmin();
    params.password = strapi.admin.services.auth.hashPassword(params.password);
    params.roles = [superAdminRole._id]
    strapi.query("plugin::users-permissions.user").create({...params});
  },  
};

Could you give any hint on where to continue?
Thanks for your help!!

I’ve been able to give some steps reading the source code. Now my code looks li ke this:

  async bootstrap({ strapi }) {
    // create user admin if it doesn't exist
    await strapi.admin.services.role.createRolesIfNoneExist();
    const superAdminRole = await strapi.db.query('admin::role').findOne({where: {code: 'strapi-super-admin'}
    const superAdmin = await strapi.db.query('admin::user').findOne({where: {username: 'admin'}});
    if (!superAdmin) {
      const params = { 
        username: 'admin',
        email: 'admin@email.com',
        blocked: false,
        confirmed: true
      }   
      params.password = await strapi.admin.services.auth.hashPassword("Admin1234");
      params.roles = [superAdminRole._id]
      await strapi.db.query("admin::user").create({
        data: {...params},
        populate: ['roles']
      }); 
    }   
  },  
};

However, this is not working properly, and even the logs complain:

Admin UI built successfully
[2022-07-19 11:33:13.148] warn: Your application doesn't have a super admin user.
[2022-07-19 11:33:13.205] warn: Some users (1) don't have any role.

Any hint on how to properly relate the user with its role so after this bootstrap we can say there is a super-admin user?

Thanks for your time! :pray:

@yami how did you solve it finally?

Hi @yami ,

Thanks for your post. I took your suggested code and applied a couple of tweaks and it worked for me on v4.4.1:

// src/index.ts
export default {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register(/*{ strapi }*/) {},

  /**
   * An asynchronous bootstrap function that runs before
   * your application gets started.
   *
   * This gives you an opportunity to set up your data model,
   * run jobs, or perform some special logic.
   */
   async bootstrap({ strapi }) {
    // create user admin if it doesn't exist
    await strapi.admin.services.role.createRolesIfNoneExist();
    const superAdminRole = await strapi.db.query('admin::role').findOne({where: {code: 'strapi-super-admin'}});
    const superAdmin = await strapi.db.query('admin::user').findOne({where: {username: 'admin'}});
    if (!superAdmin) {
      const params = { 
        username: 'admin',
        email: 'admin@email.com',
        blocked: false,
        isActive: true,
        confirmed: true,
        password: null,
        roles: null
      }   
      params.password = await strapi.admin.services.auth.hashPassword("Admin1234");
      params.roles = [superAdminRole.id]
      await strapi.db.query("admin::user").create({
        data: {...params},
        populate: ['roles']
      }); 
    } 
  }
};

I hope this helps.

1 Like