@itrocks/schema-to-mysql 0.0.1 → 0.0.3

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.
@@ -1,11 +1,15 @@
1
1
  import { Column } from '@itrocks/schema';
2
+ import { Index } from '@itrocks/schema';
2
3
  import { Table } from '@itrocks/schema';
3
4
  import { Type } from '@itrocks/schema';
4
5
  export declare class SchemaToMysql {
5
6
  columnSql(column: Column): string;
6
7
  columnsSql(columns: Column[]): string;
7
8
  defaultSql(value: any): string;
9
+ indexSql(index: Index): string;
10
+ indexesSql(indexes: Index[]): string;
8
11
  sql(table: Table): string;
9
12
  tableSql(table: Table): string;
13
+ tableSqlEnd(table: Table): string;
10
14
  typeSql(type: Type): string;
11
15
  }
@@ -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');
@@ -24,14 +27,79 @@ class SchemaToMysql {
24
27
  }
25
28
  return '' + value;
26
29
  }
30
+ indexSql(index) {
31
+ const keysSql = ' ('
32
+ + index.keys.map(key => '`' + key.columnName + '`' + (key.length ? '(' + key.length + ')' : '')).join(',')
33
+ + ')';
34
+ return (index.type === 'primary')
35
+ ? ('PRIMARY KEY' + keysSql)
36
+ : (((index.type === 'unique') ? 'UNIQUE ' : '') + 'KEY `' + index.name + '`' + keysSql);
37
+ }
38
+ indexesSql(indexes) {
39
+ return indexes.map(index => this.indexSql(index)).join(',\n');
40
+ }
27
41
  sql(table) {
28
- return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)';
42
+ const columnsSql = this.columnsSql(table.columns);
43
+ const indexesSql = this.indexesSql(table.indexes);
44
+ return this.tableSql(table) + ' (\n'
45
+ + columnsSql
46
+ + (indexesSql ? ',' + indexesSql : '') + '\n)'
47
+ + this.tableSqlEnd(table);
29
48
  }
30
49
  tableSql(table) {
31
50
  return 'CREATE TABLE `' + table.name + '`';
32
51
  }
52
+ tableSqlEnd(table) {
53
+ return ' CHARSET ' + table.charset
54
+ + ' COLLATE ' + table.collation
55
+ + ' ENGINE ' + table.engine;
56
+ }
33
57
  typeSql(type) {
34
- return type.name
58
+ const length = type.length;
59
+ const name = type.name;
60
+ let typeSql;
61
+ switch (name) {
62
+ case 'decimal':
63
+ typeSql = 'decimal(' + type.length + ',' + type.precision + ')';
64
+ break;
65
+ case 'integer':
66
+ const maxValue = type.maxValue;
67
+ if (maxValue !== undefined) {
68
+ if (type.signed) {
69
+ typeSql = (maxValue > 2_147_483_647) ? 'bigint'
70
+ : (maxValue > 8_388_607) ? 'int'
71
+ : (maxValue > 32_767) ? 'mediumint'
72
+ : (maxValue > 127) ? 'smallint'
73
+ : 'tinyint';
74
+ }
75
+ else {
76
+ typeSql = (maxValue > 4_294_967_295) ? 'bigint'
77
+ : (maxValue > 16_777_215) ? 'int'
78
+ : (maxValue > 65_535) ? 'mediumint'
79
+ : (maxValue > 255) ? 'smallint'
80
+ : 'tinyint';
81
+ }
82
+ }
83
+ else {
84
+ typeSql = (length === undefined) ? 'bigint'
85
+ : (length > 9) ? 'bigint'
86
+ : (length > 7) ? 'int'
87
+ : (length > 4) ? 'mediumint'
88
+ : (length > 2) ? 'smallint'
89
+ : 'tinyint';
90
+ }
91
+ break;
92
+ case 'string':
93
+ typeSql = (length === undefined) ? 'longtext'
94
+ : (length > 16_777_215) ? 'longtext'
95
+ : (length > 65_535) ? 'mediumtext'
96
+ : (length > 255) ? 'text'
97
+ : ((type.variableLength ? 'var' : '') + 'char(' + length + ')');
98
+ break;
99
+ default:
100
+ typeSql = name;
101
+ }
102
+ return typeSql
35
103
  + ((type.signed === false) ? ' UNSIGNED' : '')
36
104
  + (type.zeroFill ? ' ZEROFILL' : '');
37
105
  }
