iamacup

payload relationship enhancer

by iamacup
Databases & data
v2
^2.0.0

Overview

Summary generated from the README

Adds two behaviours to Payload relationship fields through per-field custom config: cleaning up references when the target document is deleted, and keeping paired relationship fields on two collections in sync. It is registered as a plugin in the Payload config and works with the Mongo adapter only. Written for Payload 2.x and described as an early release.

Features

  • Removes dangling references when a referenced document is deleted
  • Keeps paired relationship fields on two collections in sync
  • Configured per field through a custom relationshipEnhancer option
  • Exposes reverse relationships in the GraphQL and REST schema
  • Works with relationship fields inside groups, tabs and blocks

Use cases

  • Avoid broken references after deleting documents that other collections point to
  • Expose both sides of a relationship without reverse-lookup queries at query time

Installation

npm i payload-relationship-enhancer

12

Stars

0

Forks

3

Open Issues

2 years ago

Last Updated

895

Weekly Downloads

v0.0.10

npm Version

542.3 KB

Unpacked Size

1

Dependencies

GitHub stars

120 since Sep 6

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 202612
Dec 4, 202612
Dec 3, 202612
Dec 2, 202612
Dec 1, 202612
Nov 30, 202612
Nov 29, 202612
Nov 28, 202612
Nov 27, 202612
Nov 26, 202612
Nov 25, 202612
Nov 24, 202612
Nov 23, 202612
Nov 22, 202612
Nov 21, 202612
Nov 20, 202612
Nov 19, 202612
Nov 18, 202612
Nov 17, 202612
Nov 16, 202612
Nov 15, 202612
Nov 14, 202612
Nov 13, 202612
Nov 12, 202612
Nov 11, 202612
Nov 10, 202612
Nov 9, 202612
Nov 8, 202612
Nov 7, 202612
Nov 6, 202612
Nov 5, 202612
Nov 4, 202612
Nov 3, 202612
Nov 2, 202612
Nov 1, 202612
Oct 31, 202612
Oct 30, 202612
Oct 29, 202612
Oct 28, 202612
Oct 27, 202612
Oct 26, 202612
Oct 25, 202612
Oct 24, 202612
Oct 23, 202612
Oct 22, 202612
Oct 21, 202612
Oct 20, 202612
Oct 19, 202612
Oct 18, 202612
Oct 17, 202612
Oct 16, 202612
Oct 15, 202612
Oct 14, 202612
Oct 13, 202612
Oct 12, 202612
Oct 11, 202612
Oct 10, 202612
Oct 9, 202612
Oct 8, 202612
Oct 7, 202612
Oct 6, 202612
Oct 5, 202612
Oct 4, 202612
Oct 3, 202612
Oct 2, 202612
Oct 1, 202612
Sep 30, 202612
Sep 29, 202612
Sep 28, 202612
Sep 27, 202612
Sep 26, 202612
Sep 25, 202612
Sep 24, 202612
Sep 23, 202612
Sep 22, 202612
Sep 21, 202612
Sep 20, 202612
Sep 19, 202612
Sep 18, 202612
Sep 17, 202612
Sep 16, 202612
Sep 15, 202612
Sep 14, 202612
Sep 13, 202612
Sep 12, 202612
Sep 11, 202612
Sep 10, 202612
Sep 9, 202612
Sep 8, 202612
Sep 7, 202612
Sep 6, 202612
npm weekly downloads

