@strapi/database 4.5.0 → 4.5.2
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/lib/dialects/dialect.js +4 -0
- package/lib/dialects/mysql/constants.js +6 -0
- package/lib/dialects/mysql/database-inspector.js +37 -0
- package/lib/dialects/mysql/index.js +19 -0
- package/lib/dialects/postgresql/index.js +1 -1
- package/lib/dialects/sqlite/index.js +2 -2
- package/lib/entity-manager/regular-relations.js +118 -13
- package/lib/index.d.ts +2 -2
- package/lib/schema/index.d.ts +2 -2
- package/lib/utils/knex.js +10 -0
- package/package.json +3 -2
package/lib/dialects/dialect.js
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { MARIADB, MYSQL } = require('./constants');
|
|
4
|
+
|
|
5
|
+
const SQL_QUERIES = {
|
|
6
|
+
VERSION: `SELECT version() as version`,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
class MysqlDatabaseInspector {
|
|
10
|
+
constructor(db) {
|
|
11
|
+
this.db = db;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async getInformation() {
|
|
15
|
+
let database;
|
|
16
|
+
let versionNumber;
|
|
17
|
+
try {
|
|
18
|
+
const [results] = await this.db.connection.raw(SQL_QUERIES.VERSION);
|
|
19
|
+
const versionSplit = results[0].version.split('-');
|
|
20
|
+
const databaseName = versionSplit[1];
|
|
21
|
+
versionNumber = versionSplit[0];
|
|
22
|
+
database = databaseName && databaseName.toLowerCase() === 'mariadb' ? MARIADB : MYSQL;
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return {
|
|
25
|
+
database: null,
|
|
26
|
+
version: null,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
database,
|
|
32
|
+
version: versionNumber,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = MysqlDatabaseInspector;
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const semver = require('semver');
|
|
4
|
+
|
|
3
5
|
const { Dialect } = require('../dialect');
|
|
4
6
|
const MysqlSchemaInspector = require('./schema-inspector');
|
|
7
|
+
const MysqlDatabaseInspector = require('./database-inspector');
|
|
8
|
+
const { MYSQL } = require('./constants');
|
|
5
9
|
|
|
6
10
|
class MysqlDialect extends Dialect {
|
|
7
11
|
constructor(db) {
|
|
8
12
|
super(db);
|
|
9
13
|
|
|
10
14
|
this.schemaInspector = new MysqlSchemaInspector(db);
|
|
15
|
+
this.databaseInspector = new MysqlDatabaseInspector(db);
|
|
16
|
+
this.info = null;
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
configure() {
|
|
@@ -38,6 +44,8 @@ class MysqlDialect extends Dialect {
|
|
|
38
44
|
} catch (err) {
|
|
39
45
|
// Ignore error due to lack of session permissions
|
|
40
46
|
}
|
|
47
|
+
|
|
48
|
+
this.info = await this.databaseInspector.getInformation();
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
async startSchemaUpdate() {
|
|
@@ -57,6 +65,17 @@ class MysqlDialect extends Dialect {
|
|
|
57
65
|
return true;
|
|
58
66
|
}
|
|
59
67
|
|
|
68
|
+
supportsWindowFunctions() {
|
|
69
|
+
const isMysqlDB = !this.info.database || this.info.database === MYSQL;
|
|
70
|
+
const isBeforeV8 = !semver.valid(this.info.version) || semver.lt(this.info.version, '8.0.0');
|
|
71
|
+
|
|
72
|
+
if (isMysqlDB && isBeforeV8) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
60
79
|
usesForeignKeys() {
|
|
61
80
|
return true;
|
|
62
81
|
}
|
|
@@ -15,7 +15,7 @@ class PostgresDialect extends Dialect {
|
|
|
15
15
|
return true;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
initialize() {
|
|
18
|
+
async initialize() {
|
|
19
19
|
this.db.connection.client.driver.types.setTypeParser(1082, 'text', (v) => v); // Don't cast DATE string to Date()
|
|
20
20
|
this.db.connection.client.driver.types.setTypeParser(1700, 'text', parseFloat);
|
|
21
21
|
}
|
|
@@ -5,13 +5,13 @@ const fse = require('fs-extra');
|
|
|
5
5
|
|
|
6
6
|
const errors = require('../../errors');
|
|
7
7
|
const { Dialect } = require('../dialect');
|
|
8
|
-
const
|
|
8
|
+
const SqliteSchemaInspector = require('./schema-inspector');
|
|
9
9
|
|
|
10
10
|
class SqliteDialect extends Dialect {
|
|
11
11
|
constructor(db) {
|
|
12
12
|
super(db);
|
|
13
13
|
|
|
14
|
-
this.schemaInspector = new
|
|
14
|
+
this.schemaInspector = new SqliteSchemaInspector(db);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
configure() {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { map, isEmpty } = require('lodash/fp');
|
|
4
|
+
|
|
4
5
|
const {
|
|
5
6
|
isBidirectional,
|
|
6
7
|
isOneToAny,
|
|
@@ -10,6 +11,7 @@ const {
|
|
|
10
11
|
hasInverseOrderColumn,
|
|
11
12
|
} = require('../metadata/relations');
|
|
12
13
|
const { createQueryBuilder } = require('../query');
|
|
14
|
+
const { addSchema } = require('../utils/knex');
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* If some relations currently exist for this oneToX relation, on the one side, this function removes them and update the inverse order if needed.
|
|
@@ -195,6 +197,12 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
|
|
|
195
197
|
return;
|
|
196
198
|
}
|
|
197
199
|
|
|
200
|
+
// Handle databases that don't support window function ROW_NUMBER
|
|
201
|
+
if (!strapi.db.dialect.supportsWindowFunctions()) {
|
|
202
|
+
await cleanOrderColumnsForOldDatabases({ id, attribute, db, inverseRelIds, transaction: trx });
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
198
206
|
const { joinTable } = attribute;
|
|
199
207
|
const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
|
|
200
208
|
const update = [];
|
|
@@ -226,8 +234,7 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
|
|
|
226
234
|
// https://github.com/knex/knex/issues/2504
|
|
227
235
|
switch (strapi.db.dialect.client) {
|
|
228
236
|
case 'mysql':
|
|
229
|
-
await db
|
|
230
|
-
.getConnection()
|
|
237
|
+
await db.connection
|
|
231
238
|
.raw(
|
|
232
239
|
`UPDATE
|
|
233
240
|
?? as a,
|
|
@@ -242,21 +249,22 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
|
|
|
242
249
|
)
|
|
243
250
|
.transacting(trx);
|
|
244
251
|
break;
|
|
245
|
-
default:
|
|
246
|
-
|
|
247
|
-
|
|
252
|
+
default: {
|
|
253
|
+
const joinTableName = addSchema(joinTable.name);
|
|
254
|
+
await db.connection
|
|
248
255
|
.raw(
|
|
249
256
|
`UPDATE ?? as a
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
[
|
|
257
|
+
SET ${update.join(', ')}
|
|
258
|
+
FROM (
|
|
259
|
+
SELECT ${select.join(', ')}
|
|
260
|
+
FROM ??
|
|
261
|
+
WHERE ${where.join(' OR ')}
|
|
262
|
+
) AS b
|
|
263
|
+
WHERE b.id = a.id`,
|
|
264
|
+
[joinTableName, ...updateBinding, ...selectBinding, joinTableName, ...whereBinding]
|
|
258
265
|
)
|
|
259
266
|
.transacting(trx);
|
|
267
|
+
}
|
|
260
268
|
/*
|
|
261
269
|
`UPDATE :joinTable: as a
|
|
262
270
|
SET :orderColumn: = b.src_order, :inverseOrderColumn: = b.inv_order
|
|
@@ -273,6 +281,103 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
|
|
|
273
281
|
}
|
|
274
282
|
};
|
|
275
283
|
|
|
284
|
+
const cleanOrderColumnsForOldDatabases = async ({
|
|
285
|
+
id,
|
|
286
|
+
attribute,
|
|
287
|
+
db,
|
|
288
|
+
inverseRelIds,
|
|
289
|
+
transaction: trx,
|
|
290
|
+
}) => {
|
|
291
|
+
const { joinTable } = attribute;
|
|
292
|
+
const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
|
|
293
|
+
|
|
294
|
+
const now = new Date().valueOf();
|
|
295
|
+
|
|
296
|
+
if (hasOrderColumn(attribute) && id) {
|
|
297
|
+
const tempOrderTableName = `tempOrderTableName_${now}`;
|
|
298
|
+
try {
|
|
299
|
+
await db.connection
|
|
300
|
+
.raw(
|
|
301
|
+
`
|
|
302
|
+
CREATE TEMPORARY TABLE :tempOrderTableName:
|
|
303
|
+
SELECT
|
|
304
|
+
id,
|
|
305
|
+
(
|
|
306
|
+
SELECT count(*)
|
|
307
|
+
FROM :joinTableName: b
|
|
308
|
+
WHERE a.:orderColumnName: >= b.:orderColumnName: AND a.:joinColumnName: = b.:joinColumnName: AND a.:joinColumnName: = :id
|
|
309
|
+
) AS src_order
|
|
310
|
+
FROM :joinTableName: a`,
|
|
311
|
+
{
|
|
312
|
+
tempOrderTableName,
|
|
313
|
+
joinTableName: joinTable.name,
|
|
314
|
+
orderColumnName,
|
|
315
|
+
joinColumnName: joinColumn.name,
|
|
316
|
+
id,
|
|
317
|
+
}
|
|
318
|
+
)
|
|
319
|
+
.transacting(trx);
|
|
320
|
+
await db.connection
|
|
321
|
+
.raw(
|
|
322
|
+
`UPDATE ?? as a, (SELECT * FROM ??) AS b
|
|
323
|
+
SET ?? = b.src_order
|
|
324
|
+
WHERE a.id = b.id`,
|
|
325
|
+
[joinTable.name, tempOrderTableName, orderColumnName]
|
|
326
|
+
)
|
|
327
|
+
.transacting(trx);
|
|
328
|
+
} finally {
|
|
329
|
+
await db.connection
|
|
330
|
+
.raw(`DROP TEMPORARY TABLE IF EXISTS ??`, [tempOrderTableName])
|
|
331
|
+
.transacting(trx);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (hasInverseOrderColumn(attribute) && !isEmpty(inverseRelIds)) {
|
|
336
|
+
const tempInvOrderTableName = `tempInvOrderTableName_${now}`;
|
|
337
|
+
try {
|
|
338
|
+
await db.connection
|
|
339
|
+
.raw(
|
|
340
|
+
`
|
|
341
|
+
CREATE TEMPORARY TABLE ??
|
|
342
|
+
SELECT
|
|
343
|
+
id,
|
|
344
|
+
(
|
|
345
|
+
SELECT count(*)
|
|
346
|
+
FROM ?? b
|
|
347
|
+
WHERE a.?? >= b.?? AND a.?? = b.?? AND a.?? IN (${inverseRelIds
|
|
348
|
+
.map(() => '?')
|
|
349
|
+
.join(', ')})
|
|
350
|
+
) AS inv_order
|
|
351
|
+
FROM ?? a`,
|
|
352
|
+
[
|
|
353
|
+
tempInvOrderTableName,
|
|
354
|
+
joinTable.name,
|
|
355
|
+
inverseOrderColumnName,
|
|
356
|
+
inverseOrderColumnName,
|
|
357
|
+
inverseJoinColumn.name,
|
|
358
|
+
inverseJoinColumn.name,
|
|
359
|
+
inverseJoinColumn.name,
|
|
360
|
+
...inverseRelIds,
|
|
361
|
+
joinTable.name,
|
|
362
|
+
]
|
|
363
|
+
)
|
|
364
|
+
.transacting(trx);
|
|
365
|
+
await db.connection
|
|
366
|
+
.raw(
|
|
367
|
+
`UPDATE ?? as a, (SELECT * FROM ??) AS b
|
|
368
|
+
SET ?? = b.inv_order
|
|
369
|
+
WHERE a.id = b.id`,
|
|
370
|
+
[joinTable.name, tempInvOrderTableName, inverseOrderColumnName]
|
|
371
|
+
)
|
|
372
|
+
.transacting(trx);
|
|
373
|
+
} finally {
|
|
374
|
+
await db.connection
|
|
375
|
+
.raw(`DROP TEMPORARY TABLE IF EXISTS ??`, [tempInvOrderTableName])
|
|
376
|
+
.transacting(trx);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
276
381
|
module.exports = {
|
|
277
382
|
deletePreviousOneToAnyRelations,
|
|
278
383
|
deletePreviousAnyToOneRelations,
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LifecycleProvider } from './lifecycles';
|
|
2
2
|
import { MigrationProvider } from './migrations';
|
|
3
|
-
import {
|
|
3
|
+
import { SchemaProvider } from './schema';
|
|
4
4
|
|
|
5
5
|
type LogicalOperators<T> = {
|
|
6
6
|
$and?: WhereParams<T>[];
|
|
@@ -154,7 +154,7 @@ interface DatabaseConfig {
|
|
|
154
154
|
models: ModelConfig[];
|
|
155
155
|
}
|
|
156
156
|
export interface Database {
|
|
157
|
-
schema:
|
|
157
|
+
schema: SchemaProvider;
|
|
158
158
|
lifecycles: LifecycleProvider;
|
|
159
159
|
migrations: MigrationProvider;
|
|
160
160
|
entityManager: EntityManager;
|
package/lib/schema/index.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface Model {
|
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
export interface
|
|
41
|
+
export interface SchemaProvider {
|
|
42
42
|
sync(): Promise<void>;
|
|
43
43
|
syncSchema(): Promise<void>;
|
|
44
44
|
reset(): Promise<void>;
|
|
@@ -46,4 +46,4 @@ export interface SchemaProvideer {
|
|
|
46
46
|
drop(): Promise<void>;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export default function(db: Database):
|
|
49
|
+
export default function(db: Database): SchemaProvider;
|
package/lib/utils/knex.js
CHANGED
|
@@ -7,6 +7,16 @@ const isKnexQuery = (value) => {
|
|
|
7
7
|
return value instanceof KnexBuilder || value instanceof KnexRaw;
|
|
8
8
|
};
|
|
9
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
|
+
|
|
10
19
|
module.exports = {
|
|
11
20
|
isKnexQuery,
|
|
21
|
+
addSchema,
|
|
12
22
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/database",
|
|
3
|
-
"version": "4.5.
|
|
3
|
+
"version": "4.5.2",
|
|
4
4
|
"description": "Strapi's database layer",
|
|
5
5
|
"homepage": "https://strapi.io",
|
|
6
6
|
"bugs": {
|
|
@@ -36,11 +36,12 @@
|
|
|
36
36
|
"fs-extra": "10.0.0",
|
|
37
37
|
"knex": "1.0.7",
|
|
38
38
|
"lodash": "4.17.21",
|
|
39
|
+
"semver": "7.3.8",
|
|
39
40
|
"umzug": "3.1.1"
|
|
40
41
|
},
|
|
41
42
|
"engines": {
|
|
42
43
|
"node": ">=14.19.1 <=18.x.x",
|
|
43
44
|
"npm": ">=6.0.0"
|
|
44
45
|
},
|
|
45
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "bcb1b7f472aae2556a9b59d59ee66d241c497a3e"
|
|
46
47
|
}
|