Deno MongoDB 增删查改 接口

你。 提交于 2020-08-09 14:42:28

#Deno MongoDB 增删查改 接口

视频演示: https://www.bilibili.com/video/BV1BT4y1E7Nh/?p=10

一起来完成以下步骤:

  1. 使用上个工程代码(09.mysql增删查改工程)。 如果你没有看上个视频,建议看一下。 本节将不在重复的之前的内容和代码

  2. 修改employee 实体类,把ID删除掉

  3. 新增mongodb 数据库连接

  4. 修改仓库类的增删查改(controller到service层基本上不用修改代码)

  5. 测试

  6. 运行命令 deno run --allow-net --allow-write --allow-read --allow-plugin --unstable main.ts

#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 = {  name: bodyValue.value.name,  age: bodyValue.value.age,  };  const result = await employeeService.save(employee);  console.log("insert employee:" + result);  ctx.response.body = result;  }   static async findAll(ctx: any) {  const employees = await employeeService.findAll();  ctx.response.body = employees;  }   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 update(ctx: any) {  //获取更新ID  const id = ctx.params.id;  //更新对象放在Body 里面  var bodyValue = await ctx.request.body();  let updateEmployee: Employee = {  name: bodyValue.value.name,  age: bodyValue.value.age,  };  const updatedEmployee = await employeeService.update(id, updateEmployee);  ctx.response.body = updatedEmployee;  }   static async deleteById(ctx: any) {  const id = ctx.params.id;  const result = await employeeService.deleteById(id);  ctx.response.body = result;  } }  export default Controller;  

#database/database.ts

import { MongoClient } from "https://deno.land/x/mongo@v0.8.0/mod.ts";

const client = new MongoClient();
client.connectWithUri("mongodb://localhost:27017");

const db = client.database("test"); export const employees = db.collection("employee");  

#models/employee.ts

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

#routers/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.get("/findAll",Controller.findAll); router.get("/findById/:id",Controller.findById); router.put("/update/:id",Controller.update); 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 findAll() {  return emlpoyeeRepo.findAll();  }  static async findById(id: string) {  return emlpoyeeRepo.findById(id);  }   static async update(id: string, employee: Employee) {  return emlpoyeeRepo.update(id, employee);  }  static async deleteById(id: string) {  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 })  

 

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