1.1K+953 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20261,059
Dec 4, 2026940
Dec 3, 2026786
Dec 2, 2026565
Dec 1, 2026551
Nov 30, 2026551
Nov 29, 2026485
Nov 28, 2026259
Nov 27, 2026151
Nov 26, 2026104
Nov 25, 202633
Nov 24, 202633
Nov 23, 202634
Nov 22, 202680
Nov 21, 2026279
Nov 20, 2026315
Nov 19, 2026455
Nov 18, 2026622
Nov 17, 2026826
Nov 16, 2026826
Nov 15, 2026885
Nov 14, 2026907
Nov 13, 20261,180
Nov 12, 20261,441
Nov 11, 20261,326
Nov 10, 20261,123
Nov 9, 20261,131
Nov 8, 20261,076
Nov 7, 2026927
Nov 6, 2026724
Nov 5, 2026539
Nov 4, 2026733
Nov 3, 2026745
Nov 2, 2026998
Nov 1, 2026975
Oct 31, 20261,648
Oct 30, 20261,541
Oct 29, 20261,324
Oct 28, 20261,076
Oct 27, 20261,063
Oct 26, 2026800
Oct 25, 2026848
Oct 24, 2026144
Oct 23, 2026136
Oct 22, 2026207
Oct 21, 2026256
Oct 20, 2026254
Oct 19, 2026254
Oct 18, 2026277
Oct 17, 2026280
Oct 16, 2026336
Oct 15, 2026312
Oct 14, 2026441
Oct 13, 2026440
Oct 12, 2026440
Oct 11, 2026379
Oct 10, 2026424
Oct 9, 2026452
Oct 8, 2026553
Oct 7, 2026402
Oct 6, 2026404
Oct 5, 2026404
Oct 4, 2026851
Oct 3, 2026942
Oct 2, 2026959
Oct 1, 2026882
Sep 30, 20261,462
Sep 29, 20262,010
Sep 28, 20262,462
Sep 27, 20262,020
Sep 26, 20262,142
Sep 25, 20262,206
Sep 24, 20262,339
Sep 23, 20262,037
Sep 22, 20261,499
Sep 21, 20261,134
Sep 20, 20261,203
Sep 19, 2026986
Sep 18, 2026883
Sep 17, 2026722
Sep 16, 2026658
Sep 15, 2026646
Sep 14, 2026561
Sep 13, 2026439
Sep 12, 2026349
Sep 11, 2026287
Sep 10, 2026245
Sep 9, 20265
Sep 8, 20265
Sep 7, 20267
Sep 6, 2026106

Each point is npm's rolling 7-day download count.

Health Score

31/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/729925210-root.svg)](https://payloaddirectory.dev/plugins/729925210-root)
HTML
<a href="https://payloaddirectory.dev/plugins/729925210-root"><img src="https://payloaddirectory.dev/badge/729925210-root.svg" alt="Payload Directory"></a>
Updated Jan 2, 2024Created Dec 10, 2023Published Jan 2, 2024

README

payload-relationship-enhancer

This is a very early release of a plugin designed to enhance the relationships within payload to do two things:

  1. Provide referential integrity through deletes - i.e. if A references B and then B is deleted, A should no longer reference A
  2. Provide bi-directional relationships - i.e. if A references B, allow B to reference A and keep them in sync.

Motivation: if you have used keystonejs you know why, but more importantly, this stops gymnastics at query time to try and reverse lookup a relationship and instead builds it into the GraphQL / REST schema

A word of warning

This was written by someone who is bad at coding, and until a week ago had never used Mongo or Payload. Performance is probably bad and it might delete all your relationships.

I am almost certain this will not work properly for everyone and in every case, please leave an issue with details about how your relationships were setup!

Payload would appear to be considering bi-directional relationships as a core feature (although it is not on roadmap) in the future, so this should be seen as a stop-gap for applications prior to its official implementation.

Getting started

  1. bun add payload-relationship-enhancer

  2. update your payload config like this:

import RelationshipEnhancerPlugin from "payload-relationship-enhancer";

plugins: [payloadCloud(), RelationshipEnhancerPlugin],
  1. configure it like this:
    {
      type: "relationship",
      relationTo: "c",
      hasMany: true,
      name: "amixedblocks",
      custom: {
        relationshipEnhancer: {
          relationshipIntegrity: true,
          biDirectional: [
            {
              relationTo: "c", // must match one or more of the things in relationTo
              path: "cmixedblocks", // this is the path of the field on c that is ALSO a relationship field AND has a biDirectional config pointing back here
            },
            ...
          ],
        },
      },
    },

Config type is

        {
          relationshipIntegrity?: boolean,
          biDirectional?: [
            {
              relationTo: string;
              path: string;
            },
            ...
          ],
        }

Limitations / TODO

  • Currently any relationship field nested under an array will be ignored by this plugin, groups and tabs and blocks should be fine.
  • Only Mongo is supported as a database, it is very possible to add Postgres.
  • Support for localised relationship fields is not yet there
  • The changes made by this plugin do NOT trigger lifecycle hooks - i.e. if you have a bi-directional relationship between A and B, and set the value in A to some B document, the plugin will update a B document but no update hook will run - this is because we use the mongoose adapter directly and I am not sure exactly how to manually trigger updates.
  • This really needs some tests
  • I don't think the structure of this plugin follows best practice
  • Should add some plugin init config params for profiling performance
  • Better docs
  • The config will let you point two fields on 1 collection to 1 field on another - this breaks stuff. need to sanitise out in config check step
  • Some janky stuff with 'required' relationship fields that needs to be tested out

Contributing

Please do.

Dev mode

  • bun install
  • bun run dev

Mongo & payload will start - localhost:3000. Payload user is [email protected] with password test

Similar plugins