@itrocks/mysql-to-schema 0.0.3 → 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.
package/cjs/to-column.js CHANGED
@@ -33,6 +33,9 @@ class ToColumn {
33
33
  }
34
34
  rowToColumn(row) {
35
35
  const type = this.toType.convert(row.COLUMN_TYPE);
36
+ if (row.COLLATION_NAME) {
37
+ type.collate = row.COLLATION_NAME;
38
+ }
36
39
  const column = new schema_1.Column(row.COLUMN_NAME, type, {
37
40
  autoIncrement: row.EXTRA.includes('auto_increment'),
38
41
  canBeNull: row.IS_NULLABLE === 'YES',
package/cjs/to-type.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Type } from '@itrocks/schema';
2
2
  export declare class ToType {
3
3
  convert(mysqlType: string): Type;
4
- collate(mysqlType: string): string | undefined;
5
4
  defaultLength(rawType: string): number | undefined;
6
5
  defaultPrecision(rawType: string): number | undefined;
7
6
  length(rawType: string, mysqlType: string): number | undefined;
package/cjs/to-type.js CHANGED
@@ -21,15 +21,15 @@ class ToType {
21
21
  case 'boolean':
22
22
  return new schema_1.Type('boolean');
23
23
  case 'date':
24
- return new schema_1.Type('date');
24
+ return new schema_1.Type('date', { length: 10 });
25
25
  case 'datetime':
26
- return new schema_1.Type('datetime');
26
+ return new schema_1.Type('datetime', { length: 19 });
27
27
  case 'decimal':
28
28
  case 'double':
29
29
  case 'float':
30
30
  case 'numeric':
31
31
  case 'real':
32
- return new schema_1.Type('float', {
32
+ return new schema_1.Type((rawType === 'decimal') ? 'decimal' : 'float', {
33
33
  length: this.length(rawType, mysqlType),
34
34
  precision: this.precision(rawType, mysqlType),
35
35
  signed: !mysqlType.includes('unsigned'),
@@ -49,7 +49,6 @@ class ToType {
49
49
  case 'enum':
50
50
  case 'set':
51
51
  return new schema_1.Type(rawType, {
52
- collate: this.collate(mysqlType),
53
52
  values: this.values(mysqlType)
54
53
  });
55
54
  case 'time':
@@ -61,18 +60,10 @@ class ToType {
61
60
  }
62
61
  // char, longtext, mediumtext, text, tinytext, varchar
63
62
  return new schema_1.Type('string', {
64
- collate: this.collate(mysqlType),
65
63
  length: this.length(rawType, mysqlType),
66
64
  variableLength: rawType.startsWith('var')
67
65
  });
68
66
  }
69
- collate(mysqlType) {
70
- const position = mysqlType.indexOf('collate ');
71
- if (position > 0) {
72
- const end = (mysqlType + ' ').indexOf(' ', position + 8);
73
- return mysqlType.slice(position + 8, end);
74
- }
75
- }
76
67
  defaultLength(rawType) {
77
68
  switch (rawType) {
78
69
  case 'bigint':
@@ -155,8 +146,8 @@ class ToType {
155
146
  type.length = 16_777_215;
156
147
  else if (type.length > 255)
157
148
  type.length = 65_535;
158
- else
159
- type.length = 255;
149
+ else if (type.length > 3)
150
+ Object.assign(type, { length: 255, variableLength: true });
160
151
  break;
161
152
  case 'boolean':
162
153
  type.length = 1;
package/esm/to-column.js CHANGED
@@ -30,6 +30,9 @@ export class ToColumn {
30
30
  }
31
31
  rowToColumn(row) {
32
32
  const type = this.toType.convert(row.COLUMN_TYPE);
33
+ if (row.COLLATION_NAME) {
34
+ type.collate = row.COLLATION_NAME;
35
+ }
33
36
  const column = new Column(row.COLUMN_NAME, type, {
34
37
  autoIncrement: row.EXTRA.includes('auto_increment'),
35
38
  canBeNull: row.IS_NULLABLE === 'YES',
package/esm/to-type.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Type } from '@itrocks/schema';
2
2
  export declare class ToType {
3
3
  convert(mysqlType: string): Type;
4
- collate(mysqlType: string): string | undefined;
5
4
  defaultLength(rawType: string): number | undefined;
6
5
  defaultPrecision(rawType: string): number | undefined;
7
6
  length(rawType: string, mysqlType: string): number | undefined;
package/esm/to-type.js CHANGED
@@ -18,15 +18,15 @@ export class ToType {
18
18
  case 'boolean':
19
19
  return new Type('boolean');
20
20
  case 'date':
21
- return new Type('date');
21
+ return new Type('date', { length: 10 });
22
22
  case 'datetime':
23
- return new Type('datetime');
23
+ return new Type('datetime', { length: 19 });
24
24
  case 'decimal':
25
25
  case 'double':
26
26
  case 'float':
27
27
  case 'numeric':
28
28
  case 'real':
29
- return new Type('float', {
29
+ return new Type((rawType === 'decimal') ? 'decimal' : 'float', {
30
30
  length: this.length(rawType, mysqlType),
31
31
  precision: this.precision(rawType, mysqlType),
32
32
  signed: !mysqlType.includes('unsigned'),
@@ -46,7 +46,6 @@ export class ToType {
46
46
  case 'enum':
47
47
  case 'set':
48
48
  return new Type(rawType, {
49
- collate: this.collate(mysqlType),
50
49
  values: this.values(mysqlType)
51
50
  });
52
51
  case 'time':
@@ -58,18 +57,10 @@ export class ToType {
58
57
  }
59
58
  // char, longtext, mediumtext, text, tinytext, varchar
60
59
  return new Type('string', {
61
- collate: this.collate(mysqlType),
62
60
  length: this.length(rawType, mysqlType),
63
61
  variableLength: rawType.startsWith('var')
64
62
  });
65
63
  }
66
- collate(mysqlType) {
67
- const position = mysqlType.indexOf('collate ');
68
- if (position > 0) {
69
- const end = (mysqlType + ' ').indexOf(' ', position + 8);
70
- return mysqlType.slice(position + 8, end);
71
- }
72
- }
73
64
  defaultLength(rawType) {
74
65
  switch (rawType) {
75
66
  case 'bigint':
@@ -152,8 +143,8 @@ export class ToType {
152
143
  type.length = 16_777_215;
153
144
  else if (type.length > 255)
154
145
  type.length = 65_535;
155
- else
156
- type.length = 255;
146
+ else if (type.length > 3)
147
+ Object.assign(type, { length: 255, variableLength: true });
157
148
  break;
158
149
  case 'boolean':
159
150
  type.length = 1;
package/package.json CHANGED
@@ -45,5 +45,5 @@
45
45
  "build:esm": "tsc -p tsconfig.esm.json"
46
46
  },
47
47
  "types": "./esm/mysql-to-schema.d.ts",
48
- "version": "0.0.3"
48
+ "version": "0.0.4"
49
49
  }