资讯专栏INFORMATION COLUMN

ThinkJS2 mongoose 使用

Jeff / 1891人阅读

摘要:根据李成银大大的文章示例想做的使用,发现有部分问题直接使用创建,每次创建实例导致重新创建新的连接,导致数据库连接无限上涨将缓存使用,导致的复写将这两个问题修复后,代码如下不过修改,自动编译不会的不会直接修复,需要重启服务生效创建连接连接数据

根据李成银大大的文章示例想做ThinkJS的mongoose使用,发现有部分问题:
1、mongoose直接使用createConnection创建,每次Model创建实例导致重新创建新的连接,导致数据库连接无限上涨;
2、将connection缓存使用,导致mongoose的Model复写;

将这两个问题修复后,代码如下:
不过修改schema,自动编译不会mongoose的Model不会直接修复,需要重启服务生效;

"use strict";

let mongoose = require("mongoose");

let conn = null;
/**
 * model
 */
export default class extends think.base {
  /**
   * schema
   * @type {Object}
   */
  schema = {};
  /**
   * model instance
   * @type {[type]}
   */
  _model = null;

  /**
   * constructor
   * @param  {[type]} name   [description]
   * @param  {Object} config [description]
   * @return {[type]}        [description]
   */
  constructor(name, config = {}) {
    super();
    if (think.isObject(name)) {
      config = name;
      name = "";
    }
    this.name = name;
    this.config = think.parseConfig(config);
  }

  /**
   * 创建连接
   * @return {[type]} [description]
   */
  getConnection() {
    if (conn) {
      return conn;
    }
    let user = "";
    if (this.config.user) {
      user = this.config.user + ":" + this.config.password + "@";
    }
    let host = this.config.host || "127.0.0.1";
    let port = this.config.port || 27017;
    let str = `mongodb://${user}${host}:${port}/${this.config.database}`;
    conn = mongoose.createConnection(str);
    conn.on("connected", function (err) {
      if (err) {
        think.log("连接数据库失败:" + err);
      } else {
        think.log("连接数据库成功!");
      }
    });
    mongoose.Promise = Promise;
    return conn;
  }

  /**
   * 获取 Mongoose 里的 Model
   * @return {[type]} [description]
   */
  getModel() {
    if (!this._model) {
      let connection = this.getConnection();
      if(this.name in connection.models){
        this._model = connection.model(this.name);
      }else{
        this._model = connection.model(this.name, new mongoose.Schema(this.schema));
      }
    }
    return this._model;
  }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/90101.html

相关文章

  • 在Node中基于Mongoose对MongoDB进行增删查改(CRUD)操作(一)

    摘要:如图连接成功后,显示你的数据库,在这个节目可以对数据库进行操作。如图安装与加载首先假定你已经安装了,命令行工具输入在使用的文件中即可。创建读取更新删除单值读取上文是在中基于对进行增删查改操作的简单介绍,以后会有进阶的文章。 关键词:mongodb安装 mongoose使用 robomongo mongoose的CRUD操作 mongoose的查询,增加,修改,删除 工具介绍 Mon...

    lemon 评论0 收藏0
  • 在Node中基于Mongoose对MongoDB进行增删查改(CRUD)操作(一)

    摘要:如图连接成功后,显示你的数据库,在这个节目可以对数据库进行操作。如图安装与加载首先假定你已经安装了,命令行工具输入在使用的文件中即可。创建读取更新删除单值读取上文是在中基于对进行增删查改操作的简单介绍,以后会有进阶的文章。 关键词:mongodb安装 mongoose使用 robomongo mongoose的CRUD操作 mongoose的查询,增加,修改,删除 工具介绍 Mon...

    SillyMonkey 评论0 收藏0
  • mongodb数据库的使用

    最近在学习node,所以听说node和mongodb更配哦。。所以我就来学习mongodb了showImg(https://segmentfault.com/img/remote/1460000006818697); 一、mongodb的开启和关闭 1. 查找mongod是否可用 which mongod 2. 启动mongodb 指定path 和log日志 mongod --dbpath /...

    刘玉平 评论0 收藏0

发表评论

0条评论

Jeff

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<