@@ -1,11 +1,15 @@
1
1
  import { Column } from '@itrocks/schema';
2
+ import { Index } from '@itrocks/schema';
2
3
  import { Table } from '@itrocks/schema';
3
4
  import { Type } from '@itrocks/schema';
4
5
  export declare class SchemaToMysql {
5
6
  columnSql(column: Column): string;
6
7
  columnsSql(columns: Column[]): string;
7
8
  defaultSql(value: any): string;
9
+ indexSql(index: Index): string;
10
+ indexesSql(indexes: Index[]): string;
8
11
  sql(table: Table): string;
9
12
  tableSql(table: Table): string;
13
+ tableSqlEnd(table: Table): string;
10
14
  typeSql(type: Type): string;
11
15
  }
@@ -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');
@@ -21,14 +24,79 @@ export class SchemaToMysql {
21
24
  }
22
25
  return '' + value;
23
26
  }
27
+ indexSql(index) {
28
+ const keysSql = ' ('
29
+ + index.keys.map(key => '`' + key.columnName + '`' + (key.length ? '(' + key.length + ')' : '')).join(',')
30
+ + ')';
31
+ return (index.type === 'primary')
32
+ ? ('PRIMARY KEY' + keysSql)
33
+ : (((index.type === 'unique') ? 'UNIQUE ' : '') + 'KEY `' + index.name + '`' + keysSql);
34
+ }
35
+ indexesSql(indexes) {
36
+ return indexes.map(index => this.indexSql(index)).join(',\n');
37
+ }
24
38
  sql(table) {
25
- return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)';
39
+ const columnsSql = this.columnsSql(table.columns);
40
+ const indexesSql = this.indexesSql(table.indexes);
41
+ return this.tableSql(table) + ' (\n'
42
+ + columnsSql
43
+ + (indexesSql ? ',' + indexesSql : '') + '\n)'
44
+ + this.tableSqlEnd(table);
26
45
  }
27
46
  tableSql(table) {
28
47
  return 'CREATE TABLE `' + table.name + '`';
29
48
  }
49
+ tableSqlEnd(table) {
50
+ return ' CHARSET ' + table.charset
51
+ + ' COLLATE ' + table.collation
52
+ + ' ENGINE ' + table.engine;
53
+ }
30
54
  typeSql(type) {
31
- return type.name
55
+ const length = type.length;
56
+ const name = type.name;
57
+ let typeSql;
58
+ switch (name) {
59
+ case 'decimal':
60
+ typeSql = 'decimal(' + type.length + ',' + type.precision + ')';
61
+ break;
62
+ case 'integer':
63
+ const maxValue = type.maxValue;
64
+ if (maxValue !== undefined) {
65
+ if (type.signed) {
66
+ typeSql = (maxValue > 2_147_483_647) ? 'bigint'
67
+ : (maxValue > 8_388_607) ? 'int'
68
+ : (maxValue > 32_767) ? 'mediumint'
69
+ : (maxValue > 127) ? 'smallint'
70
+ : 'tinyint';
71
+ }
72
+ else {
73
+ typeSql = (maxValue > 4_294_967_295) ? 'bigint'
74
+ : (maxValue > 16_777_215) ? 'int'
75
+ : (maxValue > 65_535) ? 'mediumint'
76
+ : (maxValue > 255) ? 'smallint'
77
+ : 'tinyint';
78
+ }
79
+ }
80
+ else {
81
+ typeSql = (length === undefined) ? 'bigint'
82
+ : (length > 9) ? 'bigint'
83
+ : (length > 7) ? 'int'
84
+ : (length > 4) ? 'mediumint'
85
+ : (length > 2) ? 'smallint'
86
+ : 'tinyint';
87
+ }
88
+ break;
89
+ case 'string':
90
+ typeSql = (length === undefined) ? 'longtext'
91
+ : (length > 16_777_215) ? 'longtext'
92
+ : (length > 65_535) ? 'mediumtext'
93
+ : (length > 255) ? 'text'
94
+ : ((type.variableLength ? 'var' : '') + 'char(' + length + ')');
95
+ break;
96
+ default:
97
+ typeSql = name;
98
+ }
99
+ return typeSql
32
100
  + ((type.signed === false) ? ' UNSIGNED' : '')
33
101
  + (type.zeroFill ? ' ZEROFILL' : '');
34
102
  }
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.3"
56
56
  }