@akanjs/nest 0.0.97 → 0.0.99
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/index.cjs +21 -0
- package/index.js +1 -21
- package/package.json +4 -4
- package/src/{authGuards.mjs → authGuards.cjs} +56 -15
- package/src/authGuards.js +15 -56
- package/src/authentication.cjs +122 -0
- package/src/authentication.js +27 -67
- package/src/authorization.cjs +79 -0
- package/src/authorization.js +12 -46
- package/src/cacheClient.cjs +45 -0
- package/src/cacheClient.js +5 -26
- package/src/databaseClient.cjs +51 -0
- package/src/databaseClient.js +8 -29
- package/src/{decorators.mjs → decorators.cjs} +33 -6
- package/src/decorators.js +6 -33
- package/src/{exceptions.mjs → exceptions.cjs} +29 -8
- package/src/exceptions.js +8 -29
- package/src/{exporter.mjs → exporter.cjs} +40 -3
- package/src/exporter.js +3 -40
- package/src/{generateSecrets.mjs → generateSecrets.cjs} +43 -13
- package/src/generateSecrets.js +13 -43
- package/src/index.cjs +73 -0
- package/src/index.js +21 -72
- package/src/{interceptors.mjs → interceptors.cjs} +55 -36
- package/src/interceptors.js +36 -55
- package/src/mongoose.cjs +93 -0
- package/src/mongoose.js +11 -44
- package/src/{pipes.mjs → pipes.cjs} +51 -22
- package/src/pipes.js +22 -51
- package/src/redis-io.adapter.cjs +84 -0
- package/src/redis-io.adapter.js +12 -35
- package/src/{searchClient.mjs → searchClient.cjs} +28 -7
- package/src/searchClient.js +7 -28
- package/src/{sso.mjs → sso.cjs} +56 -24
- package/src/sso.js +24 -56
- package/src/verifyPayment.cjs +50 -0
- package/src/verifyPayment.js +4 -37
- package/index.mjs +0 -1
- package/src/authentication.mjs +0 -82
- package/src/authorization.mjs +0 -45
- package/src/cacheClient.mjs +0 -24
- package/src/databaseClient.mjs +0 -30
- package/src/index.mjs +0 -22
- package/src/mongoose.mjs +0 -60
- package/src/redis-io.adapter.mjs +0 -61
- package/src/verifyPayment.mjs +0 -17
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
3
18
|
var __decorateClass = (decorators, target, key, kind) => {
|
|
4
19
|
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
5
20
|
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
@@ -27,52 +42,55 @@ var __privateMethod = (obj, member, method) => {
|
|
|
27
42
|
__accessCheck(obj, member, "access private method");
|
|
28
43
|
return method;
|
|
29
44
|
};
|
|
45
|
+
var interceptors_exports = {};
|
|
46
|
+
__export(interceptors_exports, {
|
|
47
|
+
CacheInterceptor: () => CacheInterceptor,
|
|
48
|
+
LoggingInterceptor: () => LoggingInterceptor,
|
|
49
|
+
TimeoutInterceptor: () => TimeoutInterceptor
|
|
50
|
+
});
|
|
51
|
+
module.exports = __toCommonJS(interceptors_exports);
|
|
52
|
+
var import_common = require("@akanjs/common");
|
|
53
|
+
var import_signal = require("@akanjs/signal");
|
|
54
|
+
var import_common2 = require("@nestjs/common");
|
|
55
|
+
var import_graphql = require("@nestjs/graphql");
|
|
56
|
+
var import_rxjs = require("rxjs");
|
|
57
|
+
var import_operators = require("rxjs/operators");
|
|
58
|
+
var import_authGuards = require("./authGuards");
|
|
30
59
|
var _logger, _CACHE_PREFIX, _generateCacheKey, generateCacheKey_fn, _getCache, getCache_fn, _setCache, setCache_fn;
|
|
31
|
-
import { Logger } from "@akanjs/common";
|
|
32
|
-
import { getGqlMeta } from "@akanjs/signal";
|
|
33
|
-
import {
|
|
34
|
-
Inject,
|
|
35
|
-
Injectable,
|
|
36
|
-
RequestTimeoutException
|
|
37
|
-
} from "@nestjs/common";
|
|
38
|
-
import { GqlExecutionContext } from "@nestjs/graphql";
|
|
39
|
-
import { throwError, TimeoutError } from "rxjs";
|
|
40
|
-
import { catchError, map, tap, timeout } from "rxjs/operators";
|
|
41
|
-
import { getArgs, getRequest } from "./authGuards";
|
|
42
60
|
let CacheInterceptor = class {
|
|
43
61
|
constructor(redis) {
|
|
44
62
|
this.redis = redis;
|
|
45
63
|
__privateAdd(this, _generateCacheKey);
|
|
46
64
|
__privateAdd(this, _getCache);
|
|
47
65
|
__privateAdd(this, _setCache);
|
|
48
|
-
__privateAdd(this, _logger, new Logger("CacheInterceptor"));
|
|
66
|
+
__privateAdd(this, _logger, new import_common.Logger("CacheInterceptor"));
|
|
49
67
|
__privateAdd(this, _CACHE_PREFIX, "signal:");
|
|
50
68
|
}
|
|
51
69
|
async intercept(context, next) {
|
|
52
70
|
const handler = context.getHandler();
|
|
53
71
|
const signalKey = handler.name;
|
|
54
|
-
const gqlMeta = getGqlMeta(context.getClass(), signalKey);
|
|
72
|
+
const gqlMeta = (0, import_signal.getGqlMeta)(context.getClass(), signalKey);
|
|
55
73
|
if (gqlMeta.type !== "Query" || !gqlMeta.signalOption.cache) {
|
|
56
74
|
if (gqlMeta.signalOption.cache)
|
|
57
75
|
__privateGet(this, _logger).warn(`CacheInterceptor: ${signalKey} is not Query endpoint or cache is not set`);
|
|
58
76
|
return next.handle();
|
|
59
77
|
}
|
|
60
|
-
const args = getArgs(context);
|
|
78
|
+
const args = (0, import_authGuards.getArgs)(context);
|
|
61
79
|
const cacheKey = __privateMethod(this, _generateCacheKey, generateCacheKey_fn).call(this, signalKey, args);
|
|
62
80
|
const cachedData = await __privateMethod(this, _getCache, getCache_fn).call(this, cacheKey);
|
|
63
81
|
if (cachedData) {
|
|
64
82
|
__privateGet(this, _logger).debug(`Cache hit for key: ${cacheKey}`);
|
|
65
83
|
return next.handle().pipe(
|
|
66
|
-
map(() => cachedData),
|
|
67
|
-
catchError((error) => {
|
|
84
|
+
(0, import_operators.map)(() => cachedData),
|
|
85
|
+
(0, import_operators.catchError)((error) => {
|
|
68
86
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
69
87
|
__privateGet(this, _logger).error(`Error in cache interceptor for ${cacheKey}: ${errorMessage}`);
|
|
70
|
-
return throwError(() => error);
|
|
88
|
+
return (0, import_rxjs.throwError)(() => error);
|
|
71
89
|
})
|
|
72
90
|
);
|
|
73
91
|
}
|
|
74
92
|
return next.handle().pipe(
|
|
75
|
-
map((data) => {
|
|
93
|
+
(0, import_operators.map)((data) => {
|
|
76
94
|
const cacheDuration = gqlMeta.signalOption.cache;
|
|
77
95
|
if (typeof cacheDuration === "number") {
|
|
78
96
|
void __privateMethod(this, _setCache, setCache_fn).call(this, cacheKey, data, cacheDuration);
|
|
@@ -80,10 +98,10 @@ let CacheInterceptor = class {
|
|
|
80
98
|
}
|
|
81
99
|
return data;
|
|
82
100
|
}),
|
|
83
|
-
catchError((error) => {
|
|
101
|
+
(0, import_operators.catchError)((error) => {
|
|
84
102
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
85
103
|
__privateGet(this, _logger).error(`Error in cache interceptor for ${cacheKey}: ${errorMessage}`);
|
|
86
|
-
return throwError(() => error);
|
|
104
|
+
return (0, import_rxjs.throwError)(() => error);
|
|
87
105
|
})
|
|
88
106
|
);
|
|
89
107
|
}
|
|
@@ -119,40 +137,40 @@ setCache_fn = async function(key, data, ttlMs) {
|
|
|
119
137
|
}
|
|
120
138
|
};
|
|
121
139
|
CacheInterceptor = __decorateClass([
|
|
122
|
-
Injectable(),
|
|
123
|
-
__decorateParam(0, Inject("REDIS_CLIENT"))
|
|
140
|
+
(0, import_common2.Injectable)(),
|
|
141
|
+
__decorateParam(0, (0, import_common2.Inject)("REDIS_CLIENT"))
|
|
124
142
|
], CacheInterceptor);
|
|
125
143
|
let TimeoutInterceptor = class {
|
|
126
144
|
intercept(context, next) {
|
|
127
|
-
const gqlMeta = getGqlMeta(context.getClass(), context.getHandler().name);
|
|
145
|
+
const gqlMeta = (0, import_signal.getGqlMeta)(context.getClass(), context.getHandler().name);
|
|
128
146
|
const timeoutMs = gqlMeta.signalOption.timeout ?? 3e4;
|
|
129
147
|
if (timeoutMs === 0)
|
|
130
148
|
return next.handle();
|
|
131
149
|
return next.handle().pipe(
|
|
132
|
-
timeout(timeoutMs),
|
|
133
|
-
catchError((err) => {
|
|
134
|
-
if (err instanceof TimeoutError)
|
|
135
|
-
return throwError(() => new RequestTimeoutException());
|
|
136
|
-
return throwError(() => err);
|
|
150
|
+
(0, import_operators.timeout)(timeoutMs),
|
|
151
|
+
(0, import_operators.catchError)((err) => {
|
|
152
|
+
if (err instanceof import_rxjs.TimeoutError)
|
|
153
|
+
return (0, import_rxjs.throwError)(() => new import_common2.RequestTimeoutException());
|
|
154
|
+
return (0, import_rxjs.throwError)(() => err);
|
|
137
155
|
})
|
|
138
156
|
);
|
|
139
157
|
}
|
|
140
158
|
};
|
|
141
159
|
TimeoutInterceptor = __decorateClass([
|
|
142
|
-
Injectable()
|
|
160
|
+
(0, import_common2.Injectable)()
|
|
143
161
|
], TimeoutInterceptor);
|
|
144
162
|
let LoggingInterceptor = class {
|
|
145
|
-
logger = new Logger("IO");
|
|
163
|
+
logger = new import_common.Logger("IO");
|
|
146
164
|
intercept(context, next) {
|
|
147
165
|
const gqlReq = context.getArgByIndex(3);
|
|
148
|
-
const req = getRequest(context);
|
|
166
|
+
const req = (0, import_authGuards.getRequest)(context);
|
|
149
167
|
const reqType = gqlReq?.parentType?.name ?? req.method;
|
|
150
168
|
const reqName = gqlReq?.fieldName ?? req.url;
|
|
151
169
|
const before = Date.now();
|
|
152
|
-
const ip = GqlExecutionContext.create(context).getContext().req.ip;
|
|
170
|
+
const ip = import_graphql.GqlExecutionContext.create(context).getContext().req.ip;
|
|
153
171
|
this.logger.debug(`Before ${reqType}-${reqName} / ${ip} / ${before}`);
|
|
154
172
|
return next.handle().pipe(
|
|
155
|
-
tap(() => {
|
|
173
|
+
(0, import_operators.tap)(() => {
|
|
156
174
|
const after = Date.now();
|
|
157
175
|
this.logger.debug(`After ${reqType}-${reqName} / ${ip} / ${after} (${after - before}ms)`);
|
|
158
176
|
})
|
|
@@ -160,10 +178,11 @@ let LoggingInterceptor = class {
|
|
|
160
178
|
}
|
|
161
179
|
};
|
|
162
180
|
LoggingInterceptor = __decorateClass([
|
|
163
|
-
Injectable()
|
|
181
|
+
(0, import_common2.Injectable)()
|
|
164
182
|
], LoggingInterceptor);
|
|
165
|
-
export
|
|
183
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
184
|
+
0 && (module.exports = {
|
|
166
185
|
CacheInterceptor,
|
|
167
186
|
LoggingInterceptor,
|
|
168
187
|
TimeoutInterceptor
|
|
169
|
-
};
|
|
188
|
+
});
|
package/src/interceptors.js
CHANGED
|
@@ -1,20 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
3
|
var __decorateClass = (decorators, target, key, kind) => {
|
|
19
4
|
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
20
5
|
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
@@ -42,55 +27,52 @@ var __privateMethod = (obj, member, method) => {
|
|
|
42
27
|
__accessCheck(obj, member, "access private method");
|
|
43
28
|
return method;
|
|
44
29
|
};
|
|
45
|
-
var interceptors_exports = {};
|
|
46
|
-
__export(interceptors_exports, {
|
|
47
|
-
CacheInterceptor: () => CacheInterceptor,
|
|
48
|
-
LoggingInterceptor: () => LoggingInterceptor,
|
|
49
|
-
TimeoutInterceptor: () => TimeoutInterceptor
|
|
50
|
-
});
|
|
51
|
-
module.exports = __toCommonJS(interceptors_exports);
|
|
52
|
-
var import_common = require("@akanjs/common");
|
|
53
|
-
var import_signal = require("@akanjs/signal");
|
|
54
|
-
var import_common2 = require("@nestjs/common");
|
|
55
|
-
var import_graphql = require("@nestjs/graphql");
|
|
56
|
-
var import_rxjs = require("rxjs");
|
|
57
|
-
var import_operators = require("rxjs/operators");
|
|
58
|
-
var import_authGuards = require("./authGuards");
|
|
59
30
|
var _logger, _CACHE_PREFIX, _generateCacheKey, generateCacheKey_fn, _getCache, getCache_fn, _setCache, setCache_fn;
|
|
31
|
+
import { Logger } from "@akanjs/common";
|
|
32
|
+
import { getGqlMeta } from "@akanjs/signal";
|
|
33
|
+
import {
|
|
34
|
+
Inject,
|
|
35
|
+
Injectable,
|
|
36
|
+
RequestTimeoutException
|
|
37
|
+
} from "@nestjs/common";
|
|
38
|
+
import { GqlExecutionContext } from "@nestjs/graphql";
|
|
39
|
+
import { throwError, TimeoutError } from "rxjs";
|
|
40
|
+
import { catchError, map, tap, timeout } from "rxjs/operators";
|
|
41
|
+
import { getArgs, getRequest } from "./authGuards";
|
|
60
42
|
let CacheInterceptor = class {
|
|
61
43
|
constructor(redis) {
|
|
62
44
|
this.redis = redis;
|
|
63
45
|
__privateAdd(this, _generateCacheKey);
|
|
64
46
|
__privateAdd(this, _getCache);
|
|
65
47
|
__privateAdd(this, _setCache);
|
|
66
|
-
__privateAdd(this, _logger, new
|
|
48
|
+
__privateAdd(this, _logger, new Logger("CacheInterceptor"));
|
|
67
49
|
__privateAdd(this, _CACHE_PREFIX, "signal:");
|
|
68
50
|
}
|
|
69
51
|
async intercept(context, next) {
|
|
70
52
|
const handler = context.getHandler();
|
|
71
53
|
const signalKey = handler.name;
|
|
72
|
-
const gqlMeta =
|
|
54
|
+
const gqlMeta = getGqlMeta(context.getClass(), signalKey);
|
|
73
55
|
if (gqlMeta.type !== "Query" || !gqlMeta.signalOption.cache) {
|
|
74
56
|
if (gqlMeta.signalOption.cache)
|
|
75
57
|
__privateGet(this, _logger).warn(`CacheInterceptor: ${signalKey} is not Query endpoint or cache is not set`);
|
|
76
58
|
return next.handle();
|
|
77
59
|
}
|
|
78
|
-
const args =
|
|
60
|
+
const args = getArgs(context);
|
|
79
61
|
const cacheKey = __privateMethod(this, _generateCacheKey, generateCacheKey_fn).call(this, signalKey, args);
|
|
80
62
|
const cachedData = await __privateMethod(this, _getCache, getCache_fn).call(this, cacheKey);
|
|
81
63
|
if (cachedData) {
|
|
82
64
|
__privateGet(this, _logger).debug(`Cache hit for key: ${cacheKey}`);
|
|
83
65
|
return next.handle().pipe(
|
|
84
|
-
|
|
85
|
-
|
|
66
|
+
map(() => cachedData),
|
|
67
|
+
catchError((error) => {
|
|
86
68
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
87
69
|
__privateGet(this, _logger).error(`Error in cache interceptor for ${cacheKey}: ${errorMessage}`);
|
|
88
|
-
return
|
|
70
|
+
return throwError(() => error);
|
|
89
71
|
})
|
|
90
72
|
);
|
|
91
73
|
}
|
|
92
74
|
return next.handle().pipe(
|
|
93
|
-
|
|
75
|
+
map((data) => {
|
|
94
76
|
const cacheDuration = gqlMeta.signalOption.cache;
|
|
95
77
|
if (typeof cacheDuration === "number") {
|
|
96
78
|
void __privateMethod(this, _setCache, setCache_fn).call(this, cacheKey, data, cacheDuration);
|
|
@@ -98,10 +80,10 @@ let CacheInterceptor = class {
|
|
|
98
80
|
}
|
|
99
81
|
return data;
|
|
100
82
|
}),
|
|
101
|
-
|
|
83
|
+
catchError((error) => {
|
|
102
84
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
103
85
|
__privateGet(this, _logger).error(`Error in cache interceptor for ${cacheKey}: ${errorMessage}`);
|
|
104
|
-
return
|
|
86
|
+
return throwError(() => error);
|
|
105
87
|
})
|
|
106
88
|
);
|
|
107
89
|
}
|
|
@@ -137,40 +119,40 @@ setCache_fn = async function(key, data, ttlMs) {
|
|
|
137
119
|
}
|
|
138
120
|
};
|
|
139
121
|
CacheInterceptor = __decorateClass([
|
|
140
|
-
|
|
141
|
-
__decorateParam(0,
|
|
122
|
+
Injectable(),
|
|
123
|
+
__decorateParam(0, Inject("REDIS_CLIENT"))
|
|
142
124
|
], CacheInterceptor);
|
|
143
125
|
let TimeoutInterceptor = class {
|
|
144
126
|
intercept(context, next) {
|
|
145
|
-
const gqlMeta =
|
|
127
|
+
const gqlMeta = getGqlMeta(context.getClass(), context.getHandler().name);
|
|
146
128
|
const timeoutMs = gqlMeta.signalOption.timeout ?? 3e4;
|
|
147
129
|
if (timeoutMs === 0)
|
|
148
130
|
return next.handle();
|
|
149
131
|
return next.handle().pipe(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (err instanceof
|
|
153
|
-
return
|
|
154
|
-
return
|
|
132
|
+
timeout(timeoutMs),
|
|
133
|
+
catchError((err) => {
|
|
134
|
+
if (err instanceof TimeoutError)
|
|
135
|
+
return throwError(() => new RequestTimeoutException());
|
|
136
|
+
return throwError(() => err);
|
|
155
137
|
})
|
|
156
138
|
);
|
|
157
139
|
}
|
|
158
140
|
};
|
|
159
141
|
TimeoutInterceptor = __decorateClass([
|
|
160
|
-
|
|
142
|
+
Injectable()
|
|
161
143
|
], TimeoutInterceptor);
|
|
162
144
|
let LoggingInterceptor = class {
|
|
163
|
-
logger = new
|
|
145
|
+
logger = new Logger("IO");
|
|
164
146
|
intercept(context, next) {
|
|
165
147
|
const gqlReq = context.getArgByIndex(3);
|
|
166
|
-
const req =
|
|
148
|
+
const req = getRequest(context);
|
|
167
149
|
const reqType = gqlReq?.parentType?.name ?? req.method;
|
|
168
150
|
const reqName = gqlReq?.fieldName ?? req.url;
|
|
169
151
|
const before = Date.now();
|
|
170
|
-
const ip =
|
|
152
|
+
const ip = GqlExecutionContext.create(context).getContext().req.ip;
|
|
171
153
|
this.logger.debug(`Before ${reqType}-${reqName} / ${ip} / ${before}`);
|
|
172
154
|
return next.handle().pipe(
|
|
173
|
-
|
|
155
|
+
tap(() => {
|
|
174
156
|
const after = Date.now();
|
|
175
157
|
this.logger.debug(`After ${reqType}-${reqName} / ${ip} / ${after} (${after - before}ms)`);
|
|
176
158
|
})
|
|
@@ -178,11 +160,10 @@ let LoggingInterceptor = class {
|
|
|
178
160
|
}
|
|
179
161
|
};
|
|
180
162
|
LoggingInterceptor = __decorateClass([
|
|
181
|
-
|
|
163
|
+
Injectable()
|
|
182
164
|
], LoggingInterceptor);
|
|
183
|
-
|
|
184
|
-
0 && (module.exports = {
|
|
165
|
+
export {
|
|
185
166
|
CacheInterceptor,
|
|
186
167
|
LoggingInterceptor,
|
|
187
168
|
TimeoutInterceptor
|
|
188
|
-
}
|
|
169
|
+
};
|
package/src/mongoose.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var mongoose_exports = {};
|
|
29
|
+
__export(mongoose_exports, {
|
|
30
|
+
initMongoDB: () => initMongoDB
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(mongoose_exports);
|
|
33
|
+
var import_common = require("@akanjs/common");
|
|
34
|
+
var import_mongoose = __toESM(require("mongoose"), 1);
|
|
35
|
+
const initMongoDB = ({
|
|
36
|
+
logging,
|
|
37
|
+
threshold = 5e3,
|
|
38
|
+
sendReport = false
|
|
39
|
+
}) => {
|
|
40
|
+
const mongoDBLogger = new import_common.Logger("MongoDB");
|
|
41
|
+
if (logging)
|
|
42
|
+
import_mongoose.default.set("debug", function(collection, method, ...methodArgs) {
|
|
43
|
+
mongoDBLogger.verbose(
|
|
44
|
+
`${collection}.${method}(${methodArgs.slice(0, -1).map((arg) => JSON.stringify(arg)).join(", ")})`
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
const originalExec = import_mongoose.default.Query.prototype.exec;
|
|
48
|
+
const getQueryInfo = (queryAgent) => {
|
|
49
|
+
const model = queryAgent.model;
|
|
50
|
+
const collectionName = model.collection.collectionName;
|
|
51
|
+
const dbName = model.db.name;
|
|
52
|
+
const query = queryAgent.getQuery();
|
|
53
|
+
const queryOptions = queryAgent.getOptions();
|
|
54
|
+
return { dbName, collectionName, query, queryOptions };
|
|
55
|
+
};
|
|
56
|
+
import_mongoose.default.Query.prototype.exec = function(...args) {
|
|
57
|
+
const start = Date.now();
|
|
58
|
+
return originalExec.apply(this, args).then((result) => {
|
|
59
|
+
const duration = Date.now() - start;
|
|
60
|
+
const { dbName, collectionName, query, queryOptions } = getQueryInfo(this);
|
|
61
|
+
if (logging)
|
|
62
|
+
mongoDBLogger.verbose(
|
|
63
|
+
`Queried ${dbName}.${collectionName}.query(${JSON.stringify(query)}, ${JSON.stringify(
|
|
64
|
+
queryOptions
|
|
65
|
+
)}) - ${duration}ms`
|
|
66
|
+
);
|
|
67
|
+
return result;
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
const originalAggregate = import_mongoose.default.Model.aggregate;
|
|
71
|
+
const getAggregateInfo = (aggregateModel) => {
|
|
72
|
+
const dbName = aggregateModel.db.db?.databaseName ?? "unknown";
|
|
73
|
+
const collectionName = aggregateModel.collection.collectionName;
|
|
74
|
+
return { dbName, collectionName };
|
|
75
|
+
};
|
|
76
|
+
import_mongoose.default.Model.aggregate = function(...args) {
|
|
77
|
+
const startTime = Date.now();
|
|
78
|
+
return originalAggregate.apply(this, args).then((result) => {
|
|
79
|
+
const duration = Date.now() - startTime;
|
|
80
|
+
const { dbName, collectionName } = getAggregateInfo(this);
|
|
81
|
+
if (logging)
|
|
82
|
+
mongoDBLogger.verbose(
|
|
83
|
+
`Aggregated ${dbName}.${collectionName}.aggregate(${args.map((arg) => JSON.stringify(arg)).join(", ")}) - ${duration}ms`
|
|
84
|
+
);
|
|
85
|
+
return result;
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
import_mongoose.default.set("transactionAsyncLocalStorage", true);
|
|
89
|
+
};
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
initMongoDB
|
|
93
|
+
});
|
package/src/mongoose.js
CHANGED
|
@@ -1,50 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var mongoose_exports = {};
|
|
29
|
-
__export(mongoose_exports, {
|
|
30
|
-
initMongoDB: () => initMongoDB
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(mongoose_exports);
|
|
33
|
-
var import_common = require("@akanjs/common");
|
|
34
|
-
var import_mongoose = __toESM(require("mongoose"));
|
|
1
|
+
import { Logger } from "@akanjs/common";
|
|
2
|
+
import mongoose from "mongoose";
|
|
35
3
|
const initMongoDB = ({
|
|
36
4
|
logging,
|
|
37
5
|
threshold = 5e3,
|
|
38
6
|
sendReport = false
|
|
39
7
|
}) => {
|
|
40
|
-
const mongoDBLogger = new
|
|
8
|
+
const mongoDBLogger = new Logger("MongoDB");
|
|
41
9
|
if (logging)
|
|
42
|
-
|
|
10
|
+
mongoose.set("debug", function(collection, method, ...methodArgs) {
|
|
43
11
|
mongoDBLogger.verbose(
|
|
44
12
|
`${collection}.${method}(${methodArgs.slice(0, -1).map((arg) => JSON.stringify(arg)).join(", ")})`
|
|
45
13
|
);
|
|
46
14
|
});
|
|
47
|
-
const originalExec =
|
|
15
|
+
const originalExec = mongoose.Query.prototype.exec;
|
|
48
16
|
const getQueryInfo = (queryAgent) => {
|
|
49
17
|
const model = queryAgent.model;
|
|
50
18
|
const collectionName = model.collection.collectionName;
|
|
@@ -53,7 +21,7 @@ const initMongoDB = ({
|
|
|
53
21
|
const queryOptions = queryAgent.getOptions();
|
|
54
22
|
return { dbName, collectionName, query, queryOptions };
|
|
55
23
|
};
|
|
56
|
-
|
|
24
|
+
mongoose.Query.prototype.exec = function(...args) {
|
|
57
25
|
const start = Date.now();
|
|
58
26
|
return originalExec.apply(this, args).then((result) => {
|
|
59
27
|
const duration = Date.now() - start;
|
|
@@ -67,13 +35,13 @@ const initMongoDB = ({
|
|
|
67
35
|
return result;
|
|
68
36
|
});
|
|
69
37
|
};
|
|
70
|
-
const originalAggregate =
|
|
38
|
+
const originalAggregate = mongoose.Model.aggregate;
|
|
71
39
|
const getAggregateInfo = (aggregateModel) => {
|
|
72
40
|
const dbName = aggregateModel.db.db?.databaseName ?? "unknown";
|
|
73
41
|
const collectionName = aggregateModel.collection.collectionName;
|
|
74
42
|
return { dbName, collectionName };
|
|
75
43
|
};
|
|
76
|
-
|
|
44
|
+
mongoose.Model.aggregate = function(...args) {
|
|
77
45
|
const startTime = Date.now();
|
|
78
46
|
return originalAggregate.apply(this, args).then((result) => {
|
|
79
47
|
const duration = Date.now() - startTime;
|
|
@@ -85,9 +53,8 @@ const initMongoDB = ({
|
|
|
85
53
|
return result;
|
|
86
54
|
});
|
|
87
55
|
};
|
|
88
|
-
|
|
56
|
+
mongoose.set("transactionAsyncLocalStorage", true);
|
|
89
57
|
};
|
|
90
|
-
|
|
91
|
-
0 && (module.exports = {
|
|
58
|
+
export {
|
|
92
59
|
initMongoDB
|
|
93
|
-
}
|
|
60
|
+
};
|