-
-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathauto.ts
More file actions
99 lines (85 loc) · 2.88 KB
/
auto.ts
File metadata and controls
99 lines (85 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import _ from "lodash";
import { Dialect, Sequelize } from "sequelize";
import { AutoBuilder } from "./auto-builder";
import { AutoGenerator } from "./auto-generator";
import { AutoRelater } from "./auto-relater";
import { AutoWriter } from "./auto-writer";
import { getDialect } from "./dialects/dialects";
import { AutoOptions, TableData } from "./types";
export class SequelizeAuto {
sequelize: Sequelize;
options: AutoOptions;
constructor(database: string | Sequelize, username: string, password: string, options: AutoOptions) {
if (options && options.dialect === 'sqlite' && !options.storage && database) {
options.storage = database as string;
}
if (options && options.dialect === 'mssql') {
// set defaults for tedious, to silence the warnings
options.dialectOptions = options.dialectOptions || {};
options.dialectOptions.options = options.dialectOptions.options || {};
options.dialectOptions.options.trustServerCertificate = true;
options.dialectOptions.options.enableArithAbort = true;
options.dialectOptions.options.validateBulkLoadParameters = true;
}
if (database instanceof Sequelize) {
this.sequelize = database;
} else {
this.sequelize = new Sequelize(database, username, password, options || {});
}
this.options = _.extend({
spaces: true,
indentation: 2,
directory: './models',
additional: {},
host: 'localhost',
port: this.getDefaultPort(options.dialect),
closeConnectionAutomatically: true
}, options || {});
if (!this.options.directory) {
this.options.noWrite = true;
}
}
async run(): Promise<TableData> {
let td = await this.build();
td = this.relate(td);
const tt = this.generate(td);
td.text = tt;
await this.write(td);
return td;
}
build(): Promise<TableData> {
const builder = new AutoBuilder(this.sequelize, this.options);
return builder.build().then(tableData => {
if (this.options.closeConnectionAutomatically) {
return this.sequelize.close().then(() => tableData);
}
return tableData;
});
}
relate(td: TableData): TableData {
const relater = new AutoRelater(this.options);
return relater.buildRelations(td);
}
generate(tableData: TableData) {
const dialect = getDialect(this.sequelize.getDialect() as Dialect);
const generator = new AutoGenerator(tableData, dialect, this.options);
return generator.generateText();
}
write(tableData: TableData) {
const writer = new AutoWriter(tableData, this.options);
return writer.write();
}
getDefaultPort(dialect?: Dialect) {
switch (dialect) {
case 'mssql':
return 1433;
case 'postgres':
return 5432;
default:
return 3306;
}
}
}
module.exports = SequelizeAuto;
module.exports.SequelizeAuto = SequelizeAuto;
module.exports.default = SequelizeAuto;