编程语言
首页 > 编程语言> > javascript-在Objection.js中多对多插入额外字段

javascript-在Objection.js中多对多插入额外字段

作者:互联网

我有以下疑问,我在objection.js文档中找不到清楚的地方.
我有以下2种型号:

export class Language extends BaseId {
    name: string;

    static tableName = 'Languages';

    static jsonSchema = {
        type: 'object',

        required: ['name'],

        properties: {
            name: { type: 'string', minLength: 1, maxLength: 80 }
        }
    };
}

export class Country extends BaseId {
    name: string;
    languages: Language[];

    static tableName = 'Countries';

    static jsonSchema = {
        type: 'object',

        required: ['name'],

        properties: {
           name: { type: 'string', minLength: 1, maxLength: 120 }
        }
    };

    static relationMappings: RelationMappings = {
         languages: {
              relation: Model.ManyToManyRelation,
              modelClass: Language,
              join: {
                  from: 'Countries.id',
                  through: {
                      from: 'CountriesLanguages.country_id',
                      to: 'CountriesLanguages.language_id',
                      extra: ['oficial']
                  },
                  to: 'Languages.id'
              }
          }
     };
}

我想插入一个新的国家,例如:

{
    name: "Country Name",
    languages: [
       { id: 1, official: true },
       { id: 2, official: true },
       { id: 3, official: false }
    ]
}

如您所见,我不想创建新的语言,我只想添加带有extra属性的引用.我只想与两国建立关系.
这样插入的正确方法是什么?我找不到文档,只是找到了一种创建语言的方法.

谢谢!

解决方法:

我询问了项目维护者,以便在其他人感兴趣的情况下回答他自己的问题:

Country
  .query()
  .insertGraph({
    name: 'Finland',
    languages: [{
      // This is the id of the language. You can change "#dbRef" into
      // some other property by overriding `Model.dbRefProp` but you
      // cannot use the model's id column name.
      "#dbRef": 1,
      oficial: true
    }, {
      "#dbRef": 2,
      oficial: false
    }]
  })

从:https://github.com/Vincit/objection.js/issues/441

标签:knex-js,typescript,node-js,objection-js,javascript
来源: https://codeday.me/bug/20191025/1931867.html