Koa & Mongoose & Vue實現先後端分離--03鏈接數據庫

上節回顧

  • koa搭建服務端
  • 運行服務端代碼(如何在命令行中調用本地依賴包)
  • 藉助vs code調試代碼

工做內容

  • 鏈接數據庫
  • 建立數據結構

準備工做

  • 安裝robo //數據庫可視化工具
  • 安裝mongoDb //我已經安裝過了,也沒遇到什麼坑,就再也不演示了

mongoDb

  • npm i -S mongoose // 安裝mongoose
  • brew services start mongodb-community //啓動mongoDb
  • 建立目錄html

    • |-servernode

      • |-- dbmongodb

        • |--- index.js //數據庫鏈接
      • |-- model數據庫

        • |--- user.js //數據存儲結構

鏈接數據庫

這裏mongodb安裝的時候,沒有設置密碼,直接鏈接使用;npm

// 文件:db/index.js
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/demo');
const db = mongoose.connection;
// mongoose.connect鏈接mongodb,返回一個異步對象,可監聽事件;
db.on('connected', () => {
  console.log('數據庫鏈接成功');
});

db.on('error', () => {
  console.log('發生錯誤')
})
db.on('disconnected', () => {
  console.log('斷開鏈接')
})

/server目錄下,運行node db/index.js打印出「數據庫鏈接成功」便可。數據結構

優化鏈接

  • 將域名、端口、數據庫名提出
  • mongoose.connection導出
// 文件:新建/server/config/db.js
module.exports = {
  port: 27017, // 默認端口
  host: 'localhost',
  database: 'demo', // 自定義數據庫名
}
// 優化/server/bd/index.js
const mongoose = require('mongoose');
const DB = require('../config/db');

const { port, host, database } = DB;
const DB_URL = `mongodb://${host}:${port}/${database}`;
// mongoose.connect鏈接mongodb,返回一個異步對象,可監聽事件;
mongoose.connect(DB_URL);
const db = mongoose.connection;

db.on('connected', function() {
  console.log(`Mongoose connection open to ${DB_URL}`)
})
db.on('error', function(err){
  console.log(`Mongoose connection error: ${err}`)
})
db.on('disconnected', function() {
  console.log('Mongoose connection disconnected')
})

module.exports = mongoose;

建立數據結構

數據字段

// 文件:server/model/user.js
const mongoose = require('../db/index');

const Schema = mongoose.Schema;
// 定義數據結構
const userSchema = Schema({
  __v: { // __v雙下劃線,默認生成
    select: false // select:false查詢不會將該字段查出
  },
  avatar: {
    type: String
  },
  account: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true,
    select: false
  },
  alias: {
    type: String
  },
  telephone: {
    type: String,
    select: false
  },
  email: {
    type: String,
    select: false
  }
})
// 後續的增刪改查是經過導出的User Model實現。
module.exports = mongoose.model('User', userSchema);
// 文件:server/model/approve.js
const mongoose = require('../db/index');

const Schema = mongoose.Schema;

const approveSchema = Schema({
  name: {
    type: String,
    required: true
  },
  category: {
    type: String,
    required: true
  },
  description: {
    type: String,
    select: false
  },
  author: {
    type: Schema.Types.ObjectId,  // 注意做者
    ref: 'User',
    required: true
  },
  createtime: {
    type: Number,
    required: true
  },
  latesttime: {
    type: Number,
    required: true
  }
})

module.exports = mongoose.model('Approve', approveSchema);
  • Schema定義參考文檔
  • approveSchemaauthortype:Schema.Types.ObjectId, ref='User',新增審批的時候,將用戶的Id賦值給author便可,後續經過populate,能夠經過用戶Id查出用戶信息替換該Id賦值給author字段。

參考文檔

mongoose
Schema定義參考文檔
mongoose一對多關係方案app

相關文章
相關標籤/搜索