| 1 |
import Association from "./association"; |
| 2 |
import { capitalize, camelize } from "../../utils/inflector"; |
| 3 |
import assert from "../../assert"; |
| 4 |
|
| 5 |
const identifierCache = {}; |
| 6 |
|
| 7 |
/** |
| 8 |
* The belongsTo association adds a fk to the owner of the association |
| 9 |
* |
| 10 |
* @class BelongsTo |
| 11 |
* @extends Association |
| 12 |
* @constructor |
| 13 |
* @public |
| 14 |
* @hide |
| 15 |
*/ |
| 16 |
export default class BelongsTo extends Association { |
| 17 |
get identifier() { |
| 18 |
if (typeof identifierCache[this.name] !== "string") { |
| 19 |
const identifier = `${camelize(this.name)}Id`; |
| 20 |
|
| 21 |
identifierCache[this.name] = identifier; |
| 22 |
} |
| 23 |
|
| 24 |
return identifierCache[this.name]; |
| 25 |
} |
| 26 |
|
| 27 |
get type() { |
| 28 |
return "belongsTo"; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @method getForeignKeyArray |
| 33 |
* @return {Array} Array of camelized name of the model owning the association |
| 34 |
* and foreign key for the association |
| 35 |
* @public |
| 36 |
*/ |
| 37 |
getForeignKeyArray() { |
| 38 |
return [camelize(this.ownerModelName), this.getForeignKey()]; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @method getForeignKey |
| 43 |
* @return {String} Foreign key for the association |
| 44 |
* @public |
| 45 |
*/ |
| 46 |
getForeignKey() { |
| 47 |
// we reuse identifierCache because it's the same logic as get identifier |
| 48 |
if (typeof identifierCache[this.name] !== "string") { |
| 49 |
const foreignKey = `${camelize(this.name)}Id`; |
| 50 |
|
| 51 |
identifierCache[this.name] = foreignKey; |
| 52 |
} |
| 53 |
|
| 54 |
return identifierCache[this.name]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers belongs-to association defined by given key on given model, |
| 59 |
* defines getters / setters for associated parent and associated parent's id, |
| 60 |
* adds methods for creating unsaved parent record and creating a saved one |
| 61 |
* |
| 62 |
* @method addMethodsToModelClass |
| 63 |
* @param {Function} ModelClass |
| 64 |
* @param {String} key the named key for the association |
| 65 |
* @public |
| 66 |
*/ |
| 67 |
addMethodsToModelClass(ModelClass, key) { |
| 68 |
let modelPrototype = ModelClass.prototype; |
| 69 |
let association = this; |
| 70 |
let foreignKey = this.getForeignKey(); |
| 71 |
let associationHash = { [key]: this }; |
| 72 |
|
| 73 |
modelPrototype.belongsToAssociations = Object.assign( |
| 74 |
modelPrototype.belongsToAssociations, |
| 75 |
associationHash |
| 76 |
); |
| 77 |
|
| 78 |
// update belongsToAssociationFks |
| 79 |
Object.keys(modelPrototype.belongsToAssociations).forEach((key) => { |
| 80 |
const value = modelPrototype.belongsToAssociations[key]; |
| 81 |
modelPrototype.belongsToAssociationFks[value.getForeignKey()] = value; |
| 82 |
}); |
| 83 |
|
| 84 |
// Add to target's dependent associations array |
| 85 |
this.schema.addDependentAssociation(this, this.modelName); |
| 86 |
|
| 87 |
// TODO: look how this is used. Are these necessary, seems like they could be gotten from the above? |
| 88 |
// Or we could use a single data structure to store this information? |
| 89 |
modelPrototype.associationKeys.add(key); |
| 90 |
modelPrototype.associationIdKeys.add(foreignKey); |
| 91 |
|
| 92 |
Object.defineProperty(modelPrototype, foreignKey, { |
| 93 |
/* |
| 94 |
object.parentId |
| 95 |
- returns the associated parent's id |
| 96 |
*/ |
| 97 |
get() { |
| 98 |
this._tempAssociations = this._tempAssociations || {}; |
| 99 |
let tempParent = this._tempAssociations[key]; |
| 100 |
let id; |
| 101 |
|
| 102 |
if (tempParent === null) { |
| 103 |
id = null; |
| 104 |
} else { |
| 105 |
if (association.isPolymorphic) { |
| 106 |
if (tempParent) { |
| 107 |
id = { id: tempParent.id, type: tempParent.modelName }; |
| 108 |
} else { |
| 109 |
id = this.attrs[foreignKey]; |
| 110 |
} |
| 111 |
} else { |
| 112 |
if (tempParent) { |
| 113 |
id = tempParent.id; |
| 114 |
} else { |
| 115 |
id = this.attrs[foreignKey]; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return id; |
| 121 |
}, |
| 122 |
|
| 123 |
/* |
| 124 |
object.parentId = (parentId) |
| 125 |
- sets the associated parent via id |
| 126 |
*/ |
| 127 |
set(id) { |
| 128 |
let tempParent; |
| 129 |
|
| 130 |
if (id === null) { |
| 131 |
tempParent = null; |
| 132 |
} else if (id !== undefined) { |
| 133 |
if (association.isPolymorphic) { |
| 134 |
assert( |
| 135 |
typeof id === "object", |
| 136 |
`You're setting an ID on the polymorphic association '${association.name}' but you didn't pass in an object. Polymorphic IDs need to be in the form { type, id }.` |
| 137 |
); |
| 138 |
tempParent = association.schema[ |
| 139 |
association.schema.toCollectionName(id.type) |
| 140 |
].find(id.id); |
| 141 |
} else { |
| 142 |
tempParent = |
| 143 |
association.schema[ |
| 144 |
association.schema.toCollectionName(association.modelName) |
| 145 |
].find(id); |
| 146 |
assert( |
| 147 |
tempParent, |
| 148 |
`Couldn't find ${association.modelName} with id = ${id}` |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
this[key] = tempParent; |
| 154 |
}, |
| 155 |
}); |
| 156 |
|
| 157 |
Object.defineProperty(modelPrototype, key, { |
| 158 |
/* |
| 159 |
object.parent |
| 160 |
- returns the associated parent |
| 161 |
*/ |
| 162 |
get() { |
| 163 |
this._tempAssociations = this._tempAssociations || {}; |
| 164 |
|
| 165 |
let tempParent = this._tempAssociations[key]; |
| 166 |
let foreignKeyId = this[foreignKey]; |
| 167 |
let model = null; |
| 168 |
|
| 169 |
if (tempParent) { |
| 170 |
model = tempParent; |
| 171 |
} else if (foreignKeyId !== null) { |
| 172 |
if (association.isPolymorphic) { |
| 173 |
model = association.schema[ |
| 174 |
association.schema.toCollectionName(foreignKeyId.type) |
| 175 |
].find(foreignKeyId.id); |
| 176 |
} else { |
| 177 |
model = |
| 178 |
association.schema[ |
| 179 |
association.schema.toCollectionName(association.modelName) |
| 180 |
].find(foreignKeyId); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return model; |
| 185 |
}, |
| 186 |
|
| 187 |
/* |
| 188 |
object.parent = (parentModel) |
| 189 |
- sets the associated parent via model |
| 190 |
|
| 191 |
I want to jot some notes about hasInverseFor. There used to be an |
| 192 |
association.inverse() check, but adding polymorphic associations |
| 193 |
complicated this. `comment.commentable`, you can't easily check for an |
| 194 |
inverse since `comments: hasMany()` could be on any model. |
| 195 |
|
| 196 |
Instead of making it very complex and looking for an inverse on the |
| 197 |
association in isoaltion, it was much simpler to ask the model being |
| 198 |
passed in if it had an inverse for the setting model and with its |
| 199 |
association. |
| 200 |
*/ |
| 201 |
set(model) { |
| 202 |
this._tempAssociations = this._tempAssociations || {}; |
| 203 |
this._tempAssociations[key] = model; |
| 204 |
|
| 205 |
if (model && model.hasInverseFor(association)) { |
| 206 |
let inverse = model.inverseFor(association); |
| 207 |
|
| 208 |
model.associate(this, inverse); |
| 209 |
} |
| 210 |
}, |
| 211 |
}); |
| 212 |
|
| 213 |
/* |
| 214 |
object.newParent |
| 215 |
- creates a new unsaved associated parent |
| 216 |
|
| 217 |
TODO: document polymorphic |
| 218 |
*/ |
| 219 |
modelPrototype[`new${capitalize(key)}`] = function (...args) { |
| 220 |
let modelName, attrs; |
| 221 |
|
| 222 |
if (association.isPolymorphic) { |
| 223 |
modelName = args[0]; |
| 224 |
attrs = args[1]; |
| 225 |
} else { |
| 226 |
modelName = association.modelName; |
| 227 |
attrs = args[0]; |
| 228 |
} |
| 229 |
|
| 230 |
let parent = |
| 231 |
association.schema[association.schema.toCollectionName(modelName)].new( |
| 232 |
attrs |
| 233 |
); |
| 234 |
|
| 235 |
this[key] = parent; |
| 236 |
|
| 237 |
return parent; |
| 238 |
}; |
| 239 |
|
| 240 |
/* |
| 241 |
object.createParent |
| 242 |
- creates a new saved associated parent, and immediately persists both models |
| 243 |
|
| 244 |
TODO: document polymorphic |
| 245 |
*/ |
| 246 |
modelPrototype[`create${capitalize(key)}`] = function (...args) { |
| 247 |
let modelName, attrs; |
| 248 |
if (association.isPolymorphic) { |
| 249 |
modelName = args[0]; |
| 250 |
attrs = args[1]; |
| 251 |
} else { |
| 252 |
modelName = association.modelName; |
| 253 |
attrs = args[0]; |
| 254 |
} |
| 255 |
|
| 256 |
let parent = |
| 257 |
association.schema[ |
| 258 |
association.schema.toCollectionName(modelName) |
| 259 |
].create(attrs); |
| 260 |
|
| 261 |
this[key] = parent; |
| 262 |
this.save(); |
| 263 |
|
| 264 |
return parent.reload(); |
| 265 |
}; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* |
| 270 |
* |
| 271 |
* @public |
| 272 |
*/ |
| 273 |
disassociateAllDependentsFromTarget(model) { |
| 274 |
let owner = this.ownerModelName; |
| 275 |
let fk; |
| 276 |
|
| 277 |
if (this.isPolymorphic) { |
| 278 |
fk = { type: model.modelName, id: model.id }; |
| 279 |
} else { |
| 280 |
fk = model.id; |
| 281 |
} |
| 282 |
|
| 283 |
let dependents = this.schema[this.schema.toCollectionName(owner)].where( |
| 284 |
(potentialOwner) => { |
| 285 |
let id = potentialOwner[this.getForeignKey()]; |
| 286 |
|
| 287 |
if (!id) { |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
if (typeof id === "object") { |
| 292 |
return id.type === fk.type && id.id === fk.id; |
| 293 |
} else { |
| 294 |
return id === fk; |
| 295 |
} |
| 296 |
} |
| 297 |
); |
| 298 |
|
| 299 |
dependents.models.forEach((dependent) => { |
| 300 |
dependent.disassociate(model, this); |
| 301 |
dependent.save(); |
| 302 |
}); |
| 303 |
} |
| 304 |
} |