@itrocks/mysql-to-schema 0.0.3 → 0.0.5

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,10 +1,10 @@
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;
7
+ lengthMaxValue(mysqlType: string, signed: boolean): bigint | number | undefined;
8
8
  normalize(type: Type): void;
9
9
  precision(rawType: string, mysqlType: string): number | undefined;
10
10
  values(mysqlType: string): string[];
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'),
@@ -43,13 +43,13 @@ class ToType {
43
43
  case 'tinyint':
44
44
  return new schema_1.Type('integer', {
45
45
  length: this.length(rawType, mysqlType),
46
+ maxValue: this.lengthMaxValue(mysqlType, !mysqlType.includes('unsigned')),
46
47
  signed: !mysqlType.includes('unsigned'),
47
48
  zeroFill: mysqlType.includes('zerofill')
48
49
  });
49
50
  case 'enum':
50
51
  case 'set':
51
52
  return new schema_1.Type(rawType, {
52
- collate: this.collate(mysqlType),
53
53
  values: this.values(mysqlType)
54
54
  });
55
55
  case 'time':
@@ -61,18 +61,10 @@ class ToType {
61
61
  }
62
62
  // char, longtext, mediumtext, text, tinytext, varchar
63
63
  return new schema_1.Type('string', {
64
- collate: this.collate(mysqlType),
65
64
  length: this.length(rawType, mysqlType),
66
65
  variableLength: rawType.startsWith('var')
67
66
  });
68
67
  }
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
68
  defaultLength(rawType) {
77
69
  switch (rawType) {
78
70
  case 'bigint':
@@ -140,6 +132,17 @@ class ToType {
140
132
  ? +mysqlType.substring(position, Math.min(mysqlType.indexOf(')', position), (mysqlType + ',').indexOf(',', position)))
141
133
  : this.defaultLength(rawType);
142
134
  }
135
+ lengthMaxValue(mysqlType, signed) {
136
+ switch (mysqlType) {
137
+ case 'tinyint': return signed ? 127 : 255;
138
+ case 'smallint': return signed ? 32767 : 65535;
139
+ case 'mediumint': return signed ? 8388607 : 16777215;
140
+ case 'int':
141
+ case 'integer': return signed ? 2147483647 : 4294967295;
142
+ case 'bigint': return signed ? 2n ** 63n - 1n : 2n ** 64n - 1n;
143
+ }
144
+ return undefined;
145
+ }
143
146
  normalize(type) {
144
147
  switch (type.name) {
145
148
  case 'bit':
@@ -155,8 +158,8 @@ class ToType {
155
158
  type.length = 16_777_215;
156
159
  else if (type.length > 255)
157
160
  type.length = 65_535;
158
- else
159
- type.length = 255;
161
+ else if (type.length > 3)
162
+ Object.assign(type, { length: 255, variableLength: true });
160
163
  break;
161
164
  case 'boolean':
162
165
  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,10 +1,10 @@
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;
7
+ lengthMaxValue(mysqlType: string, signed: boolean): bigint | number | undefined;
8
8
  normalize(type: Type): void;
9
9
  precision(rawType: string, mysqlType: string): number | undefined;
10
10
  values(mysqlType: string): string[];
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'),
@@ -40,13 +40,13 @@ export class ToType {
40
40
  case 'tinyint':
41
41
  return new Type('integer', {
42
42
  length: this.length(rawType, mysqlType),
43
+ maxValue: this.lengthMaxValue(mysqlType, !mysqlType.includes('unsigned')),
43
44
  signed: !mysqlType.includes('unsigned'),
44
45
  zeroFill: mysqlType.includes('zerofill')
45
46
  });
46
47
  case 'enum':
47
48
  case 'set':
48
49
  return new Type(rawType, {
49
- collate: this.collate(mysqlType),
50
50
  values: this.values(mysqlType)
51
51
  });
52
52
  case 'time':
@@ -58,18 +58,10 @@ export class ToType {
58
58
  }
59
59
  // char, longtext, mediumtext, text, tinytext, varchar
60
60
  return new Type('string', {
61
- collate: this.collate(mysqlType),
62
61
  length: this.length(rawType, mysqlType),
63
62
  variableLength: rawType.startsWith('var')
64
63
  });
65
64
  }
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
65
  defaultLength(rawType) {
74
66
  switch (rawType) {
75
67
  case 'bigint':
@@ -137,6 +129,17 @@ export class ToType {
137
129
  ? +mysqlType.substring(position, Math.min(mysqlType.indexOf(')', position), (mysqlType + ',').indexOf(',', position)))
138
130
  : this.defaultLength(rawType);
139
131
  }
132
+ lengthMaxValue(mysqlType, signed) {
133
+ switch (mysqlType) {
134
+ case 'tinyint': return signed ? 127 : 255;
135
+ case 'smallint': return signed ? 32767 : 65535;
136
+ case 'mediumint': return signed ? 8388607 : 16777215;
137
+ case 'int':
138
+ case 'integer': return signed ? 2147483647 : 4294967295;
139
+ case 'bigint': return signed ? 2n ** 63n - 1n : 2n ** 64n - 1n;
140
+ }
141
+ return undefined;
142
+ }
140
143
  normalize(type) {
141
144
  switch (type.name) {
142
145
  case 'bit':
@@ -152,8 +155,8 @@ export class ToType {
152
155
  type.length = 16_777_215;
153
156
  else if (type.length > 255)
154
157
  type.length = 65_535;
155
- else
156
- type.length = 255;
158
+ else if (type.length > 3)
159
+ Object.assign(type, { length: 255, variableLength: true });
157
160
  break;
158
161
  case 'boolean':
159
162
  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.5"
49
49
  }