@itrocks/schema-to-mysql 0.0.6 → 0.1.0
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/package.json +7 -13
- package/esm/schema-to-mysql.d.ts +0 -15
- package/esm/schema-to-mysql.js +0 -104
package/package.json
CHANGED
|
@@ -8,20 +8,17 @@
|
|
|
8
8
|
},
|
|
9
9
|
"description": "Converts a table schema into MySQL statements for table creation",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"typescript": "
|
|
11
|
+
"typescript": "^6.0"
|
|
12
12
|
},
|
|
13
13
|
"engines": {
|
|
14
|
-
"node": ">=
|
|
15
|
-
},
|
|
16
|
-
"exports": {
|
|
17
|
-
"import": "./esm/schema-to-mysql.js",
|
|
18
|
-
"require": "./cjs/schema-to-mysql.js"
|
|
14
|
+
"node": ">=24"
|
|
19
15
|
},
|
|
16
|
+
"exports": "./cjs/schema-to-mysql.js",
|
|
20
17
|
"files": [
|
|
21
18
|
"LICENSE",
|
|
22
19
|
"README.md",
|
|
23
|
-
"
|
|
24
|
-
"
|
|
20
|
+
"cjs",
|
|
21
|
+
"!*.map"
|
|
25
22
|
],
|
|
26
23
|
"homepage": "https://it.rocks",
|
|
27
24
|
"keywords": [
|
|
@@ -51,10 +48,7 @@
|
|
|
51
48
|
"url": "git+https://github.com/itrocks-ts/schema-to-mysql.git"
|
|
52
49
|
},
|
|
53
50
|
"scripts": {
|
|
54
|
-
"build": "
|
|
55
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
56
|
-
"build:esm": "tsc -p tsconfig.esm.json"
|
|
51
|
+
"build": "tsc"
|
|
57
52
|
},
|
|
58
|
-
"
|
|
59
|
-
"version": "0.0.6"
|
|
53
|
+
"version": "0.1.0"
|
|
60
54
|
}
|
package/esm/schema-to-mysql.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Column } from '@itrocks/schema';
|
|
2
|
-
import { Index } from '@itrocks/schema';
|
|
3
|
-
import { Table } from '@itrocks/schema';
|
|
4
|
-
import { Type } from '@itrocks/schema';
|
|
5
|
-
export declare class SchemaToMysql {
|
|
6
|
-
columnSql(column: Column): string;
|
|
7
|
-
columnsSql(columns: Column[]): string;
|
|
8
|
-
defaultSql(value: any): string;
|
|
9
|
-
indexSql(index: Index): string;
|
|
10
|
-
indexesSql(indexes: Index[]): string;
|
|
11
|
-
sql(table: Table): string;
|
|
12
|
-
tableSql(table: Table): string;
|
|
13
|
-
tableSqlEnd(table: Table): string;
|
|
14
|
-
typeSql(type: Type): string;
|
|
15
|
-
}
|
package/esm/schema-to-mysql.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
export class SchemaToMysql {
|
|
2
|
-
columnSql(column) {
|
|
3
|
-
return '`' + column.name + '` '
|
|
4
|
-
+ this.typeSql(column.type)
|
|
5
|
-
+ ((column.canBeNull === false) ? ' NOT NULL' : '')
|
|
6
|
-
+ ((column.default !== undefined) ? (' DEFAULT ' + this.defaultSql(column.default)) : '')
|
|
7
|
-
+ (column.autoIncrement ? ' AUTO_INCREMENT' : '')
|
|
8
|
-
+ ((['enum', 'string', 'set'].includes(column.type.name) && column.type.collate)
|
|
9
|
-
? ' COLLATE ' + column.type.collate
|
|
10
|
-
: '');
|
|
11
|
-
}
|
|
12
|
-
columnsSql(columns) {
|
|
13
|
-
return columns.map(column => this.columnSql(column)).join(',\n');
|
|
14
|
-
}
|
|
15
|
-
defaultSql(value) {
|
|
16
|
-
if (value === null) {
|
|
17
|
-
return 'NULL';
|
|
18
|
-
}
|
|
19
|
-
if (typeof value === 'string') {
|
|
20
|
-
return "'" + value.replaceAll("'", "''") + "'";
|
|
21
|
-
}
|
|
22
|
-
if (value instanceof Date) {
|
|
23
|
-
return "'" + value.toISOString().slice(0, (value.getTime() % 864e5) ? 19 : 10).replace('T', ' ') + "'";
|
|
24
|
-
}
|
|
25
|
-
return '' + value;
|
|
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
|
-
}
|
|
38
|
-
sql(table) {
|
|
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);
|
|
45
|
-
}
|
|
46
|
-
tableSql(table) {
|
|
47
|
-
return 'CREATE TABLE `' + table.name + '`';
|
|
48
|
-
}
|
|
49
|
-
tableSqlEnd(table) {
|
|
50
|
-
return ' CHARSET ' + table.charset
|
|
51
|
-
+ ' COLLATE ' + table.collation
|
|
52
|
-
+ ' ENGINE ' + table.engine;
|
|
53
|
-
}
|
|
54
|
-
typeSql(type) {
|
|
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
|
|
100
|
-
+ ((type.signed === false) ? ' unsigned' : '')
|
|
101
|
-
+ (type.zeroFill ? ' ZEROFILL' : '');
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
//# sourceMappingURL=schema-to-mysql.js.map
|