@strapi/database 4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8 → 4.9.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.
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ node_modules/
2
+ .eslintrc.js
package/.eslintrc.js ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['custom/back'],
4
+ };
package/jest.config.js CHANGED
@@ -1,10 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const baseConfig = require('../../../jest.base-config');
4
- const pkg = require('./package.json');
5
-
6
3
  module.exports = {
7
- ...baseConfig,
8
- displayName: (pkg.strapi && pkg.strapi.name) || pkg.name,
9
- roots: [__dirname],
4
+ preset: '../../../jest-preset.unit.js',
10
5
  };
@@ -1,5 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * Require our dialect-specific code
5
+ */
3
6
  const getDialectClass = (client) => {
4
7
  switch (client) {
5
8
  case 'postgres':
@@ -13,12 +16,34 @@ const getDialectClass = (client) => {
13
16
  }
14
17
  };
15
18
 
19
+ /**
20
+ * Get the dialect of a database client
21
+ *
22
+ * @param {string} The client value from a project database configuration
23
+ * @returns {string} The dialect of that client
24
+ */
25
+ const getDialectName = (client) => {
26
+ switch (client) {
27
+ case 'postgres':
28
+ return 'postgres';
29
+ case 'mysql':
30
+ case 'mysql2':
31
+ return 'mysql';
32
+ case 'sqlite':
33
+ case 'sqlite-legacy':
34
+ return 'sqlite';
35
+ default:
36
+ throw new Error(`Unknown dialect ${client}`);
37
+ }
38
+ };
39
+
16
40
  const getDialect = (db) => {
17
41
  const { client } = db.config.connection;
42
+ const dialectName = getDialectName(client);
18
43
 
19
- const constructor = getDialectClass(client);
44
+ const constructor = getDialectClass(dialectName);
20
45
  const dialect = new constructor(db);
21
- dialect.client = client;
46
+ dialect.client = dialectName;
22
47
 
23
48
  return dialect;
24
49
  };
@@ -268,9 +268,12 @@ const createEntityManager = (db) => {
268
268
  throw new Error('Nothing to insert');
269
269
  }
270
270
 
271
- await this.createQueryBuilder(uid).insert(dataToInsert).execute();
271
+ const createdEntries = await this.createQueryBuilder(uid).insert(dataToInsert).execute();
272
272
 
273
- const result = { count: data.length };
273
+ const result = {
274
+ count: data.length,
275
+ ids: createdEntries.map((entry) => (typeof entry === 'object' ? entry?.id : entry)),
276
+ };
274
277
 
275
278
  await db.lifecycles.run('afterCreateMany', uid, { params, result }, states);
276
279
 
@@ -14,9 +14,15 @@ describe('diffSchemas', () => {
14
14
  });
15
15
 
16
16
  diffSchemas = schemaDiff.diff.bind(schemaDiff);
17
+
18
+ global.strapi = {
19
+ store: {
20
+ get: () => [],
21
+ },
22
+ };
17
23
  });
18
24
 
