| 1 |
import Association from "./association"; |
| 2 |
import Collection from "../collection"; |
| 3 |
import PolymorphicCollection from "../polymorphic-collection"; |
| 4 |
import compact from "lodash.compact"; |
| 5 |
import { capitalize, camelize } from "../../utils/inflector"; |
| 6 |
import assert from "@lib/assert"; |
| 7 |
|
| 8 |
const identifierCache = {}; |
| 9 |
|
| 10 |
/** |
| 11 |
* @class HasMany |
| 12 |
* @extends Association |
| 13 |
* @constructor |
| 14 |
* @public |
| 15 |
* @hide |
| 16 |
*/ |
| 17 |
export default class HasMany extends Association { |
| 18 |
get identifier() { |
| 19 |
if (typeof identifierCache[this.name] !== "string") { |
| 20 |
const identifier = `${camelize( |
| 21 |
this._container.inflector.singularize(this.name) |
| 22 |
)}Ids`; |
| 23 |
|
| 24 |
identifierCache[this.name] = identifier; |
| 25 |
} |
| 26 |
|
| 27 |
return identifierCache[this.name]; |
| 28 |
} |
| 29 |
|
| 30 |
get type() { |
| 31 |
return "hasMany"; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @method getForeignKeyArray |
| 36 |
* @return {Array} Array of camelized model name of associated objects |
| 37 |
* and foreign key for the object owning the association |
| 38 |
* @public |
| 39 |
*/ |
| 40 |
getForeignKeyArray() { |
| 41 |
return [camelize(this.ownerModelName), this.getForeignKey()]; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @method getForeignKey |
| 46 |
* @return {String} Foreign key for the object owning the association |
| 47 |
* @public |
| 48 |
*/ |
| 49 |
getForeignKey() { |
| 50 |
// we reuse identifierCache because it's the same logic as get identifier |
| 51 |
if (typeof identifierCache[this.name] !== "string") { |
| 52 |
const foreignKey = `${this._container.inflector.singularize( |
| 53 |
camelize(this.name) |
| 54 |
)}Ids`; |
| 55 |
|
| 56 |
identifierCache[this.name] = foreignKey; |
| 57 |
} |
| 58 |
|
| 59 |
return identifierCache[this.name]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers has-many association defined by given key on given model, |
| 64 |
* defines getters / setters for associated records and associated records' ids, |
| 65 |
* adds methods for creating unsaved child records and creating saved ones |
| 66 |
* |
| 67 |
* @method addMethodsToModelClass |
| 68 |
* @param {Function} ModelClass |
| 69 |
* @param {String} key |
| 70 |
* @public |
| 71 |
*/ |
| 72 |
addMethodsToModelClass(ModelClass, key) { |
| 73 |
let modelPrototype = ModelClass.prototype; |
| 74 |
let association = this; |
| 75 |
let foreignKey = this.getForeignKey(); |
| 76 |
let associationHash = { [key]: this }; |
| 77 |
|
| 78 |
modelPrototype.hasManyAssociations = Object.assign( |
| 79 |
modelPrototype.hasManyAssociations, |
| 80 |
associationHash |
| 81 |
); |
| 82 |
|
| 83 |
// update hasManyAssociationFks |
| 84 |
Object.keys(modelPrototype.hasManyAssociations).forEach((key) => { |
| 85 |
const value = modelPrototype.hasManyAssociations[key]; |
| 86 |
modelPrototype.hasManyAssociationFks[value.getForeignKey()] = value; |
| 87 |
}); |
| 88 |
|
| 89 |
// Add to target's dependent associations array |
| 90 |
this.schema.addDependentAssociation(this, this.modelName); |
| 91 |
|
| 92 |
// TODO: look how this is used. Are these necessary, seems like they could be gotten from the above? |
| 93 |
// Or we could use a single data structure to store this information? |
| 94 |
modelPrototype.associationKeys.add(key); |
| 95 |
modelPrototype.associationIdKeys.add(foreignKey); |
| 96 |
|
| 97 |
Object.defineProperty(modelPrototype, foreignKey, { |
| 98 |
/* |
| 99 |
object.childrenIds |
| 100 |
- returns an array of the associated children's ids |
| 101 |
*/ |
| 102 |
get() { |
| 103 |
this._tempAssociations = this._tempAssociations || {}; |
| 104 |
let tempChildren = this._tempAssociations[key]; |
| 105 |
let ids = []; |
| 106 |
|
| 107 |
if (tempChildren) { |
| 108 |
if (association.isPolymorphic) { |
| 109 |
ids = tempChildren.models.map((model) => ({ |
| 110 |
type: model.modelName, |
| 111 |
id: model.id, |
| 112 |
})); |
| 113 |
} else { |
| 114 |
ids = tempChildren.models.map((model) => model.id); |
| 115 |
} |
| 116 |
} else { |
| 117 |
ids = this.attrs[foreignKey] || []; |
| 118 |
} |
| 119 |
|
| 120 |
return ids; |
| 121 |
}, |
| 122 |
|
| 123 |
/* |
| 124 |
object.childrenIds = ([childrenIds...]) |
| 125 |
- sets the associated children (via id) |
| 126 |
*/ |
| 127 |
set(ids) { |
| 128 |
let tempChildren; |
| 129 |
|
| 130 |
if (ids === null) { |
| 131 |
tempChildren = []; |
| 132 |
} else if (ids !== undefined) { |
| 133 |
assert( |
| 134 |
Array.isArray(ids), |
| 135 |
`You must pass an array in when setting ${foreignKey} on ${this}` |
| 136 |
); |
| 137 |
|
| 138 |
if (association.isPolymorphic) { |
| 139 |
assert( |
| 140 |
ids.every((el) => { |
| 141 |
return ( |
| 142 |
typeof el === "object" && |
| 143 |
typeof el.type !== undefined && |
| 144 |
typeof el.id !== undefined |
| 145 |
); |
| 146 |
}), |
| 147 |
`You must pass in an array of polymorphic identifiers (objects of shape { type, id }) when setting ${foreignKey} on ${this}` |
| 148 |
); |
| 149 |
|
| 150 |
let models = ids.map(({ type, id }) => { |
| 151 |
return association.schema[ |
| 152 |
association.schema.toCollectionName(type) |
| 153 |
].find(id); |
| 154 |
}); |
| 155 |
tempChildren = new PolymorphicCollection(models); |
| 156 |
} else { |
| 157 |
tempChildren = |
| 158 |
association.schema[ |
| 159 |
association.schema.toCollectionName(association.modelName) |
| 160 |
].find(ids); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
this[key] = tempChildren; |
| 165 |
}, |
| 166 |
}); |
| 167 |
|
| 168 |
Object.defineProperty(modelPrototype, key, { |
| 169 |
/* |
| 170 |
object.children |
| 171 |
- returns an array of associated children |
| 172 |
*/ |
| 173 |
get() { |
| 174 |
this._tempAssociations = this._tempAssociations || {}; |
| 175 |
let collection = null; |
| 176 |
|
| 177 |
if (this._tempAssociations[key]) { |
| 178 |
collection = this._tempAssociations[key]; |
| 179 |
} else { |
| 180 |
if (association.isPolymorphic) { |
| 181 |
if (this[foreignKey]) { |
| 182 |
let polymorphicIds = this[foreignKey]; |
| 183 |
let models = polymorphicIds.map(({ type, id }) => { |
| 184 |
return association.schema[ |
| 185 |
association.schema.toCollectionName(type) |
| 186 |
].find(id); |
| 187 |
}); |
| 188 |
|
| 189 |
collection = new PolymorphicCollection(models); |
| 190 |
} else { |
| 191 |
collection = new PolymorphicCollection(association.modelName); |
| 192 |
} |
| 193 |
} else { |
| 194 |
if (this[foreignKey]) { |
| 195 |
collection = association.schema[ |
| 196 |
association.schema.toCollectionName(association.modelName) |
| 197 |
].find(this[foreignKey]); |
| 198 |
} else { |
| 199 |
collection = new Collection(association.modelName); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
this._tempAssociations[key] = collection; |
| 204 |
} |
| 205 |
|
| 206 |
return collection; |
| 207 |
}, |
| 208 |
|
| 209 |
/* |
| 210 |
object.children = [model1, model2, ...] |
| 211 |
- sets the associated children (via array of models or Collection) |
| 212 |
*/ |
| 213 |
set(models) { |
| 214 |
if ( |
| 215 |
models instanceof Collection || |
| 216 |
models instanceof PolymorphicCollection |
| 217 |
) { |
| 218 |
models = models.models; |
| 219 |
} |
| 220 |
|
| 221 |
models = models ? compact(models) : []; |
| 222 |
this._tempAssociations = this._tempAssociations || {}; |
| 223 |
|
| 224 |
let collection; |
| 225 |
if (association.isPolymorphic) { |
| 226 |
collection = new PolymorphicCollection(models); |
| 227 |
} else { |
| 228 |
collection = new Collection(association.modelName, models); |
| 229 |
} |
| 230 |
this._tempAssociations[key] = collection; |
| 231 |
|
| 232 |
models.forEach((model) => { |
| 233 |
if (model.hasInverseFor(association)) { |
| 234 |
let inverse = model.inverseFor(association); |
| 235 |
|
| 236 |
model.associate(this, inverse); |
| 237 |
} |
| 238 |
}); |
| 239 |
}, |
| 240 |
}); |
| 241 |
|
| 242 |
/* |
| 243 |
object.newChild |
| 244 |
- creates a new unsaved associated child |
| 245 |
*/ |
| 246 |
modelPrototype[ |
| 247 |
`new${capitalize( |
| 248 |
camelize(this._container.inflector.singularize(association.name)) |
| 249 |
)}` |
| 250 |
] = function (...args) { |
| 251 |
let modelName, attrs; |
| 252 |
if (association.isPolymorphic) { |
| 253 |
modelName = args[0]; |
| 254 |
attrs = args[1]; |
| 255 |
} else { |
| 256 |
modelName = association.modelName; |
| 257 |
attrs = args[0]; |
| 258 |
} |
| 259 |
|
| 260 |
let child = |
| 261 |
association.schema[association.schema.toCollectionName(modelName)].new( |
| 262 |
attrs |
| 263 |
); |
| 264 |
let children = this[key].models; |
| 265 |
|
| 266 |
children.push(child); |
| 267 |
this[key] = children; |
| 268 |
|
| 269 |
return child; |
| 270 |
}; |
| 271 |
|
| 272 |
/* |
| 273 |
object.createChild |
| 274 |
- creates a new saved associated child, and immediately persists both models |
| 275 |
|
| 276 |
TODO: forgot why this[key].add(child) doesn't work, most likely |
| 277 |
because these external APIs trigger saving cascades. Should probably |
| 278 |
have an internal method like this[key]._add. |
| 279 |
*/ |
| 280 |
modelPrototype[ |
| 281 |
`create${capitalize( |
| 282 |
camelize(this._container.inflector.singularize(association.name)) |
| 283 |
)}` |
| 284 |
] = function (...args) { |
| 285 |
let modelName, attrs; |
| 286 |
if (association.isPolymorphic) { |
| 287 |
modelName = args[0]; |
| 288 |
attrs = args[1]; |
| 289 |
} else { |
| 290 |
modelName = association.modelName; |
| 291 |
attrs = args[0]; |
| 292 |
} |
| 293 |
|
| 294 |
let child = |
| 295 |
association.schema[ |
| 296 |
association.schema.toCollectionName(modelName) |
| 297 |
].create(attrs); |
| 298 |
let children = this[key].models; |
| 299 |
|
| 300 |
children.push(child); |
| 301 |
this[key] = children; |
| 302 |
|
| 303 |
this.save(); |
| 304 |
|
| 305 |
return child.reload(); |
| 306 |
}; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* |
| 311 |
* |
| 312 |
* @public |
| 313 |
*/ |
| 314 |
disassociateAllDependentsFromTarget(model) { |
| 315 |
let owner = this.ownerModelName; |
| 316 |
let fk; |
| 317 |
|
| 318 |
if (this.isPolymorphic) { |
| 319 |
fk = { type: model.modelName, id: model.id }; |
| 320 |
} else { |
| 321 |
fk = model.id; |
| 322 |
} |
| 323 |
|
| 324 |
let dependents = this.schema[this.schema.toCollectionName(owner)].where( |
| 325 |
(potentialOwner) => { |
| 326 |
let currentIds = potentialOwner[this.getForeignKey()]; |
| 327 |
|
| 328 |
// Need this check because currentIds could be null |
| 329 |
return ( |
| 330 |
currentIds && |
| 331 |
currentIds.find((id) => { |
| 332 |
if (typeof id === "object") { |
| 333 |
return id.type === fk.type && id.id === fk.id; |
| 334 |
} else { |
| 335 |
return id === fk; |
| 336 |
} |
| 337 |
}) |
| 338 |
); |
| 339 |
} |
| 340 |
); |
| 341 |
|
| 342 |
dependents.models.forEach((dependent) => { |
| 343 |
dependent.disassociate(model, this); |
| 344 |
dependent.save(); |
| 345 |
}); |
| 346 |
} |
| 347 |
} |