@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.
Files changed (242) hide show
  1. package/README.md +3 -0
  2. package/dist/connection.d.ts +3 -0
  3. package/dist/connection.d.ts.map +1 -0
  4. package/dist/dialects/dialect.d.ts +27 -0
  5. package/dist/dialects/dialect.d.ts.map +1 -0
  6. package/dist/dialects/index.d.ts +5 -0
  7. package/dist/dialects/index.d.ts.map +1 -0
  8. package/dist/dialects/mysql/constants.d.ts +3 -0
  9. package/dist/dialects/mysql/constants.d.ts.map +1 -0
  10. package/dist/dialects/mysql/database-inspector.d.ts +12 -0
  11. package/dist/dialects/mysql/database-inspector.d.ts.map +1 -0
  12. package/dist/dialects/mysql/index.d.ts +20 -0
  13. package/dist/dialects/mysql/index.d.ts.map +1 -0
  14. package/dist/dialects/mysql/schema-inspector.d.ts +13 -0
  15. package/dist/dialects/mysql/schema-inspector.d.ts.map +1 -0
  16. package/dist/dialects/postgresql/index.d.ts +14 -0
  17. package/dist/dialects/postgresql/index.d.ts.map +1 -0
  18. package/dist/dialects/postgresql/schema-inspector.d.ts +14 -0
  19. package/dist/dialects/postgresql/schema-inspector.d.ts.map +1 -0
  20. package/dist/dialects/sqlite/index.d.ts +19 -0
  21. package/dist/dialects/sqlite/index.d.ts.map +1 -0
  22. package/dist/dialects/sqlite/schema-inspector.d.ts +13 -0
  23. package/dist/dialects/sqlite/schema-inspector.d.ts.map +1 -0
  24. package/dist/entity-manager/entity-repository.d.ts +4 -0
  25. package/dist/entity-manager/entity-repository.d.ts.map +1 -0
  26. package/dist/entity-manager/index.d.ts +5 -0
  27. package/dist/entity-manager/index.d.ts.map +1 -0
  28. package/dist/entity-manager/morph-relations.d.ts +13 -0
  29. package/dist/entity-manager/morph-relations.d.ts.map +1 -0
  30. package/dist/entity-manager/regular-relations.d.ts +83 -0
  31. package/dist/entity-manager/regular-relations.d.ts.map +1 -0
  32. package/dist/entity-manager/relations/cloning/regular-relations.d.ts +17 -0
  33. package/dist/entity-manager/relations/cloning/regular-relations.d.ts.map +1 -0
  34. package/dist/entity-manager/relations-orderer.d.ts +73 -0
  35. package/dist/entity-manager/relations-orderer.d.ts.map +1 -0
  36. package/dist/entity-manager/types.d.ts +97 -0
  37. package/dist/entity-manager/types.d.ts.map +1 -0
  38. package/dist/errors/database.d.ts +5 -0
  39. package/dist/errors/database.d.ts.map +1 -0
  40. package/dist/errors/index.d.ts +8 -0
  41. package/dist/errors/index.d.ts.map +1 -0
  42. package/dist/errors/invalid-date.d.ts +5 -0
  43. package/dist/errors/invalid-date.d.ts.map +1 -0
  44. package/dist/errors/invalid-datetime.d.ts +5 -0
  45. package/dist/errors/invalid-datetime.d.ts.map +1 -0
  46. package/dist/errors/invalid-relation.d.ts +5 -0
  47. package/dist/errors/invalid-relation.d.ts.map +1 -0
  48. package/dist/errors/invalid-time.d.ts +5 -0
  49. package/dist/errors/invalid-time.d.ts.map +1 -0
  50. package/dist/errors/not-null.d.ts +7 -0
  51. package/dist/errors/not-null.d.ts.map +1 -0
  52. package/dist/fields/biginteger.d.ts +4 -0
  53. package/dist/fields/biginteger.d.ts.map +1 -0
  54. package/dist/fields/boolean.d.ts +6 -0
  55. package/dist/fields/boolean.d.ts.map +1 -0
  56. package/dist/fields/date.d.ts +6 -0
  57. package/dist/fields/date.d.ts.map +1 -0
  58. package/dist/fields/datetime.d.ts +6 -0
  59. package/dist/fields/datetime.d.ts.map +1 -0
  60. package/dist/fields/field.d.ts +7 -0
  61. package/dist/fields/field.d.ts.map +1 -0
  62. package/dist/fields/index.d.ts +4 -0
  63. package/dist/fields/index.d.ts.map +1 -0
  64. package/dist/fields/json.d.ts +6 -0
  65. package/dist/fields/json.d.ts.map +1 -0
  66. package/dist/fields/number.d.ts +6 -0
  67. package/dist/fields/number.d.ts.map +1 -0
  68. package/dist/fields/shared/parsers.d.ts +4 -0
  69. package/dist/fields/shared/parsers.d.ts.map +1 -0
  70. package/dist/fields/string.d.ts +6 -0
  71. package/dist/fields/string.d.ts.map +1 -0
  72. package/dist/fields/time.d.ts +6 -0
  73. package/dist/fields/time.d.ts.map +1 -0
  74. package/dist/fields/timestamp.d.ts +6 -0
  75. package/dist/fields/timestamp.d.ts.map +1 -0
  76. package/dist/index.d.ts +46 -0
  77. package/dist/index.d.ts.map +1 -0
  78. package/dist/index.js +6211 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/index.mjs +6179 -0
  81. package/dist/index.mjs.map +1 -0
  82. package/dist/lifecycles/index.d.ts +17 -0
  83. package/dist/lifecycles/index.d.ts.map +1 -0
  84. package/dist/lifecycles/subscribers/index.d.ts +5 -0
  85. package/dist/lifecycles/subscribers/index.d.ts.map +1 -0
  86. package/dist/lifecycles/subscribers/models-lifecycles.d.ts +6 -0
  87. package/dist/lifecycles/subscribers/models-lifecycles.d.ts.map +1 -0
  88. package/dist/lifecycles/subscribers/timestamps.d.ts +3 -0
  89. package/dist/lifecycles/subscribers/timestamps.d.ts.map +1 -0
  90. package/dist/lifecycles/types.d.ts +25 -0
  91. package/dist/lifecycles/types.d.ts.map +1 -0
  92. package/dist/metadata/index.d.ts +10 -0
  93. package/dist/metadata/index.d.ts.map +1 -0
  94. package/dist/metadata/metadata.d.ts +22 -0
  95. package/dist/metadata/metadata.d.ts.map +1 -0
  96. package/dist/metadata/relations.d.ts +16 -0
  97. package/dist/metadata/relations.d.ts.map +1 -0
  98. package/dist/migrations/index.d.ts +12 -0
  99. package/dist/migrations/index.d.ts.map +1 -0
  100. package/dist/migrations/storage.d.ts +15 -0
  101. package/dist/migrations/storage.d.ts.map +1 -0
  102. package/dist/query/helpers/index.d.ts +8 -0
  103. package/dist/query/helpers/index.d.ts.map +1 -0
  104. package/dist/query/helpers/join.d.ts +30 -0
  105. package/dist/query/helpers/join.d.ts.map +1 -0
  106. package/dist/query/helpers/order-by.d.ts +14 -0
  107. package/dist/query/helpers/order-by.d.ts.map +1 -0
  108. package/dist/query/helpers/populate/apply.d.ts +11 -0
  109. package/dist/query/helpers/populate/apply.d.ts.map +1 -0
  110. package/dist/query/helpers/populate/index.d.ts +4 -0
  111. package/dist/query/helpers/populate/index.d.ts.map +1 -0
  112. package/dist/query/helpers/populate/process.d.ts +24 -0
  113. package/dist/query/helpers/populate/process.d.ts.map +1 -0
  114. package/dist/query/helpers/search.d.ts +4 -0
  115. package/dist/query/helpers/search.d.ts.map +1 -0
  116. package/dist/query/helpers/streams/index.d.ts +2 -0
  117. package/dist/query/helpers/streams/index.d.ts.map +1 -0
  118. package/dist/query/helpers/streams/readable.d.ts +39 -0
  119. package/dist/query/helpers/streams/readable.d.ts.map +1 -0
  120. package/dist/query/helpers/transform.d.ts +8 -0
  121. package/dist/query/helpers/transform.d.ts.map +1 -0
  122. package/dist/query/helpers/where.d.ts +19 -0
  123. package/dist/query/helpers/where.d.ts.map +1 -0
  124. package/dist/query/index.d.ts +3 -0
  125. package/dist/query/index.d.ts.map +1 -0
  126. package/dist/query/query-builder.d.ts +79 -0
  127. package/dist/query/query-builder.d.ts.map +1 -0
  128. package/dist/query/types.d.ts +8 -0
  129. package/dist/query/types.d.ts.map +1 -0
  130. package/dist/schema/builder.d.ts +33 -0
  131. package/dist/schema/builder.d.ts.map +1 -0
  132. package/dist/schema/diff.d.ts +7 -0
  133. package/dist/schema/diff.d.ts.map +1 -0
  134. package/dist/schema/index.d.ts +20 -0
  135. package/dist/schema/index.d.ts.map +1 -0
  136. package/dist/schema/schema.d.ts +4 -0
  137. package/dist/schema/schema.d.ts.map +1 -0
  138. package/dist/schema/storage.d.ts +10 -0
  139. package/dist/schema/storage.d.ts.map +1 -0
  140. package/dist/schema/types.d.ts +103 -0
  141. package/dist/schema/types.d.ts.map +1 -0
  142. package/dist/transaction-context.d.ts +22 -0
  143. package/dist/transaction-context.d.ts.map +1 -0
  144. package/dist/types/index.d.ts +169 -0
  145. package/dist/types/index.d.ts.map +1 -0
  146. package/dist/utils/content-types.d.ts +13 -0
  147. package/dist/utils/content-types.d.ts.map +1 -0
  148. package/dist/utils/knex.d.ts +12 -0
  149. package/dist/utils/knex.d.ts.map +1 -0
  150. package/dist/utils/types.d.ts +10 -0
  151. package/dist/utils/types.d.ts.map +1 -0
  152. package/dist/validations/index.d.ts +6 -0
  153. package/dist/validations/index.d.ts.map +1 -0
  154. package/dist/validations/relations/bidirectional.d.ts +12 -0
  155. package/dist/validations/relations/bidirectional.d.ts.map +1 -0
  156. package/dist/validations/relations/index.d.ts +7 -0
  157. package/dist/validations/relations/index.d.ts.map +1 -0
  158. package/package.json +20 -8
  159. package/.eslintignore +0 -2
  160. package/.eslintrc.js +0 -4
  161. package/jest.config.js +0 -6
  162. package/lib/__tests__/index.test.js +0 -93
  163. package/lib/__tests__/lifecycles.test.js +0 -55
  164. package/lib/connection.js +0 -64
  165. package/lib/dialects/dialect.js +0 -63
  166. package/lib/dialects/index.js +0 -53
  167. package/lib/dialects/mysql/constants.js +0 -6
  168. package/lib/dialects/mysql/database-inspector.js +0 -37
  169. package/lib/dialects/mysql/index.js +0 -92
  170. package/lib/dialects/mysql/schema-inspector.js +0 -234
  171. package/lib/dialects/postgresql/index.js +0 -65
  172. package/lib/dialects/postgresql/schema-inspector.js +0 -283
  173. package/lib/dialects/sqlite/index.js +0 -87
  174. package/lib/dialects/sqlite/schema-inspector.js +0 -151
  175. package/lib/entity-manager/__tests__/relations-orderer.test.js +0 -186
  176. package/lib/entity-manager/__tests__/sort-connect-array.test.js +0 -79
  177. package/lib/entity-manager/entity-repository.js +0 -164
  178. package/lib/entity-manager/index.js +0 -1385
  179. package/lib/entity-manager/morph-relations.js +0 -63
  180. package/lib/entity-manager/regular-relations.js +0 -506
  181. package/lib/entity-manager/relations/cloning/regular-relations.js +0 -76
  182. package/lib/entity-manager/relations-orderer.js +0 -225
  183. package/lib/errors/database.js +0 -12
  184. package/lib/errors/index.js +0 -17
  185. package/lib/errors/invalid-date.js +0 -14
  186. package/lib/errors/invalid-datetime.js +0 -14
  187. package/lib/errors/invalid-relation.js +0 -14
  188. package/lib/errors/invalid-time.js +0 -14
  189. package/lib/errors/not-null.js +0 -15
  190. package/lib/fields/biginteger.js +0 -17
  191. package/lib/fields/boolean.js +0 -39
  192. package/lib/fields/date.js +0 -16
  193. package/lib/fields/datetime.js +0 -19
  194. package/lib/fields/field.js +0 -17
  195. package/lib/fields/index.d.ts +0 -9
  196. package/lib/fields/index.js +0 -50
  197. package/lib/fields/json.js +0 -21
  198. package/lib/fields/number.js +0 -23
  199. package/lib/fields/shared/parsers.js +0 -71
  200. package/lib/fields/string.js +0 -17
  201. package/lib/fields/time.js +0 -17
  202. package/lib/fields/timestamp.js +0 -19
  203. package/lib/index.d.ts +0 -198
  204. package/lib/index.js +0 -129
  205. package/lib/lifecycles/index.d.ts +0 -51
  206. package/lib/lifecycles/index.js +0 -90
  207. package/lib/lifecycles/subscribers/index.d.ts +0 -11
  208. package/lib/lifecycles/subscribers/models-lifecycles.js +0 -19
  209. package/lib/lifecycles/subscribers/timestamps.js +0 -65
  210. package/lib/metadata/index.js +0 -244
  211. package/lib/metadata/relations.js +0 -578
  212. package/lib/migrations/index.d.ts +0 -9
  213. package/lib/migrations/index.js +0 -75
  214. package/lib/migrations/storage.js +0 -44
  215. package/lib/query/helpers/index.js +0 -11
  216. package/lib/query/helpers/join.js +0 -96
  217. package/lib/query/helpers/order-by.js +0 -70
  218. package/lib/query/helpers/populate/apply.js +0 -664
  219. package/lib/query/helpers/populate/index.js +0 -9
  220. package/lib/query/helpers/populate/process.js +0 -102
  221. package/lib/query/helpers/search.js +0 -84
  222. package/lib/query/helpers/streams/index.js +0 -5
  223. package/lib/query/helpers/streams/readable.js +0 -174
  224. package/lib/query/helpers/transform.js +0 -84
  225. package/lib/query/helpers/where.js +0 -365
  226. package/lib/query/index.js +0 -7
  227. package/lib/query/query-builder.js +0 -514
  228. package/lib/schema/__tests__/schema-diff.test.js +0 -231
  229. package/lib/schema/builder.js +0 -386
  230. package/lib/schema/diff.js +0 -399
  231. package/lib/schema/index.d.ts +0 -49
  232. package/lib/schema/index.js +0 -94
  233. package/lib/schema/schema.js +0 -202
  234. package/lib/schema/storage.js +0 -76
  235. package/lib/transaction-context.js +0 -68
  236. package/lib/types/index.d.ts +0 -6
  237. package/lib/types/index.js +0 -35
  238. package/lib/utils/content-types.js +0 -40
  239. package/lib/utils/knex.js +0 -22
  240. package/lib/validations/index.js +0 -20
  241. package/lib/validations/relations/bidirectional.js +0 -89
  242. package/lib/validations/relations/index.js +0 -14
