tryghost / bookshelf-relations Goto Github PK
View Code? Open in Web Editor NEWA bookshelf plugin which handles relationships.
License: MIT License
A bookshelf plugin which handles relationships.
License: MIT License
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.
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?
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
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": [{}]
}]
}
Normally:
However in some cases you might want more explicit relationships like:
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?
refs https://github.com/TryGhost/Toolbox/issues/465
The default behavior of aditing related records through shortcut syntax is prone to unwanted edits.
Make "editable" relations explicit and do not allow edits through relations by default.
editable
flag to the relationshipConfig
(introduced here) identifying which relations can be edited. (1f28189)pluginOptions
) - makes the life easier and does not lock out people from using the latest version without going through massive code change (fddef9f)This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These problems occurred while renovating this repository. View logs.
These updates are awaiting their schedule. Click on a checkbox to get an update now.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
eslint
, eslint-plugin-ghost
, mocha
, sinon
)eslint-plugin-ghost
, sinon
).github/workflows/test.yml
actions/checkout v3
actions/setup-node v3
mirromutth/mysql-action v1.1
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
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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.