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-plugin-derived-field

v1.0.0-alpha.7

Published

Add derived fields in PostGraphile

Downloads

3,235

Readme

Package on npm CircleCI

postgraphile-plugin-derived-field

This plugin provides an interface for adding derived fields to the schema generated by PostGraphile v4.

The term "derived fields" is used to differentiate this approach from the standard Computed Columns support in PostGraphile. This plugin effectively adds "computed columns in JavaScript" to your toolbelt.

Getting Started

Define your derived fields in the derivedFieldDefinitions property of graphileBuildOptions:

const express = require("express");
const { postgraphile } = require("postgraphile");
const PostGraphileDerivedFieldPlugin = require("postgraphile-plugin-derived-field");

const app = express();

app.use(
  postgraphile(pgConfig, schema, {
    graphiql: true,
    appendPlugins: [PostGraphileDerivedFieldPlugin],
    graphileBuildOptions: {
      derivedFieldDefinitions: [
        // your definitions here
      ]
    }
  })
);

app.listen(5000);

Provide derivedFieldDefinitions as an array of objects with the following structure:

{
  identifiers: Array<Identifier>,
  inflect: function,
  resolve: function,
  type?: string | build => T,
  description?: string
}

The Scenarios section below provides guidance on structuring identifiers, inflect, and resolve for your specific use case.

Use type to specify the GraphQL type that the resolve function returns. This can be a string identifying the GraphQL type name, or a function (with the build helper available as the first argument) that returns a GraphQL type. Default: String

Use description to populate the field description in the schema.

Scenarios

Derive a new field from one database column

{
  identifiers: [
    {
      table: "my_schema.my_table",
      columns: ["my_column"],
    },
  ],
  inflect: fieldName => `derivedFrom${fieldName}`,
  resolve: val => `Value derived from ${val}`,
}

Derive a new field from multiple database columns

{
  identifiers: [
    {
      table: "my_schema.my_table",
      columns: ["my_column", "my_other_column"],
    },
  ],
  inflect: (...fieldNames) =>
    `derivedFrom${fieldNames.map(upperFirst).join("And")}`,
  resolve: (my_column, my_other_column) =>
    `Value derived from ${my_column} and ${my_other_column}`,
}

Derive a new field from columns that have a specific tag

This approach uses the "smart comments" feature of PostGraphile to match columns that have been assigned a specific tag.

{
  identifiers: [
    {
      tag: "mytag",
    },
  ],
  inflect: fieldName => `derivedFrom${fieldName}`,
  resolve: val => `Value derived from ${val}`,
}

Alternative syntax

Column names can be provided as a string: "my_schema.my_table.my_column". This syntax does not support generating a derived field from multiple database columns.

Tag names can be provided as a string: "@mytag"

Examples

const express = require("express");
const { postgraphile } = require("postgraphile");
const PostGraphileDerivedFieldPlugin = require("postgraphile-plugin-derived-field");

const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const bucket = "postgraphile-plugin-test";

const app = express();

app.use(
  postgraphile(pgConfig, schema, {
    graphiql: true,
    appendPlugins: [PostGraphileDerivedFieldPlugin],
    graphileBuildOptions: {
      derivedFieldDefinitions: [
        {
          identifiers: ["my_schema.my_table.my_column"],
          inflect: fieldName => `${fieldName}SignedUrl`,
          resolve: val => s3.getSignedUrl('getObject', {Bucket: bucket, Key: val, Expires: 900})
        }
      ]
    }
  })
);

app.listen(5000);