-
Notifications
You must be signed in to change notification settings - Fork 26
/
repository.js
214 lines (188 loc) · 5.25 KB
/
repository.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Model, transaction } from 'objection';
import BaseModel from '../common/base-model';
import { jsonSchema } from 'common/identity/utils';
import IdAttributeType from './id-attribute-type';
import { Logger } from 'common/logger';
import { UiSchema } from './ui-schema';
import config from 'common/config';
const env = config.attributeTypeSource;
const log = new Logger('repository-model');
const TABLE_NAME = 'repository';
export const REPOSITORY_EXPIRES_DEFAULT = 86400000; // 1 day
export class Repository extends BaseModel {
static tableName = TABLE_NAME;
static idColumn = 'id';
static jsonSchema = {
type: 'object',
required: ['url'],
properties: {
id: { type: 'integer' },
url: { type: 'string' },
name: { type: 'string' },
eager: { type: 'boolean', default: false },
content: { type: 'object' },
expires: { type: 'integer' },
env: { type: 'string', enum: ['production', 'development'], default: env }
}
};
static get relationMappings() {
const IdAttributeType = require('./id-attribute-type').default;
const UiSchema = require('./ui-schema').default;
return {
attributeTypes: {
relation: Model.ManyToManyRelation,
modelClass: IdAttributeType,
join: {
from: `${this.tableName}.id`,
through: {
from: 'repository_attribute_types.repositoryId',
to: 'repository_attribute_types.attributeTypeId'
},
to: `${IdAttributeType.tableName}.id`
}
},
uiSchemas: {
relation: Model.HasManyRelation,
modelClass: UiSchema,
join: {
from: `${this.tableName}.id`,
to: `${UiSchema.tableName}.repositoryId`
}
}
};
}
static findById(id, tx) {
return this.query(tx).findById(id);
}
static findByUrl(url, tx) {
return this.query(tx).findOne({ url, env });
}
static create(itm, tx) {
return this.query(tx).insertAndFetch({ ...itm, env });
}
static delete(id, tx) {
return this.query(tx).deleteById(id);
}
static findAll(where) {
return this.query().where({ ...where, env } || { env });
}
static async loadRemote(url) {
let remoteRepo = await jsonSchema.loadRemoteRepository(url, { env });
return {
url,
name: remoteRepo.name,
content: remoteRepo,
expires: Date.now() + (remoteRepo.expires || REPOSITORY_EXPIRES_DEFAULT)
};
}
diffAttributes(repo) {
let incomingAttributes = repo.content.identityAttributes || [];
let currAttr = this.content.identityAttributes || [];
let existingMap = currAttr.reduce((acc, curr) => {
if (typeof curr === 'string') {
curr = { json: curr };
}
if (!curr.json) return;
acc[curr.json] = curr;
return acc;
}, {});
let updates = incomingAttributes.reduce(
(acc, curr) => {
if (typeof curr === 'string') {
curr = { json: curr };
}
if (!curr.json) return acc;
let existing = existingMap[curr.json];
if (!existing) {
acc.add.push(curr);
return acc;
}
if (curr.ui !== existing.ui) {
if (existing.ui) {
acc.delete.push({ ui: existing.ui });
}
if (curr.ui) {
acc.add.push(curr);
}
}
delete existingMap[curr.json];
return acc;
},
{
delete: [],
add: []
}
);
for (let attr in existingMap) {
updates.delete.push(existingMap[attr]);
}
return updates;
}
async updateAttributes(updates, tx) {
for (let attr of updates.delete || []) {
await this.deleteAttribute(attr, tx);
}
for (let attr of updates.add || []) {
await this.addAttribute(attr, tx);
}
}
async addAttribute(attr, tx) {
if (typeof attr === 'string') {
attr = { json: attr };
}
if (!attr.json) return;
let attrType = await IdAttributeType.findByUrl(attr.json, tx);
if (!attrType) attrType = await IdAttributeType.create({ url: attr.json }, tx);
await attrType.$relatedQuery('repositories', tx).relate(this.id);
if (!attr.ui) return;
let uiSchema = await UiSchema.findByUrl(attr.ui, this.id, tx);
if (uiSchema) return;
await UiSchema.create(
{
url: attr.ui,
repositoryId: this.id,
attributeTypeId: attrType.id
},
tx
);
}
async deleteAttribute(attr, tx) {
log.info('deleting attribute type %j', attr);
if (attr.ui) {
let uiSchema = await UiSchema.findByUrl(attr.ui, this.id, tx);
if (uiSchema) await uiSchema.$query(tx).delete();
}
if (!attr.json) return;
let attrType = await IdAttributeType.findByUrl(attr.json, tx).eager('idAttributes');
if (!attrType) return;
await this.$relatedQuery('attributeTypes', tx)
.unrelate()
.where('id', attrType.id);
await attrType.$loadRelated('[repositories, idAttributes]', null, tx);
if (!attrType.repositories.length && !attrType.idAttributes.length)
await attrType.$query(tx).delete();
}
static async addRemoteRepo(url) {
let [remote, repo] = await Promise.all([this.loadRemote(url), this.findByUrl(url)]);
if (!remote) {
log.error('could not load repo %s from remote', url);
return;
}
const tx = await transaction.start(this.knex());
try {
if (!repo) {
repo = await this.create({ url, content: {} }, tx);
}
let updates = repo.diffAttributes(remote);
await repo.updateAttributes(updates, tx);
repo = await repo.$query(tx).patchAndFetch(remote);
await tx.commit();
return repo;
} catch (error) {
log.error(error);
await tx.rollback(error);
throw error;
}
}
}
export default Repository;