1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getAssociationsByRelation = exports.setAssociations = exports.getAssociations = exports.addAssociation = exports.getPreparedAssociationOptions = void 0;
|
4 | require("reflect-metadata");
|
5 | const ASSOCIATIONS_KEY = 'sequelize:associations';
|
6 | function getPreparedAssociationOptions(optionsOrForeignKey) {
|
7 | let options = {};
|
8 | if (optionsOrForeignKey) {
|
9 | if (typeof optionsOrForeignKey === 'string') {
|
10 | options.foreignKey = optionsOrForeignKey;
|
11 | }
|
12 | else {
|
13 | options = Object.assign({}, optionsOrForeignKey);
|
14 | }
|
15 | }
|
16 | return options;
|
17 | }
|
18 | exports.getPreparedAssociationOptions = getPreparedAssociationOptions;
|
19 |
|
20 |
|
21 |
|
22 | function addAssociation(target, association) {
|
23 | let associations = getAssociations(target);
|
24 | if (!associations) {
|
25 | associations = [];
|
26 | }
|
27 | associations.push(association);
|
28 | setAssociations(target, associations);
|
29 | }
|
30 | exports.addAssociation = addAssociation;
|
31 |
|
32 |
|
33 |
|
34 | function getAssociations(target) {
|
35 | const associations = Reflect.getMetadata(ASSOCIATIONS_KEY, target);
|
36 | if (associations) {
|
37 | return [...associations];
|
38 | }
|
39 | }
|
40 | exports.getAssociations = getAssociations;
|
41 | function setAssociations(target, associations) {
|
42 | Reflect.defineMetadata(ASSOCIATIONS_KEY, associations, target);
|
43 | }
|
44 | exports.setAssociations = setAssociations;
|
45 | function getAssociationsByRelation(target, relatedClass) {
|
46 | const associations = getAssociations(target);
|
47 | return (associations || []).filter((association) => {
|
48 | const _relatedClass = association.getAssociatedClass();
|
49 | return (_relatedClass.prototype === relatedClass.prototype ||
|
50 | relatedClass.prototype instanceof _relatedClass);
|
51 | });
|
52 | }
|
53 | exports.getAssociationsByRelation = getAssociationsByRelation;
|
54 |
|
\ | No newline at end of file |