Remove quotes for defaults that are keywords

This commit is contained in:
1ilit 2024-04-24 12:19:36 +03:00
parent 2751a6d6df
commit 583138949b
2 changed files with 14 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { sqlDataTypes } from "../data/constants"; import { sqlDataTypes } from "../data/constants";
import { isFunction, strHasQuotes } from "./utils"; import { isFunction, isKeyword, strHasQuotes } from "./utils";
export function getJsonType(f) { export function getJsonType(f) {
if (!sqlDataTypes.includes(f.type)) { if (!sqlDataTypes.includes(f.type)) {
@ -144,13 +144,16 @@ export function hasQuotes(type) {
} }
export function parseDefault(field) { export function parseDefault(field) {
if (strHasQuotes(field.default) || isFunction(field.default)) { if (
strHasQuotes(field.default) ||
isFunction(field.default) ||
isKeyword(field.default) ||
!hasQuotes(field.type)
) {
return field.default; return field.default;
} }
return hasQuotes(field.type) && field.default.toLowerCase() !== "null" return `'${field.default}'`;
? `'${field.default}'`
: `${field.default}`;
} }
export function jsonToMySQL(obj) { export function jsonToMySQL(obj) {

View File

@ -25,6 +25,12 @@ export function strHasQuotes(str) {
); );
} }
const keywords = ["CURRENT_TIMESTAMP", "NULL"];
export function isKeyword(str) {
return keywords.includes(str.toUpperCase());
}
export function isFunction(str) { export function isFunction(str) {
return /\w+\([^)]*\)$/.test(str); return /\w+\([^)]*\)$/.test(str);
} }