egg學習筆記第二十九天:eggjs中使用mongoose

1、egg中使用Mongoose

一、在egg項目中安裝mongoosejavascript

cnpm i mongoose --save

二、 在config>plugin.js配置egg-mongoose插件java

exports.mongoose = {
  enable: true,
  package: "egg-mongoose"
};

三、在config>config.default.js配置數據庫鏈接地址mongodb

config.mongoose = {
    client: {
      // url: "mongodb://eggadmin:123456@localhost:27017/eggcms", //有用戶名密碼的狀況
      url: "mongodb://127.0.0.1/eggcms", //無用戶名密碼的狀況
      options: {}
    }
  };

四、在app文件夾下新建model文件夾,裏面放數據庫表模型food,食物表。(food表)數據庫

module.exports = app => {
  const mongoose = app.mongoose; // 引入創建鏈接的mongoose
  const Schema = mongoose.Schema;

  // 數據庫表映射
  const FoodSchema = new Schema({
    foodName: { type: String }, // 食物名稱
    foodPrice: { type: Number }, // 食物價格(斤)
    foodColor: { type: String }, // 食物顏色
    foodSeason: { type: String } // 食物季節
  });

  return mongoose.model("Food", FoodSchema, "food");
};

五、定義兩個路由一個查詢食物表數據,一個增長食物表數據。npm

"use strict";

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get("/", controller.home.index);

  router.get("/findFood", controller.home.findFood);

  router.get("/addFood", controller.home.addFood);
};

六、controller中的方法:app

"use strict";

const Controller = require("egg").Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = "hi, egg";
  }
  // 查詢食物表
  async findFood() {
    const { ctx } = this;
    var result = await ctx.service.food.getFoodList();
    console.log(result);
    ctx.body = result;
  }
  // 增長食物表
  async addFood() {
    const { ctx } = this;
    var food = new ctx.model.Food({
      foodName: "炒米飯", // 食物名稱
      foodPrice: 12, // 食物價格(斤)
      foodColor: "白", // 食物顏色
      foodSeason: "四季" // 食物季節
    });
    var result = await food.save();
    ctx.body = result;
  }
}

module.exports = HomeController;

七、service中新建food.jsasync

const Service = require("egg").Service;

class FoodService extends Service {
  async getFoodList() {
    // 查詢food表中的數據
    return await this.ctx.model.Food.find({});
  }
}

module.exports = FoodService;

八、連續訪問addFood可得響應:mongoose

 

九、訪問findFood路徑,能夠查出比表中數據:ui

 

十、在定義一個刪除,一個修改的路由this

"use strict";

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get("/", controller.home.index);

  router.get("/findFood", controller.home.findFood);

  router.get("/addFood", controller.home.addFood);

  router.get("/editFood", controller.home.editFood);

  router.get("/deleteFood", controller.home.deleteFood);
};

十一、控制器寫入:

"use strict";

const Controller = require("egg").Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = "hi, egg";
  }
  // 查詢食物表
  async findFood() {
    const { ctx } = this;
    var result = await ctx.service.food.getFoodList();
    console.log(result);
    ctx.body = result;
  }
  // 增長食物表
  async addFood() {
    const { ctx } = this;
    var food = new ctx.model.Food({
      foodName: "炒拉條", // 食物名稱
      foodPrice: 22, // 食物價格(斤)
      foodColor: "黃", // 食物顏色
      foodSeason: "四季" // 食物季節
    });
    var result = await food.save();
    ctx.body = result;
  }
  // 增長食物表
  async editFood() {
    const { ctx } = this;
    var result = await this.ctx.model.Food.updateOne(
      {
        foodName: "炒拉條"
      },
      {
        foodName: "蘭州拉麪", // 食物名稱
        foodPrice: 8, // 食物價格(斤)
        foodColor: "白", // 食物顏色
        foodSeason: "夏天" // 食物季節
      },
      function(err, result) {}
    );
    ctx.body = result;
  }
  // 增長食物表
  async deleteFood() {
    const { ctx } = this;
    var result = await this.ctx.model.Food.deleteOne({
      foodName: "蘭州拉麪"
    });
    ctx.body = result;
  }
}

module.exports = HomeController;

十二、執行editFood可得: 炒拉條變成了蘭州拉麪

 

1三、訪問deleteFood得: 蘭州拉麪那條數據刪除成功

 

以上是使用egg-mongoose實現數據增刪改查的簡單demo.

相關文章
相關標籤/搜索