- 在编写nodejs后端时,我发现一些模板文件是十分通用的,所以想着由机器帮我生成简单的模板文件。
- 利用的是nodejs的fs能力,代码如下
-
const fs = require("fs"); const path = require("path"); const { humpTransformLine } = require(path.join(__dirname, "/js/data.js")); function createModelsFile(name) { // 首字母转化为大写 const capitalName = name.replace(/^(\w)/, (node, key) => { return key.toUpperCase(); }), interfaceName = `I${capitalName}`, tableName = humpTransformLine(name); // 没有文件件就创建文件夹 if (!fs.existsSync(path.join(`../models/${name}`))) { fs.mkdirSync(path.join(`../models/${name}`)); } // 如果文件已经有,就不进行操作了 if (fs.existsSync(path.join(`../models/${name}/index.ts`))) { return; } fs.writeFile(path.join(`../models/${name}/index.ts`), `import { modelExecute } from "../../utils/request"; import { setUpdateSqlByForm } from "../../utils/data"; interface ${interfaceName} {} /** * 查询 * @returns */ export async function query${capitalName}(): Promise<${interfaceName}[]> { const list = await modelExecute("select * from ${tableName}", {}) as ${interfaceName}[]; return list; } /** * 根据id去查询 * @returns */ export async function query${capitalName}ById(id: number | string): Promise<${interfaceName}> { const result = await modelExecute("select * from ${tableName} where id = ?", [ id ]) as [${interfaceName} | null]; return result; } /** * 增加 * @returns */ export async function add${capitalName}(${name}: ${interfaceName}): Promise<boolean> { const result = await modelExecute("insert into ${tableName} set ?", ${name}) as boolean; return result; } /** * 修改 * @returns */ export async function update${capitalName}(setForm: any, whereForm: any): Promise<boolean> { const { setSql, whereSql, paramList } = setUpdateSqlByForm(setForm, whereForm); const result = await modelExecute("update ${tableName}" + setSql + whereSql, paramList) as boolean; return result; } /** * 删除 * @returns */ export async function del${capitalName}(id: string | number): Promise<boolean> { const result = await modelExecute("delete from ${tableName} where id = ?", [id]) as boolean; return result; } `, (err) => { if (err) { console.error(err); } }); } createModelsFile("screen");
-
成功创建
-
模板文件
import { modelExecute } from "../../utils/request"; import { setUpdateSqlByForm } from "../../utils/data"; interface IScreen2 {} /** * 查询 * @returns */ export async function queryScreen2(): Promise<IScreen2[]> { const list = await modelExecute("select * from screen2", {}) as IScreen2[]; return list; } /** * 根据id去查询 * @returns */ export async function queryScreen2ById(id: number | string): Promise<IScreen2> { const result = await modelExecute("select * from screen2 where id = ?", [ id ]) as [IScreen2 | null]; return result; } /** * 增加 * @returns */ export async function addScreen2(screen2: IScreen2): Promise<boolean> { const result = await modelExecute("insert into screen2 set ?", screen2) as boolean; return result; } /** * 修改 * @returns */ export async function updateScreen2(setForm: any, whereForm: any): Promise<boolean> { const { setSql, whereSql, paramList } = setUpdateSqlByForm(setForm, whereForm); const result = await modelExecute("update screen2" + setSql + whereSql, paramList) as boolean; return result; } /** * 删除 * @returns */ export async function delScreen2(id: string | number): Promise<boolean> { const result = await modelExecute("delete from screen2 where id = ?", [id]) as boolean; return result; }