@midwayjs/typegoose 2.13.2 → 2.14.0

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,47 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.14.0](https://github.com/midwayjs/midway/compare/v2.13.5...v2.14.0) (2021-12-06)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * mongoose init before config load ([#1401](https://github.com/midwayjs/midway/issues/1401)) ([34d2ab8](https://github.com/midwayjs/midway/commit/34d2ab8b5bcfc9bddf52b9a93d6f3b44960ef537))
12
+
13
+
14
+
15
+
16
+
17
+ ## [2.13.5](https://github.com/midwayjs/midway/compare/v2.13.4...v2.13.5) (2021-10-21)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * remove connection key ([#1337](https://github.com/midwayjs/midway/issues/1337)) ([8e7e912](https://github.com/midwayjs/midway/commit/8e7e9124abbd66c27443851d67217ee76daac8fd))
23
+
24
+
25
+
26
+
27
+
28
+ ## [2.13.4](https://github.com/midwayjs/midway/compare/v2.13.3...v2.13.4) (2021-10-21)
29
+
30
+
31
+ ### Features
32
+
33
+ * add mongoose component and support multi-instance for typegoose ([#1334](https://github.com/midwayjs/midway/issues/1334)) ([ec54224](https://github.com/midwayjs/midway/commit/ec54224d775a3cfde3ff280538c8db79bcff4610))
34
+
35
+
36
+
37
+
38
+
39
+ ## [2.13.3](https://github.com/midwayjs/midway/compare/v2.13.2...v2.13.3) (2021-09-28)
40
+
41
+ **Note:** Version bump only for package @midwayjs/typegoose
42
+
43
+
44
+
45
+
46
+
6
47
  ## [2.13.2](https://github.com/midwayjs/midway/compare/v2.13.1...v2.13.2) (2021-09-09)
7
48
 
8
49
  **Note:** Version bump only for package @midwayjs/typegoose
@@ -1,5 +1,11 @@
1
- export declare class AutoConfiguration {
2
- onReady(app: any): Promise<void>;
1
+ import { IMidwayApplication, IMidwayContainer } from '@midwayjs/core';
2
+ export declare class TypegooseConfiguration {
3
+ oldMongooseConfig: any;
4
+ legacyMode: boolean;
5
+ app: IMidwayApplication;
6
+ modelMap: WeakMap<object, any>;
7
+ init(): Promise<void>;
8
+ onReady(container: IMidwayContainer): Promise<void>;
3
9
  onStop(): Promise<void>;
4
10
  }
5
11
  //# sourceMappingURL=configuration.d.ts.map
@@ -5,24 +5,76 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
8
11
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.AutoConfiguration = void 0;
12
+ exports.TypegooseConfiguration = void 0;
10
13
  const decorator_1 = require("@midwayjs/decorator");
11
- const path_1 = require("path");
12
- const mongoose = require("mongoose");
13
- let AutoConfiguration = class AutoConfiguration {
14
- async onReady(app) {
15
- await app.getAsync('mongoose:mongooseService');
14
+ const mongoose = require("@midwayjs/mongoose");
15
+ const interface_1 = require("./interface");
16
+ const typegoose_1 = require("@typegoose/typegoose");
17
+ const mongo = require("mongoose");
18
+ let TypegooseConfiguration = class TypegooseConfiguration {
19
+ constructor() {
20
+ this.legacyMode = false;
21
+ this.modelMap = new WeakMap();
22
+ }
23
+ async init() {
24
+ this.app
25
+ .getApplicationContext()
26
+ .registerDataHandler(interface_1.ENTITY_MODEL_KEY, (key) => {
27
+ // return getConnection(key.connectionName).getRepository(key.modelKey);
28
+ return this.modelMap.get(key.modelKey);
29
+ });
30
+ }
31
+ async onReady(container) {
32
+ var _a, _b;
33
+ const connectionFactory = await container.getAsync(mongoose.MongooseConnectionServiceFactory);
34
+ const Models = (0, decorator_1.listModule)(interface_1.ENTITY_MODEL_KEY);
35
+ for (const Model of Models) {
36
+ const metadata = (_a = (0, decorator_1.getClassMetadata)(interface_1.ENTITY_MODEL_KEY, Model)) !== null && _a !== void 0 ? _a : {};
37
+ const connectionName = (_b = metadata.connectionName) !== null && _b !== void 0 ? _b : 'default';
38
+ const conn = connectionFactory.get(connectionName);
39
+ if (conn) {
40
+ const model = (0, typegoose_1.getModelForClass)(Model, { existingConnection: conn });
41
+ this.modelMap.set(Model, model);
42
+ }
43
+ else {
44
+ throw new Error(`connection name ${metadata.connectionName} not found`);
45
+ }
46
+ }
47
+ // 兼容老代码
48
+ if (Models.length === 0 && this.oldMongooseConfig['uri']) {
49
+ this.legacyMode = true;
50
+ await mongo.connect(this.oldMongooseConfig.uri, this.oldMongooseConfig.options);
51
+ }
16
52
  }
17
53
  async onStop() {
18
- await mongoose.disconnect();
54
+ if (this.legacyMode) {
55
+ await mongo.disconnect();
56
+ }
19
57
  }
20
58
  };
21
- AutoConfiguration = __decorate([
59
+ __decorate([
60
+ (0, decorator_1.Config)('mongoose'),
61
+ __metadata("design:type", Object)
62
+ ], TypegooseConfiguration.prototype, "oldMongooseConfig", void 0);
63
+ __decorate([
64
+ (0, decorator_1.App)(),
65
+ __metadata("design:type", Object)
66
+ ], TypegooseConfiguration.prototype, "app", void 0);
67
+ __decorate([
68
+ (0, decorator_1.Init)(),
69
+ __metadata("design:type", Function),
70
+ __metadata("design:paramtypes", []),
71
+ __metadata("design:returntype", Promise)
72
+ ], TypegooseConfiguration.prototype, "init", null);
73
+ TypegooseConfiguration = __decorate([
22
74
  (0, decorator_1.Configuration)({
23
- namespace: 'mongoose',
24
- importConfigs: [(0, path_1.join)(__dirname, 'config')],
75
+ namespace: 'typegoose',
76
+ imports: [mongoose],
25
77
  })
26
- ], AutoConfiguration);
27
- exports.AutoConfiguration = AutoConfiguration;
78
+ ], TypegooseConfiguration);
79
+ exports.TypegooseConfiguration = TypegooseConfiguration;
28
80
  //# sourceMappingURL=configuration.js.map
@@ -0,0 +1,3 @@
1
+ import { EntityOptions } from '../interface';
2
+ export declare function EntityModel(options?: EntityOptions): ClassDecorator;
3
+ //# sourceMappingURL=entityModel.d.ts.map
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EntityModel = void 0;
4
+ const decorator_1 = require("@midwayjs/decorator");
5
+ const interface_1 = require("../interface");
6
+ function EntityModel(options) {
7
+ return (target) => {
8
+ (0, decorator_1.saveModule)(interface_1.ENTITY_MODEL_KEY, target);
9
+ (0, decorator_1.saveClassMetadata)(interface_1.ENTITY_MODEL_KEY, options, target);
10
+ };
11
+ }
12
+ exports.EntityModel = EntityModel;
13
+ //# sourceMappingURL=entityModel.js.map
@@ -0,0 +1,2 @@
1
+ export declare function InjectEntityModel(modelKey: any, connectionName?: any): (target: any, propertyKey: string) => void;
2
+ //# sourceMappingURL=injectEntityModel.d.ts.map
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InjectEntityModel = void 0;
4
+ const decorator_1 = require("@midwayjs/decorator");
5
+ const interface_1 = require("../interface");
6
+ function InjectEntityModel(modelKey, connectionName) {
7
+ return (target, propertyKey) => {
8
+ (0, decorator_1.attachClassMetadata)(interface_1.ENTITY_MODEL_KEY, {
9
+ key: {
10
+ modelKey,
11
+ },
12
+ propertyName: propertyKey,
13
+ }, target);
14
+ };
15
+ }
16
+ exports.InjectEntityModel = InjectEntityModel;
17
+ //# sourceMappingURL=injectEntityModel.js.map
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- export { AutoConfiguration as Configuration } from './configuration';
2
- export * from './service/mongoose';
3
- export * from '@typegoose/typegoose';
4
- export * from './interface/config.type';
1
+ export { TypegooseConfiguration as Configuration } from './configuration';
2
+ export * from './decorator/entityModel';
3
+ export * from './decorator/injectEntityModel';
5
4
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -12,8 +12,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.Configuration = void 0;
14
14
  var configuration_1 = require("./configuration");
15
- Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.AutoConfiguration; } });
16
- __exportStar(require("./service/mongoose"), exports);
17
- __exportStar(require("@typegoose/typegoose"), exports);
18
- __exportStar(require("./interface/config.type"), exports);
15
+ Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.TypegooseConfiguration; } });
16
+ __exportStar(require("./decorator/entityModel"), exports);
17
+ __exportStar(require("./decorator/injectEntityModel"), exports);
19
18
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,19 @@
1
+ import * as mongoose from 'mongoose';
2
+ export interface EntityOptions {
3
+ connectionName: string;
4
+ }
5
+ export declare const ENTITY_MODEL_KEY = "TYPEGOOSE:MODEL";
6
+ interface M5 {
7
+ ConnectionOptions: unknown;
8
+ }
9
+ interface M6 {
10
+ ConnectOptions: unknown;
11
+ }
12
+ declare type ExtractConnectionOptions<T> = T extends M5 ? T['ConnectionOptions'] : T extends M6 ? T['ConnectOptions'] : never;
13
+ declare type ConnectionOptions = ExtractConnectionOptions<typeof mongoose>;
14
+ export declare type DefaultConfig = {
15
+ uri: string;
16
+ options: ConnectionOptions;
17
+ };
18
+ export {};
19
+ //# sourceMappingURL=interface.d.ts.map
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ENTITY_MODEL_KEY = void 0;
4
+ exports.ENTITY_MODEL_KEY = 'TYPEGOOSE:MODEL';
5
+ //# sourceMappingURL=interface.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/typegoose",
3
- "version": "2.13.2",
3
+ "version": "2.14.0",
4
4
  "description": "Midway Component for typegoose",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -20,14 +20,15 @@
