koa --- [MVC实现之五]Model层的实现
生活随笔
收集整理的這篇文章主要介紹了
koa --- [MVC实现之五]Model层的实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
說明
- 上一篇: MVC實(shí)現(xiàn)之四
- 這一篇主要介紹:
- 項(xiàng)目中用到的Sequelize庫中的一些方法,參考使用Sequelize連接mysql
- 將Model層加入Mar類中
Service層
- 還是從業(yè)務(wù)出發(fā),Service層是調(diào)用方,調(diào)用方式和Controller層調(diào)用Service層一樣
Mar類
- 設(shè)計(jì)原則是,在不同層之間傳遞參數(shù),通過Mar類.
- 故需要在Mar類中掛載Model
- 改寫Mar類如下
Model類 - 初始化
- 新建Mar類如下
- 此時(shí)啟動服務(wù)器,訪問http://localhost:3000
Model類-操作數(shù)據(jù)庫
- 準(zhǔn)備數(shù)據(jù)庫,按照docker-compose.yml構(gòu)建容器.
- 瀏覽器打開管理頁面
注: 密碼是example - 新建數(shù)據(jù)庫 marron
- 準(zhǔn)備就緒后,連接數(shù)據(jù)庫,并測試
連接數(shù)據(jù)庫
- Sequelize庫提供的接口如下
- 寫到Model類里面
看到這句話時(shí),說明數(shù)據(jù)庫連接成功了
表模型
- sequelize提供的接口如下
- 加到Model層的addTableUser中
- 然后在Model的構(gòu)造函數(shù)中調(diào)用一次
插入一條數(shù)據(jù)到表中
- 首先要有User表的結(jié)構(gòu)
- 暫時(shí)寫在Model類的User方法中
- 寫添加user的方法
- 寫在add屬性下面
然后在constructor里面運(yùn)行一次this.add.user({name:'marron'})
查詢數(shù)據(jù)
class Model {find = {user: async () => {const User = this.User();return await User.findAll();}} }給Service提供服務(wù)
- 前面完成了連接數(shù)據(jù)庫、創(chuàng)建表、往表內(nèi)插入數(shù)據(jù)、查詢數(shù)據(jù)的接口
- 下面就是在Model中寫一個(gè)index方法,給Service層中l(wèi)et data = awati model.index(); 提供服務(wù)
總代碼如下
const koa = require('koa'); const koaRouter = require('koa-router');class Mar {constructor(conf) {this.$app = new koa(conf); // 相當(dāng)于koa的實(shí)例this.$router = new koaRouter(); // 相當(dāng)于koa-router的實(shí)例this.model = new Model(this);this.service = new Service(this);this.controller = new Controller(this);this.router = new Router(this);}listen(port) {this.$app.listen(port, async () => {console.log(`[mar]Server is running at http://localhost:${port}`);})} }class Router {constructor(app) {const { controller, $router, $app } = app;$router.get('/', controller.index);$app.use($router.routes());} } class Controller {constructor(app) {const { service } = app;console.log('Controller:', service.test());Controller.prototype.service = service;}test() {return 'Controller for Router'}async index(ctx) {const service = Controller.prototype.service;ctx.body = await service.index();} } class Service {constructor(app) {const { model } = app;Service.prototype.model = model;}test() {return 'Service for Controller'}async index() {const model = Service.prototype.model;let data = await model.index();data.age = '20';data.remarks = 'forever 18';return data;} }const Sequelize = require('sequelize');class Model {constructor(app) {this.connect();}// 給其他層的測試函數(shù)test() {return "Model for Service"}// 連接數(shù)據(jù)庫的函數(shù)connect() {this.sequelize = new Sequelize('marron', 'root', 'example', {host: 'localhost',dialect: 'mysql'})}// 測試是否連接成功testSQL() {this.sequelize.authenticate().then(() => {console.log('Connect has been established successfully.');}).catch(err => {console.error('Unable to connect to the database:', err);});}async index() {return await this.find.user()}addTable = {user: async () => {const User = this.sequelize.define('user', {// 屬性name: {type: Sequelize.STRING,allowNull: false,},date: {type: Sequelize.DATE,defaultValue: Sequelize.NOW}});User.sync({ force: true })}}User() {return this.sequelize.define('user', {// 屬性name: {type: Sequelize.STRING,allowNull: false,},date: {type: Sequelize.DATE,defaultValue: Sequelize.NOW}});}add = {user: async (person) => {const User = this.User();User.create(person);// 同步新增用戶this.sequelize.sync();}}find = {user: async () => {const User = this.User();return await User.findAll();}} }module.exports = Mar;總結(jié)
以上是生活随笔為你收集整理的koa --- [MVC实现之五]Model层的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python读取npy文件 mse_py
- 下一篇: node --- 模拟事件的异步