chore(mysql): sync mysql data types with mysql docs

This commit is contained in:
Vladimir Poluch 2018-12-21 17:56:09 +01:00
parent b1d4d7358e
commit a293885e11
5 changed files with 170 additions and 26 deletions

View File

@ -288,11 +288,12 @@ or
### Column types for `mysql` / `mariadb`
`int`, `tinyint`, `smallint`, `mediumint`, `bigint`, `float`, `double`, `dec`, `decimal`, `numeric`,
`date`, `datetime`, `timestamp`, `time`, `year`, `char`, `varchar`, `nvarchar`, `text`, `tinytext`,
`mediumtext`, `blob`, `longtext`, `tinyblob`, `mediumblob`, `longblob`, `enum`, `json`, `binary`,
`geometry`, `point`, `linestring`, `polygon`, `multipoint`, `multilinestring`, `multipolygon`,
`geometrycollection`
`bit`, `int`, `integer`, `tinyint`, `smallint`, `mediumint`, `bigint`, `float`, `double`,
`double precision`, `dec`, `decimal`, `numeric`, `fixed`, `bool`, `boolean`, `date`, `datetime`,
`timestamp`, `time`, `year`, `char`, `nchar`, `national char`, `varchar`, `nvarchar`, `national varchar`,
`text`, `tinytext`, `mediumtext`, `blob`, `longtext`, `tinyblob`, `mediumblob`, `longblob`, `enum`,
`json`, `binary`, `varbinary`, `geometry`, `point`, `linestring`, `polygon`, `multipoint`, `multilinestring`,
`multipolygon`, `geometrycollection`
### Column types for `postgres`

View File