@@ -1,202 +0,0 @@
1
- 'use strict';
2
-
3
- const types = require('../types');
4
-
5
- const createColumn = (name, attribute) => {
6
- const { type, args = [], ...opts } = getColumnType(attribute);
7
-
8
- return {
9
- name,
10
- type,
11
- args,
12
- defaultTo: null,
13
- notNullable: false,
14
- unsigned: false,
15
- ...opts,
16
- ...(attribute.column || {}),
17
- };
18
- };
19
-
20
- const createTable = (meta) => {
21
- const table = {
22
- name: meta.tableName,
23
- indexes: meta.indexes || [],
24
- foreignKeys: meta.foreignKeys || [],
25
- columns: [],
26
- };
27
-
28
- for (const key of Object.keys(meta.attributes)) {
29
- const attribute = meta.attributes[key];
30
-
31
- if (types.isRelation(attribute.type)) {
32
- if (attribute.morphColumn && attribute.owner) {
33
- const { idColumn, typeColumn } = attribute.morphColumn;
34
-
35
- table.columns.push(
36
- createColumn(idColumn.name, {
37
- type: 'integer',
38
- column: {
39
- unsigned: true,
40
- },
41
- })
42
- );
43
-
44
- table.columns.push(createColumn(typeColumn.name, { type: 'string' }));
45
- } else if (attribute.joinColumn && attribute.owner) {
46
- // NOTE: we could pass uniquness for oneToOne to avoid creating more than one to one
47
-
48
- const { name: columnName, referencedColumn, referencedTable } = attribute.joinColumn;
49
-
50
- const column = createColumn(columnName, {
51
- type: 'integer',
52
- column: {
53
- unsigned: true,
54
- },
55
- });
56
-
57
- table.columns.push(column);
58
-
59
- table.foreignKeys.push({
60
- name: `${table.name}_${columnName}_fk`,
61
- columns: [columnName],
62
- referencedTable,
63
- referencedColumns: [referencedColumn],
64
- // NOTE: could allow configuration
65
- onDelete: 'SET NULL',
66
- });
67
-
68
- table.indexes.push({
69
- name: `${table.name}_${columnName}_fk`,
70
- columns: [columnName],
71
- });
72
- }
73
- } else if (types.isScalar(attribute.type)) {
74
- const column = createColumn(attribute.columnName || key, attribute);
75
-
76
- if (column.unique) {
77
- table.indexes.push({
78
- type: 'unique',
79
- name: `${table.name}_${column.name}_unique`,
80
- columns: [column.name],
81
- });
82
- }
83
-
84
- if (column.primary) {
85
- table.indexes.push({
86
- type: 'primary',
87
- name: `${table.name}_${column.name}_primary`,
88
- columns: [column.name],
89
- });
90
- }
91
-
92
- table.columns.push(column);
93
- }
94
- }
95
-
96
- return table;
97
- };
98
-
99
- const getColumnType = (attribute) => {
100
- if (attribute.columnType) {
101
- return attribute.columnType;
102
- }
103
-
104
- switch (attribute.type) {
105
- case 'increments': {
106
- return {
107
- type: 'increments',
108
- args: [{ primary: true, primaryKey: true }],
109
- notNullable: true,
110
- };
111
- }
112
-
113
- // We might want to convert email/password to string types before going into the orm with specific validators & transformers
114
- case 'password':
115
- case 'email':
116
- case 'string':
117
- case 'enumeration': {
118
- return { type: 'string' };
119
- }
120
- case 'uid': {
121
- return {
122
- type: 'string',
123
- unique: true,
124
- };
125
- }
126
- case 'richtext':
127
- case 'text': {
128
- return {
129
- type: 'text',
130
- args: ['longtext'],
131
- };
132
- }
133
- case 'blocks':
134
- case 'json': {
135
- return { type: 'jsonb' };
136
- }
137
- case 'integer': {
138
- return { type: 'integer' };
139
- }
140
- case 'biginteger': {
141
- return { type: 'bigInteger' };
142
- }
143
- case 'float': {
144
- return { type: 'double' };
145
- }
146
- case 'decimal': {
147
- return { type: 'decimal', args: [10, 2] };
148
- }
149
- case 'date': {
150
- return { type: 'date' };
151
- }
152
- case 'time': {
153
- return { type: 'time', args: [{ precision: 3 }] };
154
- }
155
- case 'datetime': {
156
- return {
157
- type: 'datetime',
158
- args: [
159
- {
160
- useTz: false,
161
- precision: 6,
162
- },
163
- ],
164
- };
165
- }
166
- case 'timestamp': {
167
- return {
168
- type: 'timestamp',
169
- args: [
170
- {
171
- useTz: false,
172
- precision: 6,
173
- },
174
- ],
175
- };
176
- }
177
- case 'boolean': {
178
- return { type: 'boolean' };
179
- }
180
- default: {
181
- throw new Error(`Unknown type ${attribute.type}`);
182
- }
183
- }
184
- };
185
-
186
- const metadataToSchema = (metadata) => {
187
- const schema = {
188
- tables: [],
189
- addTable(table) {
190
- this.tables.push(table);
191
- return this;
192
- },
193
- };
194
-
195
- metadata.forEach((metadata) => {
196
- schema.addTable(createTable(metadata));
197
- });
198
-
199
- return schema;
200
- };
201
-
202
- module.exports = { metadataToSchema, createTable };
@@ -1,76 +0,0 @@
1
- 'use strict';
2
-
3
- const crypto = require('crypto');
4
-
5
- const TABLE_NAME = 'strapi_database_schema';
6
-
7
- module.exports = (db) => {
8
- const hasSchemaTable = () => db.getSchemaConnection().hasTable(TABLE_NAME);
9
-
10
- const createSchemaTable = () => {
11
- return db.getSchemaConnection().createTable(TABLE_NAME, (t) => {
12
- t.increments('id');
13
- t.json('schema');
14
- t.datetime('time', { useTz: false });
15
- t.string('hash');
16
- });
17
- };
18
-
19
- const checkTableExists = async () => {
20
- if (!(await hasSchemaTable())) {
21
- await createSchemaTable();
22
- }
23
- };
24
-
25
- return {
26
- async read() {
27
- await checkTableExists();
28
-
29
- const res = await db
30
- .getConnection()
31
- .select('*')
32
- .from(TABLE_NAME)
33
- .orderBy('time', 'DESC')
34
- .first();
35
-
36
- if (!res) {
37
- return null;
38
- }
39
-
40
- const parsedSchema = typeof res.schema === 'object' ? res.schema : JSON.parse(res.schema);
41
-
42
- return {
43
- ...res,
44
- schema: parsedSchema,
45
- };
46
- },
47
-
48
- hashSchema(schema) {
49
- return crypto.createHash('md5').update(JSON.stringify(schema)).digest('hex');
50
- },
51
-
52
- async add(schema) {
53
- await checkTableExists();
54
-
55
- // NOTE: we can remove this to add history
56
- await db.getConnection(TABLE_NAME).delete();
57
-
58
- const time = new Date();
59
-
60
- await db
61
- .getConnection()
62
- .insert({
63
- schema: JSON.stringify(schema),
64
- hash: this.hashSchema(schema),
65
- time,
66
- })
67
- .into(TABLE_NAME);
68
- },
69
-
70
- async clear() {
71
- await checkTableExists();
72
-
73
- await db.getConnection(TABLE_NAME).truncate();
74
- },
75
- };
76
- };
@@ -1,68 +0,0 @@
1
- 'use strict';
2
-
3
- const { AsyncLocalStorage } = require('async_hooks');
4
-
5
- const storage = new AsyncLocalStorage();
6
-
7
- const transactionCtx = {
8
- async run(store, cb) {
9
- return storage.run({ trx: store, commitCallbacks: [], rollbackCallbacks: [] }, cb);
10
- },
11
-
12
- get() {
13
- const store = storage.getStore();
14
- return store?.trx;
15
- },
16
-
17
- async commit(trx) {
18
- const store = storage.getStore();
19
-
20
- // Clear transaction from store
21
- if (store?.trx) {
22
- store.trx = null;
23
- }
24
-
25
- // Commit transaction
26
- await trx.commit();
27
-
28
- if (!store?.commitCallbacks.length) return;
29
-
30
- // Run callbacks
31
- store.commitCallbacks.forEach((cb) => cb());
32
- store.commitCallbacks = [];
33
- },
34
-
35
- async rollback(trx) {
36
- const store = storage.getStore();
37
-
38
- // Clear transaction from store
39
- if (store?.trx) {
40
- store.trx = null;
41
- }
42
-
43
- // Rollback transaction
44
- await trx.rollback();
45
-
46
- if (!store?.rollbackCallbacks.length) return;
47
-
48
- // Run callbacks
49
- store.rollbackCallbacks.forEach((cb) => cb());
50
- store.rollbackCallbacks = [];
51
- },
52
-
53
- onCommit(cb) {
54
- const store = storage.getStore();
55
- if (store?.commitCallbacks) {
56
- store.commitCallbacks.push(cb);
57
- }
58
- },
59
-
60
- onRollback(cb) {
61
- const store = storage.getStore();
62
- if (store?.rollbackCallbacks) {
63
- store.rollbackCallbacks.push(cb);
64
- }
65
- },
66
- };
67
-
68
- module.exports = transactionCtx;
@@ -1,6 +0,0 @@
1
- export function isScalar(type: string): boolean;
2
- export function isNumber(type: string): boolean;
3
- export function isString(type: string): boolean;
4
- export function isComponent(type: string): boolean;
5
- export function isDynamicZone(type: string): boolean;
6
- export function isRelation(type: string): boolean;
@@ -1,35 +0,0 @@
1
- 'use strict';
2
-
3
- const SCALAR_TYPES = [
4
- 'increments',
5
- 'password',
6
- 'email',
7
- 'string',
8
- 'uid',
9
- 'richtext',
10
- 'text',
11
- 'json',
12
- 'enumeration',
13
- 'integer',
14
- 'biginteger',
15
- 'float',
16
- 'decimal',
17
- 'date',
18
- 'time',
19
- 'datetime',
20
- 'timestamp',
21
- 'boolean',
22
- 'blocks',
23
- ];
24
-
25
- const STRING_TYPES = ['string', 'text', 'uid', 'email', 'enumeration', 'richtext'];
26
- const NUMBER_TYPES = ['biginteger', 'integer', 'decimal', 'float'];
27
-
28
- module.exports = {
29
- isString: (type) => STRING_TYPES.includes(type),
30
- isNumber: (type) => NUMBER_TYPES.includes(type),
31
- isScalar: (type) => SCALAR_TYPES.includes(type),
32
- isComponent: (type) => type === 'component',
33
- isDynamicZone: (type) => type === 'dynamiczone',
34
- isRelation: (type) => type === 'relation',
35
- };
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- const transformAttribute = (attribute) => {
4
- switch (attribute.type) {
5
- case 'media': {
6
- return {
7
- type: 'relation',
8
- relation: attribute.multiple === true ? 'morphMany' : 'morphOne',
9
- target: 'plugin::upload.file',
10
- morphBy: 'related',
11
- };
12
- }
13
- default: {
14
- return attribute;
15
- }
16
- }
17
- };
18
-
19
- // TODO: model logic outside DB
20
- const transformContentTypes = (contentTypes) => {
21
- return contentTypes.map((contentType) => {
22
- const model = {
23
- ...contentType,
24
- // reuse new model def
25
- singularName: contentType.modelName,
26
- tableName: contentType.collectionName,
27
- attributes: {
28
- ...Object.keys(contentType.attributes || {}).reduce((attrs, attrName) => {
29
- return Object.assign(attrs, {
30
- [attrName]: transformAttribute(contentType.attributes[attrName]),
31
- });
32
- }, {}),
33
- },
34
- };
35
-
36
- return model;
37
- });
38
- };
39
-
40
- module.exports = { transformContentTypes };
package/lib/utils/knex.js DELETED
@@ -1,22 +0,0 @@
1
- 'use strict';
2
-
3
- const KnexBuilder = require('knex/lib/query/querybuilder');
4
- const KnexRaw = require('knex/lib/raw');
5
-
6
- const isKnexQuery = (value) => {
7
- return value instanceof KnexBuilder || value instanceof KnexRaw;
8
- };
9
-
10
- /**
11
- * Adds the name of the schema to the table name if the schema was defined by the user.
12
- * Users can set the db schema only for Postgres in strapi database config.
13
- */
14
- const addSchema = (tableName) => {
15
- const schemaName = strapi.db.connection.getSchemaName();
16
- return schemaName ? `${schemaName}.${tableName}` : tableName;
17
- };
18
-
19
- module.exports = {
20
- isKnexQuery,
21
- addSchema,
22
- };
@@ -1,20 +0,0 @@
1
- 'use strict';
2
-
3
- const { validateRelations } = require('./relations');
4
-
5
- /**
6
- * Validate if the database is in a valid state before starting the server.
7
- *
8
- * @param {*} db - Database instance
9
- */
10
- async function validateDatabase(db) {
11
- const relationErrors = await validateRelations(db);
12
- const errorList = [...relationErrors];
13
-
14
- if (errorList.length > 0) {
15
- errorList.forEach((error) => strapi.log.error(error));
16
- throw new Error('There are errors in some of your models. Please check the logs above.');
17
- }
18
- }
19
-
20
- module.exports = { validateDatabase };
@@ -1,89 +0,0 @@
1
- 'use strict';
2
-
3
- const types = require('../../types');
4
- const { getJoinTableName } = require('../../metadata/relations');
5
-
6
- const getLinksWithoutMappedBy = (db) => {
7
- const relationsToUpdate = {};
8
-
9
- db.metadata.forEach((contentType) => {
10
- const attributes = contentType.attributes;
11
-
12
- // For each relation attribute, add the joinTable name to tablesToUpdate
13
- Object.values(attributes).forEach((attribute) => {
14
- if (!types.isRelation(attribute.type)) return;
15
-
16
- if (attribute.inversedBy) {
17
- const invRelation = db.metadata.get(attribute.target).attributes[attribute.inversedBy];
18
-
19
- // Both relations use inversedBy.
20
- if (invRelation.inversedBy) {
21
- relationsToUpdate[attribute.joinTable.name] = {
22
- relation: attribute,
23
- invRelation,
24
- };
25
- }
26
- }
27
- });
28
- });
29
-
30
- return Object.values(relationsToUpdate);
31
- };
32
-
33
- const isLinkTableEmpty = async (db, linkTableName) => {
34
- // If the table doesn't exist, it's empty
35
- const exists = await db.getSchemaConnection().hasTable(linkTableName);
36
- if (!exists) return true;
37
-
38
- const result = await db.getConnection().count('* as count').from(linkTableName);
39
- return Number(result[0].count) === 0;
40
- };
41
-
42
- /**
43
- * Validates bidirectional relations before starting the server.
44
- * - If both sides use inversedBy, one of the sides must switch to mappedBy.
45
- * When this happens, two join tables exist in the database.
46
- * This makes sure you switch the side which does not delete any data.
47
- *
48
- * @param {*} db
49
- * @return {*}
50
- */
51
- const validateBidirectionalRelations = async (db) => {
52
- const invalidLinks = getLinksWithoutMappedBy(db);
53
- const errorList = [];
54
-
55
- for (const { relation, invRelation } of invalidLinks) {
56
- const contentType = db.metadata.get(invRelation.target);
57
- const invContentType = db.metadata.get(relation.target);
58
-
59
- // Generate the join table name based on the relation target table and attribute name.
60
- const joinTableName = getJoinTableName(contentType.tableName, invRelation.inversedBy);
61
- const inverseJoinTableName = getJoinTableName(invContentType.tableName, relation.inversedBy);
62
-
63
- const joinTableEmpty = await isLinkTableEmpty(db, joinTableName);
64
- const inverseJoinTableEmpty = await isLinkTableEmpty(db, inverseJoinTableName);
65
-
66
- if (joinTableEmpty) {
67
- process.emitWarning(
68
- `Error on attribute "${invRelation.inversedBy}" in model "${contentType.singularName}" (${contentType.uid}).` +
69
- ` Please modify your ${contentType.singularName} schema by renaming the key "inversedBy" to "mappedBy".` +
70
- ` Ex: { "inversedBy": "${relation.inversedBy}" } -> { "mappedBy": "${relation.inversedBy}" }`
71
- );
72
- } else if (inverseJoinTableEmpty) {
73
- // Its safe to delete the inverse join table
74
- process.emitWarning(
75
- `Error on attribute "${relation.inversedBy}" in model "${invContentType.singularName}" (${invContentType.uid}).` +
76
- ` Please modify your ${invContentType.singularName} schema by renaming the key "inversedBy" to "mappedBy".` +
77
- ` Ex: { "inversedBy": "${invRelation.inversedBy}" } -> { "mappedBy": "${invRelation.inversedBy}" }`
78
- );
79
- } else {
80
- // Both sides have data in the join table
81
- }
82
- }
83
-
84
- return errorList;
85
- };
86
-
87
- module.exports = {
88
- validateBidirectionalRelations,
89
- };
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- const { validateBidirectionalRelations } = require('./bidirectional');
4
-
5
- /**
6
- * Validates if relations data and tables are in a valid state before
7
- * starting the server.
8
- */
9
- const validateRelations = async (db) => {
10
- const bidirectionalRelationsErrors = await validateBidirectionalRelations(db);
11
- return [...bidirectionalRelationsErrors];
12
- };
13
-
14
- module.exports = { validateRelations };