@midwayjs/mongoose 3.3.4 → 3.4.0-beta.2

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,29 @@
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
6
  init(): Promise<void>;
7
- protected createClient(config: any, name: string): Promise<mongoose.Connection>;
7
+ protected createDataSource(config: any, name: string): Promise<mongoose.Connection>;
8
8
  getName(): string;
9
- destroyClient(connection: mongoose.Connection): Promise<void>;
9
+ destroyDataSource(dataSource: mongoose.Connection): Promise<void>;
10
+ protected checkConnected(dataSource: mongoose.Connection): Promise<boolean>;
10
11
  }
12
+ /**
13
+ * @deprecated
14
+ */
15
+ export declare class MongooseConnectionServiceFactory {
16
+ mongooseDataSourceManager: MongooseDataSourceManager;
17
+ createInstance(config: any, clientName: any): Promise<void | mongoose.Connection>;
18
+ get(id: string): mongoose.Connection;
19
+ getName(): string;
20
+ has(id: string): boolean;
21
+ }
22
+ /**
23
+ * @deprecated
24
+ */
11
25
  export declare class MongooseConnectionService implements mongoose.Connection {
12
- private serviceFactory;
26
+ private mongooseDataSourceManager;
13
27
  private instance;
14
28
  init(): Promise<void>;
15
29
  }
package/dist/manager.js CHANGED
@@ -9,68 +9,113 @@ 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);
19
29
  }
20
- async createClient(config, name) {
30
+ async createDataSource(config, name) {
21
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);
54
70
  __decorate([
55
71
  (0, decorator_1.Init)(),
56
72
  __metadata("design:type", Function),
57
73
  __metadata("design:paramtypes", []),
58
74
  __metadata("design:returntype", Promise)
59
- ], MongooseConnectionServiceFactory.prototype, "init", null);
75
+ ], MongooseDataSourceManager.prototype, "init", null);
76
+ MongooseDataSourceManager = __decorate([
77
+ (0, decorator_1.Provide)(),
78
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
79
+ ], MongooseDataSourceManager);
80
+ exports.MongooseDataSourceManager = MongooseDataSourceManager;
81
+ /**
82
+ * @deprecated
83
+ */
84
+ let MongooseConnectionServiceFactory = class MongooseConnectionServiceFactory {
85
+ createInstance(config, clientName) {
86
+ return this.mongooseDataSourceManager.createInstance(config, clientName);
87
+ }
88
+ get(id) {
89
+ return this.mongooseDataSourceManager.getDataSource(id);
90
+ }
91
+ getName() {
92
+ return 'mongoose';
93
+ }
94
+ has(id) {
95
+ return this.mongooseDataSourceManager.hasDataSource(id);
96
+ }
97
+ };
98
+ __decorate([
99
+ (0, decorator_1.Inject)(),
100
+ __metadata("design:type", MongooseDataSourceManager)
101
+ ], MongooseConnectionServiceFactory.prototype, "mongooseDataSourceManager", void 0);
60
102
  MongooseConnectionServiceFactory = __decorate([
61
103
  (0, decorator_1.Provide)(),
62
104
  (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
63
105
  ], MongooseConnectionServiceFactory);
64
106
  exports.MongooseConnectionServiceFactory = MongooseConnectionServiceFactory;
107
+ /**
108
+ * @deprecated
109
+ */
65
110
  let MongooseConnectionService = class MongooseConnectionService {
66
111
  async init() {
67
- this.instance = this.serviceFactory.get('default');
112
+ this.instance = this.mongooseDataSourceManager.getDataSource('default');
68
113
  }
69
114
  };
70
115
  __decorate([
71
116
  (0, decorator_1.Inject)(),
72
- __metadata("design:type", MongooseConnectionServiceFactory)
73
- ], MongooseConnectionService.prototype, "serviceFactory", void 0);
117
+ __metadata("design:type", MongooseDataSourceManager)
118
+ ], MongooseConnectionService.prototype, "mongooseDataSourceManager", void 0);
74
119
  __decorate([
75
120
  (0, decorator_1.Init)(),
76
121
  __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.4",
3
+ "version": "3.4.0-beta.2",
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.4",
26
- "@midwayjs/decorator": "^3.3.4",
27
- "@midwayjs/mock": "^3.3.4",
25
+ "@midwayjs/core": "^3.4.0-beta.2",
26
+ "@midwayjs/decorator": "^3.4.0-beta.2",
27
+ "@midwayjs/mock": "^3.4.0-beta.2",
28
28
  "mongoose": "6.2.10"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "mongoose": "*"
32
32
  },
33
- "gitHead": "0c2785a87217f57a184a661c71b1d9562af02242"
33
+ "gitHead": "a61721d3946b30fd4cac183265edcd99b31ac72b"
34
34
  }