helpers.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. function checkNamingCollision(association) {
  3. if (Object.prototype.hasOwnProperty.call(association.source.rawAttributes, association.as)) {
  4. throw new Error(`Naming collision between attribute '${association.as}' and association '${association.as}' on model ${association.source.name}. To remedy this, change either foreignKey or as in your association definition`);
  5. }
  6. }
  7. exports.checkNamingCollision = checkNamingCollision;
  8. function addForeignKeyConstraints(newAttribute, source, target, options, key) {
  9. if (options.foreignKeyConstraint || options.onDelete || options.onUpdate) {
  10. const primaryKeys = Object.keys(source.primaryKeys).map((primaryKeyAttribute) => source.rawAttributes[primaryKeyAttribute].field || primaryKeyAttribute);
  11. if (primaryKeys.length === 1 || !primaryKeys.includes(key)) {
  12. newAttribute.references = {
  13. model: source.getTableName(),
  14. key: key || primaryKeys[0]
  15. };
  16. newAttribute.onDelete = options.onDelete;
  17. newAttribute.onUpdate = options.onUpdate;
  18. }
  19. }
  20. }
  21. exports.addForeignKeyConstraints = addForeignKeyConstraints;
  22. function mixinMethods(association, obj, methods, aliases) {
  23. aliases = aliases || {};
  24. for (const method of methods) {
  25. if (!Object.prototype.hasOwnProperty.call(obj, association.accessors[method])) {
  26. const realMethod = aliases[method] || method;
  27. obj[association.accessors[method]] = function() {
  28. return association[realMethod](this, ...Array.from(arguments));
  29. };
  30. }
  31. }
  32. }
  33. exports.mixinMethods = mixinMethods;
  34. //# sourceMappingURL=helpers.js.map