20
20
  "node": ">= 12.13.0"
21
21
  },
22
22
  "license": "MIT",
23
+ "dependencies": {
24
+ "@midwayjs/mongoose": "^2.14.0"
25
+ },
23
26
  "devDependencies": {
24
- "@midwayjs/core": "^2.13.2",
25
- "@midwayjs/decorator": "^2.13.2",
26
- "@midwayjs/koa": "^2.13.2",
27
- "@midwayjs/mock": "^2.13.2",
28
- "@typegoose/typegoose": "^8.1.0",
29
- "mongodb-memory-server": "^6.9.6",
30
- "mongoose": "~5.13.3"
27
+ "@midwayjs/core": "^2.14.0",
28
+ "@midwayjs/decorator": "^2.14.0",
29
+ "@midwayjs/mock": "^2.14.0",
30
+ "@typegoose/typegoose": "^9.0.0",
31
+ "mongoose": "~6.0.11"
31
32
  },
32
- "gitHead": "30cec549e5e393fb6ace1cecfb9c122d8ca647d0"
33
+ "gitHead": "5fd716b0e731162d8e9f0931790fde7402fb83de"
33
34
  }
@@ -1,9 +0,0 @@
1
- export declare const mongoose: {
2
- uri: string;
3
- options: {
4
- useNewUrlParser: boolean;
5
- useUnifiedTopology: boolean;
6
- dbName: string;
7
- };
8
- };
9
- //# sourceMappingURL=config.default.d.ts.map
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mongoose = void 0;
4
- exports.mongoose = {
5
- uri: 'mongodb://localhost:27017/',
6
- options: { useNewUrlParser: true, useUnifiedTopology: true, dbName: 'test' },
7
- };
8
- //# sourceMappingURL=config.default.js.map
@@ -1,6 +0,0 @@
1
- import * as mongoose from 'mongoose';
2
- export declare type DefaultConfig = {
3
- uri: string;
4
- options: mongoose.ConnectionOptions;
5
- };
6
- //# sourceMappingURL=config.type.d.ts.map
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=config.type.js.map
@@ -1,5 +0,0 @@
1
- export declare class MongooseService {
2
- config: any;
3
- init(): Promise<void>;
4
- }
5
- //# sourceMappingURL=mongoose.d.ts.map
@@ -1,35 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.MongooseService = void 0;
13
- const decorator_1 = require("@midwayjs/decorator");
14
- const mongoose = require("mongoose");
15
- let MongooseService = class MongooseService {
16
- async init() {
17
- await mongoose.connect(this.config.uri, this.config.options);
18
- }
19
- };
20
- __decorate([
21
- (0, decorator_1.Config)('mongoose'),
22
- __metadata("design:type", Object)
23
- ], MongooseService.prototype, "config", void 0);
24
- __decorate([
25
- (0, decorator_1.Init)(),
26
- __metadata("design:type", Function),
27
- __metadata("design:paramtypes", []),
28
- __metadata("design:returntype", Promise)
29
- ], MongooseService.prototype, "init", null);
30
- MongooseService = __decorate([
31
- (0, decorator_1.Provide)('mongooseService'),
32
- (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
33
- ], MongooseService);
34
- exports.MongooseService = MongooseService;
35
- //# sourceMappingURL=mongoose.js.map