簡體   English   中英

基本 typeORM 包裝器未連接創建的連接

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM