
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@anephenix/objection-relations
Advanced tools
[](https://badge.fury.io/js/%40anephenix%2Fobjection-relations) [](https:
A relations helper for Objection.js. This provides a convenient way to define
relations in the relationMappings
function on an Objection.js model.
For example, say you have a table called "Users" with this relation mapping:
class User extends Model {
static get tableName() {
return 'users';
}
static get relationMappings() {
return {
addresses: {
relation: Model.HasManyRelation,
modelClass: Address,
join: {
from: 'users.id',
to: 'addresses.user_id',
},
},
};
}
}
You can use the objection-relations helper module to write the instead:
import { ObjectionRelation } from '@anephenix/objection-relations';
class User extends Model {
static get tableName() {
return 'users';
}
static get relationMappings() {
const or = new ObjectionRelation({
subject: this.name,
modelPath: __dirname,
});
return {
addresses: or.hasMany('Address'),
};
}
}
The helper function will do the following:
Model | table name | foreign key |
---|---|---|
User | users | user_id |
npm i @anephenix/objection-relations
You can setup different kinds of database table relationships like this:
or.belongsTo('Role');
Is equivalent to writing:
{
relation: Model.BelongsToOneRelation,
modelClass: 'Role',
join: {
from: 'users.role_id',
to: 'roles.id'
}
}
or.hasOne('Setting');
Is equivalent to writing:
{
relation: Model.HasOneRelation,
modelClass: 'Setting',
join: {
from: 'users.id',
to: 'settings.user_id'
}
}
or.hasMany('Address');
Is equivalent to writing:
{
relation: Model.HasManyRelation,
modelClass: Address,
join: {
from: 'users.id',
to: 'addresses.user_id'
}
}
For relationships defined through a join table, you can write this:
or.hasManyThrough('Company', 'Employment');
This is equivalent to:
{
relation: Model.ManyToManyRelation,
modelClass: Company,
join: {
from: 'users.id',
through: {
from: 'employments.user_id',
to: 'employments.company_id'
},
to: 'companies.id'
}
}
There might be cases where the name of the database tables and foreign keys are following a different pattern from plural database tables and singular foreign keys. In such cases you can define them in the options, like this:
Say a User
model has many addresses, but the database table is called
'account_users', you can write this code:
or.hasMany('Address', { subjectTable: 'account_users' });
Which is equivalent to writing:
{
relation: Model.HasManyRelation,
modelClass: Address,
join: {
from: 'account_users.id',
to: 'addresses.user_id'
}
}
The same applies for the object table. Say for example the Address
model has
the database table 'shipping_addresses', you could write this:
or.hasMany('Address', { objectTable: 'shipping_addresses' });
Which is equivalent to writing:
{
relation: Model.HasManyRelation,
modelClass: Address,
join: {
from: 'users.id',
to: 'shipping_addresses.user_id'
}
}
If you find that the foreign key is not a singular form of the related model, then you can pass a foreign key for the subject like this:
or.hasMany('Address', { subjectForeignKey: 'account_user_id' });
Which is equivalent to writing:
{
relation: Model.HasManyRelation,
modelClass: Address,
join: {
from: 'users.id',
to: 'addresses.account_user_id'
}
}
You can pass a custom foreign key for the object (Like a Post model) like this:
or.belongsTo('User', { objectForeignKey: 'author_id' });
Is equivalent to writing:
{
relation: Model.BelongsToOneRelation,
modelClass: User,
join: {
from: 'posts.author_id',
to: 'users.id'
}
}
npm t
©2025 Anephenix OÜ. Objection-relations is licenced under the MIT Licence.
FAQs
[](https://badge.fury.io/js/%40anephenix%2Fobjection-relations) [](https:
The npm package @anephenix/objection-relations receives a total of 1 weekly downloads. As such, @anephenix/objection-relations popularity was classified as not popular.
We found that @anephenix/objection-relations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.