GithubHelp home page GithubHelp logo

bookshelf-relations's People

Contributors

aileen avatar allouis avatar daniellockyer avatar erisds avatar joeegrigg avatar johnonolan avatar kevinansfield avatar kirrg001 avatar naz avatar renovate-bot avatar renovate[bot] avatar rishabhgrg avatar simonbackx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bookshelf-relations's Issues

hasOne relations shouldn't throw errors if there's no relation to delete

See this TODO: https://github.com/TryGhost/bookshelf-relations/blob/master/test/integration/hasone_spec.js#L49

Currently, if you define a hasOne relation, bookshelf-relations doesn't do anything to enforce that the relation exists and that's OK. It's perfectly legitimate to have the parent with no child.

However, when deleting the parent, if there's no child to delete, bookshelf throws a NO ROWS DELETED error, which gets caught by bookshelf-relations and interpreted as a fatal error, causing the whole change to get rolled back.

The quick fix is to catch and ignore the error, but really bookshelf-relations needs to do a better job of finding the child (there's only one) and removing it if it exists.

A future / secondary point might be to make it possible to enforce that there is always a child, but that should be optional.

Note: there's a similar TODO for hasMany: https://github.com/TryGhost/bookshelf-relations/blob/master/test/integration/hasmany_spec.js#L42. Here the rules are different, again it may be that you want to enforce that there is at least 1 child at all times and there may be 0, 1 or many children, but there should still be no error if there wasn't anything to delete in the first place.

Add simple examples

Hi team, and congrats for your work!

There's a little documentation over the README and found no examples. I'm very interested in using this plugin with my project but unfortunately I'm not very sure how to start working with it.

It would be nice if you introduce a simple example for using it with a generic setup.

Does this plugin work with passing in nested relationships to save?

Does this plugin work with passing in nested relationships to save?

Brief.forge({ id }).save(req.body, { patch: true, transacting })

I get an error when I try to do this, and I only see set in examples and tests.

error: column "[relation name]" of relation "[model name]" does not exist

Saving target with empty body inserts wrong relation

When adding a new target and an empty object {} is passed in, getMatchingTarget generates sql similar to following:

select `tags`.* from `tags` limit 1;

Which grabs first of existing records and inserts it as a relation.

A more concrete example from library usage in Ghost appears when inserting related tags that are triggered by such payload in the PUT request to /posts:

{
	"posts": [{
		"tags": [{}]
	}]
}

Enforcing minimum relationships

Normally:

  • hasOne = has 0 or 1 child
  • hasMany = has 0, 1, or many children
  • belongsToMany = belongs to 0, 1 or many parents

However in some cases you might want more explicit relationships like:

  • hasExactlyOne = always has 1 child
  • hasAtLeastOne = has 1 or many children
  • belongsToAtLeastOne = belongs to 1 or many parents

I can imagine in some cases there are even more complex enforcements, like having exactly X relations.

Bookshelf doesn't provide any of this behaviour, but bookshelf relations has the ability, I think, to enforce this kind of rule and abstract away the logic.

In Ghost we already have a use case for hasAtLeastOne with authors and potentially a case for hasExactlyOne for dividing the post table up behind the scenes to improve performance.

We maybe should add a way to configure these extra constraints in bookshelf-relations somehow?

Make edits through relations explicit

refs https://github.com/TryGhost/Toolbox/issues/465

Problem

The default behavior of aditing related records through shortcut syntax is prone to unwanted edits.

Solution

Make "editable" relations explicit and do not allow edits through relations by default.

Technical details

  • Add an editable flag to the relationshipConfig (introduced here) identifying which relations can be edited. (1f28189)
  • Allow to pass "autoEditRelations" parameter when configuring relationship plugin (through pluginOptions) - makes the life easier and does not lock out people from using the latest version without going through massive code change (fddef9f)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Using npm packages for Renovate presets is now deprecated. Please migrate to repository-based presets instead.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • Lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/test.yml
  • actions/checkout v3
  • actions/setup-node v3
  • mirromutth/mysql-action v1.1
npm
package.json
  • @tryghost/debug ^0.1.13
  • @tryghost/errors ^1.2.3
  • bluebird ^3.7.2
  • lodash ^4.17.21
  • bookshelf 1.2.0
  • deep-diff 1.0.2
  • eslint 8.24.0
  • eslint-plugin-ghost 2.15.1
  • knex 2.4.0
  • knex-migrator 5.2.0
  • mocha 10.0.0
  • mysql2 3.2.0
  • nconf 0.12.0
  • nyc 15.1.0
  • should 13.2.3
  • sinon 15.0.0
  • sqlite3 5.1.4
  • bookshelf >=1.1.0
  • node ^18.12.1 || ^20.11.1

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

hasOne incorrectly results in many child relations

If you configure a hasOne <-> belongsTo relationship with a parent & child, the expectation is that there is only ever one child per parent. During edits, if the child is edited, the existing child fields should get overwritten.

Instead, currently, a new child is created each time the fields change.

You can see this in action here: https://github.com/ErisDS/Ghost/tree/post-table-split-spike

I'm assuming that this is a problem with bookshelf-relations. It could also be a config issue I've not understood, or a bookshelf problem, but my understanding is that bookshelf-relations is exactly designed to manage saving nested resources.

Also we know that saving hasMany <-> belongsToMany relationships works properly, without creating constant new children.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.