UNPKG

silvie

Version:

Typescript Back-end Framework

146 lines (144 loc) 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class Column { constructor(name, type, size, options = {}) { _defineProperty(this, "name", void 0); _defineProperty(this, "type", void 0); _defineProperty(this, "size", void 0); _defineProperty(this, "options", void 0); this.name = name; this.type = type; this.size = size; this.options = { primary: false, index: false, fullTextIndex: false, spatialIndex: false, unique: false, defaultValue: undefined, autoIncrement: false, nullable: false, unsigned: false, charset: null, collation: null, useCurrent: false, ...options }; } static fromQuery(query, types = {}) { let type; let size; let params = []; const [, name] = query.match(/^`(.+)`/); let [, sqlType] = query.match(/^`.+` (.+\(.+\))/) || []; if (!sqlType) { [, sqlType] = query.match(/^`.+` ([^\s]+)/); type = sqlType; } else { let paramsStr; [type, paramsStr] = sqlType.split('(', 2); // eslint-disable-next-line no-eval params = eval(`[${paramsStr.substring(0, paramsStr.length - 1)}]`); } type = types[type]; if (!['Enum', 'Set', 'Decimal'].includes(type)) { [size] = params; } const column = new Column(name, type, size); if (type === 'Decimal') { column.meta({ precision: params[0], scale: params[1] || 0 }); } if (type === 'Enum' || type === 'Set') { column.meta({ values: params }); } const [, charset] = query.match(/CHARACTER SET ([^\s]+)/i) || []; if (charset) { column.charset(charset); } const [, collation] = query.match(/COLLATE ([^\s]+)/i) || []; if (collation) { column.collate(collation); } const [, defaultValue] = query.match(/DEFAULT '(.+)'/i) || query.match(/DEFAULT ([^\s]+)/i) || []; if (defaultValue) { if (defaultValue === 'NULL') { column.default(null); } else if (['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'].includes(defaultValue)) { column.useCurrent(); } else { column.default(defaultValue); } } if (!query.includes('NOT NULL')) { column.nullable(); } if (query.includes('AUTO_INCREMENT')) { column.autoIncrement(); } return column; } meta(metaData) { this.options.meta = metaData; return this; } autoIncrement() { this.options.autoIncrement = true; return this; } nullable() { this.options.nullable = true; return this; } default(value) { this.options.defaultValue = value; return this; } useCurrent() { this.options.useCurrent = true; return this; } unsigned() { this.options.unsigned = true; return this; } primary() { this.options.primary = true; return this; } unique() { this.options.unique = true; return this; } index(name = '') { this.options.index = name || true; return this; } fullTextIndex(name = '') { this.options.fullTextIndex = name || true; return this; } spatialIndex(name = '') { this.options.spatialIndex = name || true; return this; } charset(charset) { this.options.charset = charset; return this; } collate(collation) { this.options.collation = collation; return this; } } exports.default = Column;