feat: Reference to the current instance of a related query

Open
#2,240 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, node.js
Domain
backend, databases

Research direction

Start by tracing the Relationship configuration and its beforeInsert hook, then inspect relationMappings and the ManyToManyRelation path shown in the example. Done means the hook can identify the model creating the relationship, allowing values such as fromModel.id to be used as requested.

Written by the indexing model from the issue text.

Description

It feels like there is missing context in the beforeInsert function set in a Relationship configuration. My biggest question is: Who is making this relationship?. I am looking to use a reference to the model that is creating a relationship so that I can answer that question in my DB. See example / pseudocode:



export class User extends Model {

    static tableName = 'users';

    // eslint-disable-next-line require-jsdoc
    static relationMappings: RelationMappings = {


        tags: {
            modelClass: __dirname + '/Tag',
            relation: Model.ManyToManyRelation,
            join: {
                from: 'users.id',
                through: {
                    from: 'user_tags.user_id',
                    to: 'user_tags.tag_id',
                },
                to: 'tags.id',
            },
            beforeInsert(fromModel: User, toModel: Tag, ctx) {

                toModel.created_by_id = fromModel.id; // <--- would be great if I could do this
            }
        }
    };
}

There are a lot of other useful things that can be accomplished with this as well.

Dominant language
JavaScript
Stars
7.3k
Forks
637
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Vincit/objection.js

All issues in Vincit/objection.js

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.