Deno MySQL 增删查改接口

萝らか妹 提交于 2020-08-15 23:30:23

视频演示

https://www.bilibili.com/video/BV1BT4y1E7Nh/?p=9

我们一起来完成以下步骤:

  1. 使用之前工程的代码

  2. 创建数据库脚本

  3. 创建数据库连接

  4. 新增员工model

  5. 创建员工仓库类repositories

  6. 创建员工services类

  7. 添加增删查改代码

#controllers/employeeController.ts


import { Context } from "https://deno.land/x/oak/mod.ts";
import { Employee } from "../models/employee.ts";
import employeeService  from "../services/employeeService.ts";

const { cwd } = Deno;  class Controller {  static async hello(ctx: any){  //cwd获取当前工程目录   //注意 ' !== `  ctx.render(`${cwd()}/views/index.ejs`,{  title:"Testing",  data:{name:" Deepincoding"}  });  }   static async save(ctx: Context){  const bodyValue = await ctx.request.body();  let employee:Employee={  id: 0,  name: bodyValue.value.name,  age: bodyValue.value.age  }   const savedEmployee = await employeeService.save(employee);  console.log("savedEmployee:"+savedEmployee);  ctx.response.body = savedEmployee;   }   static async update(ctx: Context){  var bodyValue = await ctx.request.body();  let updateEmployee: Employee ={  id:bodyValue.value.id,  name:bodyValue.value.name,  age:bodyValue.value.age  }  const updatedEmployee = await employeeService.update(updateEmployee);  ctx.response.body=updatedEmployee;  }   static async findById(ctx: any){  const id = ctx.params.id;  console.log("id:"+id);  const employee = await employeeService.findById(id);  ctx.response.body=employee;  }   static async findAll(ctx: any){  const employees = await employeeService.findAll();  ctx.response.body=employees;  }   static async deleteById(ctx: any){  const id = ctx.params.id;  const success = await employeeService.deleteById(id);  ctx.response.body=success;  }   }  export default Controller;  

#database/database.ts

import { Client } from "https://deno.land/x/mysql/mod.ts"

const client = await new Client().connect({
        hostname: "127.0.0.1",
        username: "root",
 db: "deno",  poolSize: 3,  password: "123456" });  export default client; 

#models/employee.ts

export interface Employee{
    id: number,
    name: string,
    age: number
}
 

#router/employeeRouter.ts

import { Router } from "https://deno.land/x/oak/mod.ts";
import Controller from "../controllers/employeeController.ts";

const router = new Router();

router.get("/",Controller.hello);  router.post("/save",Controller.save); router.put("/update",Controller.update); router.get("/findById/:id",Controller.findById); router.get("/findAll",Controller.findAll); router.delete("/deleteById/:id",Controller.deleteById);   export default router; 

#services/employeeService.ts

import { Employee } from  "../models/employee.ts";
import emlpoyeeRepo from "../repositories/employeeRepo.ts";

class employeeService{
    static async save(employee: Employee){
 return emlpoyeeRepo.save(employee);  }   static async update(employee: Employee){  return emlpoyeeRepo.update(employee);  }  static async findById(id: number){  return emlpoyeeRepo.findById(id);  }  static async findAll(){  return emlpoyeeRepo.findAll();  }   static async deleteById(id: number){  return emlpoyeeRepo.deleteById(id);  }   }  export default employeeService;   

#main.ts



import { Application } from "https://deno.land/x/oak/mod.ts"
import {viewEngine,engineFactory,adapterFactory} from "https://deno.land/x/view_engine/mod.ts";
import router from "./routers/employeeRouter.ts";
 const ejsEngine = engineFactory.getEjsEngine(); const oakAdapter = adapterFactory.getOakAdapter();  const app = new Application(); app.use(viewEngine(oakAdapter,ejsEngine)); app.use(router.routes()); app.use(router.allowedMethods());  console.log("Server Port 8000");  await app.listen({port: 8000 })    

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!