简体   繁体   English

基本 typeORM 包装器未连接创建的连接

[英]basic typeORM wrapper not connected the connection created

I would like to create a Wrapper (service or manager) to drive TypeORM for my app.我想创建一个 Wrapper(服务或管理器)来为我的应用程序驱动 TypeORM。

I encounter lot of issue and I think my wrapper badly manage connection of my database with TypeORM.我遇到了很多问题,我认为我的包装器严重管理了我的数据库与 TypeORM 的连接。

I created basic example that seems to be good but.... connection is not connected (but TypeORM says that a connection is automatically connected when is created)我创建了看起来不错的基本示例,但是....连接未连接(但 TypeORM 表示连接在创建时会自动连接)

My methods to create or get connection previously created:我创建或获取先前创建的连接的方法:

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")
    }
}

my method for testing status connection:我测试状态连接的方法:

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 !"
        }
    }
}

and my createConnection method:和我的 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
    })
}

and this is a basic persist method with test example:这是带有测试示例的基本持久方法:

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())
}

My connection variable is a good instance of Connection object TypeORM.我的连接变量是 Connection object TypeORM 的一个很好的实例。 But just after when I test if this connection is connected with this:但就在我测试此连接是否与此连接之后:

connection.isConnected.toString()

It return false.它返回假。

and this:和这个:

this.status()

return me that connection is not connected回复我连接未连接

Very strange for me.对我来说很奇怪。 I don't understand why and how manage connection into a wrapper class js.我不明白为什么以及如何管理连接到包装器 class js。 I think there is a little few tricks that I don't understand perhaps.我想可能有一些我不明白的技巧。

My logic process is : On each method of my wrapper class like Persist, update, select, etc... I test if connection ('default') exist.我的逻辑过程是:在我的包装器 class 的每种方法上,例如 Persist、更新、select 等...我测试是否存在连接(“默认”)。 If yes a get this connection whereas I create connection.如果是,则在我创建连接时获取此连接。 And after when I get Connection object I can launch my commands TypeORM into my database.当我得到 Connection object 之后,我可以将我的命令 TypeORM 启动到我的数据库中。 Have I a good mentation?我有很好的心理暗示吗?

I finished to resolve my issue about managing connection into wrapper service class TypeORM.我已经解决了有关管理与包装服务 class TypeORM 的连接的问题。 This is my main js code.这是我的主要 js 代码。 If that can help somebody...如果这可以帮助某人...

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
    })
}

Now, you can call getConnection() method in any other method that use connection TypeORM without worry about status or created connection.现在,您可以在使用连接 TypeORM 的任何其他方法中调用 getConnection() 方法,而无需担心状态或创建的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM