'use strict'; const _ = require('lodash'); const { columnType } = require('./options'); module.exports = (data) => { let { name, name_zh, columns } = data; const configArr = []; const updateArr = []; // 将插件和正常字段分开 let pluginColumns = columns.filter((f) => { const r = columnType.find((cf) => cf.value === f.type && cf.plugins); if (r) return true; return false; }); let pluginHead = ''; let pluginArea = ''; for (const p of pluginColumns) { const { title, type, required = false, remark, index, def, zh, ref, getProp } = p; const params = _.pick(p, ['zh', 'required', 'def']); params.key = title; const r = columnType.find((cf) => cf.value === type); if (!r) continue; const { path } = r; if (path) { const pluginStr = `const ${type}Plugin = require('${path}');\n`; if (pluginHead.indexOf(pluginStr) <= -1) pluginHead += pluginStr; pluginArea += `schema.plugin(${type}Plugin(${JSON.stringify(params)}));\n`; configArr.push(`'${title}'`); updateArr.push(`'${title}'`); } } columns = columns.filter((f) => { const r = columnType.find((cf) => cf.value === f.type && cf.plugins); if (r) return false; return true; }); let columnStr = `const ${name} = { \n`; let indexStr = ''; let has_ObjectId = false; let has_Secret = false; let configSearchStr = ''; for (let i = 0; i < columns.length; i++) { const { title, type, required = false, remark, index, def, zh, ref, getProp } = columns[i]; let refStr; if (ref) { const r = columns.find((f) => f.title === ref); refStr = `${r ? 'refPath' : 'ref'}: '${ref}'`; if (getProp) { const propList = getProp.split(';'); let propStr = '['; for (const p of propList) { if (p !== _.head(propList)) propStr = `${propStr}, `; propStr = `${propStr}'${p}'`; } propStr += ']'; refStr += `, getProp: ${propStr}`; } } const str = ` ${title}: { type: ${type || 'String'}, required: ${required} ${type === 'Secret' ? ', select: false' : ''} ${def ? `, default: '${def}'` : ''}${ zh ? `, zh: '${zh}'` : '' }${refStr ? `,${refStr}` : ''} }, // ${remark || ''} \n`; columnStr += str; configArr.push(`'${required ? '!' : ''}${title}'`); updateArr.push(`'${title}'`); if (index) { indexStr += `schema.index({ '${title}': 1 });\n`; configSearchStr += `'${title}': '${title}' ,\n`; } if (!has_ObjectId && type === 'ObjectId') has_ObjectId = true; else if (!has_Secret && type === 'Secret') has_Secret = true; } columnStr += '}'; const nameList = name.split('_'); const modelName = nameList.map((i) => _.upperFirst(i)).join('_'); return `'use strict'; const Schema = require('mongoose').Schema; const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin'); ${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''} ${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''} ${pluginHead} // ${name_zh || ''} ${columnStr} const schema = new Schema(${name}, { toJSON: { getters: true, virtuals: true } }); schema.index({ id: 1 }); schema.index({ 'meta.createdAt': 1 }); ${indexStr} schema.plugin(metaPlugin); ${pluginArea} module.exports = app => { const { mongoose } = app; return mongoose.model('${modelName}', schema, '${name}'); }; module.exports = { create: { requestBody: [${configArr.join(',')}], }, destroy: { params: ["!id"], service: "delete", }, update: { params: ["!id"], requestBody: [${updateArr.join(',')}], }, show: { parameters: { params: ["!id"], }, service: "fetch", }, index: { parameters: { query: { "meta.createdAt@start": "meta.createdAt@start", "meta.createdAt@end": "meta.createdAt@end", ${configSearchStr} }, // options: { // "meta.state": 0 // 默认条件 // }, }, service: "query", options: { query: ["skip", "limit"], sort: ["meta.createdAt"], desc: true, count: true, }, }, }; `; };