-
Notifications
You must be signed in to change notification settings - Fork 0
Initializers
Defined in Esrol-initializer and implemented in Esrol-server-app
Initializers provide an opportunity to configure and set up dependency injections in your application on startup. They're included before middlewares, routes, and sockets.
Initializers are instatinated successively in an asynchronous manner and the order is determined by the priority property - the lower, the better. Each initializer must be es6 class or constructor and should have static priority property with int value.
app/initializers/mysql.js
'use strict';
let mysql = require('mysql2');
module.exports = class MySQL {
static get priority() {
return 1;
}
constructor() {
// create mysql client
}
};Esrol will wait for all initializers to be resolved before any server is created, even if you have I/O code - just make sure to return a promise.
app/initializers/mysql.js
'use strict';
let mysql = require('mysql2');
module.exports = class MySQL {
static get priority() {
return 1;
}
constructor() {
return new Promise((resolve, reject) => {
// create mysql client
});
}
};However, if you have the following scenario:
app/initializers/mysql.js
'use strict';
let mysql = require('mysql2');
module.exports = class MySQL {
static get priority() {
return 1;
}
constructor() {
return new Promise((resolve, reject) => {
// create mysql client
});
}
};And
app/initializers/redis.js
'use strict';
let redis = require('redis');
module.exports = class Redis {
static get priority() {
return 2;
}
constructor() {
return new Promise((resolve, reject) => {
// create redis client
});
}
};The execution will be as follow:
- MySQL initializer is instatinated.
- Redis initializer is instatinated.
- MySQL / Redis is resolved.
- MySQL / Redis is resolved.
- Server(s) are created.
In short, Esrol will instatinate each initializer, but will not wait to be resolved in order to instatinate the next initializer.