UNPKG

venom-generator

Version:

In the way of code generation, complete the storage model->computation model (business API) transformation to achieve the goal of design and implementation

111 lines 3.54 kB
"use strict"; // 工具模块 Object.defineProperty(exports, "__esModule", { value: true }); // 需要导入参数解析[import{xxx} from nexus模块] function improtAgrs(category, args) { const mutation = /Mutation/.test(category); let argType = ''; if (args === undefined || args.length === 0) return mutation ? 'arg,' : argType; args.forEach(a => { const argArr = a.split(':'); if (argArr.length == 2) { // 是否必传,此处dataType必须这样写,不可替换成'' let dataType = trim(argArr[1], '!', ''); const databaseType = basicDataType(dataType); if (databaseType) { argType += `${initialCase(dataType)}Arg,`; } else { argType += 'arg,'; } } }); return argType; } exports.improtAgrs = improtAgrs; // aud拥有者权限解析 function authAud(str, args) { const audArr = str.split('_'); const len = audArr.length; // console.log('len---->', len) const argArr = args.split(':'); const arg = underlineToCamel(argArr[0]); const databaseType = basicDataType(argArr[1]); if (len === 1 || !databaseType) { return { param: `args as any`, external: false }; } let aud = '{ where: {'; for (let i = 0; i < len; ++i) { if (i + 1 === len) { aud += `${audArr[i]}: args.${arg}`; } else { aud += `${audArr[i]}: {`; } } audArr.forEach(_a => { aud += ` }`; }); return { param: `${aud} }`, external: true }; } exports.authAud = authAud; // 字符串处理 function trim(str, char, type) { if (char) { if (type == 'left') { return str.replace(new RegExp(`^\\${char}+`, 'g'), ''); } else if (type == 'right') { return str.replace(new RegExp(`\\${char}+$`, 'g'), ''); } return str.replace(new RegExp(`^\\${char}+|\\${char}+$`, 'g'), ''); } return str.replace(/^\s+|\s+$/g, ''); } exports.trim = trim; // 基本数据类型判断 function basicDataType(data) { if (data == undefined) return true; data = trim(data, '!', ''); return data === 'Boolean' || data === 'DateTime' || data === 'Float' || data === 'ID' || data === 'Int' || data === 'String' ? true : false; } exports.basicDataType = basicDataType; // 首字母转小写 function initialCase(str) { if (str === undefined || str === '') return ''; return str.replace(str[0], str[0].toLocaleLowerCase()); } exports.initialCase = initialCase; // 首字母转大写 function initialUpperCase(str) { if (str === undefined || str === '') return ''; return str.replace(str[0], str[0].toLocaleUpperCase()); } exports.initialUpperCase = initialUpperCase; // 下划线转驼峰 function underlineToCamel(str) { if (str === undefined || str === '') return ''; return str.replace(/\_(\w)/g, function (_all, letter) { return letter.toUpperCase(); }); } exports.underlineToCamel = underlineToCamel; // 数组去重 function delRepetitionArr(arr) { var result = []; var obj = {}; for (var i = 0; i < arr.length; i++) { if (!obj[arr[i]]) { result.push(arr[i]); obj[arr[i]] = arr[i]; } } return result; } exports.delRepetitionArr = delRepetitionArr; //# sourceMappingURL=util.js.map