[英]How can I inject knex using inversify?
How can I initialise knex and use Inversify dependency injector to inject it in 如何初始化Knex并使用Inversify依赖注入器将其注入
every where that i needed? 我需要的每个地方?
I have never used Knex
but I'm the author of InversifyJS I will try to help you. 我从未使用过
Knex
但我是Knex
的作者,我将尽力帮助您。
Based on the Knex
docs you can do the following: 根据
Knex
文档,您可以执行以下操作:
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
I also found the TS the types here . 我也在这里找到TS的类型。
I assume that what you want to inject is the knex
instance. 我假设您要注入的是
knex
实例。 You could do the following: 您可以执行以下操作:
import * as Knex from 'knex';
import { Kernel } from "inversify";
// configuration
let configuration: Knex.Config = {
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
};
// instance
let knex: Knex = Knex(configuration);
let kernel = new Kernel();
kernel.bind<Knex>("Knex").toConstantValue(knex);
That way knex would be a singleton. 那样,拐点将是一个单例。 If you don't want it to be a singleton you will need a factory:
如果您不希望它是单身人士,则需要一家工厂:
import * as Knex from 'knex';
import { interfaces, Kernel } from "inversify";
kernel.bind<interfaces.Factory<Knex>>("Factory<Knex>")
.toFactory((ctx: interfaces.Context) => {
return Knex({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
});
You could also separate the config: 您还可以分离配置:
import * as Knex from 'knex';
import { interfaces, Kernel } from "inversify";
kernel.bind<Knex.Config>("KnexConfig").toConstantValue({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
kernel.bind<interfaces.Factory<Knex>>("Factory<Knex>")
.toFactory((ctx: interfaces.Context) => {
let config = context.kernel.get<Knex.Config>("KnexConfig");
return Knex(config);
});
These solutions provide you with a factory: 这些解决方案为您提供了一个工厂:
let knexFactory = kernel.get<interfaces.Factory<Knex>>("Factory<Knex>");
let knex = knexFactory();
The factory can be injected into other classes using an @inject("Factory<Knex>")
annotation. 可以使用
@inject("Factory<Knex>")
批注将工厂注入其他类。
If you don't want to use a singleton and don't want use a factory then you will need a dynamic value injection: 如果您不想使用单例并且不想使用工厂,那么您将需要动态值注入:
kernel.bind<Knex>("Knex").toDynamicValue((context: interfaces.Context) => {
return Knex({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
});
The dynamic value injection will inject a new knex instance. 动态值注入将注入新的knex实例。 Not a singleton and not a factory:
不是单身,也不是工厂:
let knex = kernel.get<Knex>("Knex>");
Please note that I haven't test these suggestions. 请注意,我尚未测试这些建议。 I hope they will lead you in the right direction.
我希望他们会指引您正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.