TypeOrm - 如何将连接用作具有类型的独立对象? [英] TypeOrm - How to use connection as standalone object with types?
问题描述
不工作的代码只是为了说明我想要实现的目标
一些连接文件
import { ConnectionManager } from 'typeorm';const c = new ConnectionManager();//用户ormconfig.conf文件export const connection = c.createAndConnect();
在某些模型中使用
@Entity()@Table("年收入")出口类年收入{@PrimaryGeneratedColumn()身份证号码;@Column({ 长度:75 })变体:字符串;@Column("int")排序:数字;@柱子()is_active:布尔值;}
稍后在代码中的某个地方,我想与所有类似的方法建立联系
import { connection } from 'someconnection';从实体"导入{年收入};//这里有一些代码api.get('/收入', async(ctx) => {ctx.body = await connection.getRepository(AnnualIncome).find();});
通常我从 tsc
得到一个错误,在 connection
中找不到 .getRepository()
方法.但如果我这样做:
import { connection } from 'someconnection';从实体"导入{年收入};//这里有一些代码api.get('/收入', async(ctx) => {ctx.body = await connection.then(异步连接 => {return await connection.getRepository(AnnualIncome).find();}});
上面的代码适用于定义,tsc
不会抱怨不存在的方法.我想避免额外的定义 connection.then()
并使用
type<中定义的所有方法获得纯 connection
/p>
谢谢.
只需在引导应用程序时使用 createConnection
方法来创建连接.稍后您可以使用 getConnection()
方法从任何地方访问您的连接:
import {AnnualIncome} from 'entities';从'typeorm'导入{createConnection,getConnection};//在你的应用程序中的某个地方,最好是你引导 express 和其他东西的地方创建连接();//从 ormconfig.json 读取配置或将它们传递到这里//这里有一些代码api.get('/收入', async(ctx) => {ctx.body = await getConnection().getRepository(AnnualIncome).find();});
你也可以简单地使用 getRepository
方法,也可以在任何地方使用:
import {AnnualIncome} from 'entities';从'typeorm'导入{getRepository};//这里有一些代码api.get('/收入', async (ctx) => {ctx.body = await getRepository(AnnualIncome).find();});
Not working code just to illustrate what I'm trying to achieve
Some connection file
import { ConnectionManager } from 'typeorm';
const c = new ConnectionManager();
// user ormconfig.conf file
export const connection = c.createAndConnect();
using in some model
@Entity()
@Table("annual_incomes")
export class AnnualIncome
{
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 75 })
variant: string;
@Column("int")
sort: number;
@Column()
is_active: boolean;
}
later somewhere in the code I want to get connection with all methods something like that
import { connection } from 'someconnection';
import { AnnualIncome } from 'entities';
// some code here
api.get('/incomes', async(ctx) => {
ctx.body = await connection.getRepository(AnnualIncome).find();
});
Usually I'm getting an error from tsc
that .getRepository()
method was not found in connection
. But if I do something like that:
import { connection } from 'someconnection';
import { AnnualIncome } from 'entities';
// some code here
api.get('/incomes', async(ctx) => {
ctx.body = await connection.then(async connection => {
return await connection.getRepository(AnnualIncome).find();
}
});
the above code works with definitions and tsc
does not complain about unexisting method.
I'd like to avoid an extra definition connection.then()
and get plain connection
with all methods defined in <Connection>
type
Thanks.
just use createConnection
method to create your connection when you bootstrap your application. Later you can access your connection from anywhere using getConnection()
method:
import { AnnualIncome } from 'entities';
import { createConnection, getConnection } from 'typeorm';
// somewhere in your app, better where you bootstrap express and other things
createConnection(); // read config from ormconfig.json or pass them here
// some code here
api.get('/incomes', async(ctx) => {
ctx.body = await getConnection().getRepository(AnnualIncome).find();
});
Also you can simply use getRepository
method also avalible from anywhere:
import { AnnualIncome } from 'entities';
import { getRepository } from 'typeorm';
// some code here
api.get('/incomes', async (ctx) => {
ctx.body = await getRepository(AnnualIncome).find();
});
这篇关于TypeOrm - 如何将连接用作具有类型的独立对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!