base.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. const { AssociationError } = require("./../errors");
  3. class Association {
  4. constructor(source, target, options = {}) {
  5. this.source = source;
  6. this.target = target;
  7. this.options = options;
  8. this.scope = options.scope;
  9. this.isSelfAssociation = this.source === this.target;
  10. this.as = options.as;
  11. this.associationType = "";
  12. if (source.hasAlias(options.as)) {
  13. throw new AssociationError(`You have used the alias ${options.as} in two separate associations. Aliased associations must have unique aliases.`);
  14. }
  15. }
  16. toInstanceArray(input) {
  17. if (!Array.isArray(input)) {
  18. input = [input];
  19. }
  20. return input.map((element) => {
  21. if (element instanceof this.target)
  22. return element;
  23. const tmpInstance = {};
  24. tmpInstance[this.target.primaryKeyAttribute] = element;
  25. return this.target.build(tmpInstance, { isNewRecord: false });
  26. });
  27. }
  28. [Symbol.for("nodejs.util.inspect.custom")]() {
  29. return this.as;
  30. }
  31. }
  32. module.exports = Association;
  33. //# sourceMappingURL=base.js.map