Build a blog with Areto Node.js framework

Primary init

In most applications, after installation, you must make an primary database initialization. It is necessary to add an administrator and other params. Start initialization by a separate script.

bin/migrate.js

(async ()=> {
  const application = require('../module');
  const CommonHelper = require('a2/helper/CommonHelper');
  try {
    let data = CommonHelper.parseArguments(process.argv);
    await application.init('development');
    await application.migrate(data.action, data.classes);
  } catch (err) {
    application.log('error', 'Migration error', err);
  }
  process.exit();
})();

Create the migrations directory and add the Init class to it. The class will create the Administrator user with the admin role. In the future, administrator can sign in to the application control module and create other users by the web interface.

migrations/Init.js

const Base = require('areto/db/Migration');
const User = require('../model/User');
module.exports = class Init extends Base {
  async apply () {
    await this.db.createIndex('user', [{email: 1}, {unique: true}]);
    let user = new User;
    user.setAttrs({
      name: 'Administrator',
      email: 'a@a.a',
      role: 'admin',
      password: '123456'
    });
    await user.save();
  }
};
module.exports.init(module);

You can run the script either from the command line, go to the blog directory:

node bin/migrate.js apply migrations/Init

Either using Visual Studio Code:


Name: Init Areto Blog
Working directory: areto-blog
Javascript file: bin/migrate.js
Application parameters: apply migrations/Init