12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 'use strict';
- const _ = require('lodash');
- module.exports = (data) => {
- const { name, name_zh, columns } = data;
- let columnStr = `const ${name} = { \n`;
- let indexStr = '';
- let has_ObjectId = false;
- let has_Secret = false;
- const configArr = [];
- let configSearchStr = '';
- for (let i = 0; i < columns.length; i++) {
- const { title, type, required = false, remark, index, def } = columns[i];
- const str = ` ${title}: { type: ${type || 'String'}, required: ${required} ${type === 'Secret' ? ', select: false' : ''} ${def ? `, default: '${def}'` : ''}${
- remark ? `, zh: '${remark}'` : ''
- } }, // ${remark || ''} \n`;
- columnStr += str;
- configArr.push(`'${required ? '!' : ''}${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) => _.capitalize(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');" : ''}
- // ${name_zh || ''}
- ${columnStr}
- const schema = new Schema(${name}, { toJSON: { virtuals: true } });
- schema.index({ id: 1 });
- schema.index({ 'meta.createdAt': 1 });
- ${indexStr}
- schema.plugin(metaPlugin);
- 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: [${configArr.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,
- },
- },
- };
- `;
- };
|