@midwayjs/mongoose 3.3.5 → 3.4.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
1
  import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
2
+ import { MongooseDataSourceManager } from './manager';
2
3
  export declare class MongooseConfiguration implements ILifeCycle {
4
+ mongooseDataSourceManager: MongooseDataSourceManager;
3
5
  onReady(container: IMidwayContainer): Promise<void>;
4
6
  onStop(container: IMidwayContainer): Promise<void>;
5
7
  }
@@ -11,11 +11,10 @@ const decorator_1 = require("@midwayjs/decorator");
11
11
  const manager_1 = require("./manager");
12
12
  let MongooseConfiguration = class MongooseConfiguration {
13
13
  async onReady(container) {
14
- await container.getAsync(manager_1.MongooseConnectionServiceFactory);
14
+ this.mongooseDataSourceManager = await container.getAsync(manager_1.MongooseDataSourceManager);
15
15
  }
16
16
  async onStop(container) {
17
- const factory = await container.getAsync(manager_1.MongooseConnectionServiceFactory);
18
- await factory.stop();
17
+ await this.mongooseDataSourceManager.stop();
19
18
  }
20
19
  };
21
20
  MongooseConfiguration = __decorate([
package/dist/manager.d.ts CHANGED
@@ -1,15 +1,30 @@
1
- import { ServiceFactory } from '@midwayjs/core';
1
+ import { DataSourceManager } from '@midwayjs/core';
2
2
  import * as mongoose from 'mongoose';
3
- export declare class MongooseConnectionServiceFactory extends ServiceFactory<mongoose.Connection> {
3
+ export declare class MongooseDataSourceManager extends DataSourceManager<mongoose.Connection> {
4
4
  config: any;
5
5
  logger: any;
6
+ baseDir: string;
6
7
  init(): Promise<void>;
7
- protected createClient(config: any, name: string): Promise<mongoose.Connection>;
8
+ protected createDataSource(config: any, name: string): Promise<any>;
8
9
  getName(): string;
9
- destroyClient(connection: mongoose.Connection): Promise<void>;
10
+ destroyDataSource(dataSource: mongoose.Connection): Promise<void>;
11
+ protected checkConnected(dataSource: mongoose.Connection): Promise<boolean>;
10
12
  }
13
+ /**
14
+ * @deprecated
15
+ */
16
+ export declare class MongooseConnectionServiceFactory {
17
+ mongooseDataSourceManager: MongooseDataSourceManager;
18
+ createInstance(config: any, clientName: any): Promise<void | mongoose.Connection>;
19
+ get(id: string): mongoose.Connection;
20
+ getName(): string;
21
+ has(id: string): boolean;
22
+ }
23
+ /**
24
+ * @deprecated
25
+ */
11
26
  export declare class MongooseConnectionService implements mongoose.Connection {
12
- private serviceFactory;
27
+ private mongooseDataSourceManager;
13
28
  private instance;
14
29
  init(): Promise<void>;
15
30
  }
package/dist/manager.js CHANGED
@@ -9,68 +9,117 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.MongooseConnectionService = exports.MongooseConnectionServiceFactory = void 0;
12
+ exports.MongooseConnectionService = exports.MongooseConnectionServiceFactory = exports.MongooseDataSourceManager = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
14
  const decorator_1 = require("@midwayjs/decorator");
15
15
  const mongoose = require("mongoose");
16
- let MongooseConnectionServiceFactory = class MongooseConnectionServiceFactory extends core_1.ServiceFactory {
16
+ let MongooseDataSourceManager = class MongooseDataSourceManager extends core_1.DataSourceManager {
17
17
  async init() {
18
- await this.initClients(this.config);
18
+ if (this.config.client) {
19
+ this.logger.warn('[midway:mongoose] mongoose.client is deprecated, please use new config format.');
20
+ this.config.dataSource = {
21
+ default: this.config.client,
22
+ };
23
+ }
24
+ if (this.config.clients) {
25
+ this.logger.warn('[midway:mongoose] mongoose.clients is deprecated, please use new config format.');
26
+ this.config.dataSource = this.config.clients;
27
+ }
28
+ await this.initDataSource(this.config, this.baseDir);
19
29
  }
20
- async createClient(config, name) {
21
- const connection = await mongoose.createConnection(config.uri, config.options);
30
+ async createDataSource(config, name) {
31
+ const connection = (await mongoose.createConnection(config.uri, config.options));
22
32
  connection.on('error', err => {
23
- err.message = `[mongoose]${err.message}`;
33
+ err.message = `[midway:mongoose] ${err.message}`;
24
34
  this.logger.error(err);
25
35
  });
26
36
  /* istanbul ignore next */
27
37
  connection.on('disconnected', () => {
28
- this.logger.info(`[mongoose] ${name} disconnected`);
38
+ this.logger.info(`[midway:mongoose] ${name} disconnected`);
29
39
  });
30
40
  connection.on('connected', () => {
31
- this.logger.info(`[mongoose] ${name} connected successfully`);
41
+ this.logger.info(`[midway:mongoose] ${name} connected successfully`);
32
42
  });
33
43
  /* istanbul ignore next */
34
44
  connection.on('reconnected', () => {
35
- this.logger.info(`[mongoose] ${name} reconnected successfully`);
45
+ this.logger.info(`[midway:mongoose] ${name} reconnected successfully`);
36
46
  });
47
+ if (config.entities) {
48
+ connection.entities = config.entities;
49
+ }
37
50
  return connection;
38
51
  }
39
52
  getName() {
40
53
  return 'mongoose';
41
54
  }
42
- async destroyClient(connection) {
43
- await connection.close();
55
+ async destroyDataSource(dataSource) {
56
+ await dataSource.close();
57
+ }
58
+ async checkConnected(dataSource) {
59
+ return dataSource.readyState === mongoose.ConnectionStates.connected;
44
60
  }
45
61
  };
46
62
  __decorate([
47
63
  (0, decorator_1.Config)('mongoose'),
48
64
  __metadata("design:type", Object)
49
- ], MongooseConnectionServiceFactory.prototype, "config", void 0);
65
+ ], MongooseDataSourceManager.prototype, "config", void 0);
50
66
  __decorate([
51
67
  (0, decorator_1.Logger)('coreLogger'),
52
68
  __metadata("design:type", Object)
53
- ], MongooseConnectionServiceFactory.prototype, "logger", void 0);
69
+ ], MongooseDataSourceManager.prototype, "logger", void 0);
70
+ __decorate([
71
+ (0, decorator_1.Inject)(),
72
+ __metadata("design:type", String)
73
+ ], MongooseDataSourceManager.prototype, "baseDir", void 0);
54
74
  __decorate([
55
75
  (0, decorator_1.Init)(),
56
76
  __metadata("design:type", Function),
57
77
  __metadata("design:paramtypes", []),
58
78
  __metadata("design:returntype", Promise)
59
- ], MongooseConnectionServiceFactory.prototype, "init", null);
79
+ ], MongooseDataSourceManager.prototype, "init", null);
80
+ MongooseDataSourceManager = __decorate([
81
+ (0, decorator_1.Provide)(),
82
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
83
+ ], MongooseDataSourceManager);
84
+ exports.MongooseDataSourceManager = MongooseDataSourceManager;
85
+ /**
86
+ * @deprecated
87
+ */
88
+ let MongooseConnectionServiceFactory = class MongooseConnectionServiceFactory {
89
+ createInstance(config, clientName) {
90
+ return this.mongooseDataSourceManager.createInstance(config, clientName);
91
+ }
92
+ get(id) {
93
+ return this.mongooseDataSourceManager.getDataSource(id);
94
+ }
95
+ getName() {
96
+ return 'mongoose';
97
+ }
98
+ has(id) {
99
+ return this.mongooseDataSourceManager.hasDataSource(id);
100
+ }
101
+ };
102
+ __decorate([
103
+ (0, decorator_1.Inject)(),
104
+ __metadata("design:type", MongooseDataSourceManager)
105
+ ], MongooseConnectionServiceFactory.prototype, "mongooseDataSourceManager", void 0);
60
106
  MongooseConnectionServiceFactory = __decorate([
61
107
  (0, decorator_1.Provide)(),
62
108
  (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
63
109
  ], MongooseConnectionServiceFactory);
64
110
  exports.MongooseConnectionServiceFactory = MongooseConnectionServiceFactory;
111
+ /**
112
+ * @deprecated
113
+ */
65
114
  let MongooseConnectionService = class MongooseConnectionService {
66
115
  async init() {
67
- this.instance = this.serviceFactory.get('default');
116
+ this.instance = this.mongooseDataSourceManager.getDataSource('default');
68
117
  }
69
118
  };
70
119
  __decorate([
71
120
  (0, decorator_1.Inject)(),
72
- __metadata("design:type", MongooseConnectionServiceFactory)
73
- ], MongooseConnectionService.prototype, "serviceFactory", void 0);
121
+ __metadata("design:type", MongooseDataSourceManager)
122
+ ], MongooseConnectionService.prototype, "mongooseDataSourceManager", void 0);
74
123
  __decorate([
75
124
  (0, decorator_1.Init)(),
76
125
  __metadata("design:type", Function),
package/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export * from './dist/index';
3
3
 
4
4
  declare module '@midwayjs/core/dist/interface' {
5
5
  interface MidwayConfig {
6
- mongoose?: ServiceFactoryConfigOption<{
6
+ mongoose?: DataSourceManagerConfigOption<{
7
7
  uri: string;
8
8
  options: ConnectionOptions;
9
9
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mongoose",
3
- "version": "3.3.5",
3
+ "version": "3.4.0-beta.11",
4
4
  "description": "Midway Component for mongoose",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -22,13 +22,13 @@
22
22
  },
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@midwayjs/core": "^3.3.5",
26
- "@midwayjs/decorator": "^3.3.4",
27
- "@midwayjs/mock": "^3.3.5",
28
- "mongoose": "6.2.10"
25
+ "@midwayjs/core": "^3.4.0-beta.11",
26
+ "@midwayjs/decorator": "^3.4.0-beta.11",
27
+ "@midwayjs/mock": "^3.4.0-beta.11",
28
+ "mongoose": "6.4.4"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "mongoose": "*"
32
32
  },
33
- "gitHead": "5a835008aaa26e1b3c7d99c525f4a9fdaa3e41d1"
33
+ "gitHead": "b1c7a439b0df37d3e381cd182ea3b9e74323107b"
34
34
  }