How to use the objection.Model.ManyToManyRelation function in objection

To help you get started, we’ve selected a few objection examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Vincit / objection.js / examples / express-es7 / src / models / Person.js View on Github external
// This object defines the relations to other models.
  static relationMappings = {
    pets: {
      relation: Model.HasManyRelation,
      // The related model. This can be either a Model subclass constructor or an
      // absolute file path to a module that exports one. We use the file path version
      // here to prevent require loops.
      modelClass: Animal,
      join: {
        from: 'persons.id',
        to: 'animals.ownerId'
      }
    },

    movies: {
      relation: Model.ManyToManyRelation,
      modelClass: Movie,
      join: {
        from: 'persons.id',
        // ManyToMany relation needs the `through` object to describe the join table.
        through: {
          from: 'persons_movies.personId',
          to: 'persons_movies.movieId'
        },
        to: 'movies.id'
      }
    },

    children: {
      relation: Model.HasManyRelation,
      modelClass: Person,
      join: {
github Vincit / objection.js / examples / koa / models / Movie.js View on Github external
static get relationMappings() {
    // One way to prevent circular references
    // is to require the model classes here.
    const Person = require('./Person')

    return {
      actors: {
        relation: Model.ManyToManyRelation,

        // The related model. This can be either a Model subclass constructor or an
        // absolute file path to a module that exports one.
        modelClass: Person,

        join: {
          from: 'movies.id',
          // ManyToMany relation needs the `through` object to describe the join table.
          through: {
            from: 'persons_movies.movieId',
            to: 'persons_movies.personId'
          },
          to: 'persons.id'
        }
      }
    }
github strues / boldr / packages / boldr-api / src / models / page.js View on Github external
static get relationMappings() {
    return {
      templates: {
        relation: Model.ManyToManyRelation,
        modelClass: Template,
        join: {
          from: 'page.id',
          through: {
            from: 'template_page.page_id',
            to: 'template_page.template_id',
            // modelClass: UserRole,
          },
          to: 'template.id',
        },
      },
    };
  }
}
github strues / boldr / packages / boldr-api / src / models / template.js View on Github external
static get relationMappings() {
    return {
      pages: {
        relation: Model.ManyToManyRelation,
        modelClass: Page,
        join: {
          from: 'template.id',
          through: {
            from: 'template_page.template_id',
            to: 'template_page.page_id',
            // modelClass: UserRole,
          },
          to: 'page.id',
        },
      },
    };
  }
}
github Vincit / objection-graphql / tests / setup / models / Person.js View on Github external
static get relationMappings() {
    const Review = require('./Review');
    const Movie = require('./Movie');

    return {
      reviews: {
        relation: Model.HasManyRelation,
        modelClass: Review,
        join: {
          from: 'Person.id',
          to: 'Review.reviewerId',
        },
      },

      movies: {
        relation: Model.ManyToManyRelation,
        modelClass: Movie,
        join: {
          from: 'Person.id',
          through: {
            from: 'Person_Movie.personId',
            to: 'Person_Movie.movieId',
          },
          to: 'Movie.id',
        },
      },

      parent: {
        relation: Model.BelongsToOneRelation,
        modelClass: Person,
        join: {
          from: 'Person.parentId',
github strues / boldr / server / models / user.js View on Github external
static get relationMappings() {
    return {
      roles: {
        relation: Model.ManyToManyRelation,
        modelClass: Role,
        join: {
          from: 'user.id',
          through: {
            from: 'user_role.user_id',
            to: 'user_role.role_id',
          },
          to: 'role.id',
        },
      },
      posts: {
        relation: Model.HasManyRelation,
        modelClass: Post,
        join: {
          from: 'user.id',
          to: 'post.user_id',
github strues / boldr / packages / boldr-api / src / models / post.js View on Github external
},
      },
      tags: {
        relation: Model.ManyToManyRelation,
        modelClass: Tag,
        join: {
          from: 'post.id',
          through: {
            from: 'post_tag.post_id',
            to: 'post_tag.tag_id',
          },
          to: 'tag.id',
        },
      },
      attachments: {
        relation: Model.ManyToManyRelation,
        modelClass: Attachment,
        join: {
          from: 'post.id',
          through: {
            from: 'post_attachment.post_id',
            to: 'post_attachment.attachment_id',
          },
          to: 'attachment.id',
        },
      },
      comments: {
        relation: Model.ManyToManyRelation,
        modelClass: Comment,
        join: {
          from: 'post.id',
          through: {
github stefanvanherwijnen / quasar-auth-starter / backend / src / api / models / user.ts View on Github external
properties: {
      id: { type: 'integer' },
      email: { type: 'string', format: 'email' },
      name: { type: 'string', minLength: 1, maxLength: 255 },
      password: { type: 'string' },
      verified: { type: 'boolean' },
      verificationToken: { type: 'string' },
      passwordResetToken: { type: 'string' },
      tokensRevokedAt: { type: 'string', format: 'date-time' },
      roleNames: { type: 'array' },
    },
  }

  public static relationMappings = {
    roles: {
      relation: Model.ManyToManyRelation,
      modelClass: __dirname + '/role',
      join: {
        from: 'users.id',
        through: {
          from: 'roles_roleable.user_id',
          to: 'roles_roleable.role_id',
        },
        to: 'roles.id',
      },
    },
  }

  public $afterGet(): void {
    if (this.roles) {
      this.roleNames = this.roles.map((role): string => { return role.name })
    }
github contentjet / contentjet-api / src / models / MediaTag.ts View on Github external
static get relationMappings(): RelationMappings {
    return {
      project: {
        relation: Model.BelongsToOneRelation,
        modelClass: `${__dirname}/Project`,
        join: {
          from: 'mediaTag.projectId',
          to: 'project.id'
        }
      },
      media: {
        relation: Model.ManyToManyRelation,
        modelClass: `${__dirname}/Media`,
        join: {
          from: 'mediaTag.id',
          through: {
            from: 'media_mediaTag.mediaTagId',
            to: 'media_mediaTag.mediaId'
          },
          to: 'media.id'
        }
      }
    };
  }
github strues / boldr / src / api / routes / role / role.model.js View on Github external
static get relationMappings() {
    return {
      role: {
        relation: Model.ManyToManyRelation,
        modelClass: User,
        join: {
          from: 'role.id',
          through: {
            from: 'user_role.role_id',
            to: 'user_role.user_id',
          },
          to: 'user.id',
        },
      },
    };
  }
}