Search code examples
node.jssequelize.jstedious

How to set application name in sequelize connection object?


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:

enter image description here

How can I set the Application Name properly?


Solution

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