Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduction
Adds Attributes Processor plugin. It allows defining custom processor functions that handle transformation of attribute values whenever they are
.set()
on a model.It also adds documentation to the
Bookshelf#plugin()
method which was totally absent.There is also a new wiki page for this plugin at Plugin: Processors.
Motivation
This seems like a worthy addition and was also mentioned as part of #552. Adding it as a plugin for now like was mentioned in issue #877.
Proposed solution
Mostly based on the code proposed by @rhys-vdw on this comment. The Cast Plugin proposed in the above linked PR was a bit too specific in terms of naming conventions and didn't support having pre-built processors (or cast functions using that plugin's nomenclature) that could be assigned to specific attributes by string name. It was otherwise functionally identical to the code proposed by @rhys-vdw.
On the other hand, the code proposed by @rhys-vdw implied having some processors already in Bookshelf, but that seemed like an unnecessary inclusion. These processors are probably specific to the user's application and can be converted to standalone npm packages or community plugins if needed with the present solution.
The solution proposed in #1220 may seem different from this at first glance, but it's actually trying to achieve the same thing. The problem with that solution is that it's too complicated since it tries to attach the functionality to the Virtuals plugin which means it had to come up with workarounds for things like the possibility of infinite recursion.
Fixes #809.
Closes #1220.
Closes #591.
Ticks the box for missing documentation of
Bookshelf#plugin
on #800.Alternatives considered
Considered merging PR #591, but it was a little bit too specific about casting values, and didn't include as much documentation as the current PR.
Current PR Issues
There are several ways of adding processors right now. Maybe the
[add|remove]Processor
methods aren't that necessary since it's still possible to reuse code if users just keep them in a separate module andrequire()
it as necessary in models. This also implies that the ability to assign attribute processors by string name would become irrelevant as well. One could just do:This PR also adds a new Wiki page instead of moving the plugin wikis to JSDoc.