Insert admin migration

A migration to create an admin user.

Now, let's create a blank migration to insert an admin user in the database.

yarn migration:create src/database/migrations/insert-admin

And then, write the query to insert him manually.

export class insertAdminTimestamp implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      INSERT INTO "user"
        ("name", "email", "phone", "password", "role")
      VALUES
        ('admin', 'admin@mail.com', '955555555', '$2a$10$wD8NCyhfdIWhVY4BAL9pTOH/HIuEvb8Rv6ZcgdY3LH9P.rtu2LE3i', 'ADMIN')
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      DELETE FROM "user"
      WHERE "email" = 'admin@mail.com'
    `);
  }
}

The password in the migration is already hashed, otherwise the admin would not be able to log in. It corresponds to the password AAaa11!!. It was obtained from the bcrypt generator.

Commit - Inserting admin user in migration

Last updated