Assembles the new model or collection we're creating an instance of, gathering any relevant primitives from the parent object, without keeping any hard references.
If the parent object is eager loading, and it's a polymorphic morphTo
relation,
we can't know what the target will be until the models are sorted and matched.
Initializes a through
relation, setting the Target
model and options
,
which includes any additional keys for the relation.
Set the parentFk as appropriate now.
Set the appropriate foreign key if we're doing a belongsToMany, for convenience.
Generates and returns a specified key, for convenience... one of
foreignKey
, otherKey
, throughForeignKey
.
Injects the necessary select
constraints into a knex
query builder.
The base select column
The belongsToMany
and through
relations have joins & pivot columns.
If this is a single relation and we're not eager loading, limit the query to a single item.
Finally, add (and validate) the where conditions, necessary for constraining the relation.
Inject & validates necessary through
constraints for the current model.
Generates the join clauses necessary for the current relation.
A belongsTo
-> through
is currently the only relation with two joins.
Check that there isn't an incorrect foreign key set, vs. the one passed in when the relation was formed.
Fetches all eagerKeys
from the current relation.
Generates the appropriate standard join table.
Creates a new model or collection instance, depending on
the relatedData
settings and the models passed in.
If it's a single model, check whether there's already a model we can pick from... otherwise create a new instance.
Allows us to just use a model, but create a temporary collection for a "*-many" relation.
Groups the related response according to the type of relationship we're handling, for easy attachment to the parent models.
If this is a morphTo, we only want to pair on the morphValue for the current relation.
If this is a through
or belongsToMany
relation, we need to cleanup & setup the interim
model.
Group all of the related models for easier association with their parent models.
Loop over the parentModels
and attach the grouped sub-models,
keeping the relatedData
on the new related instance.
Now that related models have been successfully paired, update each with its parsed attributes
The models
is an array of models returned from the fetch,
after they're set
... parsing out any of the _pivot_
items from the
join table and assigning them on the pivot model or object as appropriate.
A few predicates to help clarify some of the logic above.
Sets the pivotColumns
to be retrieved along with the current model.
Simple memoization of the singularize call.
Specific to many-to-many relationships, these methods are mixed
into the belongsToMany
relationships when they are created,
providing helpers for attaching and detaching related models.
Attach one or more "ids" from a foreign table to the current. Creates & saves a new model and attaches the model with a join table entry.
Detach related object from their pivot tables. If a model or id is passed, it attempts to remove the pivot table based on that foreign key. If a hash is passed, it attempts to remove the item based on a where clause with these parameters. If no parameters are specified, we assume we will detach all related associations.
Update an existing relation's pivot table entry.
Selects any additional columns on the pivot table, taking a hash of columns which specifies the pivot column name, and the value the column should take on the output to the model attributes.
Helper for handling either the attach
or detach
call on
the belongsToMany
or hasOne
/ hasMany
:through relationship.
Handles setting the appropriate constraints and shelling out
to either the insert
or delete
call for the current model,
returning a promise.
Grab the knex
query builder for the current model, and
check if we have any additional constraints for the query.
If the item is an object, it's either a model that we're looking to attach to this model, or a hash of attributes to set in the relation.
Relation