@strapi/database 4.14.4 → 4.14.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/connection.d.ts +3 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/dialects/dialect.d.ts +27 -0
- package/dist/dialects/dialect.d.ts.map +1 -0
- package/dist/dialects/index.d.ts +5 -0
- package/dist/dialects/index.d.ts.map +1 -0
- package/dist/dialects/mysql/constants.d.ts +3 -0
- package/dist/dialects/mysql/constants.d.ts.map +1 -0
- package/dist/dialects/mysql/database-inspector.d.ts +12 -0
- package/dist/dialects/mysql/database-inspector.d.ts.map +1 -0
- package/dist/dialects/mysql/index.d.ts +20 -0
- package/dist/dialects/mysql/index.d.ts.map +1 -0
- package/dist/dialects/mysql/schema-inspector.d.ts +13 -0
- package/dist/dialects/mysql/schema-inspector.d.ts.map +1 -0
- package/dist/dialects/postgresql/index.d.ts +14 -0
- package/dist/dialects/postgresql/index.d.ts.map +1 -0
- package/dist/dialects/postgresql/schema-inspector.d.ts +14 -0
- package/dist/dialects/postgresql/schema-inspector.d.ts.map +1 -0
- package/dist/dialects/sqlite/index.d.ts +19 -0
- package/dist/dialects/sqlite/index.d.ts.map +1 -0
- package/dist/dialects/sqlite/schema-inspector.d.ts +13 -0
- package/dist/dialects/sqlite/schema-inspector.d.ts.map +1 -0
- package/dist/entity-manager/entity-repository.d.ts +4 -0
- package/dist/entity-manager/entity-repository.d.ts.map +1 -0
- package/dist/entity-manager/index.d.ts +5 -0
- package/dist/entity-manager/index.d.ts.map +1 -0
- package/dist/entity-manager/morph-relations.d.ts +13 -0
- package/dist/entity-manager/morph-relations.d.ts.map +1 -0
- package/dist/entity-manager/regular-relations.d.ts +83 -0
- package/dist/entity-manager/regular-relations.d.ts.map +1 -0
- package/dist/entity-manager/relations/cloning/regular-relations.d.ts +17 -0
- package/dist/entity-manager/relations/cloning/regular-relations.d.ts.map +1 -0
- package/dist/entity-manager/relations-orderer.d.ts +73 -0
- package/dist/entity-manager/relations-orderer.d.ts.map +1 -0
- package/dist/entity-manager/types.d.ts +97 -0
- package/dist/entity-manager/types.d.ts.map +1 -0
- package/dist/errors/database.d.ts +5 -0
- package/dist/errors/database.d.ts.map +1 -0
- package/dist/errors/index.d.ts +8 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/invalid-date.d.ts +5 -0
- package/dist/errors/invalid-date.d.ts.map +1 -0
- package/dist/errors/invalid-datetime.d.ts +5 -0
- package/dist/errors/invalid-datetime.d.ts.map +1 -0
- package/dist/errors/invalid-relation.d.ts +5 -0
- package/dist/errors/invalid-relation.d.ts.map +1 -0
- package/dist/errors/invalid-time.d.ts +5 -0
- package/dist/errors/invalid-time.d.ts.map +1 -0
- package/dist/errors/not-null.d.ts +7 -0
- package/dist/errors/not-null.d.ts.map +1 -0
- package/dist/fields/biginteger.d.ts +4 -0
- package/dist/fields/biginteger.d.ts.map +1 -0
- package/dist/fields/boolean.d.ts +6 -0
- package/dist/fields/boolean.d.ts.map +1 -0
- package/dist/fields/date.d.ts +6 -0
- package/dist/fields/date.d.ts.map +1 -0
- package/dist/fields/datetime.d.ts +6 -0
- package/dist/fields/datetime.d.ts.map +1 -0
- package/dist/fields/field.d.ts +7 -0
- package/dist/fields/field.d.ts.map +1 -0
- package/dist/fields/index.d.ts +4 -0
- package/dist/fields/index.d.ts.map +1 -0
- package/dist/fields/json.d.ts +6 -0
- package/dist/fields/json.d.ts.map +1 -0
- package/dist/fields/number.d.ts +6 -0
- package/dist/fields/number.d.ts.map +1 -0
- package/dist/fields/shared/parsers.d.ts +4 -0
- package/dist/fields/shared/parsers.d.ts.map +1 -0
- package/dist/fields/string.d.ts +6 -0
- package/dist/fields/string.d.ts.map +1 -0
- package/dist/fields/time.d.ts +6 -0
- package/dist/fields/time.d.ts.map +1 -0
- package/dist/fields/timestamp.d.ts +6 -0
- package/dist/fields/timestamp.d.ts.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6211 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +6179 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lifecycles/index.d.ts +17 -0
- package/dist/lifecycles/index.d.ts.map +1 -0
- package/dist/lifecycles/subscribers/index.d.ts +5 -0
- package/dist/lifecycles/subscribers/index.d.ts.map +1 -0
- package/dist/lifecycles/subscribers/models-lifecycles.d.ts +6 -0
- package/dist/lifecycles/subscribers/models-lifecycles.d.ts.map +1 -0
- package/dist/lifecycles/subscribers/timestamps.d.ts +3 -0
- package/dist/lifecycles/subscribers/timestamps.d.ts.map +1 -0
- package/dist/lifecycles/types.d.ts +25 -0
- package/dist/lifecycles/types.d.ts.map +1 -0
- package/dist/metadata/index.d.ts +10 -0
- package/dist/metadata/index.d.ts.map +1 -0
- package/dist/metadata/metadata.d.ts +22 -0
- package/dist/metadata/metadata.d.ts.map +1 -0
- package/dist/metadata/relations.d.ts +16 -0
- package/dist/metadata/relations.d.ts.map +1 -0
- package/dist/migrations/index.d.ts +12 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/migrations/storage.d.ts +15 -0
- package/dist/migrations/storage.d.ts.map +1 -0
- package/dist/query/helpers/index.d.ts +8 -0
- package/dist/query/helpers/index.d.ts.map +1 -0
- package/dist/query/helpers/join.d.ts +30 -0
- package/dist/query/helpers/join.d.ts.map +1 -0
- package/dist/query/helpers/order-by.d.ts +14 -0
- package/dist/query/helpers/order-by.d.ts.map +1 -0
- package/dist/query/helpers/populate/apply.d.ts +11 -0
- package/dist/query/helpers/populate/apply.d.ts.map +1 -0
- package/dist/query/helpers/populate/index.d.ts +4 -0
- package/dist/query/helpers/populate/index.d.ts.map +1 -0
- package/dist/query/helpers/populate/process.d.ts +24 -0
- package/dist/query/helpers/populate/process.d.ts.map +1 -0
- package/dist/query/helpers/search.d.ts +4 -0
- package/dist/query/helpers/search.d.ts.map +1 -0
- package/dist/query/helpers/streams/index.d.ts +2 -0
- package/dist/query/helpers/streams/index.d.ts.map +1 -0
- package/dist/query/helpers/streams/readable.d.ts +39 -0
- package/dist/query/helpers/streams/readable.d.ts.map +1 -0
- package/dist/query/helpers/transform.d.ts +8 -0
- package/dist/query/helpers/transform.d.ts.map +1 -0
- package/dist/query/helpers/where.d.ts +19 -0
- package/dist/query/helpers/where.d.ts.map +1 -0
- package/dist/query/index.d.ts +3 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/query-builder.d.ts +79 -0
- package/dist/query/query-builder.d.ts.map +1 -0
- package/dist/query/types.d.ts +8 -0
- package/dist/query/types.d.ts.map +1 -0
- package/dist/schema/builder.d.ts +33 -0
- package/dist/schema/builder.d.ts.map +1 -0
- package/dist/schema/diff.d.ts +7 -0
- package/dist/schema/diff.d.ts.map +1 -0
- package/dist/schema/index.d.ts +20 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/schema.d.ts +4 -0
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/schema/storage.d.ts +10 -0
- package/dist/schema/storage.d.ts.map +1 -0
- package/dist/schema/types.d.ts +103 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/transaction-context.d.ts +22 -0
- package/dist/transaction-context.d.ts.map +1 -0
- package/dist/types/index.d.ts +169 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/utils/content-types.d.ts +13 -0
- package/dist/utils/content-types.d.ts.map +1 -0
- package/dist/utils/knex.d.ts +12 -0
- package/dist/utils/knex.d.ts.map +1 -0
- package/dist/utils/types.d.ts +10 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/validations/index.d.ts +6 -0
- package/dist/validations/index.d.ts.map +1 -0
- package/dist/validations/relations/bidirectional.d.ts +12 -0
- package/dist/validations/relations/bidirectional.d.ts.map +1 -0
- package/dist/validations/relations/index.d.ts +7 -0
- package/dist/validations/relations/index.d.ts.map +1 -0
- package/package.json +20 -8
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -4
- package/jest.config.js +0 -6
- package/lib/__tests__/index.test.js +0 -93
- package/lib/__tests__/lifecycles.test.js +0 -55
- package/lib/connection.js +0 -64
- package/lib/dialects/dialect.js +0 -63
- package/lib/dialects/index.js +0 -53
- package/lib/dialects/mysql/constants.js +0 -6
- package/lib/dialects/mysql/database-inspector.js +0 -37
- package/lib/dialects/mysql/index.js +0 -92
- package/lib/dialects/mysql/schema-inspector.js +0 -234
- package/lib/dialects/postgresql/index.js +0 -65
- package/lib/dialects/postgresql/schema-inspector.js +0 -283
- package/lib/dialects/sqlite/index.js +0 -87
- package/lib/dialects/sqlite/schema-inspector.js +0 -151
- package/lib/entity-manager/__tests__/relations-orderer.test.js +0 -186
- package/lib/entity-manager/__tests__/sort-connect-array.test.js +0 -79
- package/lib/entity-manager/entity-repository.js +0 -164
- package/lib/entity-manager/index.js +0 -1385
- package/lib/entity-manager/morph-relations.js +0 -63
- package/lib/entity-manager/regular-relations.js +0 -506
- package/lib/entity-manager/relations/cloning/regular-relations.js +0 -76
- package/lib/entity-manager/relations-orderer.js +0 -225
- package/lib/errors/database.js +0 -12
- package/lib/errors/index.js +0 -17
- package/lib/errors/invalid-date.js +0 -14
- package/lib/errors/invalid-datetime.js +0 -14
- package/lib/errors/invalid-relation.js +0 -14
- package/lib/errors/invalid-time.js +0 -14
- package/lib/errors/not-null.js +0 -15
- package/lib/fields/biginteger.js +0 -17
- package/lib/fields/boolean.js +0 -39
- package/lib/fields/date.js +0 -16
- package/lib/fields/datetime.js +0 -19
- package/lib/fields/field.js +0 -17
- package/lib/fields/index.d.ts +0 -9
- package/lib/fields/index.js +0 -50
- package/lib/fields/json.js +0 -21
- package/lib/fields/number.js +0 -23
- package/lib/fields/shared/parsers.js +0 -71
- package/lib/fields/string.js +0 -17
- package/lib/fields/time.js +0 -17
- package/lib/fields/timestamp.js +0 -19
- package/lib/index.d.ts +0 -198
- package/lib/index.js +0 -129
- package/lib/lifecycles/index.d.ts +0 -51
- package/lib/lifecycles/index.js +0 -90
- package/lib/lifecycles/subscribers/index.d.ts +0 -11
- package/lib/lifecycles/subscribers/models-lifecycles.js +0 -19
- package/lib/lifecycles/subscribers/timestamps.js +0 -65
- package/lib/metadata/index.js +0 -244
- package/lib/metadata/relations.js +0 -578
- package/lib/migrations/index.d.ts +0 -9
- package/lib/migrations/index.js +0 -75
- package/lib/migrations/storage.js +0 -44
- package/lib/query/helpers/index.js +0 -11
- package/lib/query/helpers/join.js +0 -96
- package/lib/query/helpers/order-by.js +0 -70
- package/lib/query/helpers/populate/apply.js +0 -664
- package/lib/query/helpers/populate/index.js +0 -9
- package/lib/query/helpers/populate/process.js +0 -102
- package/lib/query/helpers/search.js +0 -84
- package/lib/query/helpers/streams/index.js +0 -5
- package/lib/query/helpers/streams/readable.js +0 -174
- package/lib/query/helpers/transform.js +0 -84
- package/lib/query/helpers/where.js +0 -365
- package/lib/query/index.js +0 -7
- package/lib/query/query-builder.js +0 -514
- package/lib/schema/__tests__/schema-diff.test.js +0 -231
- package/lib/schema/builder.js +0 -386
- package/lib/schema/diff.js +0 -399
- package/lib/schema/index.d.ts +0 -49
- package/lib/schema/index.js +0 -94
- package/lib/schema/schema.js +0 -202
- package/lib/schema/storage.js +0 -76
- package/lib/transaction-context.js +0 -68
- package/lib/types/index.d.ts +0 -6
- package/lib/types/index.js +0 -35
- package/lib/utils/content-types.js +0 -40
- package/lib/utils/knex.js +0 -22
- package/lib/validations/index.js +0 -20
- package/lib/validations/relations/bidirectional.js +0 -89
- package/lib/validations/relations/index.js +0 -14
|
@@ -1,578 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module relations
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
'use strict';
|
|
6
|
-
|
|
7
|
-
const _ = require('lodash/fp');
|
|
8
|
-
|
|
9
|
-
const hasInversedBy = _.has('inversedBy');
|
|
10
|
-
const hasMappedBy = _.has('mappedBy');
|
|
11
|
-
|
|
12
|
-
const isPolymorphic = (attribute) =>
|
|
13
|
-
['morphOne', 'morphMany', 'morphToOne', 'morphToMany'].includes(attribute.relation);
|
|
14
|
-
const isOneToAny = (attribute) => ['oneToOne', 'oneToMany'].includes(attribute.relation);
|
|
15
|
-
const isManyToAny = (attribute) => ['manyToMany', 'manyToOne'].includes(attribute.relation);
|
|
16
|
-
const isAnyToOne = (attribute) => ['oneToOne', 'manyToOne'].includes(attribute.relation);
|
|
17
|
-
const isAnyToMany = (attribute) => ['oneToMany', 'manyToMany'].includes(attribute.relation);
|
|
18
|
-
const isBidirectional = (attribute) => hasInversedBy(attribute) || hasMappedBy(attribute);
|
|
19
|
-
const isOwner = (attribute) => !isBidirectional(attribute) || hasInversedBy(attribute);
|
|
20
|
-
const shouldUseJoinTable = (attribute) => attribute.useJoinTable !== false;
|
|
21
|
-
const getJoinTableName = (tableName, attributeName) =>
|
|
22
|
-
_.snakeCase(`${tableName}_${attributeName}_links`);
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Creates a oneToOne relation metadata
|
|
26
|
-
*
|
|
27
|
-
* if owner then
|
|
28
|
-
* if with join table then
|
|
29
|
-
* create join table
|
|
30
|
-
* else
|
|
31
|
-
* create joinColumn
|
|
32
|
-
* if bidirectional then
|
|
33
|
-
* set inverse attribute joinCol or joinTable info correctly
|
|
34
|
-
* else
|
|
35
|
-
* this property must be set by the owner side
|
|
36
|
-
* verify the owner side is valid // should be done before or at the same time ?
|
|
37
|
-
*
|
|
38
|
-
* @param {string} attributeName
|
|
39
|
-
* @param {Attribute} attribute
|
|
40
|
-
* @param {ModelMetadata} meta
|
|
41
|
-
* @param {Metadata} metadata
|
|
42
|
-
* @retuns void
|
|
43
|
-
*/
|
|
44
|
-
const createOneToOne = (attributeName, attribute, meta, metadata) => {
|
|
45
|
-
if (isOwner(attribute)) {
|
|
46
|
-
if (shouldUseJoinTable(attribute)) {
|
|
47
|
-
createJoinTable(metadata, {
|
|
48
|
-
attribute,
|
|
49
|
-
attributeName,
|
|
50
|
-
meta,
|
|
51
|
-
});
|
|
52
|
-
} else {
|
|
53
|
-
createJoinColum(metadata, {
|
|
54
|
-
attribute,
|
|
55
|
-
attributeName,
|
|
56
|
-
meta,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
// TODO: verify other side is valid
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Creates a oneToMany relation metadata
|
|
66
|
-
*
|
|
67
|
-
* if unidirectional then
|
|
68
|
-
* create join table
|
|
69
|
-
* if bidirectional then
|
|
70
|
-
* cannot be owning side
|
|
71
|
-
* do nothing
|
|
72
|
-
*
|
|
73
|
-
* @param {string} attributeName
|
|
74
|
-
* @param {Attribute} attribute
|
|
75
|
-
* @param {ModelMetadata} meta
|
|
76
|
-
* @param {Metadata} metadata
|
|
77
|
-
*/
|
|
78
|
-
const createOneToMany = (attributeName, attribute, meta, metadata) => {
|
|
79
|
-
if (!isBidirectional(attribute)) {
|
|
80
|
-
createJoinTable(metadata, {
|
|
81
|
-
attribute,
|
|
82
|
-
attributeName,
|
|
83
|
-
meta,
|
|
84
|
-
});
|
|
85
|
-
} else if (isOwner(attribute)) {
|
|
86
|
-
throw new Error('one side of a oneToMany cannot be the owner side in a bidirectional relation');
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Creates a manyToOne relation metadata
|
|
92
|
-
*
|
|
93
|
-
* if unidirectional then
|
|
94
|
-
* if with join table then
|
|
95
|
-
* create join table
|
|
96
|
-
* else
|
|
97
|
-
* create join column
|
|
98
|
-
* else
|
|
99
|
-
* must be the owner side
|
|
100
|
-
* if with join table then
|
|
101
|
-
* create join table
|
|
102
|
-
* else
|
|
103
|
-
* create join column
|
|
104
|
-
* set inverse attribute joinCol or joinTable info correctly
|
|
105
|
-
*
|
|
106
|
-
* @param {string} attributeName
|
|
107
|
-
* @param {Attribute} attribute
|
|
108
|
-
* @param {ModelMetadata} meta
|
|
109
|
-
* @param {Metadata} metadata
|
|
110
|
-
*/
|
|
111
|
-
const createManyToOne = (attributeName, attribute, meta, metadata) => {
|
|
112
|
-
if (isBidirectional(attribute) && !isOwner(attribute)) {
|
|
113
|
-
throw new Error('The many side of a manyToOne must be the owning side');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (shouldUseJoinTable(attribute)) {
|
|
117
|
-
createJoinTable(metadata, {
|
|
118
|
-
attribute,
|
|
119
|
-
attributeName,
|
|
120
|
-
meta,
|
|
121
|
-
});
|
|
122
|
-
} else {
|
|
123
|
-
createJoinColum(metadata, {
|
|
124
|
-
attribute,
|
|
125
|
-
attributeName,
|
|
126
|
-
meta,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Creates a manyToMany relation metadata
|
|
133
|
-
*
|
|
134
|
-
* if unidirectional
|
|
135
|
-
* create join table
|
|
136
|
-
* else
|
|
137
|
-
* if owner then
|
|
138
|
-
* if with join table then
|
|
139
|
-
* create join table
|
|
140
|
-
* else
|
|
141
|
-
* do nothing
|
|
142
|
-
*
|
|
143
|
-
* @param {string} attributeName
|
|
144
|
-
* @param {Attribute} attribute
|
|
145
|
-
* @param {ModelMetadata} meta
|
|
146
|
-
* @param {Metadata} metadata
|
|
147
|
-
*/
|
|
148
|
-
const createManyToMany = (attributeName, attribute, meta, metadata) => {
|
|
149
|
-
if (!isBidirectional(attribute) || isOwner(attribute)) {
|
|
150
|
-
createJoinTable(metadata, {
|
|
151
|
-
attribute,
|
|
152
|
-
attributeName,
|
|
153
|
-
meta,
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Creates a morphToOne relation metadata
|
|
160
|
-
*
|
|
161
|
-
* if with join table then
|
|
162
|
-
* create join table
|
|
163
|
-
* else
|
|
164
|
-
* create join columnsa
|
|
165
|
-
*
|
|
166
|
-
* if bidirectionnal
|
|
167
|
-
* set info in the traget
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* @param {string} attributeName
|
|
171
|
-
* @param {Attribute} attribute
|
|
172
|
-
* @param {ModelMetadata} meta
|
|
173
|
-
* @param {Metadata} metadata
|
|
174
|
-
*/
|
|
175
|
-
const createMorphToOne = (attributeName, attribute /* meta, metadata */) => {
|
|
176
|
-
const idColumnName = 'target_id';
|
|
177
|
-
const typeColumnName = 'target_type';
|
|
178
|
-
|
|
179
|
-
Object.assign(attribute, {
|
|
180
|
-
owner: true,
|
|
181
|
-
morphColumn: {
|
|
182
|
-
// TODO: add referenced column
|
|
183
|
-
typeColumn: {
|
|
184
|
-
name: typeColumnName,
|
|
185
|
-
},
|
|
186
|
-
idColumn: {
|
|
187
|
-
name: idColumnName,
|
|
188
|
-
referencedColumn: 'id',
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
// TODO: implement bidirectional
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Creates a morphToMany relation metadata
|
|
198
|
-
*
|
|
199
|
-
* @param {string} attributeName
|
|
200
|
-
* @param {Attribute} attribute
|
|
201
|
-
* @param {ModelMetadata} meta
|
|
202
|
-
* @param {Metadata} metadata
|
|
203
|
-
*/
|
|
204
|
-
const createMorphToMany = (attributeName, attribute, meta, metadata) => {
|
|
205
|
-
const joinTableName = _.snakeCase(`${meta.tableName}_${attributeName}_morphs`);
|
|
206
|
-
|
|
207
|
-
const joinColumnName = _.snakeCase(`${meta.singularName}_id`);
|
|
208
|
-
const morphColumnName = _.snakeCase(`${attributeName}`);
|
|
209
|
-
const idColumnName = `${morphColumnName}_id`;
|
|
210
|
-
const typeColumnName = `${morphColumnName}_type`;
|
|
211
|
-
|
|
212
|
-
metadata.add({
|
|
213
|
-
uid: joinTableName,
|
|
214
|
-
tableName: joinTableName,
|
|
215
|
-
attributes: {
|
|
216
|
-
id: {
|
|
217
|
-
type: 'increments',
|
|
218
|
-
},
|
|
219
|
-
[joinColumnName]: {
|
|
220
|
-
type: 'integer',
|
|
221
|
-
column: {
|
|
222
|
-
unsigned: true,
|
|
223
|
-
},
|
|
224
|
-
},
|
|
225
|
-
[idColumnName]: {
|
|
226
|
-
type: 'integer',
|
|
227
|
-
column: {
|
|
228
|
-
unsigned: true,
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
[typeColumnName]: {
|
|
232
|
-
type: 'string',
|
|
233
|
-
},
|
|
234
|
-
field: {
|
|
235
|
-
type: 'string',
|
|
236
|
-
},
|
|
237
|
-
order: {
|
|
238
|
-
type: 'float',
|
|
239
|
-
column: {
|
|
240
|
-
unsigned: true,
|
|
241
|
-
},
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
indexes: [
|
|
245
|
-
{
|
|
246
|
-
name: `${joinTableName}_fk`,
|
|
247
|
-
columns: [joinColumnName],
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
name: `${joinTableName}_order_index`,
|
|
251
|
-
columns: ['order'],
|
|
252
|
-
type: null,
|
|
253
|
-
},
|
|
254
|
-
{
|
|
255
|
-
name: `${joinTableName}_id_column_index`,
|
|
256
|
-
columns: [idColumnName],
|
|
257
|
-
type: null,
|
|
258
|
-
},
|
|
259
|
-
],
|
|
260
|
-
foreignKeys: [
|
|
261
|
-
{
|
|
262
|
-
name: `${joinTableName}_fk`,
|
|
263
|
-
columns: [joinColumnName],
|
|
264
|
-
referencedColumns: ['id'],
|
|
265
|
-
referencedTable: meta.tableName,
|
|
266
|
-
onDelete: 'CASCADE',
|
|
267
|
-
},
|
|
268
|
-
],
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
const joinTable = {
|
|
272
|
-
name: joinTableName,
|
|
273
|
-
joinColumn: {
|
|
274
|
-
name: joinColumnName,
|
|
275
|
-
referencedColumn: 'id',
|
|
276
|
-
},
|
|
277
|
-
morphColumn: {
|
|
278
|
-
typeColumn: {
|
|
279
|
-
name: typeColumnName,
|
|
280
|
-
},
|
|
281
|
-
idColumn: {
|
|
282
|
-
name: idColumnName,
|
|
283
|
-
referencedColumn: 'id',
|
|
284
|
-
},
|
|
285
|
-
},
|
|
286
|
-
orderBy: {
|
|
287
|
-
order: 'asc',
|
|
288
|
-
},
|
|
289
|
-
pivotColumns: [joinColumnName, typeColumnName, idColumnName],
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
attribute.joinTable = joinTable;
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Creates a morphOne relation metadata
|
|
297
|
-
*
|
|
298
|
-
* @param {string} attributeName
|
|
299
|
-
* @param {Attribute} attribute
|
|
300
|
-
* @param {ModelMetadata} meta
|
|
301
|
-
* @param {Metadata} metadata
|
|
302
|
-
*/
|
|
303
|
-
const createMorphOne = (attributeName, attribute, meta, metadata) => {
|
|
304
|
-
const targetMeta = metadata.get(attribute.target);
|
|
305
|
-
|
|
306
|
-
if (!targetMeta) {
|
|
307
|
-
throw new Error(`Morph target not found. Looking for ${attribute.target}`);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (!_.has(attribute.morphBy, targetMeta.attributes)) {
|
|
311
|
-
throw new Error(`Morph target attribute not found. Looking for ${attribute.morphBy}`);
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* Creates a morphMany relation metadata
|
|
317
|
-
*
|
|
318
|
-
* @param {string} attributeName
|
|
319
|
-
* @param {Attribute} attribute
|
|
320
|
-
* @param {ModelMetadata} meta
|
|
321
|
-
* @param {Metadata} metadata
|
|
322
|
-
*/
|
|
323
|
-
const createMorphMany = (attributeName, attribute, meta, metadata) => {
|
|
324
|
-
const targetMeta = metadata.get(attribute.target);
|
|
325
|
-
|
|
326
|
-
if (!targetMeta) {
|
|
327
|
-
throw new Error(`Morph target not found. Looking for ${attribute.target}`);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
if (!_.has(attribute.morphBy, targetMeta.attributes)) {
|
|
331
|
-
throw new Error(`Morph target attribute not found. Looking for ${attribute.morphBy}`);
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Creates a relation metadata
|
|
337
|
-
*
|
|
338
|
-
* @param {string} attributeName
|
|
339
|
-
* @param {Attribute} attribute
|
|
340
|
-
* @param {ModelMetadata} meta
|
|
341
|
-
* @param {Metadata} metadata
|
|
342
|
-
*/
|
|
343
|
-
const createRelation = (attributeName, attribute, meta, metadata) => {
|
|
344
|
-
switch (attribute.relation) {
|
|
345
|
-
case 'oneToOne':
|
|
346
|
-
return createOneToOne(attributeName, attribute, meta, metadata);
|
|
347
|
-
case 'oneToMany':
|
|
348
|
-
return createOneToMany(attributeName, attribute, meta, metadata);
|
|
349
|
-
case 'manyToOne':
|
|
350
|
-
return createManyToOne(attributeName, attribute, meta, metadata);
|
|
351
|
-
case 'manyToMany':
|
|
352
|
-
return createManyToMany(attributeName, attribute, meta, metadata);
|
|
353
|
-
case 'morphToOne':
|
|
354
|
-
return createMorphToOne(attributeName, attribute, meta, metadata);
|
|
355
|
-
case 'morphToMany':
|
|
356
|
-
return createMorphToMany(attributeName, attribute, meta, metadata);
|
|
357
|
-
case 'morphOne':
|
|
358
|
-
return createMorphOne(attributeName, attribute, meta, metadata);
|
|
359
|
-
case 'morphMany':
|
|
360
|
-
return createMorphMany(attributeName, attribute, meta, metadata);
|
|
361
|
-
default: {
|
|
362
|
-
throw new Error(`Unknown relation ${attribute.relation}`);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Creates a join column info and add them to the attribute meta
|
|
369
|
-
* @param {Object} metadata metadata registry
|
|
370
|
-
* @param {Object} param
|
|
371
|
-
* @param {Object} param.attribute associated attribute
|
|
372
|
-
* @param {string} param.attributeName name of the associated attribute
|
|
373
|
-
* @param {Object} param.meta model metadata
|
|
374
|
-
*/
|
|
375
|
-
const createJoinColum = (metadata, { attribute, attributeName /* meta */ }) => {
|
|
376
|
-
const targetMeta = metadata.get(attribute.target);
|
|
377
|
-
|
|
378
|
-
const joinColumnName = _.snakeCase(`${attributeName}_id`);
|
|
379
|
-
const joinColumn = {
|
|
380
|
-
name: joinColumnName,
|
|
381
|
-
referencedColumn: 'id',
|
|
382
|
-
referencedTable: targetMeta.tableName,
|
|
383
|
-
};
|
|
384
|
-
|
|
385
|
-
Object.assign(attribute, { owner: true, joinColumn });
|
|
386
|
-
|
|
387
|
-
if (isBidirectional(attribute)) {
|
|
388
|
-
const inverseAttribute = targetMeta.attributes[attribute.inversedBy];
|
|
389
|
-
|
|
390
|
-
Object.assign(inverseAttribute, {
|
|
391
|
-
joinColumn: {
|
|
392
|
-
name: joinColumn.referencedColumn,
|
|
393
|
-
referencedColumn: joinColumn.name,
|
|
394
|
-
},
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Creates a join table and add it to the attribute meta
|
|
401
|
-
* @param {Object} metadata metadata registry
|
|
402
|
-
* @param {Object} param
|
|
403
|
-
* @param {Object} param.attribute associated attribute
|
|
404
|
-
* @param {string} param.attributeName name of the associated attribute
|
|
405
|
-
* @param {Object} param.meta model metadata
|
|
406
|
-
*/
|
|
407
|
-
const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
|
|
408
|
-
const targetMeta = metadata.get(attribute.target);
|
|
409
|
-
|
|
410
|
-
if (!targetMeta) {
|
|
411
|
-
throw new Error(`Unknown target ${attribute.target}`);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
const joinTableName = getJoinTableName(meta.tableName, attributeName);
|
|
415
|
-
|
|
416
|
-
const joinColumnName = _.snakeCase(`${meta.singularName}_id`);
|
|
417
|
-
let inverseJoinColumnName = _.snakeCase(`${targetMeta.singularName}_id`);
|
|
418
|
-
|
|
419
|
-
// if relation is self referencing
|
|
420
|
-
if (joinColumnName === inverseJoinColumnName) {
|
|
421
|
-
inverseJoinColumnName = `inv_${inverseJoinColumnName}`;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
const orderColumnName = _.snakeCase(`${targetMeta.singularName}_order`);
|
|
425
|
-
let inverseOrderColumnName = _.snakeCase(`${meta.singularName}_order`);
|
|
426
|
-
|
|
427
|
-
// if relation is self referencing
|
|
428
|
-
if (attribute.relation === 'manyToMany' && orderColumnName === inverseOrderColumnName) {
|
|
429
|
-
inverseOrderColumnName = `inv_${inverseOrderColumnName}`;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
const metadataSchema = {
|
|
433
|
-
uid: joinTableName,
|
|
434
|
-
tableName: joinTableName,
|
|
435
|
-
attributes: {
|
|
436
|
-
id: {
|
|
437
|
-
type: 'increments',
|
|
438
|
-
},
|
|
439
|
-
[joinColumnName]: {
|
|
440
|
-
type: 'integer',
|
|
441
|
-
column: {
|
|
442
|
-
unsigned: true,
|
|
443
|
-
},
|
|
444
|
-
},
|
|
445
|
-
[inverseJoinColumnName]: {
|
|
446
|
-
type: 'integer',
|
|
447
|
-
column: {
|
|
448
|
-
unsigned: true,
|
|
449
|
-
},
|
|
450
|
-
},
|
|
451
|
-
// TODO: add extra pivot attributes -> user should use an intermediate entity
|
|
452
|
-
},
|
|
453
|
-
indexes: [
|
|
454
|
-
{
|
|
455
|
-
name: `${joinTableName}_fk`,
|
|
456
|
-
columns: [joinColumnName],
|
|
457
|
-
},
|
|
458
|
-
{
|
|
459
|
-
name: `${joinTableName}_inv_fk`,
|
|
460
|
-
columns: [inverseJoinColumnName],
|
|
461
|
-
},
|
|
462
|
-
{
|
|
463
|
-
name: `${joinTableName}_unique`,
|
|
464
|
-
columns: [joinColumnName, inverseJoinColumnName],
|
|
465
|
-
type: 'unique',
|
|
466
|
-
},
|
|
467
|
-
],
|
|
468
|
-
foreignKeys: [
|
|
469
|
-
{
|
|
470
|
-
name: `${joinTableName}_fk`,
|
|
471
|
-
columns: [joinColumnName],
|
|
472
|
-
referencedColumns: ['id'],
|
|
473
|
-
referencedTable: meta.tableName,
|
|
474
|
-
onDelete: 'CASCADE',
|
|
475
|
-
},
|
|
476
|
-
{
|
|
477
|
-
name: `${joinTableName}_inv_fk`,
|
|
478
|
-
columns: [inverseJoinColumnName],
|
|
479
|
-
referencedColumns: ['id'],
|
|
480
|
-
referencedTable: targetMeta.tableName,
|
|
481
|
-
onDelete: 'CASCADE',
|
|
482
|
-
},
|
|
483
|
-
],
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
const joinTable = {
|
|
487
|
-
name: joinTableName,
|
|
488
|
-
joinColumn: {
|
|
489
|
-
name: joinColumnName,
|
|
490
|
-
referencedColumn: 'id',
|
|
491
|
-
},
|
|
492
|
-
inverseJoinColumn: {
|
|
493
|
-
name: inverseJoinColumnName,
|
|
494
|
-
referencedColumn: 'id',
|
|
495
|
-
},
|
|
496
|
-
pivotColumns: [joinColumnName, inverseJoinColumnName],
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
// order
|
|
500
|
-
if (isAnyToMany(attribute)) {
|
|
501
|
-
metadataSchema.attributes[orderColumnName] = {
|
|
502
|
-
type: 'float',
|
|
503
|
-
column: {
|
|
504
|
-
unsigned: true,
|
|
505
|
-
defaultTo: null,
|
|
506
|
-
},
|
|
507
|
-
};
|
|
508
|
-
metadataSchema.indexes.push({
|
|
509
|
-
name: `${joinTableName}_order_fk`,
|
|
510
|
-
columns: [orderColumnName],
|
|
511
|
-
});
|
|
512
|
-
joinTable.orderColumnName = orderColumnName;
|
|
513
|
-
joinTable.orderBy = { [orderColumnName]: 'asc' };
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
// inv order
|
|
517
|
-
if (isBidirectional(attribute) && isManyToAny(attribute)) {
|
|
518
|
-
metadataSchema.attributes[inverseOrderColumnName] = {
|
|
519
|
-
type: 'float',
|
|
520
|
-
column: {
|
|
521
|
-
unsigned: true,
|
|
522
|
-
defaultTo: null,
|
|
523
|
-
},
|
|
524
|
-
};
|
|
525
|
-
|
|
526
|
-
metadataSchema.indexes.push({
|
|
527
|
-
name: `${joinTableName}_order_inv_fk`,
|
|
528
|
-
columns: [inverseOrderColumnName],
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
joinTable.inverseOrderColumnName = inverseOrderColumnName;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
metadata.add(metadataSchema);
|
|
535
|
-
|
|
536
|
-
attribute.joinTable = joinTable;
|
|
537
|
-
|
|
538
|
-
if (isBidirectional(attribute)) {
|
|
539
|
-
const inverseAttribute = targetMeta.attributes[attribute.inversedBy];
|
|
540
|
-
|
|
541
|
-
if (!inverseAttribute) {
|
|
542
|
-
throw new Error(
|
|
543
|
-
`inversedBy attribute ${attribute.inversedBy} not found target ${targetMeta.uid}`
|
|
544
|
-
);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
inverseAttribute.joinTable = {
|
|
548
|
-
name: joinTableName,
|
|
549
|
-
joinColumn: joinTable.inverseJoinColumn,
|
|
550
|
-
inverseJoinColumn: joinTable.joinColumn,
|
|
551
|
-
pivotColumns: joinTable.pivotColumns,
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
if (isManyToAny(attribute)) {
|
|
555
|
-
inverseAttribute.joinTable.orderColumnName = inverseOrderColumnName;
|
|
556
|
-
inverseAttribute.joinTable.orderBy = { [inverseOrderColumnName]: 'asc' };
|
|
557
|
-
}
|
|
558
|
-
if (isAnyToMany(attribute)) {
|
|
559
|
-
inverseAttribute.joinTable.inverseOrderColumnName = orderColumnName;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
const hasOrderColumn = (attribute) => isAnyToMany(attribute);
|
|
565
|
-
const hasInverseOrderColumn = (attribute) => isBidirectional(attribute) && isManyToAny(attribute);
|
|
566
|
-
|
|
567
|
-
module.exports = {
|
|
568
|
-
createRelation,
|
|
569
|
-
isPolymorphic,
|
|
570
|
-
isBidirectional,
|
|
571
|
-
isOneToAny,
|
|
572
|
-
isManyToAny,
|
|
573
|
-
isAnyToOne,
|
|
574
|
-
isAnyToMany,
|
|
575
|
-
hasOrderColumn,
|
|
576
|
-
hasInverseOrderColumn,
|
|
577
|
-
getJoinTableName,
|
|
578
|
-
};
|
package/lib/migrations/index.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fse = require('fs-extra');
|
|
5
|
-
const { Umzug } = require('umzug');
|
|
6
|
-
|
|
7
|
-
const createStorage = require('./storage');
|
|
8
|
-
|
|
9
|
-
const wrapTransaction = (db) => (fn) => () =>
|
|
10
|
-
db.connection.transaction((trx) => Promise.resolve(fn(trx)));
|
|
11
|
-
|
|
12
|
-
// TODO: check multiple commands in one sql statement
|
|
13
|
-
const migrationResolver = ({ name, path, context }) => {
|
|
14
|
-
const { db } = context;
|
|
15
|
-
|
|
16
|
-
// if sql file run with knex raw
|
|
17
|
-
if (path.match(/\.sql$/)) {
|
|
18
|
-
const sql = fse.readFileSync(path, 'utf8');
|
|
19
|
-
|
|
20
|
-
return {
|
|
21
|
-
name,
|
|
22
|
-
up: wrapTransaction(db)((knex) => knex.raw(sql)),
|
|
23
|
-
down() {},
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// NOTE: we can add some ts register if we want to handle ts migration files at some point
|
|
28
|
-
const migration = require(path);
|
|
29
|
-
return {
|
|
30
|
-
name,
|
|
31
|
-
up: wrapTransaction(db)(migration.up),
|
|
32
|
-
down: wrapTransaction(db)(migration.down),
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const createUmzugProvider = (db) => {
|
|
37
|
-
const migrationDir = path.join(strapi.dirs.app.root, 'database/migrations');
|
|
38
|
-
|
|
39
|
-
fse.ensureDirSync(migrationDir);
|
|
40
|
-
|
|
41
|
-
return new Umzug({
|
|
42
|
-
storage: createStorage({ db, tableName: 'strapi_migrations' }),
|
|
43
|
-
context: { db },
|
|
44
|
-
migrations: {
|
|
45
|
-
glob: ['*.{js,sql}', { cwd: migrationDir }],
|
|
46
|
-
resolve: migrationResolver,
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// NOTE: when needed => add internal migrations for core & plugins. How do we overlap them with users migrations ?
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Creates migrations provider
|
|
55
|
-
* @type {import('.').createMigrationsProvider}
|
|
56
|
-
*/
|
|
57
|
-
const createMigrationsProvider = (db) => {
|
|
58
|
-
const migrations = createUmzugProvider(db);
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
async shouldRun() {
|
|
62
|
-
const pending = await migrations.pending();
|
|
63
|
-
|
|
64
|
-
return pending.length > 0 && db.config.settings.runMigrations;
|
|
65
|
-
},
|
|
66
|
-
async up() {
|
|
67
|
-
await migrations.up();
|
|
68
|
-
},
|
|
69
|
-
async down() {
|
|
70
|
-
await migrations.down();
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
module.exports = { createMigrationsProvider };
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const createStorage = (opts = {}) => {
|
|
4
|
-
const { db, tableName = 'strapi_migrations' } = opts;
|
|
5
|
-
|
|
6
|
-
const hasMigrationTable = () => db.getSchemaConnection().hasTable(tableName);
|
|
7
|
-
|
|
8
|
-
const createMigrationTable = () => {
|
|
9
|
-
return db.getSchemaConnection().createTable(tableName, (table) => {
|
|
10
|
-
table.increments('id');
|
|
11
|
-
table.string('name');
|
|
12
|
-
table.datetime('time', { useTz: false });
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
async logMigration({ name }) {
|
|
18
|
-
await db
|
|
19
|
-
.getConnection()
|
|
20
|
-
.insert({
|
|
21
|
-
name,
|
|
22
|
-
time: new Date(),
|
|
23
|
-
})
|
|
24
|
-
.into(tableName);
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
async unlogMigration({ name }) {
|
|
28
|
-
await db.getConnection(tableName).del().where({ name });
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
async executed() {
|
|
32
|
-
if (!(await hasMigrationTable())) {
|
|
33
|
-
await createMigrationTable();
|
|
34
|
-
return [];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const logs = await db.getConnection(tableName).select().from(tableName).orderBy('time');
|
|
38
|
-
|
|
39
|
-
return logs.map((log) => log.name);
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
module.exports = createStorage;
|