@viplance/nestjs-logger 0.1.2 → 0.1.4
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 +31 -27
- package/dist/entities/log.entity.d.ts +5 -0
- package/dist/entities/log.entity.js +5 -0
- package/dist/entities/log.entity.js.map +1 -1
- package/dist/exception.filter.d.ts +4 -0
- package/dist/exception.filter.js +34 -0
- package/dist/exception.filter.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/log.interceptor.d.ts +3 -2
- package/dist/log.interceptor.js.map +1 -1
- package/dist/log.module.js +1 -1
- package/dist/log.module.js.map +1 -1
- package/dist/services/log.service.d.ts +12 -9
- package/dist/services/log.service.js +63 -22
- package/dist/services/log.service.js.map +1 -1
- package/dist/services/memory-db.service.d.ts +14 -5
- package/dist/services/memory-db.service.js +74 -23
- package/dist/services/memory-db.service.js.map +1 -1
- package/dist/types/context.type.d.ts +7 -0
- package/dist/types/context.type.js +3 -0
- package/dist/types/context.type.js.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -1
- package/package.json +1 -1
- package/src/entities/log.entity.ts +6 -1
- package/src/index.ts +1 -0
- package/src/log.interceptor.ts +3 -6
- package/src/log.module.ts +1 -1
- package/src/services/log.service.ts +95 -36
- package/src/services/memory-db.service.ts +115 -22
- package/src/types/context.type.ts +7 -0
- package/src/types/index.ts +1 -0
package/README.md
CHANGED
|
@@ -1,34 +1,38 @@
|
|
|
1
1
|
## @viplance/nestjs-logger
|
|
2
2
|
# NestJS internal logging system
|
|
3
3
|
|
|
4
|
-
1. Install the package npm i @viplance/nestjs-logger
|
|
5
|
-
2. Import the module in app.module.ts
|
|
6
|
-
import { LogModule } from '@viplance/nestjs-logger'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
3.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
The logs could be available at your_application_url/<path>
|
|
30
|
-
By default the logs will be stored in memory and deleted when the application stops.
|
|
4
|
+
1. Install the package npm i @viplance/nestjs-logger<br />
|
|
5
|
+
2. Import the module in app.module.ts<br />
|
|
6
|
+
import { LogModule } from '@viplance/nestjs-logger';<br />
|
|
7
|
+
@Module({<br />
|
|
8
|
+
imports: [<br />
|
|
9
|
+
...,<br />
|
|
10
|
+
LogModule,<br />
|
|
11
|
+
]<br />
|
|
12
|
+
})<br />
|
|
13
|
+
<br />
|
|
14
|
+
3. Connect the module in main.ts
|
|
15
|
+
// use the memory to store logs
|
|
16
|
+
await LogModule.connect(app, {<br />
|
|
17
|
+
path: '/logs',<br />
|
|
18
|
+
});<br /><br />
|
|
19
|
+
// use the database to store logs
|
|
20
|
+
await LogModule.connect(app, {<br />
|
|
21
|
+
path: '/logs',<br />
|
|
22
|
+
database: {<br />
|
|
23
|
+
type: 'mongodb',<br />
|
|
24
|
+
host: 'localhost',<br />
|
|
25
|
+
port: 27017,<br />
|
|
26
|
+
collection: 'logs'<br />
|
|
27
|
+
}<br />
|
|
28
|
+
});<br />
|
|
31
29
|
|
|
30
|
+
* `path` and `database` options are optional<br />
|
|
31
|
+
4. Use the LogService in case of custom logs to debug the application.
|
|
32
|
+
<br />
|
|
33
|
+
The logs could be available at your_application_url/<path><br />
|
|
34
|
+
By default the logs will be stored in memory and deleted when the application stops.<br />
|
|
35
|
+
<br />
|
|
32
36
|
Available service methods:
|
|
33
37
|
- log()
|
|
34
38
|
- error()
|
|
@@ -13,6 +13,11 @@ function createLogEntity(name) {
|
|
|
13
13
|
},
|
|
14
14
|
type: { type: String },
|
|
15
15
|
message: { type: String },
|
|
16
|
+
count: { type: Number, default: 1 },
|
|
17
|
+
context: { type: String, nullable: true },
|
|
18
|
+
trace: { type: String, nullable: true },
|
|
19
|
+
createdAt: { type: Date },
|
|
20
|
+
updatedAt: { type: Date },
|
|
16
21
|
},
|
|
17
22
|
});
|
|
18
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.entity.js","sourceRoot":"","sources":["../../src/entities/log.entity.ts"],"names":[],"mappings":";;AAEA,
|
|
1
|
+
{"version":3,"file":"log.entity.js","sourceRoot":"","sources":["../../src/entities/log.entity.ts"],"names":[],"mappings":";;AAEA,0CAkBC;AApBD,qCAA4C;AAE5C,SAAgB,eAAe,CAAC,IAAY;IAC1C,OAAO,IAAI,sBAAY,CAAC;QACtB,IAAI;QACJ,OAAO,EAAE;YACP,GAAG,EAAE;gBACH,IAAI,EAAE,MAAM;gBACZ,QAAQ,EAAE,IAAI;gBACd,OAAO,EAAE,IAAI;aACd;YACD,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;YACtB,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;YACzB,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE;YACnC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;YACzC,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;YACvC,SAAS,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;YACzB,SAAS,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;SAC1B;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.HttpExceptionFilter = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
// import { Request, Response } from "express";
|
|
12
|
+
let HttpExceptionFilter = class HttpExceptionFilter {
|
|
13
|
+
catch(exception, host) {
|
|
14
|
+
const ctx = host.switchToHttp();
|
|
15
|
+
const status = exception.getStatus();
|
|
16
|
+
console.log("CTX", ctx);
|
|
17
|
+
console.log("status", status);
|
|
18
|
+
console.log("Exception", exception);
|
|
19
|
+
// const response = ctx.getResponse<Response>();
|
|
20
|
+
// const request = ctx.getRequest<Request>();
|
|
21
|
+
// const status = exception.getStatus();
|
|
22
|
+
// response.status(status).json({
|
|
23
|
+
// statusCode: status,
|
|
24
|
+
// timestamp: new Date().toISOString(),
|
|
25
|
+
// path: request.url,
|
|
26
|
+
// message: exception.message,
|
|
27
|
+
// });
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.HttpExceptionFilter = HttpExceptionFilter;
|
|
31
|
+
exports.HttpExceptionFilter = HttpExceptionFilter = __decorate([
|
|
32
|
+
(0, common_1.Catch)(common_1.HttpException)
|
|
33
|
+
], HttpExceptionFilter);
|
|
34
|
+
//# sourceMappingURL=exception.filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exception.filter.js","sourceRoot":"","sources":["../src/exception.filter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAKwB;AACxB,+CAA+C;AAGxC,IAAM,mBAAmB,GAAzB,MAAM,mBAAmB;IAC9B,KAAK,CAAC,SAAwB,EAAE,IAAmB;QACjD,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,EAAE,CAAC;QACrC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACxB,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAC9B,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;QACpC,gDAAgD;QAChD,6CAA6C;QAC7C,wCAAwC;QAExC,iCAAiC;QACjC,wBAAwB;QACxB,yCAAyC;QACzC,uBAAuB;QACvB,gCAAgC;QAChC,MAAM;IACR,CAAC;CACF,CAAA;AAlBY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,cAAK,EAAC,sBAAa,CAAC;GACR,mBAAmB,CAkB/B"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,5 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./log.interceptor"), exports);
|
|
18
18
|
__exportStar(require("./log.module"), exports);
|
|
19
|
+
__exportStar(require("./types/index"), exports);
|
|
19
20
|
__exportStar(require("./services/log.service"), exports);
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,+CAA6B;AAC7B,yDAAuC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,+CAA6B;AAC7B,gDAA8B;AAC9B,yDAAuC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type { CallHandler,
|
|
1
|
+
import type { CallHandler, NestInterceptor } from "@nestjs/common";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
3
|
import { LogService } from "./services/log.service";
|
|
4
|
+
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
4
5
|
export declare class LogInterceptor implements NestInterceptor {
|
|
5
6
|
private readonly logService;
|
|
6
7
|
constructor(logService: LogService);
|
|
7
|
-
intercept(context:
|
|
8
|
+
intercept(context: ExecutionContextHost, next: CallHandler): Observable<any>;
|
|
8
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.interceptor.js","sourceRoot":"","sources":["../src/log.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"log.interceptor.js","sourceRoot":"","sources":["../src/log.interceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,2CAAoD;AACpD,+BAAuC;AACvC,wDAAoD;AAI7C,IAAM,cAAc,GAApB,MAAM,cAAc;IACzB,YAAiD,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;IAAG,CAAC;IAE3E,SAAS,CAAC,OAA6B,EAAE,IAAiB;QACxD,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CACvB,IAAA,UAAG,EAAC;YACF,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;;gBACf,gBAAgB;gBAChB,MAAA,IAAI,CAAC,UAAU,0CAAE,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YAC9D,CAAC;YACD,QAAQ,EAAE,GAAG,EAAE;gBACb,iBAAiB;YACnB,CAAC;SACF,CAAC,CACH,CAAC;IACJ,CAAC;CACF,CAAA;AAhBY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAEE,WAAA,IAAA,eAAM,EAAC,wBAAU,CAAC,CAAA;qCAA8B,wBAAU;GAD5D,cAAc,CAgB1B"}
|
package/dist/log.module.js
CHANGED
|
@@ -20,7 +20,7 @@ let LogModule = class LogModule {
|
|
|
20
20
|
if (options === null || options === void 0 ? void 0 : options.path) {
|
|
21
21
|
const httpAdapter = app.getHttpAdapter();
|
|
22
22
|
httpAdapter.get(options.path, async (req, res) => {
|
|
23
|
-
res.json(logService.getAll());
|
|
23
|
+
res.json(await logService.getAll());
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
if (options) {
|
package/dist/log.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAgD;AAChD,wDAAoD;AACpD,oEAA+D;AAC/D,uDAAmD;AAEnD,6CAAgD;AAQzC,IAAM,SAAS,GAAf,MAAM,SAAS;IACb,MAAM,CAAC,KAAK,CAAC,OAAO,CACzB,GAAQ,EACR,OAA0B;QAE1B,GAAG,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAExB,MAAM,UAAU,GAAe,MAAM,GAAG,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAE7D,GAAG,CAAC,qBAAqB,CAAC,IAAI,gCAAc,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,uBAAuB;QAElF,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,EAAE,CAAC;YAClB,MAAM,WAAW,GAAG,GAAG,CAAC,cAAc,EAAE,CAAC;YACzC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,GAAQ,EAAE,GAAQ,EAAE,EAAE;gBACzD,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAgD;AAChD,wDAAoD;AACpD,oEAA+D;AAC/D,uDAAmD;AAEnD,6CAAgD;AAQzC,IAAM,SAAS,GAAf,MAAM,SAAS;IACb,MAAM,CAAC,KAAK,CAAC,OAAO,CACzB,GAAQ,EACR,OAA0B;QAE1B,GAAG,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAExB,MAAM,UAAU,GAAe,MAAM,GAAG,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAE7D,GAAG,CAAC,qBAAqB,CAAC,IAAI,gCAAc,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,uBAAuB;QAElF,IAAI,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,EAAE,CAAC;YAClB,MAAM,WAAW,GAAG,GAAG,CAAC,cAAc,EAAE,CAAC;YACzC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,GAAQ,EAAE,GAAQ,EAAE,EAAE;gBACzD,GAAG,CAAC,IAAI,CAAC,MAAM,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;YACtC,CAAC,CAAC,CAAC;QACL,CAAC;QAED,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;CACF,CAAA;AAtBY,8BAAS;oBAAT,SAAS;IANrB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,uBAAa,CAAC;QACxB,SAAS,EAAE,CAAC,wBAAU,EAAE,mCAAe,CAAC;QACxC,OAAO,EAAE,CAAC,uBAAa,EAAE,wBAAU,EAAE,mCAAe,CAAC;KACtD,CAAC;GACW,SAAS,CAsBrB"}
|
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LoggerService } from "@nestjs/common";
|
|
2
2
|
import { MemoryDbService } from "./memory-db.service";
|
|
3
3
|
import { LogModuleOptions } from "../types";
|
|
4
4
|
import { DataSource, EntitySchema } from "typeorm";
|
|
5
|
+
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
5
6
|
export declare class LogService implements LoggerService {
|
|
6
|
-
private readonly
|
|
7
|
+
private readonly memoryDbService;
|
|
7
8
|
static connection: DataSource;
|
|
8
9
|
static Log: EntitySchema;
|
|
9
|
-
constructor(
|
|
10
|
+
constructor(memoryDbService: MemoryDbService);
|
|
10
11
|
connectDb(options: LogModuleOptions): Promise<DataSource>;
|
|
11
|
-
log(message: string, context?:
|
|
12
|
-
error(message: string, trace?: string, context?:
|
|
13
|
-
warn(message: string, context?:
|
|
14
|
-
debug(message: string, context?:
|
|
15
|
-
verbose(message: string, context?:
|
|
16
|
-
getAll(): any[]
|
|
12
|
+
log(message: string, context?: ExecutionContextHost): void;
|
|
13
|
+
error(message: string, trace?: string, context?: ExecutionContextHost): void;
|
|
14
|
+
warn(message: string, context?: ExecutionContextHost): void;
|
|
15
|
+
debug(message: string, context?: ExecutionContextHost): void;
|
|
16
|
+
verbose(message: string, context?: ExecutionContextHost): void;
|
|
17
|
+
getAll(): Promise<any[]>;
|
|
17
18
|
private smartInsert;
|
|
19
|
+
private getConnection;
|
|
20
|
+
private parseContext;
|
|
18
21
|
}
|
|
@@ -18,14 +18,12 @@ const types_1 = require("../types");
|
|
|
18
18
|
const typeorm_1 = require("typeorm");
|
|
19
19
|
const log_entity_1 = require("../entities/log.entity");
|
|
20
20
|
let LogService = LogService_1 = class LogService {
|
|
21
|
-
constructor(
|
|
22
|
-
|
|
23
|
-
this.MemoryDbService = MemoryDbService;
|
|
21
|
+
constructor(memoryDbService) {
|
|
22
|
+
this.memoryDbService = memoryDbService;
|
|
24
23
|
}
|
|
25
24
|
async connectDb(options) {
|
|
26
25
|
var _a, _b, _c, _d, _e, _f;
|
|
27
|
-
|
|
28
|
-
LogService_1.Log = (0, log_entity_1.createLogEntity)(tableName);
|
|
26
|
+
LogService_1.Log = (0, log_entity_1.createLogEntity)(((_a = options.database) === null || _a === void 0 ? void 0 : _a.collection) || ((_b = options.database) === null || _b === void 0 ? void 0 : _b.table) || defaults_1.defaultTable);
|
|
29
27
|
const dataSourceOptions = {
|
|
30
28
|
type: (_c = options.database) === null || _c === void 0 ? void 0 : _c.type,
|
|
31
29
|
database: (_d = options.database) === null || _d === void 0 ? void 0 : _d.database,
|
|
@@ -38,14 +36,14 @@ let LogService = LogService_1 = class LogService {
|
|
|
38
36
|
return LogService_1.connection;
|
|
39
37
|
}
|
|
40
38
|
log(message, context) {
|
|
41
|
-
this.smartInsert(
|
|
39
|
+
this.smartInsert({
|
|
42
40
|
type: types_1.LogType.LOG,
|
|
43
41
|
message,
|
|
44
42
|
context,
|
|
45
43
|
});
|
|
46
44
|
}
|
|
47
45
|
error(message, trace, context) {
|
|
48
|
-
this.smartInsert(
|
|
46
|
+
this.smartInsert({
|
|
49
47
|
type: types_1.LogType.ERROR,
|
|
50
48
|
message,
|
|
51
49
|
trace,
|
|
@@ -53,47 +51,90 @@ let LogService = LogService_1 = class LogService {
|
|
|
53
51
|
});
|
|
54
52
|
}
|
|
55
53
|
warn(message, context) {
|
|
56
|
-
this.smartInsert(
|
|
54
|
+
this.smartInsert({
|
|
57
55
|
type: types_1.LogType.WARN,
|
|
58
56
|
message,
|
|
59
57
|
context,
|
|
60
58
|
});
|
|
61
59
|
}
|
|
62
60
|
debug(message, context) {
|
|
63
|
-
this.smartInsert(
|
|
61
|
+
this.smartInsert({
|
|
64
62
|
type: types_1.LogType.DEBUG,
|
|
65
63
|
message,
|
|
66
64
|
context,
|
|
67
65
|
});
|
|
68
66
|
}
|
|
69
67
|
verbose(message, context) {
|
|
70
|
-
this.smartInsert(
|
|
68
|
+
this.smartInsert({
|
|
71
69
|
type: types_1.LogType.VERBOSE,
|
|
72
70
|
message,
|
|
73
71
|
context,
|
|
74
72
|
});
|
|
75
73
|
}
|
|
76
|
-
getAll() {
|
|
77
|
-
return this.
|
|
74
|
+
async getAll() {
|
|
75
|
+
return this.getConnection().find(LogService_1.Log, {
|
|
76
|
+
select: ["type", "message", "count", "createdAt", "updatedAt"],
|
|
77
|
+
});
|
|
78
78
|
}
|
|
79
|
-
async smartInsert(
|
|
80
|
-
|
|
79
|
+
async smartInsert(data) {
|
|
80
|
+
const currentDate = new Date();
|
|
81
|
+
const connection = this.getConnection();
|
|
82
|
+
// find the same log in DB
|
|
83
|
+
const log = await connection.findOne(LogService_1.Log, {
|
|
84
|
+
where: {
|
|
85
|
+
type: data.type,
|
|
86
|
+
message: data.message,
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
const context = data.context ? this.parseContext(data.context) : undefined;
|
|
90
|
+
if (log) {
|
|
91
|
+
return await connection.update(LogService_1.Log, log._id, {
|
|
92
|
+
context,
|
|
93
|
+
trace: data.trace,
|
|
94
|
+
count: log.count + 1,
|
|
95
|
+
updatedAt: currentDate,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return await connection.insert(LogService_1.Log, {
|
|
81
99
|
type: data.type,
|
|
82
100
|
message: data.message,
|
|
101
|
+
context,
|
|
102
|
+
trace: data.trace,
|
|
83
103
|
count: 1,
|
|
104
|
+
createdAt: currentDate,
|
|
105
|
+
updatedAt: currentDate,
|
|
84
106
|
});
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
107
|
+
}
|
|
108
|
+
getConnection() {
|
|
109
|
+
return LogService_1.connection.manager || this.memoryDbService;
|
|
110
|
+
}
|
|
111
|
+
parseContext(context) {
|
|
112
|
+
const res = {};
|
|
113
|
+
const args = context.getArgs();
|
|
114
|
+
for (const arg of args) {
|
|
115
|
+
if (arg.rawHeaders) {
|
|
116
|
+
res.rawHeaders = arg.rawHeaders;
|
|
117
|
+
}
|
|
118
|
+
if (arg.url) {
|
|
119
|
+
res.url = arg.url;
|
|
120
|
+
}
|
|
121
|
+
if (arg.method) {
|
|
122
|
+
res.method = arg.method;
|
|
123
|
+
}
|
|
124
|
+
if (arg.params) {
|
|
125
|
+
res.params = arg.params;
|
|
126
|
+
}
|
|
127
|
+
if (arg.body) {
|
|
128
|
+
res.body = arg.body;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return res;
|
|
91
132
|
}
|
|
92
133
|
};
|
|
93
134
|
exports.LogService = LogService;
|
|
135
|
+
LogService.Log = (0, log_entity_1.createLogEntity)(defaults_1.defaultTable);
|
|
94
136
|
exports.LogService = LogService = LogService_1 = __decorate([
|
|
95
137
|
(0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }),
|
|
96
|
-
__metadata("design:paramtypes", [memory_db_service_1.MemoryDbService
|
|
97
|
-
])
|
|
138
|
+
__metadata("design:paramtypes", [memory_db_service_1.MemoryDbService])
|
|
98
139
|
], LogService);
|
|
99
140
|
//# sourceMappingURL=log.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.service.js","sourceRoot":"","sources":["../../src/services/log.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"log.service.js","sourceRoot":"","sources":["../../src/services/log.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAkE;AAClE,2DAAsD;AACtD,0CAA2C;AAC3C,oCAA8D;AAC9D,qCAKiB;AACjB,uDAAyD;AAIlD,IAAM,UAAU,kBAAhB,MAAM,UAAU;IAIrB,YAA6B,eAAgC;QAAhC,oBAAe,GAAf,eAAe,CAAiB;IAAG,CAAC;IAEjE,KAAK,CAAC,SAAS,CAAC,OAAyB;;QACvC,YAAU,CAAC,GAAG,GAAG,IAAA,4BAAe,EAC9B,CAAA,MAAA,OAAO,CAAC,QAAQ,0CAAE,UAAU,MAAI,MAAA,OAAO,CAAC,QAAQ,0CAAE,KAAK,CAAA,IAAI,uBAAY,CACxE,CAAC;QAEF,MAAM,iBAAiB,GAAG;YACxB,IAAI,EAAE,MAAA,OAAO,CAAC,QAAQ,0CAAE,IAAI;YAC5B,QAAQ,EAAE,MAAA,OAAO,CAAC,QA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IAAI,IAAI,CAAC,eAAe,CAAC;IAC/D,CAAC;IAEO,YAAY,CAAC,OAA6B;QAChD,MAAM,GAAG,GAAqB,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;QAE/B,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,GAAG,CAAC,UAAU,EAAE,CAAC;gBACnB,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC;YAClC,CAAC;YAED,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;gBACZ,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;YACpB,CAAC;YAED,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;gBACf,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;YAC1B,CAAC;YAED,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;gBACf,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;YAC1B,CAAC;YAED,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;gBACb,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;YACtB,CAAC;QACH,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC;;AAhJU,gCAAU;AAEd,cAAG,GAAiB,IAAA,4BAAe,EAAC,uBAAY,CAAC,AAA9C,CAA+C;qBAF9C,UAAU;IADtB,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,SAAS,EAAE,CAAC;qCAKS,mCAAe;GAJlD,UAAU,CAiJtB"}
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
+
import { EntitySchema } from "typeorm";
|
|
1
2
|
export declare class MemoryDbService {
|
|
2
3
|
private db;
|
|
3
4
|
constructor();
|
|
4
|
-
insert(
|
|
5
|
-
update(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
insert(entity: EntitySchema, data: any): string;
|
|
6
|
+
update(entity: EntitySchema, condition: any, data: any): Promise<string>;
|
|
7
|
+
find(entity: EntitySchema, options?: {
|
|
8
|
+
select?: string[];
|
|
9
|
+
}): Promise<any[]>;
|
|
10
|
+
getOneById(entity: EntitySchema, _id: string): Promise<any>;
|
|
11
|
+
findByProperty(entity: EntitySchema, field: string, value: string): any[];
|
|
12
|
+
findOne(entity: EntitySchema, condition: {
|
|
13
|
+
where: any;
|
|
14
|
+
}): Promise<any>;
|
|
15
|
+
private findIndex;
|
|
16
|
+
private partialEqual;
|
|
17
|
+
private getTableName;
|
|
9
18
|
}
|
|
@@ -18,42 +18,93 @@ const tables = [defaults_1.defaultTable];
|
|
|
18
18
|
let MemoryDbService = class MemoryDbService {
|
|
19
19
|
constructor() {
|
|
20
20
|
this.db = {};
|
|
21
|
+
this.getTableName = (entity) => entity.options.name;
|
|
21
22
|
for (const table of tables) {
|
|
22
23
|
this.db[table] = [];
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
|
-
insert(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
26
|
+
insert(entity, data) {
|
|
27
|
+
const table = this.getTableName(entity);
|
|
28
|
+
// generate new random _id
|
|
29
|
+
const randomData = (0, crypto_1.randomBytes)(24).toString("hex");
|
|
30
|
+
const _id = (0, crypto_1.createHash)("sha256").update(randomData).digest("hex");
|
|
29
31
|
this.db[table].push({
|
|
30
32
|
...data,
|
|
31
|
-
|
|
33
|
+
_id, // unique index
|
|
32
34
|
});
|
|
33
|
-
return
|
|
35
|
+
return _id;
|
|
34
36
|
}
|
|
35
|
-
update(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
async update(entity, condition, data) {
|
|
38
|
+
const table = this.getTableName(entity);
|
|
39
|
+
let index = null;
|
|
40
|
+
if (typeof condition === "string") {
|
|
41
|
+
index = this.findIndex(entity, { where: { _id: condition } });
|
|
42
|
+
}
|
|
43
|
+
if (condition === null || condition === void 0 ? void 0 : condition.where) {
|
|
44
|
+
index = this.findIndex(entity, condition);
|
|
45
|
+
}
|
|
46
|
+
if (index !== null) {
|
|
47
|
+
this.db[table][index] = {
|
|
48
|
+
...this.db[table][index],
|
|
49
|
+
...data,
|
|
50
|
+
};
|
|
51
|
+
return Promise.resolve(this.db[table][index]._id);
|
|
52
|
+
}
|
|
53
|
+
return Promise.reject();
|
|
44
54
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
async find(entity, options) {
|
|
56
|
+
const table = this.getTableName(entity);
|
|
57
|
+
let mapOptions = (obj) => obj;
|
|
58
|
+
if (options === null || options === void 0 ? void 0 : options.select) {
|
|
59
|
+
mapOptions = (obj) => {
|
|
60
|
+
const newObj = {};
|
|
61
|
+
for (const key of options.select || []) {
|
|
62
|
+
newObj[key] = obj[key];
|
|
63
|
+
}
|
|
64
|
+
return newObj;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return Promise.resolve(this.db[table].map(mapOptions));
|
|
50
68
|
}
|
|
51
|
-
getOneById(
|
|
52
|
-
|
|
69
|
+
async getOneById(entity, _id) {
|
|
70
|
+
const table = this.getTableName(entity);
|
|
71
|
+
return Promise.resolve(this.db[table].find((item) => item._id === _id));
|
|
53
72
|
}
|
|
54
|
-
|
|
73
|
+
findByProperty(entity, field, value) {
|
|
74
|
+
const table = this.getTableName(entity);
|
|
55
75
|
return this.db[table].filter((item) => item[field] === value);
|
|
56
76
|
}
|
|
77
|
+
findOne(entity, condition) {
|
|
78
|
+
const table = this.getTableName(entity);
|
|
79
|
+
return Promise.resolve(this.db[table].find((item) => this.partialEqual(condition.where, item)));
|
|
80
|
+
}
|
|
81
|
+
findIndex(entity, condition) {
|
|
82
|
+
const table = this.getTableName(entity);
|
|
83
|
+
return this.db[table].findIndex((item) => this.partialEqual(condition.where, item));
|
|
84
|
+
}
|
|
85
|
+
partialEqual(obj1, obj2) {
|
|
86
|
+
// compare primitive type values
|
|
87
|
+
if (obj1 === obj2)
|
|
88
|
+
return true;
|
|
89
|
+
// handle null or non-object values
|
|
90
|
+
if (obj1 == null ||
|
|
91
|
+
obj2 == null ||
|
|
92
|
+
typeof obj1 !== "object" ||
|
|
93
|
+
typeof obj2 !== "object") {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
// compare keys length
|
|
97
|
+
const keys1 = Object.keys(obj1);
|
|
98
|
+
const keys2 = Object.keys(obj2);
|
|
99
|
+
// compare values recursively
|
|
100
|
+
for (const key of keys1) {
|
|
101
|
+
if (!keys2.includes(key))
|
|
102
|
+
return false;
|
|
103
|
+
if (!this.partialEqual(obj1[key], obj2[key]))
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
57
108
|
};
|
|
58
109
|
exports.MemoryDbService = MemoryDbService;
|
|
59
110
|
exports.MemoryDbService = MemoryDbService = __decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-db.service.js","sourceRoot":"","sources":["../../src/services/memory-db.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kBAAkB;AAClB,2CAA4C;AAC5C,mCAAiD;AACjD,0CAA2C;
|
|
1
|
+
{"version":3,"file":"memory-db.service.js","sourceRoot":"","sources":["../../src/services/memory-db.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kBAAkB;AAClB,2CAA4C;AAC5C,mCAAiD;AACjD,0CAA2C;AAG3C,MAAM,MAAM,GAAG,CAAC,uBAAY,CAAC,CAAC;AAGvB,IAAM,eAAe,GAArB,MAAM,eAAe;IAG1B;QAFQ,OAAE,GAA6B,EAAE,CAAC;QA2IlC,iBAAY,GAAG,CAAC,MAAoB,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC;QAxInE,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B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AAI,CAAC,CAAC;QAEhC,6BAA6B;QAC7B,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;YACxB,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;gBAAE,OAAO,KAAK,CAAC;YAEvC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;gBAAE,OAAO,KAAK,CAAC;QAC7D,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CAGF,CAAA;AA7IY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;;GACA,eAAe,CA6I3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.type.js","sourceRoot":"","sources":["../../src/types/context.type.ts"],"names":[],"mappings":""}
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./context.type"), exports);
|
|
17
18
|
__exportStar(require("./log.type"), exports);
|
|
18
19
|
__exportStar(require("./options.type"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,iDAA+B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,6CAA2B;AAC3B,iDAA+B"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntitySchema } from "typeorm";
|
|
1
|
+
import { Any, EntitySchema } from "typeorm";
|
|
2
2
|
|
|
3
3
|
export function createLogEntity(name: string) {
|
|
4
4
|
return new EntitySchema({
|
|
@@ -11,6 +11,11 @@ export function createLogEntity(name: string) {
|
|
|
11
11
|
},
|
|
12
12
|
type: { type: String },
|
|
13
13
|
message: { type: String },
|
|
14
|
+
count: { type: Number, default: 1 },
|
|
15
|
+
context: { type: String, nullable: true },
|
|
16
|
+
trace: { type: String, nullable: true },
|
|
17
|
+
createdAt: { type: Date },
|
|
18
|
+
updatedAt: { type: Date },
|
|
14
19
|
},
|
|
15
20
|
});
|
|
16
21
|
}
|
package/src/index.ts
CHANGED
package/src/log.interceptor.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CallHandler,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
NestInterceptor,
|
|
5
|
-
} from "@nestjs/common";
|
|
1
|
+
import type { CallHandler, NestInterceptor } from "@nestjs/common";
|
|
6
2
|
import { Inject, Injectable } from "@nestjs/common";
|
|
7
3
|
import { Observable, tap } from "rxjs";
|
|
8
4
|
import { LogService } from "./services/log.service";
|
|
5
|
+
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
9
6
|
|
|
10
7
|
@Injectable()
|
|
11
8
|
export class LogInterceptor implements NestInterceptor {
|
|
12
9
|
constructor(@Inject(LogService) private readonly logService: LogService) {}
|
|
13
10
|
|
|
14
|
-
intercept(context:
|
|
11
|
+
intercept(context: ExecutionContextHost, next: CallHandler): Observable<any> {
|
|
15
12
|
return next.handle().pipe(
|
|
16
13
|
tap({
|
|
17
14
|
error: (error) => {
|
package/src/log.module.ts
CHANGED
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ExecutionContext,
|
|
3
|
-
Injectable,
|
|
4
|
-
LoggerService,
|
|
5
|
-
Scope,
|
|
6
|
-
} from "@nestjs/common";
|
|
1
|
+
import { Injectable, LoggerService, Scope } from "@nestjs/common";
|
|
7
2
|
import { MemoryDbService } from "./memory-db.service";
|
|
8
3
|
import { defaultTable } from "../defaults";
|
|
9
|
-
import { LogModuleOptions, LogType } from "../types";
|
|
10
|
-
import {
|
|
4
|
+
import { Context, LogModuleOptions, LogType } from "../types";
|
|
5
|
+
import {
|
|
6
|
+
DataSource,
|
|
7
|
+
DataSourceOptions,
|
|
8
|
+
EntityManager,
|
|
9
|
+
EntitySchema,
|
|
10
|
+
} from "typeorm";
|
|
11
11
|
import { createLogEntity } from "../entities/log.entity";
|
|
12
|
+
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
12
13
|
|
|
13
14
|
@Injectable({ scope: Scope.TRANSIENT })
|
|
14
15
|
export class LogService implements LoggerService {
|
|
15
16
|
static connection: DataSource;
|
|
16
|
-
static Log: EntitySchema;
|
|
17
|
+
static Log: EntitySchema = createLogEntity(defaultTable);
|
|
17
18
|
|
|
18
|
-
constructor(
|
|
19
|
-
private readonly MemoryDbService: MemoryDbService // @InjectRepository(Log) // private readonly userRepository: Repository<Log>
|
|
20
|
-
) {}
|
|
19
|
+
constructor(private readonly memoryDbService: MemoryDbService) {}
|
|
21
20
|
|
|
22
21
|
async connectDb(options: LogModuleOptions): Promise<DataSource> {
|
|
23
|
-
|
|
24
|
-
options.database?.collection || options.database?.table ||
|
|
25
|
-
|
|
26
|
-
LogService.Log = createLogEntity(tableName);
|
|
22
|
+
LogService.Log = createLogEntity(
|
|
23
|
+
options.database?.collection || options.database?.table || defaultTable
|
|
24
|
+
);
|
|
27
25
|
|
|
28
26
|
const dataSourceOptions = {
|
|
29
27
|
type: options.database?.type,
|
|
@@ -40,16 +38,16 @@ export class LogService implements LoggerService {
|
|
|
40
38
|
return LogService.connection;
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
log(message: string, context?:
|
|
44
|
-
this.smartInsert(
|
|
41
|
+
log(message: string, context?: ExecutionContextHost) {
|
|
42
|
+
this.smartInsert({
|
|
45
43
|
type: LogType.LOG,
|
|
46
44
|
message,
|
|
47
45
|
context,
|
|
48
46
|
});
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
error(message: string, trace?: string, context?:
|
|
52
|
-
this.smartInsert(
|
|
49
|
+
error(message: string, trace?: string, context?: ExecutionContextHost) {
|
|
50
|
+
this.smartInsert({
|
|
53
51
|
type: LogType.ERROR,
|
|
54
52
|
message,
|
|
55
53
|
trace,
|
|
@@ -57,45 +55,106 @@ export class LogService implements LoggerService {
|
|
|
57
55
|
});
|
|
58
56
|
}
|
|
59
57
|
|
|
60
|
-
warn(message: string, context?:
|
|
61
|
-
this.smartInsert(
|
|
58
|
+
warn(message: string, context?: ExecutionContextHost) {
|
|
59
|
+
this.smartInsert({
|
|
62
60
|
type: LogType.WARN,
|
|
63
61
|
message,
|
|
64
62
|
context,
|
|
65
63
|
});
|
|
66
64
|
}
|
|
67
65
|
|
|
68
|
-
debug(message: string, context?:
|
|
69
|
-
this.smartInsert(
|
|
66
|
+
debug(message: string, context?: ExecutionContextHost) {
|
|
67
|
+
this.smartInsert({
|
|
70
68
|
type: LogType.DEBUG,
|
|
71
69
|
message,
|
|
72
70
|
context,
|
|
73
71
|
});
|
|
74
72
|
}
|
|
75
73
|
|
|
76
|
-
verbose(message: string, context?:
|
|
77
|
-
this.smartInsert(
|
|
74
|
+
verbose(message: string, context?: ExecutionContextHost) {
|
|
75
|
+
this.smartInsert({
|
|
78
76
|
type: LogType.VERBOSE,
|
|
79
77
|
message,
|
|
80
78
|
context,
|
|
81
79
|
});
|
|
82
80
|
}
|
|
83
81
|
|
|
84
|
-
getAll(): any[] {
|
|
85
|
-
return this.
|
|
82
|
+
async getAll(): Promise<any[]> {
|
|
83
|
+
return this.getConnection().find(LogService.Log, {
|
|
84
|
+
select: ["type", "message", "count", "createdAt", "updatedAt"],
|
|
85
|
+
});
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
private async smartInsert(
|
|
89
|
-
|
|
88
|
+
private async smartInsert(data: {
|
|
89
|
+
type: LogType;
|
|
90
|
+
message: string;
|
|
91
|
+
context?: ExecutionContextHost;
|
|
92
|
+
trace?: any;
|
|
93
|
+
}): Promise<any> {
|
|
94
|
+
const currentDate = new Date();
|
|
95
|
+
|
|
96
|
+
const connection = this.getConnection();
|
|
97
|
+
|
|
98
|
+
// find the same log in DB
|
|
99
|
+
const log = await connection.findOne(LogService.Log, {
|
|
100
|
+
where: {
|
|
101
|
+
type: data.type,
|
|
102
|
+
message: data.message,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const context = data.context ? this.parseContext(data.context) : undefined;
|
|
107
|
+
|
|
108
|
+
if (log) {
|
|
109
|
+
return await connection.update(LogService.Log, log._id, {
|
|
110
|
+
context,
|
|
111
|
+
trace: data.trace,
|
|
112
|
+
count: log.count + 1,
|
|
113
|
+
updatedAt: currentDate,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return await connection.insert(LogService.Log, {
|
|
90
118
|
type: data.type,
|
|
91
119
|
message: data.message,
|
|
120
|
+
context,
|
|
121
|
+
trace: data.trace,
|
|
92
122
|
count: 1,
|
|
123
|
+
createdAt: currentDate,
|
|
124
|
+
updatedAt: currentDate,
|
|
93
125
|
});
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private getConnection(): EntityManager {
|
|
129
|
+
return LogService.connection.manager || this.memoryDbService;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private parseContext(context: ExecutionContextHost): Partial<Context> {
|
|
133
|
+
const res: Partial<Context> = {};
|
|
134
|
+
const args = context.getArgs();
|
|
135
|
+
|
|
136
|
+
for (const arg of args) {
|
|
137
|
+
if (arg.rawHeaders) {
|
|
138
|
+
res.rawHeaders = arg.rawHeaders;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (arg.url) {
|
|
142
|
+
res.url = arg.url;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (arg.method) {
|
|
146
|
+
res.method = arg.method;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (arg.params) {
|
|
150
|
+
res.params = arg.params;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (arg.body) {
|
|
154
|
+
res.body = arg.body;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return res;
|
|
100
159
|
}
|
|
101
160
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { Injectable } from "@nestjs/common";
|
|
3
3
|
import { createHash, randomBytes } from "crypto";
|
|
4
4
|
import { defaultTable } from "../defaults";
|
|
5
|
+
import { EntitySchema, FindManyOptions } from "typeorm";
|
|
5
6
|
|
|
6
7
|
const tables = [defaultTable];
|
|
7
8
|
|
|
@@ -15,44 +16,136 @@ export class MemoryDbService {
|
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
public insert(
|
|
19
|
-
|
|
19
|
+
public insert(entity: EntitySchema, data: any): string {
|
|
20
|
+
const table = this.getTableName(entity);
|
|
21
|
+
|
|
22
|
+
// generate new random _id
|
|
20
23
|
const randomData = randomBytes(24).toString("hex");
|
|
21
|
-
const
|
|
24
|
+
const _id = createHash("sha256").update(randomData).digest("hex");
|
|
22
25
|
|
|
23
26
|
this.db[table].push({
|
|
24
27
|
...data,
|
|
25
|
-
|
|
28
|
+
_id, // unique index
|
|
26
29
|
});
|
|
27
30
|
|
|
28
|
-
return
|
|
31
|
+
return _id;
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
public update(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
public async update(
|
|
35
|
+
entity: EntitySchema,
|
|
36
|
+
condition: any,
|
|
37
|
+
data: any
|
|
38
|
+
): Promise<string> {
|
|
39
|
+
const table = this.getTableName(entity);
|
|
40
|
+
let index: number | null = null;
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
if (typeof condition === "string") {
|
|
43
|
+
index = this.findIndex(entity, { where: { _id: condition } });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (condition?.where) {
|
|
47
|
+
index = this.findIndex(entity, condition);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (index !== null) {
|
|
51
|
+
this.db[table][index] = {
|
|
52
|
+
...this.db[table][index],
|
|
53
|
+
...data,
|
|
54
|
+
};
|
|
40
55
|
|
|
41
|
-
|
|
56
|
+
return Promise.resolve(this.db[table][index]._id);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return Promise.reject();
|
|
42
60
|
}
|
|
43
61
|
|
|
44
|
-
public
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
62
|
+
public async find(
|
|
63
|
+
entity: EntitySchema,
|
|
64
|
+
options?: {
|
|
65
|
+
select?: string[];
|
|
66
|
+
}
|
|
67
|
+
): Promise<any[]> {
|
|
68
|
+
const table = this.getTableName(entity);
|
|
69
|
+
|
|
70
|
+
let mapOptions = (obj: any) => obj;
|
|
71
|
+
|
|
72
|
+
if (options?.select) {
|
|
73
|
+
mapOptions = (obj: any) => {
|
|
74
|
+
const newObj: any = {};
|
|
75
|
+
|
|
76
|
+
for (const key of options.select || []) {
|
|
77
|
+
newObj[key] = obj[key];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return newObj;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return Promise.resolve(this.db[table].map(mapOptions));
|
|
49
85
|
}
|
|
50
86
|
|
|
51
|
-
public getOneById(
|
|
52
|
-
|
|
87
|
+
public async getOneById(entity: EntitySchema, _id: string): Promise<any> {
|
|
88
|
+
const table = this.getTableName(entity);
|
|
89
|
+
|
|
90
|
+
return Promise.resolve(this.db[table].find((item) => item._id === _id));
|
|
53
91
|
}
|
|
54
92
|
|
|
55
|
-
public
|
|
93
|
+
public findByProperty(
|
|
94
|
+
entity: EntitySchema,
|
|
95
|
+
field: string,
|
|
96
|
+
value: string
|
|
97
|
+
): any[] {
|
|
98
|
+
const table = this.getTableName(entity);
|
|
99
|
+
|
|
56
100
|
return this.db[table].filter((item) => item[field] === value);
|
|
57
101
|
}
|
|
102
|
+
|
|
103
|
+
public findOne(
|
|
104
|
+
entity: EntitySchema,
|
|
105
|
+
condition: { where: any }
|
|
106
|
+
): Promise<any> {
|
|
107
|
+
const table = this.getTableName(entity);
|
|
108
|
+
|
|
109
|
+
return Promise.resolve(
|
|
110
|
+
this.db[table].find((item) => this.partialEqual(condition.where, item))
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private findIndex(entity: EntitySchema, condition: { where: any }): number {
|
|
115
|
+
const table = this.getTableName(entity);
|
|
116
|
+
|
|
117
|
+
return this.db[table].findIndex((item) =>
|
|
118
|
+
this.partialEqual(condition.where, item)
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private partialEqual(obj1: any, obj2: any) {
|
|
123
|
+
// compare primitive type values
|
|
124
|
+
if (obj1 === obj2) return true;
|
|
125
|
+
|
|
126
|
+
// handle null or non-object values
|
|
127
|
+
if (
|
|
128
|
+
obj1 == null ||
|
|
129
|
+
obj2 == null ||
|
|
130
|
+
typeof obj1 !== "object" ||
|
|
131
|
+
typeof obj2 !== "object"
|
|
132
|
+
) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// compare keys length
|
|
137
|
+
const keys1 = Object.keys(obj1);
|
|
138
|
+
const keys2 = Object.keys(obj2);
|
|
139
|
+
|
|
140
|
+
// compare values recursively
|
|
141
|
+
for (const key of keys1) {
|
|
142
|
+
if (!keys2.includes(key)) return false;
|
|
143
|
+
|
|
144
|
+
if (!this.partialEqual(obj1[key], obj2[key])) return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private getTableName = (entity: EntitySchema) => entity.options.name;
|
|
58
151
|
}
|
package/src/types/index.ts
CHANGED