@nyffels/mynodeorm
Advanced tools
Comparing version 1.0.0-alpha118 to 1.0.0-alpha119
@@ -65,2 +65,3 @@ import { propertyType } from "./property.models.js"; | ||
}; | ||
getSqlType(): string; | ||
primary(): this; | ||
@@ -67,0 +68,0 @@ getPrimary(): boolean; |
@@ -112,3 +112,3 @@ import { Factory } from "./factory.models.js"; | ||
schema[table.getDbName()].columns[column.getDbName()] = { | ||
type: column.getType().type, | ||
type: column.getSqlType(), | ||
primary: column.getPrimary(), | ||
@@ -188,2 +188,104 @@ nullable: column.getNullable(), | ||
} | ||
getSqlType() { | ||
var _a, _b; | ||
try { | ||
const type = (_a = this._type.type) !== null && _a !== void 0 ? _a : "string"; | ||
let length = this._type.length; | ||
switch (type) { | ||
case "string": { | ||
const strLength = +(length !== null && length !== void 0 ? length : "255"); | ||
if (Number.isNaN(strLength)) { | ||
console.error(`❌ Could not parse type length in property '${this.getDbName()}'.`); | ||
process.exit(1); | ||
} | ||
if (strLength <= 0) { | ||
console.error(`❌ Length cannot be lesser than 1 for type string in property '${this.getDbName()}'.`); | ||
process.exit(1); | ||
} | ||
if (strLength > 65500) { | ||
return "LONGTEXT"; | ||
} | ||
else { | ||
return `VARCHAR(${strLength})`; | ||
} | ||
} | ||
case "bigstring": { | ||
return "LONGTEXT"; | ||
} | ||
case "guid": { | ||
return "VARCHAR(36)"; | ||
} | ||
case "number": { | ||
const lengths = (_b = (length !== null && length !== void 0 ? length : "255").split('.')) !== null && _b !== void 0 ? _b : []; | ||
if (lengths.length > 1) { | ||
// DECIMAL | ||
// @ts-ignore | ||
const intLength = +lengths[0]; | ||
// @ts-ignore | ||
const decimalLength = +lengths[1]; | ||
if (Number.isNaN(intLength)) { | ||
console.error(`❌ Could not parse type length (size) in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
if (intLength <= 0) { | ||
console.error(`❌ Length cannot be lesser than 1 for type number (size) in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
if (intLength > 65) { | ||
console.error(`❌ Maximum length for type number (size) is 65 in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
if (Number.isNaN(decimalLength)) { | ||
console.error(`❌ Could not parse type length (decimal) in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
if (decimalLength <= 0) { | ||
console.error(`❌ Length cannot be lesser than 1 for type number (decimal) in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
if (decimalLength > 30) { | ||
console.error(`❌ Maximum length for type number (decimal) is 30 in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
return `DECIMAL(${intLength}, ${decimalLength})`; | ||
} | ||
else { | ||
// @ts-ignore | ||
const intLength = +lengths[0]; | ||
if (Number.isNaN(intLength)) { | ||
console.error(`❌ Could not parse type length in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
if (intLength <= 0) { | ||
console.error(`❌ Length cannot be lesser than 1 for type number in property '${this._dbName}'.`); | ||
process.exit(1); | ||
} | ||
return intLength === 255 ? `INT` : `INT(${intLength})`; | ||
} | ||
} | ||
case "bignumber": { | ||
return "BIGINT"; | ||
} | ||
case "boolean": { | ||
return "TINYINT"; | ||
} | ||
case "date": { | ||
return "DATE"; | ||
} | ||
case "time": { | ||
return "TIME"; | ||
} | ||
case "datetime": { | ||
return "DATETIME"; | ||
} | ||
default: { | ||
console.error(`❌ MyNodeORM type '${type}' given in property '${this._dbName}' is not known to MyNodeORM.'`); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
catch (ex) { | ||
return "VARCHAR(255)"; // Default to VARCHAR max length | ||
} | ||
} | ||
primary() { | ||
@@ -190,0 +292,0 @@ this._primary = true; |
{ | ||
"name": "@nyffels/mynodeorm", | ||
"version": "1.0.0-alpha118", | ||
"version": "1.0.0-alpha119", | ||
"description": "A full-fledged ORM framework for NodeJS and MySQL with develop friendly code aimed to handle database migrations, MySQL Query builder / helper and property mapping.", | ||
@@ -5,0 +5,0 @@ "private": false, |
Sorry, the diff of this file is not supported yet
189672
2621