DenoMySQlORM

编程

视频演示

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

一起来完成以下步骤:

  1. 创建数据库连接

  2. 创建Model,也就是实体类

  3. 写新增、修改、查询和删除

  4. 运行:deno run --allow-net --allow-read --allow-write main.ts

#controllers/employeeController.ts

//引入Context

import { Context } from"https://deno.land/x/oak/mod.ts";

//引入仓库类

import employeeRepo from"../repositories/employeeRepo.ts";

//获取当前目录方法const { cwd } = Deno; class Controller { staticasync hello(ctx: any) { //cwd获取当前工程目录 //注意 " !== ` ctx.render(`${cwd()}/views/index.ejs`, { title: "Testing", data: { name: "www.deepincoding.com" } }); } //保存staticasync save(ctx: Context) { const {value} = await ctx.request.body(); const result = await employeeRepo.save(value); ctx.response.body = result; } //获取所有staticasync findAll(ctx: Context){ const result = await employeeRepo.findAll(); ctx.response.body = result; } //更新staticasync update(ctx: Context) { const {value} = await ctx.request.body(); const result = await employeeRepo.update(value); ctx.response.body = result; } //根据ID获取staticasync findById(ctx: any){ const id = ctx.params.id; const result = await employeeRepo.findById(id); ctx.response.body = result; } //根据ID删除staticasync deleteById(ctx: any){ const id = ctx.params.id; const result = await employeeRepo.deleteById(id); ctx.response.body = result; } } exportdefault Controller;

#database/database.ts

import { DataTypes, Database, Model } from"https://deno.land/x/denodb/mod.ts";

//MySQL连接

const mysqldb = new Database("mysql", {

host: "127.0.0.1",

username: "root", password: "123456", database: "deno"}); //导出exportdefault mysqldb;

#models/employeeModel.ts

import { DataTypes, Database, Model } from"https://deno.land/x/denodb/mod.ts";

//员工实体

class EmployeeModel extends Model {

//表名static table = "employee"; //字段static fields = { id: { primaryKey: true, autoIncrement: true }, name: DataTypes.STRING, age: DataTypes.INTEGER }; } exportdefault EmployeeModel;

#repositories/employeeRepo.ts

//引入EmployeeModel实体

import EmployeeModel from"../models/employeeModel.ts";

import mysqldb from"../database/database.ts";

class employeeRepo{ //初始化数据库staticasync init(){ mysqldb.link([EmployeeModel]); await mysqldb.sync({ drop: false }); } //新增staticasync save (employee: any){ return EmployeeModel.create(employee); } //获取所有staticasync findAll(){ return EmployeeModel.all(); } //更新staticasync update (employee: any){ return EmployeeModel.where("id",employee.id).update(employee); } //根据ID查询staticasync findById(id: number){ return EmployeeModel.where("id",id).get(); } //删除staticasync deleteById(id: number){ return EmployeeModel.deleteById(id); } } exportdefault employeeRepo;

#routers/employeeRouter.ts

//引入Router

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

//引入自定义Controller

import Controller from"../controllers/employeeController.ts";

//创建routerconst router = new Router(); router.get("/",Controller.hello); router.post("/save",Controller.save); router.get("/findAll",Controller.findAll); router.put("/update",Controller.update); router.get("/findById/:id",Controller.findById); router.delete("/deleteById/:id",Controller.deleteById); exportdefault router;

#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";

import employeeRepo from"./repositories/employeeRepo.ts";

//初始化数据库await employeeRepo.init(); //创建相关页面引擎对象const ejsEngine = engineFactory.getEjsEngine(); const oakAdapter = adapterFactory.getOakAdapter(); //创建appconst app = new Application(); //应用页面引擎app.use(viewEngine(oakAdapter,ejsEngine)); //应用路由app.use(router.routes()); app.use(router.allowedMethods()); //log 日志console.log("Server Port 8000"); //端口await app.listen({port: 8000 })

 

以上是 DenoMySQlORM 的全部内容, 来源链接: utcz.com/z/517758.html

回到顶部