@anyme/anymejs
Version:
95 lines (90 loc) • 4.12 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var inversify = require('inversify');
var index$1 = require('./node_modules/.pnpm/@anyme_cache@0.1.3/node_modules/@anyme/cache/lib/index.js');
var index = require('./config/index.js');
var constants = require('./utils/constants.js');
var inversifyAdapter = require('./utils/inversify-adapter.js');
var logger = require('./core/logger.js');
var service = require('./core/service.js');
var app = require('./core/app.js');
var db = require('./core/db.js');
var redis = require('./core/redis.js');
class DI {
container = new inversify.Container({
autobind: true,
defaultScope: "Singleton",
});
registered = false;
register = (app$1) => {
if (this.registered)
return this.container.getAsync(constants.SYMBOLS.App);
this.container
.bind(constants.SYMBOLS.IocAdapter)
.toConstantValue(new inversifyAdapter.default(this.container));
this.container.bind(constants.SYMBOLS.Config).to(index.Config);
this.container
.bind(constants.SYMBOLS.CoreConfig)
.toResolvedValue(async (config) => await config.loadCore(), [constants.SYMBOLS.Config]);
this.container
.bind(constants.SYMBOLS.Logger)
.toResolvedValue((config) => new logger.WinstonLogger(config.logger).logger, [constants.SYMBOLS.CoreConfig]);
this.container
.bind(constants.SYMBOLS.Cache)
.toResolvedValue((config) => new index$1.Cache(config.cache.options), [constants.SYMBOLS.CoreConfig]);
this.container
.bind(constants.SYMBOLS.DB)
.toResolvedValue((logger, config) => new db.DBManager(logger, config.db), [constants.SYMBOLS.Logger, constants.SYMBOLS.CoreConfig]);
this.container
.bind(constants.SYMBOLS.Redis)
.toResolvedValue((config, logger) => new redis.RedisManager(config.redis, logger), [constants.SYMBOLS.CoreConfig, constants.SYMBOLS.Logger]);
this.container.bind(constants.SYMBOLS.Context).toResolvedValue((config, logger, db, redis, cache) => ({
config,
logger,
db,
redis,
cache,
}), [constants.SYMBOLS.Config, constants.SYMBOLS.Logger, constants.SYMBOLS.DB, constants.SYMBOLS.Redis, constants.SYMBOLS.Cache]);
this.container
.bind(constants.SYMBOLS.Service)
.toResolvedValue((iocAdapter, context) => new service.Service(app$1, context, iocAdapter), [constants.SYMBOLS.IocAdapter, constants.SYMBOLS.Context]);
this.container.bind(constants.SYMBOLS.App).to(app.Anyme);
this.registered = true;
return this.container.getAsync(constants.SYMBOLS.App);
};
inject(identifier, options) {
if (typeof identifier === "string" &&
[
"SSE",
"Logger",
"Redis",
"Cache",
"DB",
"Config",
"Repository",
].includes(identifier)) {
if (identifier === "Repository") {
const entityName = typeof options.entity === "function"
? options.entity.name
: options.entity.name || String(options.entity);
const token = Symbol.for(`Repository_${entityName}`);
if (!this.container.isBound(token))
this.container
.bind(token)
.toResolvedValue((dataSource) => {
return dataSource
.get(options?.dataSource ?? "default")
?.getRepository(options.entity);
}, [constants.SYMBOLS.DB])
.inRequestScope();
return inversify.inject(token);
}
return inversify.inject(constants.SYMBOLS[identifier]);
}
else
return inversify.inject(identifier);
}
}
var di = new DI();
exports.default = di;
//# sourceMappingURL=inversify.config.js.map