koa --- [MVC实现之五]Model层的实现
生活随笔
收集整理的這篇文章主要介紹了
koa --- [MVC实现之五]Model层的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明
- 上一篇: MVC實現之四
- 這一篇主要介紹:
- 項目中用到的Sequelize庫中的一些方法,參考使用Sequelize連接mysql
- 將Model層加入Mar類中
Service層
- 還是從業務出發,Service層是調用方,調用方式和Controller層調用Service層一樣
Mar類
- 設計原則是,在不同層之間傳遞參數,通過Mar類.
- 故需要在Mar類中掛載Model
- 改寫Mar類如下
Model類 - 初始化
- 新建Mar類如下
- 此時啟動服務器,訪問http://localhost:3000
Model類-操作數據庫
- 準備數據庫,按照docker-compose.yml構建容器.
- 瀏覽器打開管理頁面
注: 密碼是example - 新建數據庫 marron
- 準備就緒后,連接數據庫,并測試
連接數據庫
- Sequelize庫提供的接口如下
- 寫到Model類里面
看到這句話時,說明數據庫連接成功了
表模型
- sequelize提供的接口如下
- 加到Model層的addTableUser中
- 然后在Model的構造函數中調用一次
插入一條數據到表中
- 首先要有User表的結構
- 暫時寫在Model類的User方法中
- 寫添加user的方法
- 寫在add屬性下面
然后在constructor里面運行一次this.add.user({name:'marron'})
查詢數據
class Model {find = {user: async () => {const User = this.User();return await User.findAll();}} }給Service提供服務
- 前面完成了連接數據庫、創建表、往表內插入數據、查詢數據的接口
- 下面就是在Model中寫一個index方法,給Service層中let data = awati model.index(); 提供服務
總代碼如下
const koa = require('koa'); const koaRouter = require('koa-router');class Mar {constructor(conf) {this.$app = new koa(conf); // 相當于koa的實例this.$router = new koaRouter(); // 相當于koa-router的實例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();}// 給其他層的測試函數test() {return "Model for Service"}// 連接數據庫的函數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;總結
以上是生活随笔為你收集整理的koa --- [MVC实现之五]Model层的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python读取npy文件 mse_py
- 下一篇: c++代码整洁之道pdf_别再问如何用P