![](/img/trans.png)
[英]Typeorm Connection “default” was not found when connection is created in jest globalSetup
[英]basic typeORM wrapper not connected the connection created
我想创建一个 Wrapper(服务或管理器)来为我的应用程序驱动 TypeORM。
我遇到了很多问题,我认为我的包装器严重管理了我的数据库与 TypeORM 的连接。
我创建了看起来不错的基本示例,但是....连接未连接(但 TypeORM 表示连接在创建时会自动连接)
我创建或获取先前创建的连接的方法:
getConnection(): Connection
{
if (!typeorm.getConnectionManager().has("default"))
{
this.createConnection()
.then((connection) => {return connection})
.catch((error) => {
console.log('create connection database failed')
dialog.showErrorBox('Error!', 'create connection database failed')
})
}
else
{
return typeorm.getConnectionManager().get("default")
}
}
我测试状态连接的方法:
status(): string
{
if (!typeorm.getConnectionManager().has("default"))
{
return "nothing (default) connection"
}
else
{
let isConnected = typeorm.getConnectionManager().get("default").isConnected
if (isConnected)
{
return "connected !"
}
else
{
return "not connected !"
}
}
}
和我的 createConnection 方法:
createConnection(): Promise<Connection>
{
return typeorm.createConnection({
name: 'default',
type: 'better-sqlite3',
database: './src/../data/database/mydb.db',
entities: [
xxxxx,
xxxxxx,
xxxxxx,
xxxxxx
],
synchronize: true
})
}
这是带有测试示例的基本持久方法:
persist(entityObject: any): any
{
let connection = this.getConnection()
if (connection instanceof Connection)
{
dialog.showErrorBox('DEBUG', 'connection is instance of Connection')
}
else
{
dialog.showErrorBox('DEBUG', 'connection is not instance of Connection')
}
dialog.showErrorBox('Connection Status', this.status())
dialog.showErrorBox('Connection is connected ?', connection.isConnected.toString())
}
我的连接变量是 Connection object TypeORM 的一个很好的实例。 但就在我测试此连接是否与此连接之后:
connection.isConnected.toString()
它返回假。
和这个:
this.status()
回复我连接未连接
对我来说很奇怪。 我不明白为什么以及如何管理连接到包装器 class js。 我想可能有一些我不明白的技巧。
我的逻辑过程是:在我的包装器 class 的每种方法上,例如 Persist、更新、select 等...我测试是否存在连接(“默认”)。 如果是,则在我创建连接时获取此连接。 当我得到 Connection object 之后,我可以将我的命令 TypeORM 启动到我的数据库中。 我有很好的心理暗示吗?
我已经解决了有关管理与包装服务 class TypeORM 的连接的问题。 这是我的主要 js 代码。 如果这可以帮助某人...
getConnection(): Promise<Connection>
{
if (!typeorm.getConnectionManager().has("default"))
{
return this.createConnection()
}
else
{
return new Promise((resolve, reject) =>
{
let connection = typeorm.getConnectionManager().get("default")
if (connection instanceof Connection)
{
resolve(connection)
}
else
{
reject(() => {
let message = "impossible to get Connection"
dialog.showErrorBox('Error!', message)
})
}
})
}
}
status(): string
{
if (!typeorm.getConnectionManager().has("default"))
{
return "nothing (default) connection"
}
else
{
let isConnected = typeorm.getConnectionManager().get("default").isConnected
if (isConnected)
{
return "connected !"
}
else
{
return "not connected !"
}
}
}
close()
{
if (!typeorm.getConnectionManager().has('default'))
{
let message = "nothing connection named default"
console.log(message)
dialog.showErrorBox('Error!', message)
}
else
{
let connection = typeorm.getConnectionManager().get('default')
connection.close().then(() => {return true}).catch(() => {
throw "impossible de clore la connection"
})
}
}
async createConnection(): Promise<Connection>
{
return await typeorm.createConnection({
name: "default",
type: 'better-sqlite3',
database: './src/../xxx/xxxxxxx/mydb.db',
entities: [xxx,xxx,xxxxxx,xxxxx],
synchronize: true
})
}
现在,您可以在使用连接 TypeORM 的任何其他方法中调用 getConnection() 方法,而无需担心状态或创建的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.