UNPKG

yekonga-server

Version:
1,552 lines (1,309 loc) 169 kB
// @ts-nocheck /* global Yekonga, serverLibrary */ // Helper functions const logger = require('./logger'); // @ts-ignore const path = serverLibrary.path; // @ts-ignore const fs = serverLibrary.fs; const sharp = serverLibrary.sharp; // @ts-ignore const moment = serverLibrary.moment; const graphql = serverLibrary.graphql; const buildSchema = serverLibrary.buildSchema; const makeExecutableSchema = serverLibrary.makeExecutableSchema; const modifyString = serverLibrary.modifyString; const pluralize = serverLibrary.pluralize; const request = serverLibrary.request; const config = Yekonga.Config; const bcrypt = serverLibrary.bcrypt; const jwt = serverLibrary.jwt; const ExcelJs = serverLibrary.ExcelJs; const io_client = serverLibrary.io_client; // @ts-ignore const CryptoJS = serverLibrary.CryptoJS; const encHex = serverLibrary.encHex; const nodemailer = serverLibrary.nodemailer; const childProcess = serverLibrary.childProcess; const sizeOf = serverLibrary.imageSize; // @ts-ignore const ObjectId = serverLibrary.mongodb.ObjectId; const dirname = serverLibrary.__dirname; const webBrowser = serverLibrary.webBrowser; const { Worker } = serverLibrary.workerThreads; const ClazzData = Yekonga.DataTypes.ClazzData; const FieldData = Yekonga.DataTypes.FieldData; const Middleware = require('./middleware'); const graphqlFields = require('./utils/graphqlFields'); const socket_url = "http://127.0.0.1:" + ((config && config.ports && config.ports.server) ? config.ports.server : 1987); const io = (io_client) ? io_client.connect(socket_url) : null; const DEFAULT_MAXBUFFER_SIZE = 20 * 1024 * 1024; const Helper = {}; Helper.ObjectId = ObjectId; Helper.ObjectIdNone = () => { return new ObjectId('000000000000000000000000'); } Helper.file_type_keys = [ 'file', 'logo', 'profile', 'document', 'picture', 'image' ]; Helper.tableActions = ['renameTable', 'dropTable', 'emptyTable']; Helper.execAsync = function(cmd, pipe, callback) { var stdout = ''; var stderr = ''; // @ts-ignore opts = { env: process.env, maxBuffer: DEFAULT_MAXBUFFER_SIZE } // @ts-ignore var c = childProcess.exec(cmd, opts, function(err) { if (callback) { // 0=OK, 1=See essue, n=Error var _err = (err) ? err.code : 1; callback(_err, stdout, stderr); } }); if (pipe) c.stdin.end(pipe); c.stdout.on('data', function(data) { stdout += data; // @ts-ignore if (!opts.silent) process.stdout.write(data); }); c.stderr.on('data', function(data) { stderr += data; // @ts-ignore if (!opts.silent) process.stderr.write(data); }); return c; } Helper.logger = function({req, res, next, path, output}) { logger({path, output})(req, res, next); } Helper.customLogger = function({filepath, requestBody, responseBody}) { try { const dir = path.dirname(filepath); var extracted = path.basename(filepath).split('.'); extracted.pop(); const extension = path.extname(filepath); const basename = extracted.join('.'); const requestStart = Date.now(); const filename = basename + `-` + moment().format('YYYY-MM-DD') + extension; const logPath = path.join(dir, filename); if (!fs.existsSync(logPath)) { Yekonga.Helper.writeFile(logPath, ''); } var loggerContent = { timestamp: Date.now(), processingTime: "not set", request: requestBody, response: responseBody, } var data = JSON.stringify(loggerContent); fs.appendFile(logPath, data + "\n", (error) => { chunks = null; loggerContent = null; }); } catch (error) { console.error('customLogger', error) } } Helper.encrypt = function(data) { var key = (Yekonga.Config.authentication && Yekonga.Config.authentication.cryptojsKey) ? Yekonga.Config.authentication.cryptojsKey : "123456789"; return CryptoJS.AES.encrypt(data, key).toString(); } Helper.decrypt = function(data) { var key = (Yekonga.Config.authentication && Yekonga.Config.authentication.cryptojsKey) ? Yekonga.Config.authentication.cryptojsKey : "123456789"; return CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8); } Helper.bcrypt = function(value) { var key = (Yekonga.Config.authentication && Yekonga.Config.authentication.saltRound) ? Yekonga.Config.authentication.saltRound : "123456789"; var salt = bcrypt.genSaltSync(key); return bcrypt.hashSync(value, salt); } Helper.md5 = function(data) { try { // CryptoJS.enc.Utf8 return CryptoJS.MD5(data).toString(); } catch (error) { console.debug("Helper.md5: data", data); console.debug("Helper.md5: error", error); } return null; } Helper.formatPhone = function(value) { if (value) { value = modifyString(value).replaceAll(' ', '') .replaceAll('-', '').replaceAll('_', '') .replaceAll('.', '').replaceAll(',', '') .toString(); if (value.substr(0, 1) == '+') { value = value.substr(1); } else if (value.substr(0, 3) == '255') { value = value; } else if (value.substr(0, 1) == '0' && value.length == 10) { value = `255${value.substr(1)}`; } else if (value.substr(0, 1) != '0' && value.length == 9) { value = `255${value}`; } if (value.length < 10 || (value.substr(0, 3) == '255' && value.length != 12)) { value = null; } } return value; } Helper.phoneFormat = Helper.formatPhone; Helper.get = async function(url, headers = {}) { var httpsAgent = null; try { httpsAgent = new serverLibrary.https.Agent({ rejectUnauthorized: false }); } catch (error) { console.warn('httpsAgent', error); } try { const res = await request.get(url, { headers, httpsAgent }); return res.data; } catch (error) { return { error: error.message, data: error.data } } } Helper.post = async function(url, body = {}, headers = {}, multipart = false) { var json = true; var formData = null; var httpsAgent = null; try { httpsAgent = new serverLibrary.https.Agent({ rejectUnauthorized: false }); } catch (error) { console.warn('httpsAgent', error); } if (multipart) { json = false; formData = new FormData(); if(body && typeof body == 'object') { for (const key in body) { if (Object.hasOwnProperty.call(body, key)) { formData.append(key, body[key]); } } } } else { formData = body; } try { const res = await request.post(url, formData, { headers, json, httpsAgent }); // console.debug(res); return (res)? res.data: res; } catch (error) { return { error: error.message, data: (error.response)? error.response.data: error.response } } } Helper.request = async function(method, {url, headers = {}, body = {}}) { if(!method) method = 'GET'; method = method.toLowerCase(); if(method == 'get') { return await this.get(url, headers); } return await this.post(url, body, headers); } Helper.download = async function(url, body = {}, headers = {}) { try { if(!headers) headers = {} headers['responseType'] = 'arraybuffer'; headers['rejectUnauthorized'] = false; const filename = this.getBasePath(`public/tmp/${this.getHexString()}`) const res = await request.get(url, { ...headers }); const fileData = Buffer.from(res.data, 'binary'); // this.writeFile(filename, fileData); return fileData; } catch (error) { return { error: error.message, data: error.data } } } Helper.downloadPost = async function(url, body = {}, headers = {}) { try { if(!headers) headers = {} // headers['responseType'] = 'blob'; headers['responseType'] = 'arraybuffer'; headers['rejectUnauthorized'] = false; const filename = this.getBasePath(`public/tmp/${this.getHexString()}`) const res = await request.post(url, body, { ...headers }); const fileData = Buffer.from(res.data, 'binary'); // this.writeFile(filename, fileData); return fileData; } catch (error) { return { error: error.message, data: error.data } } } Helper.downloadBase64Image = async function(url, headers = {}) { try { var buffer = await this.download(url, headers); // console.debug(buffer) var ext = url.split('.').pop(); if(ext == 'svg') ext = "svg+xml" return `data:image/${ext};base64,` + buffer.toString('base64'); } catch (error) { console.debug('Helper.downloadBase64Image', error); } return null; } Helper.putRequest = async function(url, body = {}, headers = {}, multipart = false) { var json = true; var formData = null; if (multipart) { json = false; formData = new FormData(); if(body && typeof body == 'object') { for (const key in body) { if (Object.hasOwnProperty.call(body, key)) { formData.append(key, body[key]); } } } } else { formData = body; } try { const res = await request.put(url, formData, { headers, json }); return (res)? res.data: res; } catch (error) { return { error: error.message, data: error.response.data } } } Helper.deleteRequest = async function(url, headers = {}) { try { const res = await request.delete(url, { headers }); return res.data; } catch (error) { return { error: error.message, data: error.response.data } } } Helper.publicDir = function() { var pub = Array.isArray(Yekonga.Config.public) ? Yekonga.Config.public[0] : Yekonga.Config.public; if (!pub) { pub = `public`; } return pub; } Helper.publicPath = function() { return this.getBasePath(this.publicDir()); } Helper.saveFile = function(data, dir = 'public/uploads') { var files = (Array.isArray(data)) ? data : ((data) ? [data] : []); var values = []; for (const file of files) { if (typeof file == 'string') { values.push(file); } else if (file) { file.fileType = file.fileType? file.fileType.toLowerCase(): ""; if ( ['url', 'link'].includes(file.fileType) || file.base64 && ( file.base64.startsWith('http://') || file.base64.startsWith('https://') ) ) { values.push(path.basename(file.fileName)); } else if (file.base64) { var filename = Helper.uuid4() + path.extname(file.fileName); var filePath = path.join(serverLibrary.__dirname, dir, filename); var base64Data = file.base64.split(',').pop(); var response = this.saveBase64(filePath, base64Data); // @ts-ignore if (!response) { values.push(filename); } else { console.error('SAVE FILE', response); } } } } return Array.isArray(data) ? values : values.pop(); } Helper.saveBase64 = function(filename, base64) { var base64Data = base64.split(',').pop(); this.createFile(filename, ''); var response = fs.writeFileSync(filename, base64Data, 'base64'); return response; } Helper.base64ToBuffer = function(base64) { var base64Data = base64.split(',').pop(); var bufferData = null; try { if (typeof Buffer.from === "function") { bufferData = Buffer.from(base64Data, 'base64'); // Ta-da } else { bufferData = new Buffer(base64Data, 'base64'); // Ta-da } } catch (error) { console.error(error) } return bufferData; } Helper.excelToCsv = async function(source) { var data = null; try { var bufferData = Buffer.from(''); if(source instanceof Buffer) { bufferData = source; } else if(fs.existsSync(source)) { bufferData = fs.readFileSync(source); } else { if(source && typeof source == 'object') { if(typeof source.base64 == 'string') { source = source.base64; } } else if(!source) { source = ''; } bufferData = Helper.base64ToBuffer(source); } const workbook = new ExcelJs.Workbook(); await workbook.xlsx.load(bufferData); const buffer = await workbook.csv.writeBuffer(); data = buffer.toString('utf-8'); } catch (error) { console.error(error); } return data; } Helper.generateFile = function(template, options) { var file = this.rootPath(template); // @ts-ignore var template = swig.compileFile(file); var output = template(options); return output; } Helper.createFile = function(file, data, isRoot = false) { return this.writeFile(file, data, isRoot); } Helper.writeFile = function(file, data, isRoot = false) { if (file) { if (isRoot) { file = path.join(serverLibrary.__dirname, file); } // @ts-ignore var dir_array = modifyString(file).replaceAll('\\', '/').splitLeft('/').filter((e) => (e != '')); var dirname = (process.platform == 'win32') ? '' : '/'; for (var i = 0; i < dir_array.length; i++) { dirname = path.join(dirname, dir_array[i], (i) ? '' : '/'); // console.debug(dirname); if (dir_array.length <= (i + 1)) { if (typeof data !== 'string' && !(data instanceof Buffer)) { data = JSON.stringify(data); } var result = fs.writeFileSync(dirname, data); // @ts-ignore if (result && result.error && result.error.code === 'EEXIST') { console.debug('file exist'); } } else { if (!fs.existsSync(dirname)) { fs.mkdirSync(dirname); } } } } return; } Helper.createFolder = function(file, isRoot = false) { return Helper.createDir(file, isRoot) } Helper.createDir = function(file, isRoot = false) { if (file && !fs.existsSync(file)) { if (isRoot) { file = path.join(serverLibrary.__dirname, file); } // @ts-ignore var dir_array = modifyString(file).replaceAll('\\', '/').splitLeft('/').filter((e) => (e != '')); var dirname = (process.platform == 'win32') ? '' : '/'; for (var i = 0; i < dir_array.length; i++) { dirname = path.join(dirname, dir_array[i], (i) ? '' : '/'); // console.debug(dirname); if (!fs.existsSync(dirname)) { fs.mkdirSync(dirname); } } } return; } Helper.getVersion = function() { var value = Yekonga.Config.version; if(!value) { value = '1.0.0'; var filename = Helper.getBasePath('version'); if(!fs.existsSync(filename)) { filename = Helper.getBasePath('.version'); } var str = Helper.readFile(filename, false); if(!Helper.isEmpty(str)) { value = str; } } return value; } Helper.decodeBase64 = function(value) { return Buffer.from(value, 'base64').toString('utf8'); } Helper.encodeBase64 = function(value) { return Buffer.from(value, 'utf8').toString('base64'); } Helper.getBase64 = function(file) { var content = null; try { content = fs.readFileSync(file, {encoding: 'base64'}); content = JSON.parse(content); } catch (error) { //console.debug(error) } return content; } Helper.getBase64Image = function(file) { var content = null; try { var imageSize = Helper.imageSize(file); content = fs.readFileSync(file, {encoding: 'base64'}); if(imageSize.type == 'svg') imageSize.type = `${imageSize.type}+xml` content = `data:image/${imageSize.type};base64,${content}`; } catch (error) { console.debug('getBase64Image', error) } return content; } Helper.readFile = function(file, logError = true) { var content = null; if(fs.existsSync(file)) { var str = fs.readFileSync(file, {encoding: 'utf8'}); try { if(!Helper.isEmpty(str)) { var d = JSON.parse(str); if(d) str = d; } } catch (error) { if(logError) console.error(error.message); } content = str; } return content; } Helper.copy = function(source, dist, root) { if (!root && !fs.existsSync(source)) { source = Helper.rootPath(source); } var source_file = source; try { if (fs.existsSync(source_file)) { var files = fs.readdirSync(source_file); if (Array.isArray(files)) { for (var i = 0; i < files.length; i++) { var filename = files[i]; // @ts-ignore var source_file = path.join(source, filename); var dist_file = path.join(dist, filename); Helper.copy(source_file, dist_file, true); } } } } catch (error) { if (fs.existsSync(source_file) && error.code === 'ENOTDIR') { var data = fs.readFileSync(source_file, {encoding: 'utf8'}); Helper.createFile(dist, data); } } } Helper.rootPath = function(filename) { var root = path.join(serverLibrary.__dirname, '../'); if (filename) { if (path.isAbsolute(filename)) { return path.join(filename); } else { return path.join(root, filename); } } return root; } Helper.realpath = function(filename) { fs.access(filename, fs.constants.F_OK, (err) => { if (err) { path.join(serverLibrary.__dirname, '../' + filename); } }); } Helper.filename = function(filename) { return path.basename(filename); } Helper.setLocation = function(location = '') { // @ts-ignore location = html_entity_decode(location); // @ts-ignore location = json_decode(location, false); // @ts-ignore if (isset(location.country)) { // @ts-ignore country_id = this.checkLocation(location.country, 'country'); } else { return null; } // @ts-ignore if (isset(location.region)) { // @ts-ignore region_id = this.checkLocation(location.region, 'region', country_id); // @ts-ignore } else { return country_id; } // @ts-ignore if (isset(location.district)) { // @ts-ignore district_id = this.checkLocation(location.district, 'district', region_id); // @ts-ignore } else { return region_id; } // @ts-ignore if (isset(location.ward)) { // @ts-ignore ward_id = this.checkLocation(location.ward, 'ward', district_id); // @ts-ignore } else { return district_id; } // @ts-ignore if (isset(location.streat)) { // @ts-ignore streat_id = this.checkLocation(location.streat, 'streat', ward_id); // @ts-ignore } else { return ward_id; } // @ts-ignore return streat_id; } Helper.checkLocation = function(name, tag, parent = null) { // @ts-ignore location = DB.table('locations') .where('name', name) .where('tag', tag) .where('name', name) .findOne(); // @ts-ignore if (is_null(location)) { // @ts-ignore location = DB.table('locations').create({ 'name': name, 'tag': tag, 'parent': parent, }); } // @ts-ignore if (location) { // @ts-ignore return location.location_id; } else { return parent; } } // @ts-ignore Helper.storeFile = async function(req, res) { var current_date = moment().format('YYYY-MM-DD hh:mm:ss'); var input = res.req.file; var media_body = {}; media_body.name = (input.originalname) ? input.originalname : ''; media_body.filename = (input.filename) ? input.filename : ''; media_body.status = (input.status) ? input.status : 0; media_body.type = (input.mimetype) ? input.mimetype : ''; media_body.size = (input.size) ? input.size : 0; media_body.created_at = current_date; media_body.updated_at = current_date; // @ts-ignore var media = await DB.table('medias') .create(media_body); var media_reference_body = {}; media_reference_body.media_id = (media) ? media.media_id : null; media_reference_body.reference_id = null; media_reference_body.type = ''; media_reference_body.status = 0; if (media.error) { console.debug(media) } else { // @ts-ignore var media_reference = await DB.table('media_references') .create(media_reference_body); if (media_reference.error) { console.debug(media_reference) } } res.send({ "media_id": media.media_id }); } Helper.setLocalAddress = async function(content) { var body = content; // console.debug(body); var db_path = path.join(serverLibrary.__dirname + '/../', config.json_db); var content = this.readFile(db_path); if (!Array.isArray(content)) { content = []; } if (body && body.account_id) { if (!Array.isArray(body.account_id)) { try { body.account_id = JSON.parse(body.account_id) } catch (error) { body.account_id = [] } } for (const account_id of body.account_id) { var exists = false; var new_account = {} var update_account = null; if (!(body.port && body.address && body.uuid && account_id)) continue; new_account.port = body.port + ""; new_account.address = body.address; new_account.account_id = account_id; new_account.uuid = body.uuid; for (let i = 0; i < content.length; i++) { var row = content[i]; if ( row.uuid == new_account.uuid && row.account_id == new_account.account_id ) { update_account = i; exists = true; } } if (exists) { content[update_account] = new_account; } else { content.push(new_account); } } } // @ts-ignore yekonga.white_file(db_path, content); return { status: 'done', data: content } } Helper.getSystemInfo = function() { // @ts-ignore var system_info = this.readFile(serverLibrary.__dirname + '/system.info'); return system_info; } Helper.extractingNestedData = function(init, parentKey, data) { if(!init) init = {}; for (const key in data) { if (Object.hasOwnProperty.call(data, key)) { const value = data[key]; const childKey = (typeof parentKey == 'string' && parentKey.trim() != '') ? `${parentKey}.${key}`: key; if(value && typeof value == 'object') { init = Helper.extractingNestedData(init, childKey, value); } else { init[childKey] = value; } } } return init; } const Objectifier = (function() { var objectifier = function(splits, create, context = {}) { var result = context; for (var i = 0, s; result && (s = splits[i]); i++) { result = (s in result ? result[s] : (create ? result[s] = {} : null)); } return result; }; return { get: function(name, create, context) { return objectifier(name.split('.'), create, context); }, exists: function(name, context) { return this.get(name, false, context) !== null; } }; })(); Helper.textTemplate = function(templateString, data, regexPattern) { data = Helper.extractingNestedData({}, null, data); if(!regexPattern) regexPattern = /{{\s*(?<name>[a-zA-Z0-9_]+)[$\s]*}}/g; var result = templateString; var match = null; do { match = regexPattern.exec(templateString); if (match) { try { if(match[1]) { // console.debug(match[1]); result = result.replace(match[0], Objectifier.get(match[1], false, data)); } } catch (error) { console.error(error); } } } while (match); return result; } Helper.uuid = function() { return serverLibrary.uuidv1(); } Helper.uuid3 = function() { // @ts-ignore return serverLibrary.uuidv3(); } Helper.uuid4 = function() { return serverLibrary.uuidv4(); } Helper.uuid5 = function() { // @ts-ignore return serverLibrary.uuidv5(); } Helper.uniqueId = function(table) { var timeObject = new Date(); var id_length = 8; var pID_a = timeObject.getMilliseconds() + table; // @ts-ignore var pID_b = Buffer(pID_a).toString('base64'); var pID_c = pID_b.toLowerCase(); if (config.database.generateID && config.database.generateIDLength) { id_length = config.database.generateIDLength; } return pID_c.substr(0, id_length); } Helper.isEmpty = function(value) { if(value === null) { return true; } else if(typeof value == 'undefined') { return true; } else if(typeof value == 'string' && value.trim() == "") { return true; } return false; } Helper.isNotEmpty = function(value) { return !(this.isEmpty(value)); } Helper.getRandomString = function(length = 16, type = 'all') { let result = ''; let chars = ''; let n = '0123456789' + Date.now(), l = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', h = 'ABCDEFabcdef'; switch (type) { case 'number': chars = n; break; case 'letter': chars = l; break; case 'hex': chars = n + h; break; default: chars = n + l; break; } for (let i = length; i > 0; --i) { if (type == 'number' && i == 0) { result += chars.substr(1)[Math.floor(Math.random() * chars.length)]; } result += chars[Math.floor(Math.random() * chars.length)]; } return result; } Helper.getRandomInt = function(length = 9) { return Helper.getRandomString(length, 'number'); } Helper.getHexString = function(length = 24) { return Helper.getRandomString(length, 'hex'); } Helper.databaseUUID = function() { var dt = new Date().getTime(); return 'xxxxxxxxxxxx4xxxfxxxxxxx'.replace(/[xy]/g, function(c) { var r = (dt + Math.random() * 16) % 16 | 0; dt = Math.floor(dt / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); } Helper.getTitle = function(value) { value = this.getUnderscore(value); if (this.isRelation(value)) { value = value.substr(0, (value.length - 3)).toString(); } value = modifyString(value).humanize().titleCase().toString(); return value; } Helper.getHeading = function(value) { return this.getSentence(value); } Helper.getSentence = function(value) { return modifyString(this.getTitle(value)).humanize().s; } Helper.getSlug = function(value) { value = this.getUnderscore(value); value = modifyString(value).slugify().toString(); return value; } Helper.getLink = function(value) { return this.getSlug(value); } Helper.getName = function(value, classVariable = false) { if (classVariable) { value = this.getClassVariable(value, false); } else { value = this.getClass(value); } return value; } Helper.getTable = function(value) { value = this.getUnderscore(value); if (this.isRelation(value)) { value = value.substr(0, (value.length - 3)).toString(); } value = pluralize.plural(value); value = modifyString(value).underscore().s return value; } Helper.getClass = function(value) { value = this.getUnderscore(value); // @ts-ignore if (this.isRelation(value, true)) { value = value.substr(0, (value.length - 3)).toString(); } if (value) { value = pluralize.singular(value); value = modifyString(value).capitalize().camelize().toString(); } // if (value == 'class') value = "ClassName"; // if (value == 'package') value = "packageName"; return value; } Helper.getClassVariable = function(value, singular = true) { value = this.getClass(value); if (singular) { value = pluralize.singular(value); } return value; } Helper.getVariable = function(value, singular = false) { var namingConvention = this.variableConvention(); if (value) { value = this.getUnderscore(value); if (value.substr(0, 1) == '_') value = value.substr(1); if (namingConvention == 'camelCase') { value = modifyString(value).camelize().toString(); } if (singular) { value = pluralize.singular(value); } } return value; } Helper.getColumn = function(value) { if (value && value != '_id') { var namingConvention = this.databaseConvention(); if (namingConvention == 'underscoreCase') { value = this.getUnderscore(value); } else if (namingConvention == 'camelCase') { value = modifyString(value).camelize().toString(); } } return value; } Helper.variableConvention = function() { return (Yekonga.Config && Yekonga.Config.namingConvention) ? Yekonga.Config.namingConvention : 'camelCase'; } Helper.databaseConvention = function() { return (Yekonga.Config && Yekonga.Config.columnNamingConvention) ? Yekonga.Config.columnNamingConvention : 'camelCase'; } Helper.formatToVariables = function(data, secondaryKey = null, collection = null) { // Yekonga.time("formatToVariables"); var values = null; var variableConvention = this.variableConvention(); var databaseConvention = this.databaseConvention(); if (variableConvention != databaseConvention) { if (Array.isArray(data)) { values = []; for (const row of data) { var _values = {} if (typeof row === 'string' || typeof row === 'number') { _values = row; } else { for (const key in row) { if (row.hasOwnProperty(key)) { var _v = this.copyJson(row[key]); if (_v && (Array.isArray(_v) || typeof _v === 'object')) { _values[this.getVariable(key)] = this.formatToVariables(_v, secondaryKey, collection); } else { _values[this.getVariable(key)] = _v; } if (key == secondaryKey) { _values['uuid'] = _v; } } } } if (secondaryKey && _values[secondaryKey]) { _values['uuid'] = _values[secondaryKey]; } if(collection) { _values.__ = { collection: collection, secondaryKey: secondaryKey, } } values.push(_values) } } else if (data) { values = {} for (const key in data) { if (data.hasOwnProperty(key)) { var _v = this.copyJson(data[key]); if (_v && (Array.isArray(_v) || typeof _v === 'object')) { values[this.getVariable(key)] = this.formatToVariables(_v, secondaryKey, collection); } else { values[this.getVariable(key)] = _v; } if (key == secondaryKey) { values['uuid'] = _v; } } } } } else { values = data; if (values && secondaryKey) { if (Array.isArray(values)) { for (let i = 0; i < values.length; i++) { if (values[i][secondaryKey]) { values[i]['uuid'] = values[i][secondaryKey]; } if(collection) { values[i].__ = { collection: collection, secondaryKey: secondaryKey, } } } } else if (values[secondaryKey]) { values['uuid'] = values[secondaryKey]; if(collection) { values.__ = { collection: collection, secondaryKey: secondaryKey, } } } } } // Yekonga.timeEnd("formatToVariables"); return values } Helper.formatToColumn = function(data) { var values = null; if (Array.isArray(data)) { values = []; for (const row of data) { if (typeof row === 'string') { values.push(this.getColumn(row)); } else { var _values = {} for (const key in row) { if (key == '_id') { _values[key] = row[key]; } else if (row.hasOwnProperty(key)) { _values[this.getColumn(key)] = row[key]; } } values.push(_values); } } } else if (data) { values = {}; for (const key in data) { if (key == '_id') { values[key] = data[key]; } else if (data.hasOwnProperty(key)) { values[this.getColumn(key)] = data[key]; } } } return values; } Helper.getDefaultValues = function(validFields) { var values = {}; if (Array.isArray(validFields)) { for (const key of validFields) { if(typeof key == 'string') { values[key] = null; } else if(typeof key == 'object' && key && key.key) { values[key.key] = Helper.getDefaultValues(key); } } } else if (validFields) { for (const key in validFields) { if (validFields.hasOwnProperty(key)) { const field = validFields[key]; var dv = field.default; if (typeof dv == 'undefined') { const type = (field.type) ? field.type.toLowerCase() : null; switch (type) { case "datetime": case "timestamp": dv = this.getIsoTimestamp(); break; case "date": dv = this.getIsoTimestamp(); break; case "time": dv = this.getIsoTimestamp(); break; case "number": dv = 0; break; case "array": dv = []; break; case "object": dv = {}; break; case "boolean": dv = false; break; default: dv = null; break; } } else if (dv == "now") { dv = this.getIsoTimestamp(); } values[key] = dv; } } } return values; } Helper.getParentRelativeName = function(collection, secondaryKey, foreignKey) { var classVariable = this.getVariable(this.getName(foreignKey)); var testKey = this.getUnderscore(foreignKey); if(testKey && testKey.endsWith('_ids')) { classVariable = this.getVariable(this.getName(testKey.substring(0, (testKey.length - 4)))); } if (classVariable == pluralize.singular(foreignKey)) { classVariable = this.getVariable(`${this.getUnderscore(foreignKey)}_info`); if(foreignKey != secondaryKey) { classVariable = this.getVariable(`${this.getUnderscore(foreignKey)}_info`); } } return classVariable; } Helper.getChildRelativeName = function(collection, secondaryKey, foreignKey) { var classVariable = this.getVariable(this.toPlural(`${collection}`)); if (foreignKey != secondaryKey) { var foreignName = this.getVariable(this.getClass(`${this.toSingular(foreignKey)}`)); foreignName = (classVariable.startsWith(foreignName)) ? "" : foreignName; classVariable = this.getVariable(`${foreignName}_${classVariable}`); } return classVariable; } Helper.getValidFields = function({ input, validFields, isCreate = false, fileFields = [], relations = [], request = {}, secondaryKey = null, }) { request = request || {}; const _yekonga = request.Yekonga; const Auth = (_yekonga)? _yekonga.Auth: {}; var values = []; var _data = (Array.isArray(input)) ? input : [input]; var defaultValues = (isCreate) ? this.getDefaultValues(validFields) : {}; if(!Array.isArray(fileFields)) fileFields = []; for (const row of _data) { var _value = {}; if (isCreate) { if (Array.isArray(validFields)) { for (const key of validFields) { var val = row[key]; if(validFields[key].type && validFields[key].type.toLowerCase() == 'id') { if(val) { _value[key] = new ObjectId(val); } else { if(key == secondaryKey) { _value[key] = new ObjectId(Helper.getHexString(24)); } } } else if (this.isColumnUrl(key) || fileFields.includes(key)) { _value[key] = this.saveFile(val); } else if (val && validFields[key].type && validFields[key].type.toLowerCase() == 'date') { _value[key] = this.getLocalTimestamp(val); } else if (key == 'password') { _value[key] = this.bcryptPassword((typeof val == 'undefined') ? this.getRandomString() : val); } else if (key == 'phone') { _value[key] = this.formatPhone(val); } else { _value[key] = (typeof val == 'undefined') ? defaultValues[key] : val; } if (key == 'profileId' && !_value[key]) { if (Auth && Auth.profileId) { _value[key] = new ObjectId(Auth.profileId); } else { _value[key] = (_value[key]) ? _value[key] : null; } } if ( Array.isArray(relations) && relations.includes(key) && _value[key] ) { if( typeof _value[key] == 'string' && _value[key].length == 24 ) { _value[key] = new ObjectId(_value[key]) } else if(Array.isArray(_value[key])) { for (let i = 0; i < _value[key].length; i++) { if( typeof _value[key][i] == 'string' && _value[key][i].length == 24 ) { _value[key][i] = new ObjectId(_value[key][i]) } } } } } } else if (validFields && typeof validFields == 'object') { for (const key in validFields) { // if (key == '_id') continue; if (validFields.hasOwnProperty(key)) { var val = row[key]; if(validFields[key].type && validFields[key].type.toLowerCase() == 'id') { if(val) { _value[key] = new ObjectId(val); } else { if(key == secondaryKey) { _value[key] = new ObjectId(Helper.getHexString(24)); } } } else if (this.isColumnUrl(key) || fileFields.includes(key)) { _value[key] = this.saveFile(val); } else if (val && validFields[key].type && validFields[key].type.toLowerCase() == 'date') { _value[key] = this.getLocalTimestamp(val); } else if (key == 'password') { _value[key] = this.bcryptPassword((typeof val == 'undefined') ? this.getRandomString() : val); } else if (key == 'phone') { _value[key] = this.formatPhone(val); } else { _value[key] = (typeof val == 'undefined') ? defaultValues[key] : val; } if (key == 'profileId' && !_value[key]) { if (Auth && Auth.profileId) { _value[key] = new ObjectId(Auth.profileId); } else { _value[key] = (_value[key]) ? _value[key] : null; } } if ( Array.isArray(relations) && relations.includes(key) && _value[key] ) { if ( typeof _value[key] == 'string' && _value[key].length == 24 ) { _value[key] = new ObjectId(_value[key]) } else if (Array.isArray(_value[key])) { for (let i = 0; i < _value[key].length; i++) { if ( typeof _value[key][i] == 'string' && _value[key][i].length == 24 ) { _value[key][i] = new ObjectId(_value[key][i]) } } } } } } } } else { for (const key in row) { if(key === secondaryKey) continue; // console.debug(`key: ${key}, value: ${row[key]}, hasOwnProperty: ${row.hasOwnProperty(key)}`); if (row.hasOwnProperty(key)) { if (Array.isArray(validFields) && validFields.includes(key)) { if (this.isColumnUrl(key) || fileFields.includes(key)) { _value[key] = this.saveFile(row[key]); } else if (row[key] && validFields[key].type && validFields[key].type.toLowerCase() == 'date') { _value[key] = this.getLocalTimestamp(row[key]); } else if (key == 'password') { _value[key] = this.bcryptPassword((typeof row[key] == 'undefined') ? this.getRandomString() : row[key]); } else if (key == 'phone') { _value[key] = this.formatPhone(row[key]); } else { _value[key] = row[key]; } if ( Array.isArray(relations) && relations.includes(key) && _value[key] ) { if ( typeof _value[key] == 'string' && _value[key].length == 24 ) { _value[key] = new ObjectId(_value[key]) } else if (Array.isArray(_value[key])) { for (let i = 0; i < _value[key].length; i++) { if ( typeof _value[key][i] == 'string' && _value[key][i].length == 24 ) { _value[key][i] = new ObjectId(_value[key][i]) } } } } } else if (validFields && validFields[key]) { if (this.isColumnUrl(key) || fileFields.includes(key)) { _value[key] = this.saveFile(row[key]); } else if (validFields[key].type && validFields[key].type.toLowerCase() == 'date') { _value[key] = this.getLocalTimestamp(row[key]); } else if (key == 'password') { _value[key] = this.bcryptPassword((typeof row[key] == 'undefined') ? this.getRandomString() : row[key]); } else if (key == 'phone') { _value[key] = this.formatPhone(row[key]); } else { _value[key] = row[key]; } if ( Array.isArray(relations) && relations.includes(key) && _value[key] ) { if ( typeof _value[key] == 'string' && _value[key].length == 24 ) { _value[key] = new ObjectId(_value[key]) } else if (Array.isArray(_value[key])) { for (let i = 0; i < _value[key].length; i++) { if ( typeof _value[key][i] == 'string' && _value[key][i].length == 24 ) { _value[key][i] = new ObjectId(_value[key][i]) } } } } } } } } values.push(_value); } return values; } Helpe