@ -76,27 +76,40 @@ export class MysqlDriver implements Driver {
* Gets list of supported column data types by a driver.
*
* @see https://www.tutorialspoint.com/mysql/mysql-data-types.htm
* @see https://dev.mysql.com/doc/refman/5.7/en/data-types.html
* @see https://dev.mysql.com/doc/refman/8.0/en/data-types.html
*/
supportedDataTypes: ColumnType[] = [
// numeric types
"bit",
"int",
"integer", // synonym for int
"tinyint",
"smallint",
"mediumint",
"bigint",
"float",
"double",
"dec",
"double precision", // synonym for double
"real", // synonym for double
"decimal",
"numeric",
"dec", // synonym for decimal
"numeric", // synonym for decimal
"fixed", // synonym for decimal
"bool", // synonym for tinyint
"boolean", // synonym for tinyint
// date and time types
"date",
"datetime",
"timestamp",
"time",
"year",
// string types
"char",
"nchar", // synonym for national char
"national char",
"varchar",
"nvarchar",
"nvarchar", // synonym for national varchar
"national varchar",
"blob",
"text",
"tinyblob",
@ -106,9 +119,11 @@ export class MysqlDriver implements Driver {
"longblob",
"longtext",
"enum",
"json",
"binary",
"varbinary",
// json data type
"json",
// spatial data types
"geometry",
"point",
"linestring",
@ -138,20 +153,27 @@ export class MysqlDriver implements Driver {
*/
withLengthColumnTypes: ColumnType[] = [
"char",
"nchar",
"national char",
"varchar",
"nvarchar",
"national varchar",
"binary",
"varbinary"
"varbinary",
"blob",
"text"
];
/**
* Gets list of column data types that support length by a driver.
*/
withWidthColumnTypes: ColumnType[] = [
"bit",
"tinyint",
"smallint",
"mediumint",
"int",
"integer",
"bigint"
];
@ -160,8 +182,13 @@ export class MysqlDriver implements Driver {
*/
withPrecisionColumnTypes: ColumnType[] = [
"decimal",
"dec",
"numeric",
"fixed",
"float",
"double",
"double precision",
"real",
"time",
"datetime",
"timestamp"
@ -172,8 +199,13 @@ export class MysqlDriver implements Driver {
*/
withScaleColumnTypes: ColumnType[] = [
"decimal",
"dec",
"numeric",
"fixed",
"float",
"double",
"double precision",
"real"
];
/**
@ -181,13 +213,19 @@ export class MysqlDriver implements Driver {
*/
unsignedAndZerofillTypes: ColumnType[] = [
"int",
"integer",
"smallint",
"tinyint",
"mediumint",
"bigint",
"decimal",
"dec",
"numeric",
"fixed",
"float",
"double"
"double",
"double precision",
"real"
];
/**
@ -220,13 +258,20 @@ export class MysqlDriver implements Driver {
*/
dataTypeDefaults: DataTypeDefaults = {
"varchar": { length: 255 },
"nvarchar": { length: 255 },
"national varchar": { length: 255 },
"char": { length: 1 },
"binary": { length: 1 },
"varbinary": { length: 255 },
"decimal": { precision: 10, scale: 0 },
"dec": { precision: 10, scale: 0 },
"numeric": { precision: 10, scale: 0 },
"fixed": { precision: 10, scale: 0 },
"float": { precision: 12 },
"double": { precision: 22 },
"bit": { width: 1 },
"int": { width: 11 },
"integer": { width: 11 },
"tinyint": { width: 4 },
"smallint": { width: 6 },
"mediumint": { width: 9 },
@ -411,7 +456,7 @@ export class MysqlDriver implements Driver {
if (value === null || value === undefined)
return value;
if (columnMetadata.type === Boolean) {
if (columnMetadata.type === Boolean || columnMetadata.type === "bool" || columnMetadata.type === "boolean") {
value = value ? true : false;
} else if (columnMetadata.type === "datetime" || columnMetadata.type === Date) {
@ -446,7 +491,7 @@ export class MysqlDriver implements Driver {
if (column.type === Number || column.type === "integer") {
return "int";
} else if (column.type === String || column.type === "nvarchar") {
} else if (column.type === String) {
return "varchar";
} else if (column.type === Date) {
@ -458,15 +503,27 @@ export class MysqlDriver implements Driver {
} else if (column.type === Boolean) {
return "tinyint";
} else if (column.type === "numeric" || column.type === "dec") {
return "decimal";
} else if (column.type === "uuid") {
return "varchar";
} else if (column.type === "simple-array" || column.type === "simple-json") {
return "text";
} else if (column.type === "double precision" || column.type === "real") {
return "double";
} else if (column.type === "dec" || column.type === "numeric" || column.type === "fixed") {
return "decimal";
} else if (column.type === "bool" || column.type === "boolean") {
return "tinyint";
} else if (column.type === "nvarchar" || column.type === "national varchar") {
return "varchar";
} else if (column.type === "nchar" || column.type === "national char") {
return "char";
} else {
return column.type as string || "";
}
@ -519,6 +576,7 @@ export class MysqlDriver implements Driver {
case String:
case "varchar":
case "nvarchar":
case "national varchar":
return "255";
case "varbinary":
return "255";

View File

@ -6,13 +6,14 @@ export type PrimaryGeneratedColumnType = "int" // mysql, mssql, oracle, sqlite
|"int2" // postgres, sqlite
|"int4" // postgres
|"int8" // postgres, sqlite
|"integer" // postgres, oracle, sqlite
|"integer" // postgres, oracle, sqlite, mysql
|"tinyint" // mysql, mssql, sqlite
|"smallint" // mysql, postgres, mssql, oracle, sqlite
|"mediumint" // mysql, sqlite
|"bigint" // mysql, postgres, mssql, sqlite
|"dec" // oracle, mssql
|"decimal" // mysql, postgres, mssql, sqlite
|"fixed" // mysql
|"numeric" // postgres, mssql, sqlite
|"number"; // oracle
@ -27,11 +28,12 @@ export type SpatialColumnType = "geometry" // postgres
*/
export type WithPrecisionColumnType = "float" // mysql, mssql, oracle, sqlite
|"double" // mysql, sqlite
|"dec" // oracle, mssql
|"dec" // oracle, mssql, mysql
|"decimal" // mysql, postgres, mssql, sqlite
|"numeric" // postgres, mssql, sqlite
|"fixed" // mysql
|"numeric" // postgres, mssql, sqlite, mysql
|"real" // mysql, postgres, mssql, oracle, sqlite
|"double precision" // postgres, oracle, sqlite
|"double precision" // postgres, oracle, sqlite, mysql
|"number" // oracle
|"datetime" // mssql, mysql, sqlite
|"datetime2" // mssql
@ -49,12 +51,14 @@ export type WithPrecisionColumnType = "float" // mysql, mssql, oracle, sqlite
*/
export type WithLengthColumnType = "character varying" // postgres
|"varying character" // sqlite
|"nvarchar" // mssql
|"nvarchar" // mssql, mysql
|"national varchar" // mysql
|"character" // mysql, postgres, sqlite
|"native character" // sqlite
|"varchar" // mysql, postgres, mssql, sqlite
|"char" // mysql, postgres, mssql, oracle
|"nchar" // mssql, oracle, sqlite
|"nchar" // mssql, oracle, sqlite, mysql
|"national char" // mysql
|"varchar2" // oracle
|"nvarchar2" // oracle, sqlite
|"raw" // oracle
@ -90,8 +94,8 @@ export type SimpleColumnType =
|"money" // postgres, mssql
// boolean types
|"boolean" // postgres, sqlite
|"bool" // postgres
|"boolean" // postgres, sqlite, mysql
|"bool" // postgres, mysql
// text/binary types
|"tinyblob" // mysql

View File

@ -27,16 +27,29 @@ describe("database schema > column types > mysql", () => {
const post = new Post();
post.id = 1;
post.bit = Buffer.from([0]);
post.int = 2147483647;
post.integer = 2147483647;
post.tinyint = 127;
post.smallint = 32767;
post.mediumint = 8388607;
post.bigint = "8223372036854775807";
post.float = 10.53;
post.double = 10.1234;
post.doublePrecision = 10.1234;
post.real = 10.1234;
post.dec = "822337";
post.decimal = "822337";
post.numeric = "822337";
post.fixed = "822337";
post.bool = true;
post.boolean = false;
post.char = "A";
post.nChar = "A";
post.nationalChar = "A";
post.varchar = "This is varchar";
post.nVarchar = "This is varchar";
post.nationalVarchar = "This is varchar";
post.text = "This is text";
post.tinytext = "This is tinytext";
post.mediumtext = "This is mediumtext";
@ -71,6 +84,7 @@ describe("database schema > column types > mysql", () => {
const loadedPost = (await postRepository.findOne(1))!;
loadedPost.id.should.be.equal(post.id);
loadedPost.bit.toString().should.be.equal(post.bit.toString());
loadedPost.int.should.be.equal(post.int);
loadedPost.tinyint.should.be.equal(post.tinyint);
loadedPost.smallint.should.be.equal(post.smallint);
@ -78,9 +92,20 @@ describe("database schema > column types > mysql", () => {
loadedPost.bigint.should.be.equal(post.bigint);
loadedPost.float.should.be.equal(post.float);
loadedPost.double.should.be.equal(post.double);
loadedPost.doublePrecision.should.be.equal(post.doublePrecision);
loadedPost.real.should.be.equal(post.real);
loadedPost.dec.should.be.equal(post.dec);
loadedPost.decimal.should.be.equal(post.decimal);
loadedPost.numeric.should.be.equal(post.numeric);
loadedPost.fixed.should.be.equal(post.fixed);
loadedPost.bool.should.be.equal(post.bool);
loadedPost.boolean.should.be.equal(post.boolean);
loadedPost.char.should.be.equal(post.char);
loadedPost.nChar.should.be.equal(post.nChar);
loadedPost.nationalChar.should.be.equal(post.nationalChar);
loadedPost.varchar.should.be.equal(post.varchar);
loadedPost.nVarchar.should.be.equal(post.nVarchar);
loadedPost.nationalVarchar.should.be.equal(post.nationalVarchar);
loadedPost.text.should.be.equal(post.text);
loadedPost.tinytext.should.be.equal(post.tinytext);
loadedPost.mediumtext.should.be.equal(post.mediumtext);
@ -113,16 +138,29 @@ describe("database schema > column types > mysql", () => {
loadedPost.simpleJson.param.should.be.equal(post.simpleJson.param);
table!.findColumnByName("id")!.type.should.be.equal("int");
table!.findColumnByName("bit")!.type.should.be.equal("bit");
table!.findColumnByName("int")!.type.should.be.equal("int");
table!.findColumnByName("integer")!.type.should.be.equal("int");
table!.findColumnByName("tinyint")!.type.should.be.equal("tinyint");
table!.findColumnByName("smallint")!.type.should.be.equal("smallint");
table!.findColumnByName("mediumint")!.type.should.be.equal("mediumint");
table!.findColumnByName("bigint")!.type.should.be.equal("bigint");
table!.findColumnByName("float")!.type.should.be.equal("float");
table!.findColumnByName("double")!.type.should.be.equal("double");
table!.findColumnByName("doublePrecision")!.type.should.be.equal("double");
table!.findColumnByName("real")!.type.should.be.equal("double");
table!.findColumnByName("dec")!.type.should.be.equal("decimal");
table!.findColumnByName("decimal")!.type.should.be.equal("decimal");
table!.findColumnByName("numeric")!.type.should.be.equal("decimal");
table!.findColumnByName("fixed")!.type.should.be.equal("decimal");
table!.findColumnByName("bool")!.type.should.be.equal("tinyint");
table!.findColumnByName("boolean")!.type.should.be.equal("tinyint");
table!.findColumnByName("char")!.type.should.be.equal("char");
table!.findColumnByName("nChar")!.type.should.be.equal("char");
table!.findColumnByName("nationalChar")!.type.should.be.equal("char");
table!.findColumnByName("varchar")!.type.should.be.equal("varchar");
table!.findColumnByName("nVarchar")!.type.should.be.equal("varchar");
table!.findColumnByName("nationalVarchar")!.type.should.be.equal("varchar");
table!.findColumnByName("text")!.type.should.be.equal("text");
table!.findColumnByName("tinytext")!.type.should.be.equal("tinytext");
table!.findColumnByName("mediumtext")!.type.should.be.equal("mediumtext");

View File

@ -13,9 +13,15 @@ export class Post {
@PrimaryColumn()
id: number;
@Column("bit")
bit: Buffer;
@Column("int")
int: number;
@Column("integer")
integer: number;
@Column("tinyint")
tinyint: number;
@ -34,9 +40,34 @@ export class Post {
@Column("double")
double: number;
@Column("double precision")
doublePrecision: number;
@Column("real")
real: number;
@Column("dec")
dec: string;
@Column("decimal")
decimal: string;
@Column("numeric")
numeric: string;
@Column("fixed")
fixed: string;
// -------------------------------------------------------------------------
// Boolean Type
// -------------------------------------------------------------------------
@Column("boolean")
boolean: boolean;
@Column("bool")
bool: boolean;
// -------------------------------------------------------------------------
// String Types
// -------------------------------------------------------------------------
@ -44,9 +75,21 @@ export class Post {
@Column("char")
char: string;
@Column("nchar")
nChar: string;
@Column("national char")
nationalChar: string;
@Column("varchar")
varchar: string;
@Column("nvarchar")
nVarchar: string;
@Column("national varchar")
nationalVarchar: string;
@Column("text")
text: string;
@ -149,4 +192,4 @@ export class Post {
@Column("simple-json")
simpleJson: { param: string };
}
}