如何在sequelize连接对象中设置应用程序名称?

时间:2019-07-05 13:08:06

标签: node.js sequelize.js tedious

摘要:

初始化新的sequalize对象时,我想更改application name中的connection string。基于this stackoverflow question,我将DialectOptions的appName设置如下:

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

问题:

当我执行类似以下代码的事务时,application name不会传递到SQL Server。当我监视SQL查询的执行时,下图显示Tedous已发送到应用程序名称。

交易代码:

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

,SQL Profiler图片为:

enter image description here

如何正确设置Application Name

2 个答案:

答案 0 :(得分:1)

您必须在options对象中设置appName,以便使用正确的语法

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

答案 1 :(得分:0)

在postgre上,您需要像这样在public static dynamic Cast(dynamic obj, Type castTo) => Convert.ChangeType(obj, castTo); public Type GetType(string typeName) => assemblies.Select(a => a.GetType(typeName)).FirstOrDefault(type => type != null); private void UpdateAddress() { var manager = Cast(companyObj.Manager, GetType("Namespace.Manager")); var addressProperty = manager.GetType().GetProperty("Address", BindingFlags.Public | BindingFlags.Instance); var address = Cast(addressProperty, GetType("Namespace.Address")); var street= address.GetType().GetProperty("Street", BindingFlags.Public | BindingFlags.Instance); street.SetValue(address, "Bank Str", null); } 对象中设置属性application_name

dialectOptions