npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

objectionjs-relationship

v1.3.1

Published

ObjectionJs Relationship facade to make relationship mapper more easier

Downloads

4

Readme

🚀 Relation maker for Objection.js

npm

This plugin provide a way to create relationship objects between Objection.js models.

Doc

For more documentation, you can view the Github page of this repository.

Installation

NPM


npm i objectionjs-relationship --save

Yarn


yarn add objectionjs-relationship

Usage

Basic Use

import {
  ModelRelationshipFacade,
  RelationshipEnum,
} from "objectionjs-relationship";

class Animal extends Model {
  static tableName = "animals";
}

class Person extends Model {
  static tableName = "persons";

  /** Example 1: */
  static relationMappings = new ModelRelationshipFacade(Person)
    .belongsToOne(Animal)
    .getRelationships();

  /** Example 2: Or you can use the add function, sending the relation type */
  static relationMappings = new ModelRelationshipFacade(Person)
    .add(Animal, RelationshipEnum.BelongsToOneRelation)
    .getRelationships();

  // Result:
  // static relationMappings: {
  //     animal: {
  //         relation: Model.BelongsToOneRelation,
  //         modelClass: Animal,
  //         join: {
  //             from: 'persons.animalId',
  //             to: 'animals.id'
  //         }
  //     }
  // }
}

Chaining Relationships

// Thoses Models are example models like Person or Animal.

/** Example 1 */
const relation = new ModelRelationshipFacade(Person)
  .belongsToOne(Country)
  .hasMany(Animal)
  .hasOne(Contact)
  .manyToMany(Activity)
  .hasOneThrough(Vehicle);
  .getRelationships();

/** Example 2 */
const relationWithAddFunction = new ModelRelationshipFacade(Person)
  .add(Country, RelationshipEnum.BelongsToOneRelation)
  .add(Animal, RelationshipEnum.HasManyRelation)
  .add(Contact, RelationshipEnum.HasOneRelation)
  .add(Activity, RelationshipEnum.ManyToManyRelation)
  .add(Vehicle, RelationshipEnum.HasOneThroughRelation)
  .getRelationships();

// Output:
// {
//   country: {
//       relation: Model.BelongsToOneRelation,
//       modelClass: Country,
//       join: {
//           from: 'persons.countryId',
//           to: 'countries.id'
//       }
//   },
//   animals: {
//       relation: Model.HasManyRelation,
//       modelClass: Animal,
//       join: {
//           from: 'persons.id',
//           to: 'animals.personId'
//       }
//   },
//   contact: {
//       relation: Model.HasOneRelation,
//       modelClass: Contact,
//       join: {
//           from: 'persons.id',
//           to: 'contacts.personId'
//       }
//   },
//   activities: {
//       relation: Model.ManyToManyRelation,
//       modelClass: Activity,
//       join: {
//           from: 'persons.id',
//           through: {
//               from: 'activities_persons.personId',
//               to: 'activities_persons.activityId'
//           },
//           to: 'activities.id'
//       }
//   },
//   vehicle: {
//       relation: Model.HasOneThroughRelation,
//       modelClass: Vehicle,
//       join: {
//           from: 'persons.id',
//           through: {
//               from: 'persons_vehicles.personId',
//               to: 'persons_vehicles.vehicleId'
//           },
//           to: 'vehicles.id'
//       }
//   }
// };

Options

IObjectionModelRelationshipAddConfig: This interface are the option that you can send to the add function and override the relation fields.

These options can be provided when instantiating the plugin.

Example 1

import { ModelRelationshipFacade } from "objectionjs-relationship";

class Animal extends Model {
  static tableName = "pets";
}

class Person extends Model {
  static tableName = "owners";

  static relationMappings = new ModelRelationshipFacade(Person)
    .belongsToOne(Animal, {
      relationName: "pet",
      fromField: "petId",
      fromTable: "owners",
      toField: "superId",
      toTable: "pets",
    })
    .getRelationships();

  // Result:
  // static relationMappings: {
  //   pet: {
  //       relation: Model.BelongsToOneRelation,
  //       modelClass: Animal,
  //       join: {
  //           from: 'owners.petId',
  //           to: 'pets.superId'
  //       }
  //   }
  // }
}

Example 2

import { ModelRelationshipFacade } from "objectionjs-relationship";

class Animal extends Model {
  static tableName = "pets";
}

class Person extends Model {
  static tableName = "owners";

  static relationMappings = new ModelRelationshipFacade(Person)
    .belongsToOne(Animal, {
      relationName: "pet",
      from: "owners.petId",
      to: "pets.superId",
    })
    .getRelationships();

  // Result:
  // static relationMappings: {
  //   pet: {
  //       relation: Model.BelongsToOneRelation,
  //       modelClass: Animal,
  //       join: {
  //           from: 'owners.petId',
  //           to: 'pets.superId'
  //       }
  //   }
  // }
}

The same can be done with through relationship. Read the IObjectionModelRelationshipAddConfig interface.

Debugger helper

You can send a options object to the getRelationships, witch one of the props is log

This was created for the purpose of testing. Log param can be a boolean or a function that will get an IObjectionModelRelationship as a parameter to do whatever the function wants to do with the data, like print with a custom logger or something.

import {
  ModelRelationshipFacade,
  IObjectionModelRelationship,
} from "objectionjs-relationship";

class Animal extends Model {
  static tableName = "animals";
}

class Person extends Model {
  static tableName = "persons";

  /** Example with default logger */
  static relationMappings = new ModelRelationshipFacade(Person)
    .belongsToOne(Animal)
    .getRelationships({
      log: true,
    });

  /** Example with custom log strategy */
  static relationMappings = new ModelRelationshipFacade(Person)
    .belongsToOne(Animal)
    .getRelationships({
      log: (relations: IObjectionModelRelationship<Animal>) => {
        // You can same logger or wathever you want
        console.log(relations);
      },
    });
}

The terminal will show (console.log as default):

{
    animal: {
        relation: Model.BelongsToOneRelation,
        modelClass: Animal,
        join: {
            from: 'animals.personId',
            to: 'persons.id'
        }
    }
}

Tests

Run the tests from the root directory:

npm test

Contributing & Development

Contributing

Found a bug or want to suggest something? Take a look first on the current and closed issues. If it is something new, please submit an issue.

Develop

It will be awesome if you can help us evolve objectionjs-relationship. Want to help?

  1. Fork it.

  2. npm install.

  3. Hack away.

  4. Run the tests: npm test.

  5. Create a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details