UNPKG

@odyssoft/tsorm

Version:
115 lines (114 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseValue = exports.parseOptions = exports.operator = exports.mapKey = exports.getInsertValues = exports.getInsertKeys = exports.getIdKey = exports.formatValue = void 0; const functions_1 = require("./functions"); const formatValue = (input, keys) => { if (typeof input === 'undefined' || input === null) { return 'NULL'; } if (typeof input === 'number') { return input; } if (input === true || input === false) { return input ? 1 : 0; } if ((0, functions_1.isFunction)(input)) { return input; } let output = `${input}`.replace(/'/g, "\\'"); output = `'${output}'`; if (keys && input && keys.includes(input)) { output = input; } return output; }; exports.formatValue = formatValue; const getIdKey = (keys) => { let id = ''; for (const key in keys) { if (key === 'id' || key.startsWith('id') || key.endsWith('id')) { if (keys[key].primaryKey) { id = key; break; } } if (keys[key].primaryKey) { id = key; break; } } return id; }; exports.getIdKey = getIdKey; const getInsertKeys = (data) => { const keys = []; Array.isArray(data) ? data.forEach((d) => { // @ts-ignore Object.keys(d).forEach((key) => { if (!keys.includes(key)) { keys.push(key); } }); }) : // @ts-ignore keys.push(...Object.keys(data)); return keys; }; exports.getInsertKeys = getInsertKeys; const getInsertValues = (data, keys) => { const sql = ['VALUES']; const rows = data; if (Array.isArray(rows)) { const insert = []; rows.forEach((row) => insert.push(`(${keys.map((k) => (0, exports.formatValue)(row[k])).join(', ')})`)); sql.push(insert.join(', ')); } else { sql.push(`(${keys.map((k) => (0, exports.formatValue)(rows[k])).join(', ')})`); } return sql.join(' '); }; exports.getInsertValues = getInsertValues; const mapKey = (key, options) => { const tableKey = [`\`${key}\` ${options.type}`]; options.autoIncrement && tableKey.push('AUTO_INCREMENT'); // options.primaryKey && tableKey.push('PRIMARY KEY') options.required && tableKey.push('NOT NULL'); options.default !== undefined && tableKey.push(`DEFAULT ${options.default}`); return tableKey.join(' '); }; exports.mapKey = mapKey; const operator = (key, keys) => ({ $between: ({ max, min }) => `${key} BETWEEN ${(0, exports.formatValue)(min, keys)} AND ${(0, exports.formatValue)(max, keys)}`, $equals: (input) => `${key} = ${(0, exports.formatValue)(input, keys)}`, $greaterThan: (input) => `${key} > ${(0, exports.formatValue)(input, keys)}`, $greaterThanEqual: (input) => `${key} >= ${(0, exports.formatValue)(input, keys)}`, $in: (input) => `${key} IN (${input.map((i) => (0, exports.formatValue)(i)).join(', ')})`, $lessThan: (input) => `${key} < ${(0, exports.formatValue)(input, keys)}`, $lessThanEqual: (input) => `${key} <= ${(0, exports.formatValue)(input, keys)}`, $like: (input) => `${key} LIKE ${(0, exports.formatValue)(input)}`, $notBetween: ({ max, min }) => `${key} NOT BETWEEN ${(0, exports.formatValue)(min)} AND ${(0, exports.formatValue)(max)}`, $notEquals: (input) => `${key} != ${(0, exports.formatValue)(input)}`, $notIn: (input) => `${key} NOT IN (${input.map((i) => (0, exports.formatValue)(i)).join(', ')})`, $notLike: (input) => `${key} NOT LIKE ${(0, exports.formatValue)(input)}`, }); exports.operator = operator; const parseOptions = (options, keys) => `${Object.keys(options) .map((key) => key === '$or' ? `(${options[key].map((value) => (0, exports.parseOptions)(value, keys)).join(' OR ')})` : (0, exports.parseValue)(key, options[key], keys)) .join(' AND ')}`; exports.parseOptions = parseOptions; const parseValue = (key, value, keys) => { if (value === null) { return `${key} IS NULL`; } if (typeof value !== 'object') { return `${key} = ${(0, exports.formatValue)(value, keys)}`; } const Operator = (0, exports.operator)(key, keys); const [id] = Object.keys(value); return Operator[id](value[id]); }; exports.parseValue = parseValue;