How to set application name in sequelize connection object?

淺唱寂寞╮ 提交于 2019-12-12 09:54:43

问题


Summary:

I want to change the application name of the connection string when initialize a new sequalize object. based on this stackoverflow question, I set the appName of dialectOptions as follows:

    let conn = new Sequelize(this.models.sequelize.config.database, this.models.sequelize.config.username,
                this.models.sequelize.config.password, {
                host: this.models.sequelize.config.host,
                dialect: this.models.sequelize.getDialect(),
                dialectOptions: {
                    appName: "userid=-2@gid=" + gid
                }
            });

Question:

When I execute a transaction like the following code, the application name does not pass to the SQL server. When I monitor the execution of SQL queries, the following picture shows that Tedious was sent to the application name.

transaction code:

    await conn.transaction(async t => {
                for(let i in this.collect){
                    let queryBuilder = this.collect[i];
                    let options = {replacements: queryBuilder.replacement, transaction: t};
                    if(queryBuilder.type === 'insert'){
                        options.type = conn.QueryTypes.INSERT;
                    }
                    let row = await conn.query(queryBuilder.query + ';select @@IDENTITY as id', options);
                    progressBar.update(parseInt(i) + 1);
                }

and the SQL Profiler picture is:

How can I set the Application Name properly?


回答1:


you must set appName in options object so correct syntax is

let conn = new Sequelize(this.models.sequelize.config.database, this.models.sequelize.config.username,
            this.models.sequelize.config.password, {
            host: this.models.sequelize.config.host,
            dialect: this.models.sequelize.getDialect(),
            dialectOptions: {
               options: {
                  appName: "userid=-2@gid=" + gid
               }
            }
        });


来源:https://stackoverflow.com/questions/56903706/how-to-set-application-name-in-sequelize-connection-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!