19
- test('New Table', () => {
25
+ test('New Table', async () => {
20
26
  const testTable = {
21
27
  name: 'my_table',
22
28
  };
@@ -29,7 +35,7 @@ describe('diffSchemas', () => {
29
35
  tables: [testTable],
30
36
  };
31
37
 
32
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
38
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
33
39
  status: 'CHANGED',
34
40
  diff: {
35
41
  tables: {
@@ -42,7 +48,7 @@ describe('diffSchemas', () => {
42
48
  });
43
49
  });
44
50
 
45
- test('Removed Table', () => {
51
+ test('Removed Table', async () => {
46
52
  const testTable = {
47
53
  name: 'my_table',
48
54
  };
@@ -55,7 +61,7 @@ describe('diffSchemas', () => {
55
61
  tables: [],
56
62
  };
57
63
 
58
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
64
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
59
65
  status: 'CHANGED',
60
66
  diff: {
61
67
  tables: {
@@ -68,7 +74,7 @@ describe('diffSchemas', () => {
68
74
  });
69
75
  });
70
76
 
71
- test('Unchanged Table', () => {
77
+ test('Unchanged Table', async () => {
72
78
  const testTable = {
73
79
  name: 'my_table',
74
80
  columns: [],
@@ -84,7 +90,7 @@ describe('diffSchemas', () => {
84
90
  tables: [testTable],
85
91
  };
86
92
 
87
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
93
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
88
94
  status: 'UNCHANGED',
89
95
  diff: {
90
96
  tables: {
@@ -98,7 +104,7 @@ describe('diffSchemas', () => {
98
104
  });
99
105
 
100
106
  describe('Changed table', () => {
101
- test('added column', () => {
107
+ test('added column', async () => {
102
108
  const srcSchema = {
103
109
  tables: [
104
110
  {
@@ -125,7 +131,7 @@ describe('diffSchemas', () => {
125
131
  ],
126
132
  };
127
133
 
128
- expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({
134
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
129
135
  status: 'CHANGED',
130
136
  diff: {
131
137
  tables: {
@@ -178,4 +184,48 @@ describe('diffSchemas', () => {
178
184
  test.todo('unchanged foreign key');
179
185
  test.todo('removed foreign key');
180
186
  });
187
+
188
+ test('With persisted DB tables', async () => {
189
+ const testTables = [
190
+ {
191
+ name: 'my_table',
192
+ },
193
+ {
194
+ name: 'my_table_1',
195
+ },
196
+ ];
197
+
198
+ const coreStoreTable = {
199
+ name: 'strapi_core_store_settings',
200
+ columns: [],
201
+ indexes: [],
202
+ foreignKeys: [],
203
+ };
204
+
205
+ global.strapi = {
206
+ store: {
207
+ get: async () => [testTables[0].name, 'table2'],
208
+ },
209
+ };
210
+
211
+ const srcSchema = {
212
+ tables: [...testTables, coreStoreTable],
213
+ };
214
+
215
+ const destSchema = {
216
+ tables: [coreStoreTable],
217
+ };
218
+
219
+ expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({
220
+ status: 'CHANGED',
221
+ diff: {
222
+ tables: {
223
+ added: [],
224
+ updated: [],
225
+ unchanged: [coreStoreTable],
226
+ removed: [testTables[1]],
227
+ },
228
+ },
229
+ });
230
+ });
181
231
  });
@@ -322,7 +322,7 @@ module.exports = (db) => {
322
322
  };
323
323
  };
324
324
 
325
- const diffSchemas = (srcSchema, destSchema) => {
325
+ const diffSchemas = async (srcSchema, destSchema) => {
326
326
  const addedTables = [];
327
327
  const updatedTables = [];
328
328
  const unchangedTables = [];
@@ -344,10 +344,17 @@ module.exports = (db) => {
344
344
  }
345
345
  }
346
346
 
347
+ const persistedTables = helpers.hasTable(srcSchema, 'strapi_core_store_settings')
348
+ ? (await strapi.store.get({
349
+ type: 'core',
350
+ key: 'persisted_tables',
351
+ })) ?? []
352
+ : [];
353
+
347
354
  for (const srcTable of srcSchema.tables) {
348
355
  if (
349
356
  !helpers.hasTable(destSchema, srcTable.name) &&
350
- !RESERVED_TABLE_NAMES.includes(srcTable.name)
357
+ ![...RESERVED_TABLE_NAMES, ...persistedTables].includes(srcTable.name)
351
358
  ) {
352
359
  removedTables.push(srcTable);
353
360
  }
@@ -50,7 +50,7 @@ const createSchemaProvider = (db) => {
50
50
 
51
51
  const DBSchema = await db.dialect.schemaInspector.getSchema();
52
52
 
53
- const { status, diff } = this.schemaDiff.diff(DBSchema, schema);
53
+ const { status, diff } = await this.schemaDiff.diff(DBSchema, schema);
54
54
 
55
55
  if (status === 'CHANGED') {
56
56
  await this.builder.updateSchema(diff);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.9.0-exp.90df253ba90fd6879eb56a720a1f80d04ff745b8",
3
+ "version": "4.9.1",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -28,7 +28,9 @@
28
28
  "lib": "./lib"
29
29
  },
30
30
  "scripts": {
31
- "test:unit": "jest --verbose"
31
+ "test:unit": "run -T jest",
32
+ "test:unit:watch": "run -T jest --watch",
33
+ "lint": "run -T eslint ."
32
34
  },
33
35
  "dependencies": {
34
36
  "date-fns": "2.29.3",
@@ -43,5 +45,5 @@
43
45
  "node": ">=14.19.1 <=18.x.x",
44
46
  "npm": ">=6.0.0"
45
47
  },
46
- "gitHead": "366eb8a0d0f06935914854c6d9c4b3fe859468e0"
48
+ "gitHead": "c8f2f0543b45dda8eadde21a0782b64d156fc786"
47
49
  }
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
4
- const { isKnexQuery } = require('../utils/knex');
5
-
6
- let strapi;
7
-
8
- describe('knex', () => {
9
- beforeAll(async () => {
10
- strapi = await createStrapiInstance();
11
- });
12
-
13
- afterAll(async () => {
14
- await strapi.destroy();
15
- });
16
-
17
- describe('isKnexQuery', () => {
18
- test('knex query: true', () => {
19
- const res = isKnexQuery(strapi.db.connection('strapi_core_store_settings'));
20
- expect(res).toBe(true);
21
- });
22
-
23
- test('knex raw: true', () => {
24
- const res = isKnexQuery(strapi.db.connection.raw('SELECT * FROM strapi_core_store_settings'));
25
- expect(res).toBe(true);
26
- });
27
-
28
- test.each([[''], [{}], [[]], [2], [new Date()]])('%s: false', (value) => {
29
- const res = isKnexQuery(value);
30
- expect(res).toBe(false);
31
- });
32
- });
33
- });
@@ -1,308 +0,0 @@
1
- 'use strict';
2
-
3
- const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
4
-
5
- let strapi;
6
-
7
- describe('transactions', () => {
8
- let original;
9
- beforeAll(async () => {
10
- strapi = await createStrapiInstance();
11
- original = await strapi.db
12
- .queryBuilder('strapi::core-store')
13
- .select(['*'])
14
- .where({ id: 1 })
15
- .execute();
16
- });
17
-
18
- afterAll(async () => {
19
- await strapi.destroy();
20
- });
21
-
22
- afterEach(async () => {
23
- await strapi.db
24
- .queryBuilder('strapi::core-store')
25
- .update({
26
- key: original[0].key,
27
- })
28
- .where({ id: 1 })
29
- .execute();
30
- });
31
-
32
- describe('using a transaction method', () => {
33
- test('commits successfully', async () => {
34
- await strapi.db.transaction(async () => {
35
- await strapi.db
36
- .queryBuilder('strapi::core-store')
37
- .update({
38
- key: 'wrong key',
39
- })
40
- .where({ id: 1 })
41
- .execute();
42
-
43
- await strapi.db
44
- .queryBuilder('strapi::core-store')
45
- .update({
46
- key: 'new key',
47
- })
48
- .where({ id: 1 })
49
- .execute();
50
- });
51
-
52
- const end = await strapi.db
53
- .queryBuilder('strapi::core-store')
54
- .select(['*'])
55
- .where({ id: 1 })
56
- .execute();
57
-
58
- expect(end[0].key).toEqual('new key');
59
- });
60
-
61
- test('rollback successfully', async () => {
62
- try {
63
- await strapi.db.transaction(async () => {
64
- // this is valid
65
- await strapi.db
66
- .queryBuilder('strapi::core-store')
67
- .update({
68
- key: 'wrong key',
69
- })
70
- .where({ id: 1 })
71
- .execute();
72
-
73
- // this throws
74
- await strapi.db
75
- .queryBuilder('invalid_uid')
76
- .update({
77
- key: 'bad key',
78
- invalid_key: 'error',
79
- })
80
- .where({ id: 1 })
81
- .execute();
82
- });
83
-
84
- expect('this should not be reached').toBe(false);
85
- } catch (e) {
86
- // do nothing
87
- }
88
-
89
- const end = await strapi.db
90
- .queryBuilder('strapi::core-store')
91
- .select(['*'])
92
- .where({ id: 1 })
93
- .execute();
94
-
95
- expect(end[0].key).toEqual(original[0].key);
96
- });
97
-
98
- test('nested rollback -> rollback works', async () => {
99
- try {
100
- await strapi.db.transaction(async () => {
101
- // this is valid
102
- await strapi.db
103
- .queryBuilder('strapi::core-store')
104
- .update({
105
- key: 'changed key',
106
- })
107
- .where({ id: 1 })
108
- .execute();
109
-
110
- // here we'll make a nested transaction that throws and then confirm we still have "changed key" from above
111
- try {
112
- await strapi.db.transaction(async () => {
113
- await strapi.db
114
- .queryBuilder('strapi::core-store')
115
- .update({
116
- key: 'changed key - nested',
117
- })
118
- .where({ id: 1 })
119
- .execute();
120
-
121
- // this should throw and roll back
122
- await strapi.db
123
- .queryBuilder('invalid_uid')
124
- .update({
125
- invalid_key: 'error',
126
- })
127
- .where({ id: 1 })
128
- .execute();
129
- });
130
- } catch (e) {
131
- // do nothing
132
- }
133
-
134
- // should equal the result from above
135
- const result = await strapi.db
136
- .queryBuilder('strapi::core-store')
137
- .select(['*'])
138
- .where({ id: 1 })
139
- .execute();
140
-
141
- expect(result[0].key).toEqual('changed key');
142
-
143
- // this throws
144
- await strapi.db
145
- .queryBuilder('invalid_uid')
146
- .update({
147
- key: original[0].key,
148
- invalid_key: 'error',
149
- })
150
- .where({ id: 1 })
151
- .execute();
152
- });
153
-
154
- expect('this should not be reached').toBe(false);
155
- } catch (e) {
156
- // do nothing
157
- }
158
-
159
- const end = await strapi.db
160
- .queryBuilder('strapi::core-store')
161
- .select(['*'])
162
- .where({ id: 1 })
163
- .execute();
164
-
165
- expect(end[0].key).toEqual(original[0].key);
166
- });
167
-
168
- test('nested commit -> rollback works', async () => {
169
- try {
170
- await strapi.db.transaction(async () => {
171
- // this is valid
172
- await strapi.db
173
- .queryBuilder('strapi::core-store')
174
- .update({
175
- key: 'changed key',
176
- })
177
- .where({ id: 1 })
178
- .execute();
179
-
180
- // here we'll make a nested transaction that works, and then later we'll rollback the outer transaction
181
- try {
182
- await strapi.db.transaction(async () => {
183
- await strapi.db
184
- .queryBuilder('strapi::core-store')
185
- .update({
186
- key: 'changed key - nested',
187
- })
188
- .where({ id: 1 })
189
- .execute();
190
- });
191
- } catch (e) {
192
- // do nothing
193
- }
194
-
195
- // should equal the result from above
196
- const result = await strapi.db
197
- .queryBuilder('strapi::core-store')
198
- .select(['*'])
199
- .where({ id: 1 })
200
- .execute();
201
-
202
- expect(result[0].key).toEqual('changed key - nested');
203
-
204
- // this throws
205
- await strapi.db
206
- .queryBuilder('invalid_uid')
207
- .update({
208
- key: original[0].key,
209
- invalid_key: 'error',
210
- })
211
- .where({ id: 1 })
212
- .execute();
213
- });
214
-
215
- expect('this should not be reached').toBe(false);
216
- } catch (e) {
217
- // do nothing
218
- }
219
-
220
- const end = await strapi.db
221
- .queryBuilder('strapi::core-store')
222
- .select(['*'])
223
- .where({ id: 1 })
224
- .execute();
225
-
226
- expect(end[0].key).toEqual(original[0].key);
227
- });
228
- });
229
-
230
- describe('using a transaction object', () => {
231
- test('commits successfully', async () => {
232
- const trx = await strapi.db.transaction();
233
-
234
- try {
235
- await strapi.db
236
- .queryBuilder('strapi::core-store')
237
- .update({
238
- key: 'wrong key',
239
- })
240
- .where({ id: 1 })
241
- .transacting(trx.get())
242
- .execute();
243
-
244
- await strapi.db
245
- .queryBuilder('strapi::core-store')
246
- .update({
247
- key: original[0].key,
248
- })
249
- .where({ id: 1 })
250
- .transacting(trx.get())
251
- .execute();
252
-
253
- await trx.commit();
254
- } catch (e) {
255
- await trx.rollback();
256
- console.log(e.message);
257
- expect('this should not be reached').toBe(false);
258
- }
259
-
260
- const end = await strapi.db
261
- .queryBuilder('strapi::core-store')
262
- .select(['*'])
263
- .where({ id: 1 })
264
- .execute();
265
-
266
- expect(end[0].key).toEqual('strapi_content_types_schema');
267
- });
268
-
269
- test('rollback successfully', async () => {
270
- const trx = await strapi.db.transaction();
271
-
272
- try {
273
- await strapi.db
274
- .queryBuilder('strapi::core-store')
275
- .update({
276
- key: 'wrong key',
277
- })
278
- .where({ id: 1 })
279
- .transacting(trx.get())
280
- .execute();
281
-
282
- // this query should throw because it has errors
283
- await strapi.db
284
- .queryBuilder('invalid_uid')
285
- .update({
286
- key: 123,
287
- key_not_here: 'this should error',
288
- })
289
- .where({ id: 'this should error' })
290
- .transacting(trx.get())
291
- .execute();
292
-
293
- await trx.commit();
294
- expect('this should not be reached').toBe(false);
295
- } catch (e) {
296
- await trx.rollback();
297
- }
298
-
299
- const end = await strapi.db
300
- .queryBuilder('strapi::core-store')
301
- .select(['*'])
302
- .where({ id: 1 })
303
- .execute();
304
-
305
- expect(end[0].key).toEqual('strapi_content_types_schema');
306
- });
307
- });
308
- });