@vetrivelan-cp/mongoose 1.0.4 → 1.0.5
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/README.md +21 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/tokens.d.ts +5 -0
- package/dist/utils/tokens.js +49 -0
- package/dist/utils/tokens.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ This package expects the following peer dependencies to be installed by the cons
|
|
|
18
18
|
- `@nestjs/mongoose` (`^9 || ^10`)
|
|
19
19
|
- `@nestjs/common` (`^9`)
|
|
20
20
|
- `@nestjs/core` (`^9`)
|
|
21
|
+
- `rxjs` (required for `handleRetry`)
|
|
21
22
|
|
|
22
23
|
## Usage
|
|
23
24
|
|
|
@@ -43,9 +44,7 @@ import { Module } from '@nestjs/common';
|
|
|
43
44
|
import { MongooseModule } from '@vetrivelan-cp/mongoose';
|
|
44
45
|
|
|
45
46
|
@Module({
|
|
46
|
-
imports: [
|
|
47
|
-
MongooseModule.forRoot('mongodb://localhost:27017/mydb'),
|
|
48
|
-
],
|
|
47
|
+
imports: [MongooseModule.forRoot('mongodb://localhost:27017/mydb')],
|
|
49
48
|
})
|
|
50
49
|
export class AppModule {}
|
|
51
50
|
```
|
|
@@ -88,6 +87,24 @@ export class UsersRepository extends BaseRepository<any> {
|
|
|
88
87
|
}
|
|
89
88
|
```
|
|
90
89
|
|
|
90
|
+
## Tokens & retry utilities
|
|
91
|
+
|
|
92
|
+
This package also exports token helpers and a retry operator (useful for connection initialization):
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import {
|
|
96
|
+
getModelToken,
|
|
97
|
+
getConnectionToken,
|
|
98
|
+
handleRetry,
|
|
99
|
+
raw,
|
|
100
|
+
} from '@vetrivelan-cp/mongoose';
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
- `getModelToken(model: string, connectionName?: string): string`
|
|
104
|
+
- `getConnectionToken(name?: string): string`
|
|
105
|
+
- `handleRetry(retryAttempts?: number, retryDelay?: number, verboseRetryLog?: boolean)`
|
|
106
|
+
- `raw(definition: Record<string, any>): Record<string, any>`
|
|
107
|
+
|
|
91
108
|
## Exports
|
|
92
109
|
|
|
93
110
|
This package re-exports commonly used items from:
|
|
@@ -101,6 +118,7 @@ including:
|
|
|
101
118
|
- Injection helpers: `InjectModel`, `InjectConnection`
|
|
102
119
|
- Nest module: `MongooseModule`
|
|
103
120
|
- Repository: `BaseRepository`
|
|
121
|
+
- Tokens/retry: `getModelToken`, `getConnectionToken`, `handleRetry`, `raw`
|
|
104
122
|
- Types/utilities: `ObjectId`, `toObjectId`, `CpbsDocument`, `FilterQuery`, etc.
|
|
105
123
|
|
|
106
124
|
See `src/index.ts` for the full export surface.
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { Schema, Prop, SchemaFactory, InjectModel, InjectConnection, MongooseMod
|
|
|
2
2
|
export type { SchemaOptions, PropOptions, MongooseModuleOptions, MongooseModuleAsyncOptions, MongooseOptionsFactory, } from './decorators';
|
|
3
3
|
export { MongooseModule } from './module/cpbs-mongoose.module';
|
|
4
4
|
export { BaseRepository } from './repository/base.repository';
|
|
5
|
+
export { getModelToken, getConnectionToken, handleRetry, raw } from './utils/tokens';
|
|
5
6
|
export { Document, Model, Connection, Schema as MongooseSchema, Types, SchemaTypes, mongoose, FilterQuery, UpdateQuery, QueryOptions, ProjectionType, PipelineStage, UpdateWithAggregationPipeline, SaveOptions, AggregateOptions, ObjectId, toObjectId, CpbsDocument, MixedType, Mixed, } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toObjectId = exports.mongoose = exports.SchemaTypes = exports.Types = exports.MongooseSchema = exports.Connection = exports.Model = exports.Document = exports.BaseRepository = exports.MongooseModule = exports.NestMongooseModule = exports.InjectConnection = exports.InjectModel = exports.SchemaFactory = exports.Prop = exports.Schema = void 0;
|
|
3
|
+
exports.toObjectId = exports.mongoose = exports.SchemaTypes = exports.Types = exports.MongooseSchema = exports.Connection = exports.Model = exports.Document = exports.raw = exports.handleRetry = exports.getConnectionToken = exports.getModelToken = exports.BaseRepository = exports.MongooseModule = exports.NestMongooseModule = exports.InjectConnection = exports.InjectModel = exports.SchemaFactory = exports.Prop = exports.Schema = void 0;
|
|
4
4
|
var decorators_1 = require("./decorators");
|
|
5
5
|
Object.defineProperty(exports, "Schema", { enumerable: true, get: function () { return decorators_1.Schema; } });
|
|
6
6
|
Object.defineProperty(exports, "Prop", { enumerable: true, get: function () { return decorators_1.Prop; } });
|
|
@@ -12,6 +12,11 @@ var cpbs_mongoose_module_1 = require("./module/cpbs-mongoose.module");
|
|
|
12
12
|
Object.defineProperty(exports, "MongooseModule", { enumerable: true, get: function () { return cpbs_mongoose_module_1.MongooseModule; } });
|
|
13
13
|
var base_repository_1 = require("./repository/base.repository");
|
|
14
14
|
Object.defineProperty(exports, "BaseRepository", { enumerable: true, get: function () { return base_repository_1.BaseRepository; } });
|
|
15
|
+
var tokens_1 = require("./utils/tokens");
|
|
16
|
+
Object.defineProperty(exports, "getModelToken", { enumerable: true, get: function () { return tokens_1.getModelToken; } });
|
|
17
|
+
Object.defineProperty(exports, "getConnectionToken", { enumerable: true, get: function () { return tokens_1.getConnectionToken; } });
|
|
18
|
+
Object.defineProperty(exports, "handleRetry", { enumerable: true, get: function () { return tokens_1.handleRetry; } });
|
|
19
|
+
Object.defineProperty(exports, "raw", { enumerable: true, get: function () { return tokens_1.raw; } });
|
|
15
20
|
var types_1 = require("./types");
|
|
16
21
|
Object.defineProperty(exports, "Document", { enumerable: true, get: function () { return types_1.Document; } });
|
|
17
22
|
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return types_1.Model; } });
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AA+BA,2CAOsB;AANpB,oGAAA,MAAM,OAAA;AACN,kGAAA,IAAI,OAAA;AACJ,2GAAA,aAAa,OAAA;AACb,yGAAA,WAAW,OAAA;AACX,8GAAA,gBAAgB,OAAA;AAChB,gHAAA,cAAc,OAAsB;AAYtC,sEAA+D;AAAtD,sHAAA,cAAc,OAAA;AAGvB,gEAA8D;AAArD,iHAAA,cAAc,OAAA;AAGvB,iCA0BiB;AAxBf,iGAAA,QAAQ,OAAA;AACR,8FAAA,KAAK,OAAA;AACL,mGAAA,UAAU,OAAA;AACV,uGAAA,MAAM,OAAkB;AACxB,8FAAA,KAAK,OAAA;AACL,oGAAA,WAAW,OAAA;AACX,iGAAA,QAAQ,OAAA;AAcR,mGAAA,UAAU,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AA+BA,2CAOsB;AANpB,oGAAA,MAAM,OAAA;AACN,kGAAA,IAAI,OAAA;AACJ,2GAAA,aAAa,OAAA;AACb,yGAAA,WAAW,OAAA;AACX,8GAAA,gBAAgB,OAAA;AAChB,gHAAA,cAAc,OAAsB;AAYtC,sEAA+D;AAAtD,sHAAA,cAAc,OAAA;AAGvB,gEAA8D;AAArD,iHAAA,cAAc,OAAA;AAGvB,yCAAqF;AAA5E,uGAAA,aAAa,OAAA;AAAE,4GAAA,kBAAkB,OAAA;AAAE,qGAAA,WAAW,OAAA;AAAE,6FAAA,GAAG,OAAA;AAG5D,iCA0BiB;AAxBf,iGAAA,QAAQ,OAAA;AACR,8FAAA,KAAK,OAAA;AACL,mGAAA,UAAU,OAAA;AACV,uGAAA,MAAM,OAAkB;AACxB,8FAAA,KAAK,OAAA;AACL,oGAAA,WAAW,OAAA;AACX,iGAAA,QAAQ,OAAA;AAcR,mGAAA,UAAU,OAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
export declare function getModelToken(model: string, connectionName?: string): string;
|
|
3
|
+
export declare function getConnectionToken(name?: string): string;
|
|
4
|
+
export declare function handleRetry(retryAttempts?: number, retryDelay?: number, verboseRetryLog?: boolean): <T>(source: Observable<T>) => Observable<T>;
|
|
5
|
+
export declare function raw(definition: Record<string, any>): Record<string, any>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.raw = exports.handleRetry = exports.getConnectionToken = exports.getModelToken = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
function getModelToken(model, connectionName) {
|
|
6
|
+
if (connectionName && connectionName !== 'default') {
|
|
7
|
+
return `${connectionName}Connection/${model}Model`;
|
|
8
|
+
}
|
|
9
|
+
return `${model}Model`;
|
|
10
|
+
}
|
|
11
|
+
exports.getModelToken = getModelToken;
|
|
12
|
+
function getConnectionToken(name) {
|
|
13
|
+
if (name && name !== 'default') {
|
|
14
|
+
return `${name}Connection`;
|
|
15
|
+
}
|
|
16
|
+
return 'DatabaseConnection';
|
|
17
|
+
}
|
|
18
|
+
exports.getConnectionToken = getConnectionToken;
|
|
19
|
+
function handleRetry(retryAttempts = 9, retryDelay = 3000, verboseRetryLog = false) {
|
|
20
|
+
return (source) => new rxjs_1.Observable((subscriber) => {
|
|
21
|
+
let attempts = 0;
|
|
22
|
+
const subscribeForRetry = () => {
|
|
23
|
+
const subscription = source.subscribe({
|
|
24
|
+
next: (value) => subscriber.next(value),
|
|
25
|
+
complete: () => subscriber.complete(),
|
|
26
|
+
error: (err) => {
|
|
27
|
+
attempts += 1;
|
|
28
|
+
if (attempts > retryAttempts) {
|
|
29
|
+
subscriber.error(err);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (verboseRetryLog) {
|
|
33
|
+
console.warn(`Mongoose connection failed. Retrying (${attempts}/${retryAttempts}) in ${retryDelay}ms...`);
|
|
34
|
+
}
|
|
35
|
+
setTimeout(() => subscribeForRetry(), retryDelay);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
return subscription;
|
|
39
|
+
};
|
|
40
|
+
const initialSub = subscribeForRetry();
|
|
41
|
+
return () => initialSub?.unsubscribe();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.handleRetry = handleRetry;
|
|
45
|
+
function raw(definition) {
|
|
46
|
+
return definition;
|
|
47
|
+
}
|
|
48
|
+
exports.raw = raw;
|
|
49
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../src/utils/tokens.ts"],"names":[],"mappings":";;;AAAA,+BAAkC;AAOlC,SAAgB,aAAa,CAAC,KAAa,EAAE,cAAuB;IAClE,IAAI,cAAc,IAAI,cAAc,KAAK,SAAS,EAAE;QAClD,OAAO,GAAG,cAAc,cAAc,KAAK,OAAO,CAAC;KACpD;IACD,OAAO,GAAG,KAAK,OAAO,CAAC;AACzB,CAAC;AALD,sCAKC;AAOD,SAAgB,kBAAkB,CAAC,IAAa;IAC9C,IAAI,IAAI,IAAI,IAAI,KAAK,SAAS,EAAE;QAC9B,OAAO,GAAG,IAAI,YAAY,CAAC;KAC5B;IACD,OAAO,oBAAoB,CAAC;AAC9B,CAAC;AALD,gDAKC;AAOD,SAAgB,WAAW,CACzB,aAAa,GAAG,CAAC,EACjB,UAAU,GAAG,IAAI,EACjB,eAAe,GAAG,KAAK;IAEvB,OAAO,CAAI,MAAqB,EAAE,EAAE,CAClC,IAAI,iBAAU,CAAI,CAAC,UAAU,EAAE,EAAE;QAC/B,IAAI,QAAQ,GAAG,CAAC,CAAC;QAEjB,MAAM,iBAAiB,GAAG,GAAG,EAAE;YAC7B,MAAM,YAAY,GAAG,MAAM,CAAC,SAAS,CAAC;gBACpC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;gBACvC,QAAQ,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE;gBACrC,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE;oBACb,QAAQ,IAAI,CAAC,CAAC;oBAEd,IAAI,QAAQ,GAAG,aAAa,EAAE;wBAC5B,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;wBACtB,OAAO;qBACR;oBAED,IAAI,eAAe,EAAE;wBAEnB,OAAO,CAAC,IAAI,CACV,yCAAyC,QAAQ,IAAI,aAAa,QAAQ,UAAU,OAAO,CAC5F,CAAC;qBACH;oBAED,UAAU,CAAC,GAAG,EAAE,CAAC,iBAAiB,EAAE,EAAE,UAAU,CAAC,CAAC;gBACpD,CAAC;aACF,CAAC,CAAC;YAEH,OAAO,YAAY,CAAC;QACtB,CAAC,CAAC;QAEF,MAAM,UAAU,GAAG,iBAAiB,EAAE,CAAC;QACvC,OAAO,GAAG,EAAE,CAAC,UAAU,EAAE,WAAW,EAAE,CAAC;IACzC,CAAC,CAAC,CAAC;AACP,CAAC;AAtCD,kCAsCC;AAQD,SAAgB,GAAG,CAAC,UAA+B;IACjD,OAAO,UAAU,CAAC;AACpB,CAAC;AAFD,kBAEC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vetrivelan-cp/mongoose",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "CPBS abstraction layer over Mongoose and @nestjs/mongoose. Shields applications from breaking Mongoose API changes.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|