midway icon indicating copy to clipboard operation
midway copied to clipboard

midway+mongoose+typegoose下,不支持多数据库

Open mumudev opened this issue 4 years ago • 15 comments

这是midway mongo的文档。 image

mumudev avatar Feb 03 '21 11:02 mumudev

我这边提供一个思路:先将propModel转为Schema,再通过数据库连接对象创建mongooseModel,再转为typegooseModel

feiyueXH avatar Mar 15 '21 09:03 feiyueXH

image image

feiyueXH avatar Mar 15 '21 09:03 feiyueXH

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

developeryvan avatar Apr 17 '21 03:04 developeryvan

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

joyexpr avatar Apr 24 '21 13:04 joyexpr

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

https://github.com/developeryvan/midwayjs-crud/blob/master/src/core/base_service.ts 可以参考下这里的写法

developeryvan avatar Apr 25 '21 08:04 developeryvan

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

https://github.com/developeryvan/midwayjs-crud/blob/master/src/core/base_service.ts 可以参考下这里的写法

谢谢,不错的方案

joyexpr avatar Apr 25 '21 10:04 joyexpr

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

可以启动的时候指定环境变量。

liuwensa avatar Apr 28 '21 11:04 liuwensa

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

可以启动的时候指定环境变量。

然后呢,怎么同步变动装饰器里写死的mongodb conn url ?

joyexpr avatar Apr 28 '21 12:04 joyexpr

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

可以启动的时候指定环境变量。

然后呢,怎么同步变动装饰器里写死的mongodb conn url ?

@modelOptions({ existingConnection: mongoose.createConnection( process.env.mongourl, { useNewUrlParser: true, useUnifiedTopology: true } ), })

这个环境变量在启动的时候可以指定,如果docker部署更好指定了

liuwensa avatar May 08 '21 01:05 liuwensa

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

可以启动的时候指定环境变量。

然后呢,怎么同步变动装饰器里写死的mongodb conn url ?

@modeloptions({ existingConnection: mongoose.createConnection( process.env.mongourl, { useNewUrlParser: true, useUnifiedTopology: true } ), })

这个环境变量在启动的时候可以指定,如果docker部署更好指定了

我们的具体配置在配置中心(如zk,apollo)里,不会注入到环境变量中

joyexpr avatar May 08 '21 01:05 joyexpr

import { modelOptions, mongoose, prop } from '@typegoose/typegoose';

@modelOptions({
  existingConnection: mongoose.createConnection(
    'mongodb+srv://cluster0.hy9wo.mongodb.net/',
    { useNewUrlParser: true, useUnifiedTopology: true }
  ),
})
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

不同环境的配置是不同的,在装饰器里写死不是太可行

可以启动的时候指定环境变量。

然后呢,怎么同步变动装饰器里写死的mongodb conn url ?

@modeloptions({ existingConnection: mongoose.createConnection( process.env.mongourl, { useNewUrlParser: true, useUnifiedTopology: true } ), }) 这个环境变量在启动的时候可以指定,如果docker部署更好指定了

我们的具体配置在配置中心(如zk,apollo)里,不会注入到环境变量中

有用nodejs 做服务端的配置中心吗?

developeryvan avatar May 08 '21 01:05 developeryvan

有用nodejs 做服务端的配置中心吗?

抱歉,不是太明白你的意思,你之前上面提供的base_service.ts方案应该是可以解决mongoose多连接问题的,虽然可能不是最理想简洁的,不过也有好处,这样可以控制开发者不在controller里访问数据库,只在service里访问数据库

我设想的比较理想的使用方式,希望可以支持类似下面使用模式:

// 支持多连接配置
export const mongoose = {
	clients: {
		default: {
			uri: '',
			options: {}
		},

		db2: {
			uri: '',
			options: {}
		}
	}
}
 
// model希望有一个类似MongooseConnection注解,如不配置即使用default连接
@MongooseConnection('db2')    
@ModelOptions({ schemaOptions: { collection: 'user', timestamps: true })
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

// service里可以直接注入userModel
@Provide()
export class UserService {

	@Inject
	uesrModel;
	
}

joyexpr avatar May 08 '21 02:05 joyexpr

有用nodejs 做服务端的配置中心吗?

抱歉,不是太明白你的意思,你之前上面提供的base_service.ts方案应该是可以解决mongoose多连接问题的,虽然可能不是最理想简洁的,不过也有好处,这样可以控制开发者不在controller里访问数据库,只在service里访问数据库

我设想的比较理想的使用方式,希望可以支持类似下面使用模式:

// 支持多连接配置
export const mongoose = {
	clients: {
		default: {
			uri: '',
			options: {}
		},

		db2: {
			uri: '',
			options: {}
		}
	}
}
 
// model希望有一个类似MongooseConnection注解,如不配置即使用default连接
@MongooseConnection('db2')    
@ModelOptions({ schemaOptions: { collection: 'user', timestamps: true })
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

// service里可以直接注入userModel
@Provide()
export class UserService {

	@Inject
	uesrModel;
	
}

这2天了解了下IOC的东西,新版本已经加上你想要的动能了。

developeryvan avatar May 30 '21 07:05 developeryvan

有用nodejs 做服务端的配置中心吗?

抱歉,不是太明白你的意思,你之前上面提供的base_service.ts方案应该是可以解决mongoose多连接问题的,虽然可能不是最理想简洁的,不过也有好处,这样可以控制开发者不在controller里访问数据库,只在service里访问数据库 我设想的比较理想的使用方式,希望可以支持类似下面使用模式:

// 支持多连接配置
export const mongoose = {
	clients: {
		default: {
			uri: '',
			options: {}
		},

		db2: {
			uri: '',
			options: {}
		}
	}
}
 
// model希望有一个类似MongooseConnection注解,如不配置即使用default连接
@MongooseConnection('db2')    
@ModelOptions({ schemaOptions: { collection: 'user', timestamps: true })
export class User {
  @prop() public name?: string;
  @prop({ type: () => [String] }) public jobs?: string[];
}

// service里可以直接注入userModel
@Provide()
export class UserService {

	@Inject
	uesrModel;
	
}

这2天了解了下IOC的东西,新版本已经加上你想要的动能了。

谢谢,厉害,我学习下。 另外,建议能不能提pr给官方的typegoose组件呢? 这样也能促进框架的发展~~

https://www.yuque.com/midwayjs/midway_v2/mongo 官方实现里目前看主要2点不好用,1是多库,2是在service使用model时不能直接注入还需要每次在init方法里初始化。官方文档里也写着正在跟进多库的解决方案。

@czy88840616 小挺建议能不能也关注评估下呢

joyexpr avatar May 30 '21 14:05 joyexpr

我一直在关注的😄

czy88840616 avatar May 31 '21 03:05 czy88840616

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Aug 21 '23 06:08 github-actions[bot]