sequelize-typescript-generator 11.1.0 → 12.0.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/build/bin/utils.js +1 -1
- package/build/builders/Builder.js +4 -0
- package/build/builders/ModelBuilder.js +0 -4
- package/build/config/IConfig.d.ts +0 -1
- package/build/dialects/Dialect.js +7 -14
- package/build/dialects/DialectPostgres.d.ts +1 -1
- package/build/dialects/DialectPostgres.js +7 -7
- package/package.json +1 -1
package/build/bin/utils.js
CHANGED
|
@@ -116,6 +116,7 @@ const buildConfig = (argv) => {
|
|
|
116
116
|
...argv[exports.aliasesMap.HOST] && { host: argv[exports.aliasesMap.HOST] },
|
|
117
117
|
...argv[exports.aliasesMap.PORT] && { port: argv[exports.aliasesMap.PORT] },
|
|
118
118
|
...argv[exports.aliasesMap.DATABASE] && { database: argv[exports.aliasesMap.DATABASE] },
|
|
119
|
+
...argv[exports.aliasesMap.SCHEMA] && { schema: argv[exports.aliasesMap.SCHEMA] },
|
|
119
120
|
...argv[exports.aliasesMap.USERNAME] && { username: argv[exports.aliasesMap.USERNAME] },
|
|
120
121
|
...argv[exports.aliasesMap.PASSWORD] && { password: argv[exports.aliasesMap.PASSWORD] },
|
|
121
122
|
...argv[exports.aliasesMap.SSL] && { ssl: true },
|
|
@@ -137,7 +138,6 @@ const buildConfig = (argv) => {
|
|
|
137
138
|
logging: argv[exports.aliasesMap.ENABLE_SEQUELIZE_LOGS],
|
|
138
139
|
},
|
|
139
140
|
metadata: {
|
|
140
|
-
...argv[exports.aliasesMap.SCHEMA] && { schema: argv[exports.aliasesMap.SCHEMA] },
|
|
141
141
|
...argv[exports.aliasesMap.TABLES] && {
|
|
142
142
|
tables: argv[exports.aliasesMap.TABLES]
|
|
143
143
|
.split(',')
|
|
@@ -11,6 +11,10 @@ class Builder {
|
|
|
11
11
|
constructor(config, dialect) {
|
|
12
12
|
this._config = config;
|
|
13
13
|
this._dialect = dialect;
|
|
14
|
+
// Default Postgres schema if not provided
|
|
15
|
+
if (dialect.name === 'postgres' && !config.connection.schema) {
|
|
16
|
+
config.connection.schema = 'public';
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
get config() {
|
|
16
20
|
return this._config;
|
|
@@ -52,10 +52,6 @@ const foreignKeyDecorator = 'ForeignKey';
|
|
|
52
52
|
*/
|
|
53
53
|
class ModelBuilder extends Builder_1.Builder {
|
|
54
54
|
constructor(config, dialect) {
|
|
55
|
-
// Force 'public' schema on Postgres if not specified
|
|
56
|
-
if (dialect.name === 'postgres' && config.metadata && !config.metadata.schema) {
|
|
57
|
-
config.metadata.schema = 'public';
|
|
58
|
-
}
|
|
59
55
|
super(config, dialect);
|
|
60
56
|
}
|
|
61
57
|
/**
|
|
@@ -11,7 +11,6 @@ export type TransformMap = {
|
|
|
11
11
|
export type TransformFn = (value: string, target: TransformTarget) => string;
|
|
12
12
|
export declare const TransformCases: Set<TransformCase>;
|
|
13
13
|
export interface IConfigMetadata {
|
|
14
|
-
schema?: 'public' | string;
|
|
15
14
|
tables?: string[];
|
|
16
15
|
skipTables?: string[];
|
|
17
16
|
indices?: boolean;
|
|
@@ -19,17 +19,10 @@ class Dialect {
|
|
|
19
19
|
* @returns {Promise<ITableMetadata[]>}
|
|
20
20
|
*/
|
|
21
21
|
async buildTablesMetadata(config) {
|
|
22
|
-
var _a, _b, _c, _d, _e, _f
|
|
22
|
+
var _a, _b, _c, _d, _e, _f;
|
|
23
23
|
let connection;
|
|
24
24
|
const tablesMetadata = {};
|
|
25
25
|
try {
|
|
26
|
-
// Set schema for Postgres to 'public' if not provided
|
|
27
|
-
if (config.connection.dialect === 'postgres' && !((_a = config.metadata) === null || _a === void 0 ? void 0 : _a.schema)) {
|
|
28
|
-
config.metadata = {
|
|
29
|
-
...config.metadata,
|
|
30
|
-
...{ schema: 'public' },
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
26
|
connection = (0, connection_1.createConnection)(config.connection);
|
|
34
27
|
await connection.authenticate();
|
|
35
28
|
let tables = await this.fetchTables(connection, config);
|
|
@@ -55,7 +48,7 @@ class Dialect {
|
|
|
55
48
|
for (const { name: tableName, comment: tableComment } of tables) {
|
|
56
49
|
const columnsMetadata = await this.fetchColumnsMetadata(connection, config, tableName);
|
|
57
50
|
// Fetch indices metadata if required
|
|
58
|
-
if ((
|
|
51
|
+
if ((_a = config.metadata) === null || _a === void 0 ? void 0 : _a.indices) {
|
|
59
52
|
for (const column of columnsMetadata) {
|
|
60
53
|
column.indices = await this.fetchColumnIndexMetadata(connection, config, tableName, column.name);
|
|
61
54
|
}
|
|
@@ -63,8 +56,8 @@ class Dialect {
|
|
|
63
56
|
const tableMetadata = {
|
|
64
57
|
originName: tableName,
|
|
65
58
|
name: tableName,
|
|
66
|
-
|
|
67
|
-
timestamps: (
|
|
59
|
+
schema: config.connection.schema,
|
|
60
|
+
timestamps: (_c = (_b = config.metadata) === null || _b === void 0 ? void 0 : _b.timestamps) !== null && _c !== void 0 ? _c : false,
|
|
68
61
|
columns: {},
|
|
69
62
|
comment: tableComment !== null && tableComment !== void 0 ? tableComment : undefined,
|
|
70
63
|
};
|
|
@@ -82,8 +75,8 @@ class Dialect {
|
|
|
82
75
|
connection && await connection.close();
|
|
83
76
|
}
|
|
84
77
|
// Apply associations if required
|
|
85
|
-
if ((
|
|
86
|
-
const parsedAssociations = AssociationsParser_1.AssociationsParser.parse((
|
|
78
|
+
if ((_d = config.metadata) === null || _d === void 0 ? void 0 : _d.associationsFile) {
|
|
79
|
+
const parsedAssociations = AssociationsParser_1.AssociationsParser.parse((_e = config.metadata) === null || _e === void 0 ? void 0 : _e.associationsFile);
|
|
87
80
|
for (const [tableName, association] of Object.entries(parsedAssociations)) {
|
|
88
81
|
if (!tablesMetadata[tableName]) {
|
|
89
82
|
console.warn('[WARNING]', `Associated table ${tableName} not found among (${Object.keys(tablesMetadata).join(', ')})`);
|
|
@@ -106,7 +99,7 @@ class Dialect {
|
|
|
106
99
|
}
|
|
107
100
|
}
|
|
108
101
|
// Apply transformations if required
|
|
109
|
-
if ((
|
|
102
|
+
if ((_f = config.metadata) === null || _f === void 0 ? void 0 : _f.case) {
|
|
110
103
|
for (const [tableName, tableMetadata] of Object.entries(tablesMetadata)) {
|
|
111
104
|
tablesMetadata[tableName] = (0, utils_1.caseTransformer)(tableMetadata, config.metadata.case);
|
|
112
105
|
}
|
|
@@ -102,7 +102,7 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
102
102
|
/**
|
|
103
103
|
* Map database data type to javascript data type
|
|
104
104
|
* @param {string} dbType
|
|
105
|
-
* @returns {string
|
|
105
|
+
* @returns {string}
|
|
106
106
|
*/
|
|
107
107
|
mapDbTypeToJs(dbType) {
|
|
108
108
|
return jsDataTypesMap[dbType];
|
|
@@ -129,7 +129,7 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
129
129
|
FROM information_schema.tables t
|
|
130
130
|
JOIN pg_class pc
|
|
131
131
|
ON t.table_name = pc.relname
|
|
132
|
-
WHERE t.table_schema='${config.
|
|
132
|
+
WHERE t.table_schema='${config.connection.schema}' AND pc.relkind = 'r';
|
|
133
133
|
`;
|
|
134
134
|
const tables = (await connection.query(query, {
|
|
135
135
|
type: sequelize_1.QueryTypes.SELECT,
|
|
@@ -162,7 +162,7 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
162
162
|
FROM pg_attribute a
|
|
163
163
|
LEFT OUTER JOIN pg_index x
|
|
164
164
|
ON a.attnum = ANY (x.indkey) AND a.attrelid = x.indrelid
|
|
165
|
-
WHERE a.attrelid = '${config.
|
|
165
|
+
WHERE a.attrelid = '${config.connection.schema}.\"${table}\"'::regclass AND a.attnum > 0
|
|
166
166
|
AND c.ordinal_position = a.attnum AND x.indisprimary IS TRUE
|
|
167
167
|
) AS is_primary,
|
|
168
168
|
c.*,
|
|
@@ -188,11 +188,11 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
188
188
|
ON (attrib.attnum = dep.refobjsubid AND attrib.attrelid = dep.refobjid)
|
|
189
189
|
JOIN pg_namespace pn
|
|
190
190
|
ON seqclass.relnamespace = pn.oid
|
|
191
|
-
WHERE pn.nspname = '${config.
|
|
191
|
+
WHERE pn.nspname = '${config.connection.schema}' AND depclass.relname = '${table}'
|
|
192
192
|
) seq
|
|
193
193
|
ON c.table_schema = seq.schema_name AND c.table_name = seq.table_name AND
|
|
194
194
|
c.column_name = seq.column_name
|
|
195
|
-
WHERE c.table_schema = '${config.
|
|
195
|
+
WHERE c.table_schema = '${config.connection.schema}' AND c.table_name = '${table}'
|
|
196
196
|
ORDER BY c.ordinal_position;
|
|
197
197
|
`;
|
|
198
198
|
const columns = await connection.query(query, {
|
|
@@ -214,7 +214,7 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
214
214
|
this.mapDbTypeToSequelize(column.udt_name).key
|
|
215
215
|
.split(' ')[0], // avoids 'DOUBLE PRECISION' key to include PRECISION in the mapping
|
|
216
216
|
},
|
|
217
|
-
allowNull:
|
|
217
|
+
allowNull: column.is_nullable === 'YES' && !column.is_primary,
|
|
218
218
|
primaryKey: column.is_primary,
|
|
219
219
|
autoIncrement: column.is_sequence,
|
|
220
220
|
indices: [],
|
|
@@ -270,7 +270,7 @@ class DialectPostgres extends Dialect_1.Dialect {
|
|
|
270
270
|
ON x.indexrelid = pc.oid
|
|
271
271
|
INNER JOIN pg_am am
|
|
272
272
|
ON pc.relam = am.oid
|
|
273
|
-
WHERE a.attrelid = '${config.
|
|
273
|
+
WHERE a.attrelid = '${config.connection.schema}.\"${table}\"'::regclass AND a.attnum > 0
|
|
274
274
|
AND a.attname = '${column}';
|
|
275
275
|
`;
|
|
276
276
|
const indices = await connection.query(query, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sequelize-typescript-generator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.1",
|
|
4
4
|
"description": "Automatically generates typescript models compatible with sequelize-typescript library (https://www.npmjs.com/package/sequelize-typescript) directly from your source database.",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|