krislee94 / docs

日常文档整理
1 stars 0 forks source link

egg的mongoose连接配置以及多连 #28

Open krislee94 opened 4 years ago

krislee94 commented 4 years ago

egg的多mongoose连接配置

背景


egg在使用sequelize的时候,不能同时使用egg-mongoose,当然也是有解决的办法的,egg的原则是约定高于配置。所以当同时使用的时候,egg 默认会读取app/model 下的一些操作。这个时候需要改变,mysql的配置,baseDir 默认路径读取 更改。如果不更改操作,直接使用mongoose呢?



直接使用mongoose


模仿egg-mongoose ,将mongoose绑定在上下文上。通过this.ctx.app.mongoose来读取操作。

/app.ts

//app.ts 

import mongoService from './app/lib/mongoose';

//绑定Application放在 mongoose的index文件上

export default (app:Application) => {

  app.beforeStart(()=>{
    mongoService(app);
  });

}


/app/lib/mongoose

import mongoose from 'mongoose';

export default (app:Application) => {
     mongoose.connect(app.config.mongoose.client.url, {
     useNewUrlParser: true,
     autoIndex: false,
     ...app.config.mongoose.client.options,
   });
   mongoose.Promise = global.Promise;
   mongoose.set('useFindAndModify', false); 
   app.mongoose = mongoose;

}

/app/lib/mongoose/model

import mongoose, { Schema, Model, Document } from 'mongoose';

// copy from http://git.dev.qianmi.com/x-site/x-site/blob/master/packages/x-site-core-db/src/dao/pageInfo/page-info-model.ts
export const SystemCodeList = ['d2cStore', 'd2p', 'goodsCenter', 'memberHomePage', 'shopHomePage', 'cashRegister'];
export const PlatformList = ['pc', 'weixin', 'app', 'pad', 'saleMachine', 'adScreen', 'orderMachine', 'cashRegister'];

interface IConfigOrder {
  widgetNameSpace: string;
  children: { props: any; widgetNameSpace: string }[];
}

 export interface IPageInfo extends Document {
  uid: string;
  systemCode: string;
  envCode: string;
  tplCode: string;
  platform: string;
  pageType: string;
  pageCode: string;
  status: string;
  updateTime: string;
  activated: boolean;
  shareInfo: { imgSrc: String; title: String; desc: String }; //分享信息
  isDoublePlatformTpl: boolean;
  pageStyle: string;
  title: string;
  skinType: String; //一键换肤 皮肤标记
  headLv: number;
  footLv: number;
  configOrder: IConfigOrder;
  tempConfigOrder: IConfigOrder;
}

export interface IPageInfoModel<T extends Document> extends Model<T> {}

export const pageInfoSchema = new Schema({
  uid: { type: String, required: true }, //用户ID
  systemCode: { type: String, required: true, enum: SystemCodeList }, //业务线
  envCode: { type: String, required: true },
  tplCode: { type: String, required: true }, //页面所用模板
  platform: { type: String, required: true, enum: PlatformList }, //页面所属平台
  pageType: { type: String, required: true }, //页面类型
  pageCode: { type: String, required: true }, //页面code
  status: { type: String, required: true, enum: ['published', 'notpublished'] }, //页面状态
  updateTime: { type: Date, required: true }, //页面更新时间
  activated: { type: Boolean }, //是否激活
  shareInfo: { imgSrc: String, title: String, desc: String }, //分享信息
  isDoublePlatformTpl: { type: Boolean }, //是否是双平台
  pageStyle: { type: String }, //页面风格
  title: { type: String, default: '页面标题' }, //页面标题
  skinType: String, //一键换肤 皮肤标记
  headLv: { type: Number, enum: [0, 1, 2, 3] }, //头部显示级别
  footLv: { type: Number, enum: [0, 1, 2, 3] }, //页脚显示级别
  configOrder: { type: mongoose.Schema.Types.Mixed, default: null }, //页面配置清单
  tempConfigOrder: { type: mongoose.Schema.Types.Mixed, required: true }, //页面配置清单 临时
});

 pageInfoSchema.set('toJSON', { getters: true, virtuals: true });

 export default mongoose.model<IPageInfo>('PageInfo', pageInfoSchema) as IPageInfoModel<IPageInfo>;


这样在使用的时候就可以直接使用app.mongoose.PageInfo.findOne等api了。


如果现在需求是要连接多个呢?


使用 mongoose.createConnect来连接多个数据库

具体需要更改index.ts文件即可

import mongoose from 'mongoose';
import { Application } from 'egg';
import { TemlateInfoSchema, ESCTemplate ,ITemlateInfoModel} from './model/temlate';
import {pageInfoSchema ,IPageInfoModel,IPageInfo} from './model/pageInfo';
// egg-mongoose跟egg-sequelize存在兼容问题,这里使用model方式加载mongoose
export default (app: Application) => {
  // mongoose.connect(app.config.mongoose.client.url, {
  //   useNewUrlParser: true,
  //   autoIndex: false,
  //   ...app.config.mongoose.client.options,
  // });
  // mongoose.Promise = global.Promise;
  // mongoose.set('useFindAndModify', false); 
  // app.mongoose = mongoose;
  mongoose.set('useFindAndModify', false); 
  mongoose.Promise = global.Promise;

  var con1 = mongoose.createConnection(app.config.mongoose.client.url, {
      useNewUrlParser: true,
      autoIndex: false,
      ...app.config.mongoose.client.options,
  });

  pageInfoSchema.set('toJSON', { getters: true, virtuals: true });
  app.mongooseXsite = con1.model('PageInfo',pageInfoSchema) as IPageInfoModel<IPageInfo>;

  var con2 = mongoose.createConnection(app.config.mongoose.clientCash.url, {
    useNewUrlParser: true,
    autoIndex: false,
    ...app.config.mongoose.clientCash.options,
  });
   //设置toJSON
   TemlateInfoSchema.set('toJSON', { getters: true, virtuals: true });
   app.mongooseTempLate = con2.model('TemlateInfo',TemlateInfoSchema) as ITemlateInfoModel<ESCTemplate>;

};



这样在使用的时候,我们只需要使用上下文, this.ctx.app.mongooseTempLate 来访问api了。