Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@graphile-contrib/pg-dynamic-attributes

Package Overview
Dependencies
Maintainers
4
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphile-contrib/pg-dynamic-attributes

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

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-16.67%
Maintainers
4
Weekly downloads
 
Created
Source

@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 🙌

Keywords

FAQs

Package last updated on 16 Jul 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc