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

postgraphile-polymorphic-relation-plugin

v1.1.9

Published

Extention that add Polymorphic Assocations in PostGraphile

Downloads

2,320

Readme

postgraphile-polymorphic-relation-plugin

hansololai Maintainability Test Coverage Known Vulnerabilities

Postgraphile Polymorphic Relation Plugin

This plugin create the associations linked via a polymorphic association. Polymorphic associations are defined like this in ruby on rails.

Feature

The postgraphile by default includes relation plugins that create associations based on foreign key. For example

create table users{
  id: integer primary_key,
  name: text
};
create table posts{
  id: integer primary_key,
  author_id: integer references users (id)
};

The Post model will not only have author_id field, and will also have an userByAuthorId object.

But it does not work for polymorphic associations.

create table taggs(
  id: integer primary_key,
  taggable_type: text,
  taggable_id: integer,
);

create table user(
  id: integer,
  name: text,
);

If you add a smart comment to define polymorphic associations. like so

comment on column taggs.taggable_type is E'@isPolymorphic\n@polymorphicTo User';

This will allow the plugin to know that the taggs table is polymorphic associated with user. Then the Tagg model will have a field called userAsTaggable. and User model will have a field called taggs.

allTaggs{
  nodes{
    userAsTaggable{
      id
      taggs{
        nodes{
          id
        }
      }
    }
  }
}

If there is an Unique Constraint on the two columns taggable_type and taggable_id, then the field in User is tagg (singular) instead of taggs. (It will be a single model isntead of a connection)

alter table taggs add constraint unique_taggable UNIQUE (taggable_type, taggable_id);
allUsers{
  nodes{
    id
    tagg{
      id
    }
  }
}

Also provide a connectionFilter

In the above example, And a record in taggs is

id: 50
taggable_type: 'User'
taggable_id: 1

This means the tag(id:50) is connected to User record of id:1.

If you want to filter the connections like the following:

allTaggs(filter:{
  userAsTaggable:{id:1} // This does not exist in regular connection filter
}){
  nodes{
    id
  }
}

This plugin will create the forward relationship. (this userAsTaggable) field, and also the backward relationship. (the taggs field on User Connection, and other connections that is associated). Also if the taggable_type and taggable_id are has an unique constraint. The backward filter is not a single object filter, instead of a multi-field, which consist of three fields some,every,none.

Usage

Requires [email protected]+.

Usage of connection filter

To enable this function, it requires [email protected]+ and the following plugins appended prior to this plugin:

  • postgraphile-plugin-connection-filter@^1.0.0

Also an build option is needed. like so

createPostGraphileSchema(client, ['p'], {
        appendPlugins: [
          // This is important to be added if the two options are true
          PgConnectionFilterPlugin, 
          postgraphilePolyRelationCorePlugin,
        ],
        graphileBuildOptions: {
          connectionFilterPolymorphicForward: true,
          connectionFilterPolymorphicBackward: true,
        },
      });

The two options will create the filter options.

Install it

npm install postgraphile-polymorphic-relation-plugin

Use it by adding it in

import {postgraphilePolyRelationCorePlugin} from 'postgraphile-polymorphic-relation-plugin';
createPostGraphileSchema(pgClient, [schemaName],{
  appendPlugin: [
    postgraphilePolyRelationCorePlugin
  ]
})

The npm package exposes every intermediate plugins such as

addModelTableMappingPlugin: add the model to table mapping dictionary.

definePolymorphicCustom: add the polymorphic definition in Build object, used by other plugin

addForwardPolyAssociation: Use the defined polymorphic objects to construct forward relation,

addBackwardPolyAssociation: Use the defined polymorphic objects to construct backward relation,

If you feel like it, you could use them individually, or just use one that include them all. The postgraphilePolyRelationCorePlugin.

Development

To establish a test environment, create an empty PostgreSQL database and set a TEST_DATABASE_URL environment variable with your database connection string.

createdb graphile_test
export TEST_DATABASE_URL=postgres://localhost:5432/graphile_test
yarn
yarn test