postgraphile-index-to-unique-constraint-plugin
v1.0.1
Published
PostGraphile plugin that create a root field from unique index
Downloads
121
Readme
postgraphile-index-to-unique-constraint-plugin
Convert unique index to fake unique constraint
Motivation
Postgraphile generate field for unique constraint on a table using a PgRowByUniqueConstraint.
However this only works for each unique constraints, not unique index. To resolve this issue, one could have created a PgRowByUniqueIndex
plugin, but this creates a lot of redundant code. And because the unique index behave exactly like unique constraint. So I think it would work well to inject a plugin in the build
phase, (not init
phase) to create "fake" constraint from unique index.
Since I am already making fake unique constraints, I also added a plugin called uniqueConstraintFromSmartComment
. Which create fake unique constraint based on the smart comment on views.
if you add a smart comment on view (only view, not table, not materialized view), it will be unique constraint.
comment on view xxx is E'@unique (column1,column2)';
These will be added as a fake unique constraint. The reason I do this is because postgres does not support unique constraint on views, (only on table/mat view). So If you are sure a view's column (or multiple) is unique, you can use create this unique constraint.
Usage
npm install postgraphile-index-to-unique-constraint-plugin
Then add it to the postgraphile plugins import { addFakeUniqueConstraintFromIndex } from 'postgraphile-polymorphic-relation-plugin';
createPostGraphileSchema(client, ['p'], {
appendPlugins: [
addFakeUniqueConstraintFromIndex,
],
});