2017-07-12 18 views
2

私はobjection.js文書ではっきりと見つけられなかった疑いがあります。私のような新しい国挿入したいObjection.jsに多対多の余分なフィールドを挿入します

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

: は、私は、次の2つのモデルを持っているあなたが見ることができるように

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

を、私は私がしたい、新しい言語を作成する必要はありません余分なプロパティを持つ参照を追加するだけです。私は関係を持って国を作りたいだけです。 そのようなものを挿入する正しい方法は何ですか?私はドキュメントを見つけることができませんでしたが、言語を作成する方法を見つけました。

ありがとうございます!

答えて

2

私は誰か他の人が興味を持っている場合にはので、私は彼の答えを自分の質問にお答えしますそれについて、プロジェクトのメンテナを尋ねた:

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

関連する問題