-
Notifications
You must be signed in to change notification settings - Fork 3
/
ActiveLinker.js
185 lines (170 loc) · 6.76 KB
/
ActiveLinker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
*/
'use strict';
const Base = require('../base/Base');
module.exports = class ActiveLinker extends Base {
async link (name, model, extraColumns) {
const relation = this.owner.getRelation(name);
const method = relation.isOuterLink() ? 'linkVia' : 'linkInternal';
await this[method](relation, model, extraColumns);
if (!relation.isMultiple()) {
this.owner.populateRelation(name, model);
return PromiseHelper.setImmediate();
}
const related = this.owner.getRelated(name);
if (!related) {
return PromiseHelper.setImmediate();
}
const index = relation.getIndex();
if (index) {
related[index] = model;
} else {
related.push(model);
}
return PromiseHelper.setImmediate();
}
linkInternal (relation, model) {
return relation.isBackRef()
? this.bindModels(relation.refKey, relation.linkKey, model, this.owner, relation)
: this.bindModels(relation.linkKey, relation.refKey, this.owner, model, relation);
}
linkVia (relation, model, extraColumns) {
const via = relation.getViaTable() || relation.getViaRelation();
const columns = {
[via.refKey]: this.owner.get(via.linkKey),
[relation.linkKey]: model.get(relation.refKey)
};
if (extraColumns) {
Object.assign(columns, extraColumns);
}
if (relation.getViaTable()) {
return this.owner.getDb().insert(via.getTable(), columns);
}
// unset related so that it can be reloaded to reflect the changes
this.owner.unsetRelated(relation.getViaRelationName());
model = this.owner.spawn(via.model.constructor);
model.assign(columns);
return model.insert();
}
async unlink (name, model, deletion) {
const relation = this.owner.getRelation(name);
const method = relation.isOuterLink() ? 'unlinkVia' : 'unlinkInternal';
await this[method](relation, model, deletion);
this.unsetUnlinked(name, model, relation);
return PromiseHelper.setImmediate();
}
async unlinkInternal (relation, model) {
const ref = model.get(relation.refKey);
const link = this.owner.get(relation.linkKey);
relation.isBackRef()
? await QueryHelper.unlinkInternal(ref, link, model, relation.refKey)
: await QueryHelper.unlinkInternal(link, ref, this.owner, relation.linkKey);
}
unlinkVia (relation, model, deletion = true) {
const via = relation.getViaTable() || relation.getViaRelation();
const condition = {
[via.refKey]: this.owner.get(via.linkKey),
[relation.linkKey]: model.get(relation.refKey)
};
const nulls = {
[via.refKey]: null,
[relation.linkKey]: null
};
if (relation.getViaTable()) {
return deletion
? this.owner.getDb().delete(via.getTable(), condition)
: this.owner.getDb().update(via.getTable(), condition, nulls);
}
this.owner.unsetRelated(relation.getViaRelationName());
return deletion
? via.model.find(condition).delete()
: via.model.find(condition).updateAll(nulls);
}
async unlinkAll (name, deletion) {
const relation = this.owner.getRelation(name);
if (relation) {
const method = relation.isOuterLink()
? 'unlinkViaAll'
: 'unlinkInternalAll';
await this[method](relation, deletion);
this.owner.unsetRelated(name);
}
}
async unlinkViaAll (relation, deletion = true) {
if (relation.getViaRelation()) {
this.owner.unsetRelated(relation.getViaRelationName());
}
const via = relation.getViaTable() || relation.getViaRelation();
let condition = {[via.refKey]: this.owner.get(via.linkKey)};
if (via.getWhere()) {
condition = ['and', condition, via.getWhere()];
}
const nulls = {[via.refKey]: null};
if (relation.getViaTable()) {
const db = this.owner.getDb();
condition = db.buildCondition(condition);
deletion ? await db.delete(via.getTable(), condition)
: await db.update(via.getTable(), condition, nulls);
} else if (deletion) {
const models = await via.model.find(condition).all();
for (const model of models) {
await model.delete();
}
} else {
await via.model.find(condition).updateAll(nulls);
}
}
async unlinkInternalAll (relation) {
// relation via array valued attribute
const link = this.owner.get(relation.linkKey);
if (Array.isArray(link)) {
return this.owner.directUpdate({[relation.linkKey]: []});
}
let condition = {[relation.refKey]: link};
if (relation.getWhere()) {
condition = ['and', condition, relation.getWhere()];
}
if (relation.getViaArray()) {
const table = relation.model.getTable();
await relation.model.getDb().updateAllPull(table, {}, condition);
} else {
await relation.model.find(condition).updateAll({[relation.refKey]: null});
}
}
unsetUnlinked (name, model, relation) {
if (!relation.isMultiple()) {
return this.owner.unsetRelated(name);
}
const models = this.owner.getRelated(name);
if (Array.isArray(models)) {
const id = model.getId();
const result = models.filter(m => !CommonHelper.isEqual(id, m.getId()));
this.owner.populateRelation(name, result);
}
}
bindModels (foreignKey, primaryKey, foreignModel, primaryModel, relation) {
const value = primaryModel.get(primaryKey);
if (!value) {
throw new Error(this.wrapMessage('Primary key is null'));
}
if (!relation.getViaArray()) {
return foreignModel.directUpdate({[foreignKey]: value});
}
let values = foreignModel.get(foreignKey);
if (!Array.isArray(values)) {
values = [];
}
if (!ArrayHelper.includes(value, values)) {
values.push(value);
return foreignModel.directUpdate({[foreignKey]: values});
}
}
wrapMessage (message) {
return `${this.constructor.name}: ${this.owner.wrapMessage(message)}`;
}
};
const ArrayHelper = require('../helper/ArrayHelper');
const CommonHelper = require('../helper/CommonHelper');
const PromiseHelper = require('../helper/PromiseHelper');
const QueryHelper = require('../helper/QueryHelper');