This repository was archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathrelationHelpers.js
215 lines (190 loc) · 5.94 KB
/
relationHelpers.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
215
const { snakeCase } = require('lodash/fp');
const { dbV3, isPGSQL, isSQLITE, isMYSQL, dbV4 } = require('../../config/database');
const pluralize = require('pluralize');
const { migrate } = require('./migrate');
const { migrateItem } = require('./migrateFields');
const { omit } = require('lodash');
const { singular } = pluralize;
const snakeCaseObj = obj => {
const snake_case_obj = {};
Object.keys(obj).forEach(key => {
snake_case_obj[key] = snakeCase(obj[key]);
});
return snake_case_obj;
};
function addRelation(
{ uid, model, attribute, type, modelF = undefined, attributeF = undefined, isComponent = false },
relations
) {
const entitUid = uid.split('.');
const entityName = snakeCase(entitUid[entitUid.length - 1]);
relations.push({
model,
attribute,
type,
modelF,
attributeF,
table: `${snakeCase(model)}_${snakeCase(attribute)}_links`,
entityName,
isComponent
});
}
function processRelation({ key, value, collectionName, uid, isComponent }, relations) {
if (value.model) {
addRelation(
{
uid,
model: collectionName,
attribute: key,
type: 'oneToOne',
modelF: value.model,
attributeF: value.via,
isComponent
},
relations
);
} else if (value.collection) {
if (value.dominant) {
addRelation(
{
uid,
model: collectionName,
attribute: key,
type: 'manyToMany',
modelF: value.collection,
attributeF: value.via,
isComponent
},
relations
);
} else if (!value.isVirtual) {
addRelation(
{
uid,
model: collectionName,
attribute: key,
type: 'oneToMany',
modelF: value.collection,
attributeF: value.via,
isComponent
},
relations
);
}
}
}
function makeRelationModelId(model, options = {}) {
if (options.isComponent) {
return `${snakeCase(model)}_id`;
}
return `${snakeCase(pluralize(model, 1))}_id`;
}
function oneToOneRelationMapper(relation, item) {
const id = item.id;
const idF = item[relation.attribute];
if (id && idF) {
const keyF = relation.entityName === relation.modelF
? `inv_${makeRelationModelId(relation.modelF)}`
: makeRelationModelId(relation.modelF);
return {
[makeRelationModelId(relation.entityName, { isComponent: relation.isComponent })]: id,
[makeRelationModelId(relation.modelF)]: idF,
};
}
return undefined;
}
function oneToOneCirvleRelationMapper(relation, item) {
const id = item.id;
const invId = item[relation.attribute];
if (id && invId) {
return {
[makeRelationModelId(relation.model)]: id,
[`inv_${makeRelationModelId(relation.model)}`]: invId,
};
}
return undefined;
}
async function migrateOneToOneRelation(relation) {
if (singular(relation.model) === singular(relation.modelF)) {
await migrate(relation.model, relation.table, (item) =>
oneToOneCirvleRelationMapper(relation, item)
);
} else {
await migrate(relation.model, relation.table, (item) => oneToOneRelationMapper(relation, item));
}
}
async function migrateOneToManyRelation(relation, sourceTable) {
await migrate(sourceTable, relation.table);
}
async function migrateManyToManyRelation(relation, sourceTable) {
if (pluralize(relation.model, 1) === relation.modelF) {
await migrate(sourceTable, relation.table, ({ id, ...item }) => ({
[makeRelationModelId(relation.model)]: item[`${relation.modelF}_id`],
[`inv_${makeRelationModelId(relation.model)}`]: item[`${singular(relation.attribute)}_id`],
}));
} else {
const fromModelRelation = makeRelationModelId(relation.model);
const fromNameRelation = makeRelationModelId(relation.entityName);
await migrate(sourceTable, relation.table, ({ id, ...item }) => {
if (fromModelRelation === fromNameRelation) {
return migrateItem(item);
}
const newRelationObject = {
...item,
[fromNameRelation]: item[fromModelRelation],
};
return migrateItem(omit(newRelationObject, [fromModelRelation]));
});
}
}
async function migrateRelations(tables, relations) {
let v4Tables = [];
if (isPGSQL) {
v4Tables = (
await dbV4('information_schema.tables')
.select('table_name')
.where('table_schema', process.env.DATABASE_V4_SCHEMA)
).map((row) => row.table_name);
}
if (isSQLITE) {
v4Tables = (await dbV4('sqlite_master').select('name')).map((row) => row.name);
}
if (isMYSQL) {
v4Tables = (await dbV4('information_schema.tables').select('table_name')).map(
(row) => row.table_name || row.TABLE_NAME
);
}
const mappedRelations = relations.map((r) => {
if (r.table.startsWith('users_permissions_user') && r.table.endsWith('_links')) {
return { ...r, table: r.table.replace('users_permissions_user', 'up_users') };
}
return r;
});
relations = mappedRelations.filter((r) => v4Tables.includes(r.table));
const v3RelationTables = tables.filter((t) => t.includes('__'));
for (const relation of relations) {
if (relation.type === 'oneToOne') {
await migrateOneToOneRelation(relation);
} else {
const relation_snake = snakeCaseObj(relation);
var sourceTable = v3RelationTables.find(
(t) =>
t === `${relation_snake.model}__${relation_snake.attribute}` ||
t.startsWith(`${relation_snake.model}_${relation_snake.attribute}__${relation.modelF}`) ||
(t.startsWith(`${relation_snake.modelF}`) &&
t.endsWith(`__${relation_snake.model}_${relation_snake.attribute}`))
);
if (sourceTable) {
if (relation.type === 'manyToMany') {
await migrateManyToManyRelation(relation, sourceTable);
} else if (relation.type === 'oneToMany') {
await migrateOneToManyRelation(relation, sourceTable);
}
}
}
}
}
module.exports = {
processRelation,
migrateRelations,
};