@strapi/database 4.15.0-alpha.0 → 4.15.1

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 +6189 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/index.mjs +6157 -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 +21 -9
  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,283 +0,0 @@
1
- 'use strict';
2
-
3
- const SQL_QUERIES = {
4
- TABLE_LIST: /* sql */ `
5
- SELECT *
6
- FROM information_schema.tables
7
- WHERE
8
- table_schema = ?
9
- AND table_type = 'BASE TABLE'
10
- AND table_name != 'geometry_columns'
11
- AND table_name != 'spatial_ref_sys';
12
- `,
13
- LIST_COLUMNS: /* sql */ `
14
- SELECT data_type, column_name, character_maximum_length, column_default, is_nullable
15
- FROM information_schema.columns
16
- WHERE table_schema = ? AND table_name = ?;
17
- `,
18
- INDEX_LIST: /* sql */ `
19
- SELECT
20
- ix.indexrelid,
21
- i.relname as index_name,
22
- a.attname as column_name,
23
- ix.indisunique as is_unique,
24
- ix.indisprimary as is_primary
25
- FROM
26
- pg_class t,
27
- pg_namespace s,
28
- pg_class i,
29
- pg_index ix,
30
- pg_attribute a
31
- WHERE
32
- t.oid = ix.indrelid
33
- AND i.oid = ix.indexrelid
34
- AND a.attrelid = t.oid
35
- AND a.attnum = ANY(ix.indkey)
36
- AND t.relkind = 'r'
37
- AND t.relnamespace = s.oid
38
- AND s.nspname = ?
39
- AND t.relname = ?;
40
- `,
41
- FOREIGN_KEY_LIST: /* sql */ `
42
- SELECT
43
- tco."constraint_name" as constraint_name
44
- FROM information_schema.table_constraints tco
45
- WHERE
46
- tco.constraint_type = 'FOREIGN KEY'
47
- AND tco.constraint_schema = ?
48
- AND tco.table_name = ?
49
- `,
50
- FOREIGN_KEY_REFERENCES: /* sql */ `
51
- SELECT
52
- kcu."constraint_name" as constraint_name,
53
- kcu."column_name" as column_name
54
-
55
- FROM information_schema.key_column_usage kcu
56
- WHERE kcu.constraint_name=ANY(?)
57
- AND kcu.table_schema = ?
58
- AND kcu.table_name = ?;
59
- `,
60
-
61
- FOREIGN_KEY_REFERENCES_CONSTRAIN: /* sql */ `
62
- SELECT
63
- rco.update_rule as on_update,
64
- rco.delete_rule as on_delete,
65
- rco."unique_constraint_name" as unique_constraint_name
66
- FROM information_schema.referential_constraints rco
67
- WHERE rco.constraint_name=ANY(?)
68
- AND rco.constraint_schema = ?
69
- `,
70
- FOREIGN_KEY_REFERENCES_CONSTRAIN_RFERENCE: /* sql */ `
71
- SELECT
72
- rel_kcu."table_name" as foreign_table,
73
- rel_kcu."column_name" as fk_column_name
74
- FROM information_schema.key_column_usage rel_kcu
75
- WHERE rel_kcu.constraint_name=?
76
- AND rel_kcu.table_schema = ?
77
- `,
78
- };
79
-
80
- const toStrapiType = (column) => {
81
- const rootType = column.data_type.toLowerCase().match(/[^(), ]+/)[0];
82
-
83
- switch (rootType) {
84
- case 'integer': {
85
- // find a way to figure out the increments
86
- return { type: 'integer' };
87
- }
88
- case 'text': {
89
- return { type: 'text', args: ['longtext'] };
90
- }
91
- case 'boolean': {
92
- return { type: 'boolean' };
93
- }
94
- case 'character': {
95
- return { type: 'string', args: [column.character_maximum_length] };
96
- }
97
- case 'timestamp': {
98
- return { type: 'datetime', args: [{ useTz: false, precision: 6 }] };
99
- }
100
- case 'date': {
101
- return { type: 'date' };
102
- }
103
- case 'time': {
104
- return { type: 'time', args: [{ precision: 3 }] };
105
- }
106
- case 'numeric': {
107
- return { type: 'decimal', args: [10, 2] };
108
- }
109
- case 'real':
110
- case 'double': {
111
- return { type: 'double' };
112
- }
113
- case 'bigint': {
114
- return { type: 'bigInteger' };
115
- }
116
- case 'jsonb': {
117
- return { type: 'jsonb' };
118
- }
119
- default: {
120
- return { type: 'specificType', args: [column.data_type] };
121
- }
122
- }
123
- };
124
-
125
- const getIndexType = (index) => {
126
- if (index.is_primary) {
127
- return 'primary';
128
- }
129
-
130
- if (index.is_unique) {
131
- return 'unique';
132
- }
133
-
134
- return null;
135
- };
136
-
137
- class PostgresqlSchemaInspector {
138
- constructor(db) {
139
- this.db = db;
140
- }
141
-
142
- async getSchema() {
143
- const schema = { tables: [] };
144
-
145
- const tables = await this.getTables();
146
-
147
- schema.tables = await Promise.all(
148
- tables.map(async (tableName) => {
149
- const columns = await this.getColumns(tableName);
150
- const indexes = await this.getIndexes(tableName);
151
- const foreignKeys = await this.getForeignKeys(tableName);
152
-
153
- return {
154
- name: tableName,
155
- columns,
156
- indexes,
157
- foreignKeys,
158
- };
159
- })
160
- );
161
-
162
- return schema;
163
- }
164
-
165
- getDatabaseSchema() {
166
- return this.db.connection.getSchemaName() || 'public';
167
- }
168
-
169
- async getTables() {
170
- const { rows } = await this.db.connection.raw(SQL_QUERIES.TABLE_LIST, [
171
- this.getDatabaseSchema(),
172
- ]);
173
-
174
- return rows.map((row) => row.table_name);
175
- }
176
-
177
- async getColumns(tableName) {
178
- const { rows } = await this.db.connection.raw(SQL_QUERIES.LIST_COLUMNS, [
179
- this.getDatabaseSchema(),
180
- tableName,
181
- ]);
182
-
183
- return rows.map((row) => {
184
- const { type, args = [], ...rest } = toStrapiType(row);
185
-
186
- const defaultTo =
187
- row.column_default && row.column_default.includes('nextval(') ? null : row.column_default;
188
-
189
- return {
190
- type,
191
- args,
192
- defaultTo,
193
- name: row.column_name,
194
- notNullable: row.is_nullable === 'NO',
195
- unsigned: false,
196
- ...rest,
197
- };
198
- });
199
- }
200
-
201
- async getIndexes(tableName) {
202
- const { rows } = await this.db.connection.raw(SQL_QUERIES.INDEX_LIST, [
203
- this.getDatabaseSchema(),
204
- tableName,
205
- ]);
206
-
207
- const ret = {};
208
-
209
- for (const index of rows) {
210
- if (index.column_name === 'id') {
211
- continue;
212
- }
213
-
214
- if (!ret[index.indexrelid]) {
215
- ret[index.indexrelid] = {
216
- columns: [index.column_name],
217
- name: index.index_name,
218
- type: getIndexType(index),
219
- };
220
- } else {
221
- ret[index.indexrelid].columns.push(index.column_name);
222
- }
223
- }
224
-
225
- return Object.values(ret);
226
- }
227
-
228
- async getForeignKeys(tableName) {
229
- const { rows } = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_LIST, [
230
- this.getDatabaseSchema(),
231
- tableName,
232
- ]);
233
-
234
- const ret = {};
235
-
236
- for (const fk of rows) {
237
- ret[fk.constraint_name] = {
238
- name: fk.constraint_name,
239
- columns: [],
240
- referencedColumns: [],
241
- referencedTable: null,
242
- onUpdate: null,
243
- onDelete: null,
244
- };
245
- }
246
- const constraintNames = Object.keys(ret);
247
- const dbSchema = this.getDatabaseSchema();
248
- if (constraintNames.length > 0) {
249
- const { rows: fkReferences } = await this.db.connection.raw(
250
- SQL_QUERIES.FOREIGN_KEY_REFERENCES,
251
- [[constraintNames], dbSchema, tableName]
252
- );
253
-
254
- for (const fkReference of fkReferences) {
255
- ret[fkReference.constraint_name].columns.push(fkReference.column_name);
256
-
257
- const { rows: fkReferencesConstraint } = await this.db.connection.raw(
258
- SQL_QUERIES.FOREIGN_KEY_REFERENCES_CONSTRAIN,
259
- [[fkReference.constraint_name], dbSchema]
260
- );
261
-
262
- for (const fkReferenceC of fkReferencesConstraint) {
263
- const { rows: fkReferencesConstraintReferece } = await this.db.connection.raw(
264
- SQL_QUERIES.FOREIGN_KEY_REFERENCES_CONSTRAIN_RFERENCE,
265
- [fkReferenceC.unique_constraint_name, dbSchema]
266
- );
267
- for (const fkReferenceConst of fkReferencesConstraintReferece) {
268
- ret[fkReference.constraint_name].referencedTable = fkReferenceConst.foreign_table;
269
- ret[fkReference.constraint_name].referencedColumns.push(
270
- fkReferenceConst.fk_column_name
271
- );
272
- }
273
- ret[fkReference.constraint_name].onUpdate = fkReferenceC.on_update.toUpperCase();
274
- ret[fkReference.constraint_name].onDelete = fkReferenceC.on_delete.toUpperCase();
275
- }
276
- }
277
- }
278
-
279
- return Object.values(ret);
280
- }
281
- }
282
-
283
- module.exports = PostgresqlSchemaInspector;
@@ -1,87 +0,0 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const fse = require('fs-extra');
5
-
6
- const errors = require('../../errors');
7
- const { Dialect } = require('../dialect');
8
- const SqliteSchemaInspector = require('./schema-inspector');
9
-
10
- const UNSUPPORTED_OPERATORS = ['$jsonSupersetOf'];
11
-
12
- class SqliteDialect extends Dialect {
13
- constructor(db) {
14
- super(db);
15
-
16
- this.schemaInspector = new SqliteSchemaInspector(db);
17
- }
18
-
19
- configure() {
20
- this.db.config.connection.connection.filename = path.resolve(
21
- this.db.config.connection.connection.filename
22
- );
23
-
24
- const dbDir = path.dirname(this.db.config.connection.connection.filename);
25
-
26
- fse.ensureDirSync(dbDir);
27
- }
28
-
29
- useReturning() {
30
- return true;
31
- }
32
-
33
- async initialize() {
34
- await this.db.connection.raw('pragma foreign_keys = on');
35
- }
36
-
37
- canAlterConstraints() {
38
- return false;
39
- }
40
-
41
- getSqlType(type) {
42
- switch (type) {
43
- case 'enum': {
44
- return 'text';
45
- }
46
- case 'double':
47
- case 'decimal': {
48
- return 'float';
49
- }
50
- case 'timestamp': {
51
- return 'datetime';
52
- }
53
- default: {
54
- return type;
55
- }
56
- }
57
- }
58
-
59
- supportsOperator(operator) {
60
- return !UNSUPPORTED_OPERATORS.includes(operator);
61
- }
62
-
63
- async startSchemaUpdate() {
64
- await this.db.connection.raw(`pragma foreign_keys = off`);
65
- }
66
-
67
- async endSchemaUpdate() {
68
- await this.db.connection.raw(`pragma foreign_keys = on`);
69
- }
70
-
71
- transformErrors(error) {
72
- switch (error.errno) {
73
- case 19: {
74
- throw new errors.NotNullError(); // TODO: extract column name
75
- }
76
- default: {
77
- super.transformErrors(error);
78
- }
79
- }
80
- }
81
-
82
- canAddIncrements() {
83
- return false;
84
- }
85
- }
86
-
87
- module.exports = SqliteDialect;
@@ -1,151 +0,0 @@
1
- 'use strict';
2
-
3
- const SQL_QUERIES = {
4
- TABLE_LIST: `select name from sqlite_master where type = 'table' and name NOT LIKE 'sqlite%'`,
5
- TABLE_INFO: `pragma table_info(??)`,
6
- INDEX_LIST: 'pragma index_list(??)',
7
- INDEX_INFO: 'pragma index_info(??)',
8
- FOREIGN_KEY_LIST: 'pragma foreign_key_list(??)',
9
- };
10
-
11
- const toStrapiType = (column) => {
12
- const { type } = column;
13
-
14
- const rootType = type.toLowerCase().match(/[^(), ]+/)[0];
15
-
16
- switch (rootType) {
17
- case 'integer': {
18
- if (column.pk) {
19
- return { type: 'increments', args: [{ primary: true, primaryKey: true }] };
20
- }
21
-
22
- return { type: 'integer' };
23
- }
24
- case 'float': {
25
- return { type: 'float', args: [10, 2] };
26
- }
27
- case 'bigint': {
28
- return { type: 'bigInteger' };
29
- }
30
- case 'varchar': {
31
- const length = type.slice(8, type.length - 1);
32
-
33
- return { type: 'string', args: [Number(length)] };
34
- }
35
- case 'text': {
36
- return { type: 'text', args: ['longtext'] };
37
- }
38
- case 'json': {
39
- return { type: 'jsonb' };
40
- }
41
- case 'boolean': {
42
- return { type: 'boolean' };
43
- }
44
- case 'datetime': {
45
- return { type: 'datetime', args: [{ useTz: false, precision: 6 }] };
46
- }
47
- case 'date': {
48
- return { type: 'date' };
49
- }
50
- case 'time': {
51
- return { type: 'time', args: [{ precision: 3 }] };
52
- }
53
- default: {
54
- return { type: 'specificType', args: [column.data_type] };
55
- }
56
- }
57
- };
58
-
59
- class SqliteSchemaInspector {
60
- constructor(db) {
61
- this.db = db;
62
- }
63
-
64
- async getSchema() {
65
- const schema = { tables: [] };
66
- const tables = await this.getTables();
67
-
68
- for (const tableName of tables) {
69
- const columns = await this.getColumns(tableName);
70
- const indexes = await this.getIndexes(tableName);
71
- const foreignKeys = await this.getForeignKeys(tableName);
72
-
73
- schema.tables.push({
74
- name: tableName,
75
- columns,
76
- indexes,
77
- foreignKeys,
78
- });
79
- }
80
-
81
- return schema;
82
- }
83
-
84
- async getTables() {
85
- const rows = await this.db.connection.raw(SQL_QUERIES.TABLE_LIST);
86
-
87
- return rows.map((row) => row.name);
88
- }
89
-
90
- async getColumns(tableName) {
91
- const rows = await this.db.connection.raw(SQL_QUERIES.TABLE_INFO, [tableName]);
92
-
93
- return rows.map((row) => {
94
- const { type, args = [], ...rest } = toStrapiType(row);
95
-
96
- return {
97
- type,
98
- args,
99
- name: row.name,
100
- defaultTo: row.dflt_value,
101
- notNullable: row.notnull !== null ? Boolean(row.notnull) : null,
102
- unsigned: false,
103
- ...rest,
104
- };
105
- });
106
- }
107
-
108
- async getIndexes(tableName) {
109
- const indexes = await this.db.connection.raw(SQL_QUERIES.INDEX_LIST, [tableName]);
110
-
111
- const ret = [];
112
-
113
- for (const index of indexes.filter((index) => !index.name.startsWith('sqlite_'))) {
114
- const res = await this.db.connection.raw(SQL_QUERIES.INDEX_INFO, [index.name]);
115
-
116
- ret.push({
117
- columns: res.map((row) => row.name),
118
- name: index.name,
119
- type: index.unique ? 'unique' : null,
120
- });
121
- }
122
-
123
- return ret;
124
- }
125
-
126
- async getForeignKeys(tableName) {
127
- const fks = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_LIST, [tableName]);
128
-
129
- const ret = {};
130
-
131
- for (const fk of fks) {
132
- if (!ret[fk.id]) {
133
- ret[fk.id] = {
134
- // TODO: name, // find name
135
- columns: [fk.from],
136
- referencedColumns: [fk.to],
137
- referencedTable: fk.table,
138
- onUpdate: fk.on_update.toUpperCase(),
139
- onDelete: fk.on_delete.toUpperCase(),
140
- };
141
- } else {
142
- ret[fk.id].columns.push(fk.from);
143
- ret[fk.id].referencedColumns.push(fk.to);
144
- }
145
- }
146
-
147
- return Object.values(ret);
148
- }
149
- }
150
-
151
- module.exports = SqliteSchemaInspector;
@@ -1,186 +0,0 @@
1
- 'use strict';
2
-
3
- const { relationsOrderer } = require('../relations-orderer');
4
-
5
- describe('Given I have some relations in the database', () => {
6
- describe('When I connect a relation at the end', () => {
7
- test('Then it is placed at the end with the correct order', () => {
8
- const orderer = relationsOrderer(
9
- [
10
- { id: 2, order: 4 },
11
- { id: 3, order: 10 },
12
- ],
13
- 'id',
14
- 'order'
15
- );
16
-
17
- orderer.connect([{ id: 4, position: { end: true } }, { id: 5 }]);
18
-
19
- expect(orderer.get()).toMatchObject([
20
- { id: 2, order: 4 },
21
- { id: 3, order: 10 },
22
- { id: 4, order: 10.5 },
23
- { id: 5, order: 10.5 },
24
- ]);
25
- });
26
- });
27
-
28
- describe('When I connect a relation at the start', () => {
29
- test('Then it is placed at the start with the correct order', () => {
30
- const orderer = relationsOrderer(
31
- [
32
- { id: 2, order: 4 },
33
- { id: 3, order: 10 },
34
- ],
35
- 'id',
36
- 'order'
37
- );
38
-
39
- orderer.connect([{ id: 4, position: { start: true } }]);
40
-
41
- expect(orderer.get()).toMatchObject([
42
- { id: 4, order: 0.5 },
43
- { id: 2, order: 4 },
44
- { id: 3, order: 10 },
45
- ]);
46
- });
47
- });
48
-
49
- describe('When I connect multiple relations using before', () => {
50
- test('Then they are correctly ordered', () => {
51
- const orderer = relationsOrderer(
52
- [
53
- { id: 2, order: 4 },
54
- { id: 3, order: 10 },
55
- ],
56
- 'id',
57
- 'order'
58
- );
59
-
60
- orderer.connect([
61
- { id: 4, position: { before: 3 } },
62
- { id: 5, position: { before: 4 } },
63
- ]);
64
-
65
- expect(orderer.get()).toMatchObject([
66
- { id: 2, order: 4 },
67
- { id: 5, order: 9.5 },
68
- { id: 4, order: 9.5 },
69
- { id: 3, order: 10 },
70
- ]);
71
- });
72
- });
73
-
74
- describe('When you connect multiple disordered relations', () => {
75
- test('Then they are correctly ordered', () => {
76
- const orderer = relationsOrderer(
77
- [
78
- { id: 1, order: 1 },
79
- { id: 2, order: 2 },
80
- { id: 3, order: 3 },
81
- ],
82
- 'id',
83
- 'order'
84
- );
85
-
86
- orderer.connect([
87
- { id: 5, position: { before: 1 } },
88
- { id: 1, position: { before: 2 } },
89
- { id: 2, position: { end: true } },
90
- ]);
91
-
92
- expect(orderer.get()).toMatchObject([
93
- { id: 5, order: 0.5 },
94
- { id: 1, order: 1.5 },
95
- { id: 3, order: 3 },
96
- { id: 2, order: 3.5 },
97
- ]);
98
- });
99
- });
100
-
101
- describe('When you connect a relation before a non-existing relation in non-strict mode', () => {
102
- test('Then it is placed at the end', () => {
103
- const orderer = relationsOrderer(
104
- [
105
- { id: 1, order: 1 },
106
- { id: 2, order: 2 },
107
- { id: 3, order: 3 },
108
- ],
109
- 'id',
110
- 'order',
111
- false
112
- );
113
-
114
- orderer.connect([{ id: 4, position: { before: 5 } }]);
115
-
116
- expect(orderer.get()).toMatchObject([
117
- { id: 1, order: 1 },
118
- { id: 2, order: 2 },
119
- { id: 3, order: 3 },
120
- { id: 4, order: 3.5 },
121
- ]);
122
- });
123
- });
124
-
125
- describe('When you connect a relation before one with null order', () => {
126
- test('Then it replaces null order values to 1 and properly reorders relations', () => {
127
- const orderer = relationsOrderer(
128
- [
129
- { id: 2, order: null },
130
- { id: 3, order: null },
131
- ],
132
- 'id',
133
- 'order'
134
- );
135
-
136
- orderer.connect([{ id: 4, position: { before: 3 } }, { id: 5 }]);
137
-
138
- expect(orderer.get()).toMatchObject([
139
- { id: 2, order: 1 },
140
- { id: 4, order: 0.5 },
141
- { id: 3, order: 1 },
142
- { id: 5, order: 1.5 },
143
- ]);
144
- });
145
- });
146
- });
147
-
148
- describe('Given there are no relations in the database', () => {
149
- describe('When you connect multiple new relations', () => {
150
- test('Then they are correctly ordered', () => {
151
- const orderer = relationsOrderer([], 'id', 'order');
152
-
153
- orderer.connect([
154
- { id: 1, position: { start: true } },
155
- { id: 2, position: { start: true } },
156
- { id: 3, position: { after: 1 } },
157
- ]);
158
-
159
- expect(orderer.get()).toMatchObject([
160
- { id: 2, order: 0.5 },
161
- { id: 1, order: 0.5 },
162
- { id: 3, order: 0.5 },
163
- ]);
164
- });
165
- });
166
-
167
- describe('When you connect multiple disordered relations', () => {
168
- test('Then they are correctly ordered', () => {
169
- const orderer = relationsOrderer([], 'id', 'order');
170
-
171
- orderer.connect([
172
- { id: 5, position: { before: 1 } },
173
- { id: 1, position: { before: 2 } },
174
- { id: 2, position: { end: true } },
175
- { id: 3, position: { after: 1 } },
176
- ]);
177
-
178
- expect(orderer.get()).toMatchObject([
179
- { id: 5, order: 0.5 },
180
- { id: 1, order: 0.5 },
181
- { id: 3, order: 0.5 },
182
- { id: 2, order: 0.5 },
183
- ]);
184
- });
185
- });
186
- });