Skip to content

Instantly share code, notes, and snippets.

@ap1969
Created September 19, 2021 22:10
Show Gist options
  • Save ap1969/24d599685d555b83b2c7f0fb74c823a0 to your computer and use it in GitHub Desktop.
Save ap1969/24d599685d555b83b2c7f0fb74c823a0 to your computer and use it in GitHub Desktop.
Objection.js issue
const { BaseModel } = require('./_basemodel');
const Tags = class extends BaseModel {
static get tableName() {
return 'tags';
}
static get relationMappings() {
const Users = new this.prototype.app.models.Users(this.prototype.app);
const Notifications = new this.prototype.app.models.Notifications(
this.prototype.app
);
const Links = new this.prototype.app.models.Links(this.prototype.app);
return {
user: {
relation: Model.BelongsToOneRelation,
modelClass: Users,
join: {
from: 'users.id',
to: 'tags.user_id',
},
},
notifications: {
relation: Model.ManyToManyRelation,
modelClass: Notifications,
join: {
from: 'tags.id',
through: {
from: 'notification_tag.tag_id',
to: 'notification_tag.notification_id',
},
to: 'notifications.id',
},
},
links: {
relation: Model.ManyToManyRelation,
modelClass: Links,
join: {
from: 'tags.id',
through: {
from: 'link_tag.tag_id',
to: 'link_tag.link_id',
},
to: 'links.id',
},
},
};
}
};
module.exports = function (app) {
Tags.prototype.app = app;
return Tags;
};
// See https://vincit.github.io/objection.js/#models
// for more of what you can do here.
const { BaseModel } = require('./_basemodel');
const Users = class extends BaseModel {
static get tableName() {
return "users";
}
static get relationMappings() {
const Cohorts = new this.prototype.app.models.Cohorts(this.prototype.app);
const Tokens = new this.prototype.app.models.Tokens(this.prototype.app);
const ProfileFields = new this.prototype.app.models.ProfileFields(
this.prototype.app
);
return {
cohort: {
relation: Model.BelongsToOneRelation,
modelClass: Cohorts,
join: {
from: "users.cohortId",
to: "cohorts.id"
}
},
tokens: {
relation: Model.HasManyRelation,
modelClass: Tokens,
join: {
from: "users.id",
to: "tokens.userId"
}
},
profileFields: {
relation: Model.HasManyRelation,
modelClass: ProfileFields,
join: {
from: "users.id",
to: "profile_fields.userId"
}
}
};
}
};
module.exports = function(app) {
Users.prototype.app = app;
return Users;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment