@itrocks/schema-to-mysql 0.0.1 → 0.0.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.
@@ -7,5 +7,6 @@ export declare class SchemaToMysql {
7
7
  defaultSql(value: any): string;
8
8
  sql(table: Table): string;
9
9
  tableSql(table: Table): string;
10
+ tableSqlEnd(table: Table): string;
10
11
  typeSql(type: Type): string;
11
12
  }
@@ -7,7 +7,10 @@ class SchemaToMysql {
7
7
  + this.typeSql(column.type)
8
8
  + ((column.canBeNull === false) ? ' NOT NULL' : '')
9
9
  + ((column.default !== undefined) ? (' DEFAULT ' + this.defaultSql(column.default)) : '')
10
- + (column.autoIncrement ? ' AUTO_INCREMENT' : '');
10
+ + (column.autoIncrement ? ' AUTO_INCREMENT' : '')
11
+ + ((['enum', 'string', 'set'].includes(column.type.name) && column.type.collate)
12
+ ? ' COLLATE ' + column.type.collate
13
+ : '');
11
14
  }
12
15
  columnsSql(columns) {
13
16
  return columns.map(column => this.columnSql(column)).join(',\n');
@@ -25,13 +28,62 @@ class SchemaToMysql {
25
28
  return '' + value;
26
29
  }
27
30
  sql(table) {
28
- return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)';
31
+ return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)' + this.tableSqlEnd(table);
29
32
  }
30
33
  tableSql(table) {
31
34
  return 'CREATE TABLE `' + table.name + '`';
32
35
  }
36
+ tableSqlEnd(table) {
37
+ return ' CHARSET ' + table.charset
38
+ + ' COLLATE ' + table.collation
39
+ + ' ENGINE ' + table.engine;
40
+ }
33
41
  typeSql(type) {
34
- return type.name
42
+ const length = type.length;
43
+ const name = type.name;
44
+ let typeSql;
45
+ switch (name) {
46
+ case 'decimal':
47
+ typeSql = 'decimal(' + type.length + ',' + type.precision + ')';
48
+ break;
49
+ case 'integer':
50
+ const maxValue = type.maxValue;
51
+ if (maxValue !== undefined) {
52
+ if (type.signed) {
53
+ typeSql = (maxValue > 2_147_483_647) ? 'bigint'
54
+ : (maxValue > 8_388_607) ? 'int'
55
+ : (maxValue > 32_767) ? 'mediumint'
56
+ : (maxValue > 127) ? 'smallint'
57
+ : 'tinyint';
58
+ }
59
+ else {
60
+ typeSql = (maxValue > 4_294_967_295) ? 'bigint'
61
+ : (maxValue > 16_777_215) ? 'int'
62
+ : (maxValue > 65_535) ? 'mediumint'
63
+ : (maxValue > 255) ? 'smallint'
64
+ : 'tinyint';
65
+ }
66
+ }
67
+ else {
68
+ typeSql = (length === undefined) ? 'bigint'
69
+ : (length > 9) ? 'bigint'
70
+ : (length > 7) ? 'int'
71
+ : (length > 4) ? 'mediumint'
72
+ : (length > 2) ? 'smallint'
73
+ : 'tinyint';
74
+ }
75
+ break;
76
+ case 'string':
77
+ typeSql = (length === undefined) ? 'longtext'
78
+ : (length > 16_777_215) ? 'longtext'
79
+ : (length > 65_535) ? 'mediumtext'
80
+ : (length > 255) ? 'text'
81
+ : ((type.variableLength ? 'var' : '') + 'char(' + length + ')');
82
+ break;
83
+ default:
84
+ typeSql = name;
85
+ }
86
+ return typeSql
35
87
  + ((type.signed === false) ? ' UNSIGNED' : '')
36
88
  + (type.zeroFill ? ' ZEROFILL' : '');
37
89
  }
@@ -7,5 +7,6 @@ export declare class SchemaToMysql {
7
7
  defaultSql(value: any): string;
8
8
  sql(table: Table): string;
9
9
  tableSql(table: Table): string;
10
+ tableSqlEnd(table: Table): string;
10
11
  typeSql(type: Type): string;
11
12
  }
@@ -4,7 +4,10 @@ export class SchemaToMysql {
4
4
  + this.typeSql(column.type)
5
5
  + ((column.canBeNull === false) ? ' NOT NULL' : '')
6
6
  + ((column.default !== undefined) ? (' DEFAULT ' + this.defaultSql(column.default)) : '')
7
- + (column.autoIncrement ? ' AUTO_INCREMENT' : '');
7
+ + (column.autoIncrement ? ' AUTO_INCREMENT' : '')
8
+ + ((['enum', 'string', 'set'].includes(column.type.name) && column.type.collate)
9
+ ? ' COLLATE ' + column.type.collate
10
+ : '');
8
11
  }
9
12
  columnsSql(columns) {
10
13
  return columns.map(column => this.columnSql(column)).join(',\n');
@@ -22,13 +25,62 @@ export class SchemaToMysql {
22
25
  return '' + value;
23
26
  }
24
27
  sql(table) {
25
- return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)';
28
+ return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)' + this.tableSqlEnd(table);
26
29
  }
27
30
  tableSql(table) {
28
31
  return 'CREATE TABLE `' + table.name + '`';
29
32
  }
33
+ tableSqlEnd(table) {
34
+ return ' CHARSET ' + table.charset
35
+ + ' COLLATE ' + table.collation
36
+ + ' ENGINE ' + table.engine;
37
+ }
30
38
  typeSql(type) {
31
- return type.name
39
+ const length = type.length;
40
+ const name = type.name;
41
+ let typeSql;
42
+ switch (name) {
43
+ case 'decimal':
44
+ typeSql = 'decimal(' + type.length + ',' + type.precision + ')';
45
+ break;
46
+ case 'integer':
47
+ const maxValue = type.maxValue;
48
+ if (maxValue !== undefined) {
49
+ if (type.signed) {
50
+ typeSql = (maxValue > 2_147_483_647) ? 'bigint'
51
+ : (maxValue > 8_388_607) ? 'int'
52
+ : (maxValue > 32_767) ? 'mediumint'
53
+ : (maxValue > 127) ? 'smallint'
54
+ : 'tinyint';
55
+ }
56
+ else {
57
+ typeSql = (maxValue > 4_294_967_295) ? 'bigint'
58
+ : (maxValue > 16_777_215) ? 'int'
59
+ : (maxValue > 65_535) ? 'mediumint'
60
+ : (maxValue > 255) ? 'smallint'
61
+ : 'tinyint';
62
+ }
63
+ }
64
+ else {
65
+ typeSql = (length === undefined) ? 'bigint'
66
+ : (length > 9) ? 'bigint'
67
+ : (length > 7) ? 'int'
68
+ : (length > 4) ? 'mediumint'
69
+ : (length > 2) ? 'smallint'
70
+ : 'tinyint';
71
+ }
72
+ break;
73
+ case 'string':
74
+ typeSql = (length === undefined) ? 'longtext'
75
+ : (length > 16_777_215) ? 'longtext'
76
+ : (length > 65_535) ? 'mediumtext'
77
+ : (length > 255) ? 'text'
78
+ : ((type.variableLength ? 'var' : '') + 'char(' + length + ')');
79
+ break;
80
+ default:
81
+ typeSql = name;
82
+ }
83
+ return typeSql
32
84
  + ((type.signed === false) ? ' UNSIGNED' : '')
33
85
  + (type.zeroFill ? ' ZEROFILL' : '');
34
86
  }
package/package.json CHANGED
@@ -52,5 +52,5 @@
52
52
  "build:esm": "tsc -p tsconfig.esm.json"
53
53
  },
54
54
  "types": "./esm/schema-to-mysql.d.ts",
55
- "version": "0.0.1"
55
+ "version": "0.0.2"
56
56
  }