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

@graphile-contrib/pg-dynamic-attributes

v0.1.1

Published

PostGraphile plugin to fetch, order by and filter by dynamic attributes that are fetched from records in a related table at runtime.

Downloads

12

Readme

@graphile-contrib/pg-dynamic-attributes

This plugin enhances a PostGraphile schema with the ability to order by and filter by "dynamic attributes." For the purpose of this plugin "dynamic attributes" are stored into records in a related table and aren't known at schema build time.

Example database schema:

create table objects (
  id serial primary key,
  name text not null,
  created_at timestamptz not null default now()
);

create table object_properties (
  object_id int not null references objects on delete cascade,
  attribute text not null,
  value text not null,
  primary key (object_id, attribute)
);

comment on table objects is E'@dynamicAttributes object_properties value';

This will add the Object.dynamicAttribute field to your GraphQL schema for fetching a dynamic attribute:

type Object {
  # ...

  """
  Fetches the dynamic attribute given the value of `attribute` for this
  `Object`.
  """
  dynamicAttribute(match: ObjectDynamicAttribute): String
}

It will also add a sort argument to related connections so that you can order by these attributes:

"""
When the given sortable is null, should we position this at the end of the list
or the beginning of the list?
"""
enum SortNulls {
  """
  Order nulls last when in ascending order, or first when in descending order.
  """
  DEFAULT

  """
  Order nulls first (at the beginning of the list).
  """
  FIRST

  """
  Order nulls last (at the end of the list).
  """
  LAST
}

"""
Sortable concrete fields for the `Object` type.
"""
enum ObjectSortableField {
  ID
  NAME
  CREATED_AT
}

"""
Dynamic attribute keys for the `Object` type.
"""
input ObjectDynamicAttribute {
  attribute: String!
}

"""
The specifier of what we should sort by.  Exactly one of these values must be
specified and non-null (this will use `@oneOf`
[when that feature is merged into GraphQL](https://github.com/graphql/graphql-spec/pull/825)).
"""
input ObjectSortBy {
  field: ObjectSortableField
  dynamicAttribute: ObjectDynamicAttribute
}

"""
Specifies a sort for the `Object` type - what should we sort by, should it be
ascending or descending, and how should we handle nulls?
"""
input ObjectSort {
  sortBy: ObjectSortBy!
  ascending: Boolean! = true
  nulls: SortNulls! = DEFAULT
}

type Query {
  # ...
  allObjects(
    # ...
    sort: [ObjectSort!]
  ): ObjectsConnection
}

If you are using postgraphile-plugin-connection-filter then we'll also add filters to this plugin under the dynamicAttribute key:

{
  allObjects(
    filter: {
      and: [
        {
          dynamicAttribute: {
            match: {
              attribute: "Attribute1"
            }
            filter: {
              equalTo: "Value1"
            }
          }
        },
        {
          dynamicAttribute: {
            match: {
              attribute: "Attribute2"
            }
            filter: {
              equalTo: "Value2"
            }
          }
        }
      ]
    }
  ) {
    # ...
  }
}

Crowd-funded open-source software

We rely on the community's support to keep producing and maintaining OSS; if you find this plugin helpful, please click here to find out more about sponsors and sponsorship.

Usage

From the CLI you can install this plugin and run using command line postgraphile:

yarn add postgraphile @graphile-contrib/pg-dynamic-attributes
yarn postgraphile --append-plugins @graphile-contrib/pg-dynamic-attributes -c postgres://localhost/my_db

In library mode you can use appendPlugins to install the plugin:

app.use(
  postgraphile(process.env.DATABASE_URL, process.env.SCHEMA_NAME, {
    appendPlugins: [require("@graphile-contrib/pg-dynamic-attributes")],
  }),
);

Using a unique constraint

The example assumes that your primary key contains all the required attributes; however we can also support a unique constraint by passing its name as an additional argument to the dynamicAttribute smart comment:

create table objects (
  id serial primary key,
  name text not null,
  created_at timestamptz not null default now()
);

create table object_properties (
  id serial primary key,
  object_id int not null references objects on delete cascade,
  attribute text not null,
  value text not null
);
alter table object_properties add constraint ak_object_properties_dynamic_attributes unique (object_id, attribute);

comment on table objects is E'@dynamicAttributes object_properties value ak_object_properties_dynamic_attributes';

Note: it's still critical that the first entry in the unique constraint is the column that references the parent table's primary key.

Status

Experimental; the API may yet change.

Thanks 🙏

This plugin was originally sponsored by Pixel Networks 🙌