@itrocks/schema-to-mysql 0.0.2 → 0.0.4

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,10 +1,13 @@
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;
10
13
  tableSqlEnd(table: Table): string;
@@ -27,8 +27,24 @@ class SchemaToMysql {
27
27
  }
28
28
  return '' + value;
29
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
+ }
30
41
  sql(table) {
31
- return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)' + this.tableSqlEnd(table);
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);
32
48
  }
33
49
  tableSql(table) {
34
50
  return 'CREATE TABLE `' + table.name + '`';
@@ -84,7 +100,7 @@ class SchemaToMysql {
84
100
  typeSql = name;
85
101
  }
86
102
  return typeSql
87
- + ((type.signed === false) ? ' UNSIGNED' : '')
103
+ + ((type.signed === false) ? ' unsigned' : '')
88
104
  + (type.zeroFill ? ' ZEROFILL' : '');
89
105
  }
90
106
  }
@@ -1,10 +1,13 @@
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;
10
13
  tableSqlEnd(table: Table): string;
@@ -24,8 +24,24 @@ export class SchemaToMysql {
24
24
  }
25
25
  return '' + value;
26
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
+ }
27
38
  sql(table) {
28
- return this.tableSql(table) + ' (\n' + this.columnsSql(table.columns) + '\n)' + this.tableSqlEnd(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);
29
45
  }
30
46
  tableSql(table) {
31
47
  return 'CREATE TABLE `' + table.name + '`';
@@ -81,7 +97,7 @@ export class SchemaToMysql {
81
97
  typeSql = name;
82
98
  }
83
99
  return typeSql
84
- + ((type.signed === false) ? ' UNSIGNED' : '')
100
+ + ((type.signed === false) ? ' unsigned' : '')
85
101
  + (type.zeroFill ? ' ZEROFILL' : '');
86
102
  }
87
103
  }
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.2"
55
+ "version": "0.0.4"
56
56
  }