Objection.js: [Q] Insert/Update model with references in many-to-many with extra property

Created on 12 Jul 2017  路  4Comments  路  Source: Vincit/objection.js

I have the following doubt that I couldn't find clearly in objection.js docs. I have the following 2 Models:

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

I want to insert/update a country like:

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

As you can see, I don't want to create a new Language, I want to just add the reference with the extra property. I just want to create the country with the relations. What's the correct way for inserting/updating like that? I couldn't find the docs, just found a way where it creates Languages as well.

Thanks!

question

Most helpful comment

Syntax for that is kind of nasty:

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

All 4 comments

Syntax for that is kind of nasty:

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

@koskimas Thanks very much. That's what I wanted, that part isn't clear in the api.

To update the relation is there an api to do it that it handles the modified, inserted and removed automatically? Or do I need to create my own logic for that?

Thanks

Not yet, but there will be soon

Ok, thank you

Was this page helpful?
0 / 5 - 0 ratings