@strapi/database 4.14.3 → 4.14.5
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
package/lib/connection.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import/no-extraneous-dependencies */
|
|
2
|
-
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
const knex = require('knex');
|
|
6
|
-
|
|
7
|
-
const SqliteClient = require('knex/lib/dialects/sqlite3/index');
|
|
8
|
-
|
|
9
|
-
const trySqlitePackage = (packageName) => {
|
|
10
|
-
try {
|
|
11
|
-
require.resolve(packageName);
|
|
12
|
-
return packageName;
|
|
13
|
-
} catch (error) {
|
|
14
|
-
if (error.code === 'MODULE_NOT_FOUND') {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
throw error;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
class LegacySqliteClient extends SqliteClient {
|
|
21
|
-
_driver() {
|
|
22
|
-
return require('sqlite3');
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const clientMap = {
|
|
27
|
-
'better-sqlite3': 'better-sqlite3',
|
|
28
|
-
'@vscode/sqlite3': 'sqlite',
|
|
29
|
-
sqlite3: LegacySqliteClient,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const getSqlitePackageName = () => {
|
|
33
|
-
// NOTE: allow forcing the package to use (mostly used for testing purposes)
|
|
34
|
-
if (typeof process.env.SQLITE_PKG !== 'undefined') {
|
|
35
|
-
return process.env.SQLITE_PKG;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// NOTE: this tries to find the best sqlite module possible to use
|
|
39
|
-
// while keeping retro compatibility
|
|
40
|
-
return (
|
|
41
|
-
trySqlitePackage('better-sqlite3') ||
|
|
42
|
-
trySqlitePackage('@vscode/sqlite3') ||
|
|
43
|
-
trySqlitePackage('sqlite3')
|
|
44
|
-
);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const createConnection = (config) => {
|
|
48
|
-
const knexConfig = { ...config };
|
|
49
|
-
if (knexConfig.client === 'sqlite') {
|
|
50
|
-
const sqlitePackageName = getSqlitePackageName();
|
|
51
|
-
|
|
52
|
-
knexConfig.client = clientMap[sqlitePackageName];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const knexInstance = knex(knexConfig);
|
|
56
|
-
|
|
57
|
-
return Object.assign(knexInstance, {
|
|
58
|
-
getSchemaName() {
|
|
59
|
-
return this.client.connectionSettings.schema;
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
module.exports = createConnection;
|
package/lib/dialects/dialect.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
class Dialect {
|
|
4
|
-
constructor(db) {
|
|
5
|
-
this.db = db;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
configure() {}
|
|
9
|
-
|
|
10
|
-
initialize() {}
|
|
11
|
-
|
|
12
|
-
getSqlType(type) {
|
|
13
|
-
return type;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
canAlterConstraints() {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
usesForeignKeys() {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
useReturning() {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
supportsUnsigned() {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
supportsWindowFunctions() {
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
supportsOperator() {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async startSchemaUpdate() {
|
|
41
|
-
// noop
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async endSchemaUpdate() {
|
|
45
|
-
// noop
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
transformErrors(error) {
|
|
49
|
-
if (error instanceof Error) {
|
|
50
|
-
throw error;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
throw new Error(error.message);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
canAddIncrements() {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
module.exports = {
|
|
62
|
-
Dialect,
|
|
63
|
-
};
|
package/lib/dialects/index.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Require our dialect-specific code
|
|
5
|
-
*/
|
|
6
|
-
const getDialectClass = (client) => {
|
|
7
|
-
switch (client) {
|
|
8
|
-
case 'postgres':
|
|
9
|
-
return require('./postgresql');
|
|
10
|
-
case 'mysql':
|
|
11
|
-
return require('./mysql');
|
|
12
|
-
case 'sqlite':
|
|
13
|
-
return require('./sqlite');
|
|
14
|
-
default:
|
|
15
|
-
throw new Error(`Unknown dialect ${client}`);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
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
|
-
|
|
40
|
-
const getDialect = (db) => {
|
|
41
|
-
const { client } = db.config.connection;
|
|
42
|
-
const dialectName = getDialectName(client);
|
|
43
|
-
|
|
44
|
-
const constructor = getDialectClass(dialectName);
|
|
45
|
-
const dialect = new constructor(db);
|
|
46
|
-
dialect.client = dialectName;
|
|
47
|
-
|
|
48
|
-
return dialect;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
module.exports = {
|
|
52
|
-
getDialect,
|
|
53
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
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,92 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const semver = require('semver');
|
|
4
|
-
|
|
5
|
-
const { Dialect } = require('../dialect');
|
|
6
|
-
const MysqlSchemaInspector = require('./schema-inspector');
|
|
7
|
-
const MysqlDatabaseInspector = require('./database-inspector');
|
|
8
|
-
const { MYSQL } = require('./constants');
|
|
9
|
-
|
|
10
|
-
class MysqlDialect extends Dialect {
|
|
11
|
-
constructor(db) {
|
|
12
|
-
super(db);
|
|
13
|
-
|
|
14
|
-
this.schemaInspector = new MysqlSchemaInspector(db);
|
|
15
|
-
this.databaseInspector = new MysqlDatabaseInspector(db);
|
|
16
|
-
this.info = null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
configure() {
|
|
20
|
-
this.db.config.connection.connection.supportBigNumbers = true;
|
|
21
|
-
// Only allow bigNumberStrings option set to be true if no connection option passed
|
|
22
|
-
// Otherwise bigNumberStrings option should be allowed to used from DB config
|
|
23
|
-
if (this.db.config.connection.connection.bigNumberStrings === undefined) {
|
|
24
|
-
this.db.config.connection.connection.bigNumberStrings = true;
|
|
25
|
-
}
|
|
26
|
-
this.db.config.connection.connection.typeCast = (field, next) => {
|
|
27
|
-
if (field.type === 'DECIMAL' || field.type === 'NEWDECIMAL') {
|
|
28
|
-
const value = field.string();
|
|
29
|
-
return value === null ? null : Number(value);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (field.type === 'TINY' && field.length === 1) {
|
|
33
|
-
const value = field.string();
|
|
34
|
-
return value ? value === '1' : null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (field.type === 'DATE') {
|
|
38
|
-
return field.string();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return next();
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async initialize() {
|
|
46
|
-
try {
|
|
47
|
-
await this.db.connection.raw(`set session sql_require_primary_key = 0;`);
|
|
48
|
-
} catch (err) {
|
|
49
|
-
// Ignore error due to lack of session permissions
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.info = await this.databaseInspector.getInformation();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async startSchemaUpdate() {
|
|
56
|
-
try {
|
|
57
|
-
await this.db.connection.raw(`set foreign_key_checks = 0;`);
|
|
58
|
-
await this.db.connection.raw(`set session sql_require_primary_key = 0;`);
|
|
59
|
-
} catch (err) {
|
|
60
|
-
// Ignore error due to lack of session permissions
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async endSchemaUpdate() {
|
|
65
|
-
await this.db.connection.raw(`set foreign_key_checks = 1;`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
supportsUnsigned() {
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
supportsWindowFunctions() {
|
|
73
|
-
const isMysqlDB = !this.info.database || this.info.database === MYSQL;
|
|
74
|
-
const isBeforeV8 = !semver.valid(this.info.version) || semver.lt(this.info.version, '8.0.0');
|
|
75
|
-
|
|
76
|
-
if (isMysqlDB && isBeforeV8) {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
usesForeignKeys() {
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
transformErrors(error) {
|
|
88
|
-
super.transformErrors(error);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = MysqlDialect;
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const SQL_QUERIES = {
|
|
4
|
-
TABLE_LIST: /* sql */ `
|
|
5
|
-
SELECT
|
|
6
|
-
t.table_name as table_name
|
|
7
|
-
FROM information_schema.tables t
|
|
8
|
-
WHERE table_type = 'BASE TABLE'
|
|
9
|
-
AND table_schema = schema();
|
|
10
|
-
`,
|
|
11
|
-
LIST_COLUMNS: /* sql */ `
|
|
12
|
-
SELECT
|
|
13
|
-
c.data_type as data_type,
|
|
14
|
-
c.column_name as column_name,
|
|
15
|
-
c.character_maximum_length as character_maximum_length,
|
|
16
|
-
c.column_default as column_default,
|
|
17
|
-
c.is_nullable as is_nullable,
|
|
18
|
-
c.column_type as column_type,
|
|
19
|
-
c.column_key as column_key
|
|
20
|
-
FROM information_schema.columns c
|
|
21
|
-
WHERE table_schema = database()
|
|
22
|
-
AND table_name = ?;
|
|
23
|
-
`,
|
|
24
|
-
INDEX_LIST: /* sql */ `
|
|
25
|
-
show index from ??;
|
|
26
|
-
`,
|
|
27
|
-
FOREIGN_KEY_LIST: /* sql */ `
|
|
28
|
-
SELECT
|
|
29
|
-
tc.constraint_name as constraint_name
|
|
30
|
-
FROM information_schema.table_constraints tc
|
|
31
|
-
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
32
|
-
AND tc.table_schema = database()
|
|
33
|
-
AND tc.table_name = ?;
|
|
34
|
-
`,
|
|
35
|
-
FOREIGN_KEY_REFERENCES: /* sql */ `
|
|
36
|
-
SELECT
|
|
37
|
-
kcu.constraint_name as constraint_name,
|
|
38
|
-
kcu.column_name as column_name,
|
|
39
|
-
kcu.referenced_table_name as referenced_table_name,
|
|
40
|
-
kcu.referenced_column_name as referenced_column_name
|
|
41
|
-
FROM information_schema.key_column_usage kcu
|
|
42
|
-
WHERE kcu.constraint_name in (?)
|
|
43
|
-
AND kcu.table_schema = database()
|
|
44
|
-
AND kcu.table_name = ?;
|
|
45
|
-
`,
|
|
46
|
-
FOREIGN_KEY_REFERENTIALS_CONSTRAINTS: /* sql */ `
|
|
47
|
-
SELECT
|
|
48
|
-
rc.constraint_name as constraint_name,
|
|
49
|
-
rc.update_rule as on_update,
|
|
50
|
-
rc.delete_rule as on_delete
|
|
51
|
-
FROM information_schema.referential_constraints AS rc
|
|
52
|
-
WHERE rc.constraint_name in (?)
|
|
53
|
-
AND rc.constraint_schema = database()
|
|
54
|
-
AND rc.table_name = ?;
|
|
55
|
-
`,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const toStrapiType = (column) => {
|
|
59
|
-
const rootType = column.data_type.toLowerCase().match(/[^(), ]+/)[0];
|
|
60
|
-
|
|
61
|
-
switch (rootType) {
|
|
62
|
-
case 'int': {
|
|
63
|
-
if (column.column_key === 'PRI') {
|
|
64
|
-
return { type: 'increments', args: [{ primary: true, primaryKey: true }], unsigned: false };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return { type: 'integer' };
|
|
68
|
-
}
|
|
69
|
-
case 'decimal': {
|
|
70
|
-
return { type: 'decimal', args: [10, 2] };
|
|
71
|
-
}
|
|
72
|
-
case 'double': {
|
|
73
|
-
return { type: 'double' };
|
|
74
|
-
}
|
|
75
|
-
case 'bigint': {
|
|
76
|
-
return { type: 'bigInteger' };
|
|
77
|
-
}
|
|
78
|
-
case 'enum': {
|
|
79
|
-
return { type: 'string' };
|
|
80
|
-
}
|
|
81
|
-
case 'tinyint': {
|
|
82
|
-
return { type: 'boolean' };
|
|
83
|
-
}
|
|
84
|
-
case 'longtext': {
|
|
85
|
-
return { type: 'text', args: ['longtext'] };
|
|
86
|
-
}
|
|
87
|
-
case 'varchar': {
|
|
88
|
-
return { type: 'string', args: [column.character_maximum_length] };
|
|
89
|
-
}
|
|
90
|
-
case 'datetime': {
|
|
91
|
-
return { type: 'datetime', args: [{ useTz: false, precision: 6 }] };
|
|
92
|
-
}
|
|
93
|
-
case 'date': {
|
|
94
|
-
return { type: 'date' };
|
|
95
|
-
}
|
|
96
|
-
case 'time': {
|
|
97
|
-
return { type: 'time', args: [{ precision: 3 }] };
|
|
98
|
-
}
|
|
99
|
-
case 'timestamp': {
|
|
100
|
-
return { type: 'timestamp', args: [{ useTz: false, precision: 6 }] };
|
|
101
|
-
}
|
|
102
|
-
case 'json': {
|
|
103
|
-
return { type: 'jsonb' };
|
|
104
|
-
}
|
|
105
|
-
default: {
|
|
106
|
-
return { type: 'specificType', args: [column.data_type] };
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
class MysqlSchemaInspector {
|
|
112
|
-
constructor(db) {
|
|
113
|
-
this.db = db;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async getSchema() {
|
|
117
|
-
const schema = { tables: [] };
|
|
118
|
-
|
|
119
|
-
const tables = await this.getTables();
|
|
120
|
-
|
|
121
|
-
schema.tables = await Promise.all(
|
|
122
|
-
tables.map(async (tableName) => {
|
|
123
|
-
const columns = await this.getColumns(tableName);
|
|
124
|
-
const indexes = await this.getIndexes(tableName);
|
|
125
|
-
const foreignKeys = await this.getForeignKeys(tableName);
|
|
126
|
-
|
|
127
|
-
return {
|
|
128
|
-
name: tableName,
|
|
129
|
-
columns,
|
|
130
|
-
indexes,
|
|
131
|
-
foreignKeys,
|
|
132
|
-
};
|
|
133
|
-
})
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
return schema;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async getTables() {
|
|
140
|
-
const [rows] = await this.db.connection.raw(SQL_QUERIES.TABLE_LIST);
|
|
141
|
-
|
|
142
|
-
return rows.map((row) => row.table_name);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async getColumns(tableName) {
|
|
146
|
-
const [rows] = await this.db.connection.raw(SQL_QUERIES.LIST_COLUMNS, [tableName]);
|
|
147
|
-
|
|
148
|
-
return rows.map((row) => {
|
|
149
|
-
const { type, args = [], ...rest } = toStrapiType(row);
|
|
150
|
-
|
|
151
|
-
return {
|
|
152
|
-
type,
|
|
153
|
-
args,
|
|
154
|
-
defaultTo: row.column_default,
|
|
155
|
-
name: row.column_name,
|
|
156
|
-
notNullable: row.is_nullable === 'NO',
|
|
157
|
-
unsigned: row.column_type.endsWith(' unsigned'),
|
|
158
|
-
...rest,
|
|
159
|
-
};
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async getIndexes(tableName) {
|
|
164
|
-
const [rows] = await this.db.connection.raw(SQL_QUERIES.INDEX_LIST, [tableName]);
|
|
165
|
-
|
|
166
|
-
const ret = {};
|
|
167
|
-
|
|
168
|
-
for (const index of rows) {
|
|
169
|
-
if (index.Column_name === 'id') {
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (!ret[index.Key_name]) {
|
|
174
|
-
ret[index.Key_name] = {
|
|
175
|
-
columns: [index.Column_name],
|
|
176
|
-
name: index.Key_name,
|
|
177
|
-
type: !index.Non_unique ? 'unique' : null,
|
|
178
|
-
};
|
|
179
|
-
} else {
|
|
180
|
-
ret[index.Key_name].columns.push(index.Column_name);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return Object.values(ret);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async getForeignKeys(tableName) {
|
|
188
|
-
const [rows] = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_LIST, [tableName]);
|
|
189
|
-
|
|
190
|
-
const ret = {};
|
|
191
|
-
|
|
192
|
-
for (const fk of rows) {
|
|
193
|
-
ret[fk.constraint_name] = {
|
|
194
|
-
name: fk.constraint_name,
|
|
195
|
-
columns: [],
|
|
196
|
-
referencedColumns: [],
|
|
197
|
-
referencedTable: null,
|
|
198
|
-
onUpdate: null,
|
|
199
|
-
onDelete: null,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const contraintNames = Object.keys(ret);
|
|
204
|
-
|
|
205
|
-
if (contraintNames.length > 0) {
|
|
206
|
-
const [fkReferences] = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_REFERENCES, [
|
|
207
|
-
contraintNames,
|
|
208
|
-
tableName,
|
|
209
|
-
]);
|
|
210
|
-
|
|
211
|
-
for (const fkReference of fkReferences) {
|
|
212
|
-
ret[fkReference.constraint_name].referencedTable = fkReference.referenced_table_name;
|
|
213
|
-
ret[fkReference.constraint_name].columns.push(fkReference.column_name);
|
|
214
|
-
ret[fkReference.constraint_name].referencedColumns.push(fkReference.referenced_column_name);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const [fkReferentialConstraints] = await this.db.connection.raw(
|
|
218
|
-
SQL_QUERIES.FOREIGN_KEY_REFERENTIALS_CONSTRAINTS,
|
|
219
|
-
[contraintNames, tableName]
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
for (const fkReferentialConstraint of fkReferentialConstraints) {
|
|
223
|
-
ret[fkReferentialConstraint.constraint_name].onUpdate =
|
|
224
|
-
fkReferentialConstraint.on_update.toUpperCase();
|
|
225
|
-
ret[fkReferentialConstraint.constraint_name].onDelete =
|
|
226
|
-
fkReferentialConstraint.on_delete.toUpperCase();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return Object.values(ret);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
module.exports = MysqlSchemaInspector;
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const errors = require('../../errors');
|
|
4
|
-
const { Dialect } = require('../dialect');
|
|
5
|
-
const PostgresqlSchemaInspector = require('./schema-inspector');
|
|
6
|
-
|
|
7
|
-
class PostgresDialect extends Dialect {
|
|
8
|
-
constructor(db) {
|
|
9
|
-
super(db);
|
|
10
|
-
|
|
11
|
-
this.schemaInspector = new PostgresqlSchemaInspector(db);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
useReturning() {
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async initialize() {
|
|
19
|
-
// Don't cast DATE string to Date()
|
|
20
|
-
this.db.connection.client.driver.types.setTypeParser(
|
|
21
|
-
this.db.connection.client.driver.types.builtins.DATE,
|
|
22
|
-
'text',
|
|
23
|
-
(v) => v
|
|
24
|
-
);
|
|
25
|
-
// Don't parse JSONB automatically
|
|
26
|
-
this.db.connection.client.driver.types.setTypeParser(
|
|
27
|
-
this.db.connection.client.driver.types.builtins.JSONB,
|
|
28
|
-
'text',
|
|
29
|
-
(v) => v
|
|
30
|
-
);
|
|
31
|
-
this.db.connection.client.driver.types.setTypeParser(
|
|
32
|
-
this.db.connection.client.driver.types.builtins.NUMERIC,
|
|
33
|
-
'text',
|
|
34
|
-
parseFloat
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
usesForeignKeys() {
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
getSqlType(type) {
|
|
43
|
-
switch (type) {
|
|
44
|
-
case 'timestamp': {
|
|
45
|
-
return 'datetime';
|
|
46
|
-
}
|
|
47
|
-
default: {
|
|
48
|
-
return type;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
transformErrors(error) {
|
|
54
|
-
switch (error.code) {
|
|
55
|
-
case '23502': {
|
|
56
|
-
throw new errors.NotNullError({ column: error.column });
|
|
57
|
-
}
|
|
58
|
-
default: {
|
|
59
|
-
super.transformErrors(error);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
module.exports = PostgresDialect;
|