@viplance/nestjs-logger 0.1.3 → 0.1.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 +39 -27
- package/dist/entities/log.entity.d.ts +2 -0
- package/dist/entities/log.entity.js +2 -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/log.interceptor.d.ts +3 -2
- package/dist/log.interceptor.js.map +1 -1
- package/dist/log.module.js +11 -0
- package/dist/log.module.js.map +1 -1
- package/dist/services/log.service.d.ts +12 -6
- package/dist/services/log.service.js +62 -1
- package/dist/services/log.service.js.map +1 -1
- package/dist/services/memory-db.service.d.ts +3 -1
- package/dist/services/memory-db.service.js +12 -8
- 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/dist/types/options.type.d.ts +4 -0
- package/package.json +1 -1
- package/src/entities/log.entity.ts +2 -0
- package/src/log.interceptor.ts +3 -6
- package/src/log.module.ts +10 -1
- package/src/services/log.service.ts +91 -14
- package/src/services/memory-db.service.ts +22 -11
- package/src/types/context.type.ts +7 -0
- package/src/types/index.ts +1 -0
- package/src/types/options.type.ts +4 -0
package/README.md
CHANGED
|
@@ -1,37 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# @viplance/nestjs-logger
|
|
2
|
+
## NestJS internal logging system
|
|
3
3
|
|
|
4
|
+
[](https://www.npmjs.com/package/@viplance/nestjs-logger)
|
|
5
|
+
[](https://github.com/viplance/nestjs-logger)
|
|
6
|
+
|
|
7
|
+
### Installation
|
|
4
8
|
1. Install the package npm i @viplance/nestjs-logger<br />
|
|
5
9
|
2. Import the module in app.module.ts<br />
|
|
6
|
-
|
|
10
|
+
```
|
|
11
|
+
import { LogModule } from '@viplance/nestjs-logger';<br />
|
|
12
|
+
@Module({
|
|
13
|
+
imports: [
|
|
14
|
+
...,
|
|
15
|
+
LogModule,
|
|
16
|
+
]
|
|
17
|
+
})
|
|
18
|
+
```
|
|
7
19
|
<br />
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
3. Connect the module in main.ts
|
|
21
|
+
```
|
|
22
|
+
// use the memory to store logs
|
|
23
|
+
await LogModule.connect(app, {
|
|
24
|
+
path: '/logs', // define the public URL for the log list
|
|
25
|
+
key: 'kjhj#$kj3lqq1', // use the key to protect data from unauthorized access
|
|
26
|
+
});
|
|
27
|
+
// use the database to store logs
|
|
28
|
+
await LogModule.connect(app, {
|
|
29
|
+
path: '/logs',
|
|
30
|
+
database: {
|
|
31
|
+
type: 'mongodb',
|
|
32
|
+
host: 'localhost',
|
|
33
|
+
port: 27017,
|
|
34
|
+
collection: 'logs',
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
```
|
|
15
38
|
<br />
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
 // use the database to store logs
|
|
21
|
-
 await LogModule.connect(app, {<br />
|
|
22
|
-
  path: '/logs',<br />
|
|
23
|
-
   database: {<br />
|
|
24
|
-
    type: 'mongodb',<br />
|
|
25
|
-
    host: 'localhost',<br />
|
|
26
|
-
    port: 27017,<br />
|
|
27
|
-
    collection: 'logs'<br />
|
|
28
|
-
   }<br />
|
|
29
|
-
  });<br />
|
|
30
|
-
3. Use the LogService in case of custom logs to debug the application.
|
|
39
|
+
|
|
40
|
+
### Additional information
|
|
41
|
+
|
|
42
|
+
`path` and `database` options are optional
|
|
31
43
|
<br />
|
|
44
|
+
Use the LogService in case of custom logs to debug the application.
|
|
32
45
|
<br />
|
|
33
|
-
|
|
34
|
-
The logs could be available at your_application_url/<path><br />
|
|
46
|
+
The logs could be available at your_application_url/<path>?key=<key><br />
|
|
35
47
|
By default the logs will be stored in memory and deleted when the application stops.<br />
|
|
36
48
|
<br />
|
|
37
49
|
Available service methods:
|
|
@@ -14,6 +14,8 @@ function createLogEntity(name) {
|
|
|
14
14
|
type: { type: String },
|
|
15
15
|
message: { type: String },
|
|
16
16
|
count: { type: Number, default: 1 },
|
|
17
|
+
context: { type: String, nullable: true },
|
|
18
|
+
trace: { type: String, nullable: true },
|
|
17
19
|
createdAt: { type: Date },
|
|
18
20
|
updatedAt: { type: Date },
|
|
19
21
|
},
|
|
@@ -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,qCAAuC;AAEvC,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"}
|
|
@@ -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
|
@@ -5,6 +5,9 @@ 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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
8
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
12
|
exports.LogModule = void 0;
|
|
10
13
|
const common_1 = require("@nestjs/common");
|
|
@@ -12,6 +15,7 @@ const log_service_1 = require("./services/log.service");
|
|
|
12
15
|
const memory_db_service_1 = require("./services/memory-db.service");
|
|
13
16
|
const log_interceptor_1 = require("./log.interceptor");
|
|
14
17
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
18
|
+
const node_querystring_1 = __importDefault(require("node:querystring"));
|
|
15
19
|
let LogModule = class LogModule {
|
|
16
20
|
static async connect(app, options) {
|
|
17
21
|
app.resolve(log_service_1.LogService);
|
|
@@ -20,6 +24,13 @@ let LogModule = class LogModule {
|
|
|
20
24
|
if (options === null || options === void 0 ? void 0 : options.path) {
|
|
21
25
|
const httpAdapter = app.getHttpAdapter();
|
|
22
26
|
httpAdapter.get(options.path, async (req, res) => {
|
|
27
|
+
var _a;
|
|
28
|
+
if ((_a = log_service_1.LogService.options) === null || _a === void 0 ? void 0 : _a.key) {
|
|
29
|
+
const params = node_querystring_1.default.parse(req.url.split("?")[1]);
|
|
30
|
+
if (params.key && params.key !== log_service_1.LogService.options.key) {
|
|
31
|
+
throw new common_1.HttpException("Unauthorized", 401);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
23
34
|
res.json(await logService.getAll());
|
|
24
35
|
});
|
|
25
36
|
}
|
package/dist/log.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"log.module.js","sourceRoot":"","sources":["../src/log.module.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA+D;AAC/D,wDAAoD;AACpD,oEAA+D;AAC/D,uDAAmD;AAEnD,6CAAgD;AAChD,wEAA2C;AAQpC,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,M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
|
@@ -1,19 +1,25 @@
|
|
|
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";
|
|
6
|
+
import { setInterval } from "timers";
|
|
5
7
|
export declare class LogService implements LoggerService {
|
|
6
8
|
private readonly memoryDbService;
|
|
7
9
|
static connection: DataSource;
|
|
10
|
+
static options: LogModuleOptions;
|
|
8
11
|
static Log: EntitySchema;
|
|
12
|
+
static timer: ReturnType<typeof setInterval>;
|
|
9
13
|
constructor(memoryDbService: MemoryDbService);
|
|
10
14
|
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?:
|
|
15
|
+
log(message: string, context?: ExecutionContextHost): void;
|
|
16
|
+
error(message: string, trace?: string, context?: ExecutionContextHost): void;
|
|
17
|
+
warn(message: string, context?: ExecutionContextHost): void;
|
|
18
|
+
debug(message: string, context?: ExecutionContextHost): void;
|
|
19
|
+
verbose(message: string, context?: ExecutionContextHost): void;
|
|
16
20
|
getAll(): Promise<any[]>;
|
|
17
21
|
private smartInsert;
|
|
18
22
|
private getConnection;
|
|
23
|
+
private parseContext;
|
|
24
|
+
private checkRecords;
|
|
19
25
|
}
|
|
@@ -17,6 +17,7 @@ const defaults_1 = require("../defaults");
|
|
|
17
17
|
const types_1 = require("../types");
|
|
18
18
|
const typeorm_1 = require("typeorm");
|
|
19
19
|
const log_entity_1 = require("../entities/log.entity");
|
|
20
|
+
const timers_1 = require("timers");
|
|
20
21
|
let LogService = LogService_1 = class LogService {
|
|
21
22
|
constructor(memoryDbService) {
|
|
22
23
|
this.memoryDbService = memoryDbService;
|
|
@@ -24,6 +25,7 @@ let LogService = LogService_1 = class LogService {
|
|
|
24
25
|
async connectDb(options) {
|
|
25
26
|
var _a, _b, _c, _d, _e, _f;
|
|
26
27
|
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);
|
|
28
|
+
LogService_1.options = options;
|
|
27
29
|
const dataSourceOptions = {
|
|
28
30
|
type: (_c = options.database) === null || _c === void 0 ? void 0 : _c.type,
|
|
29
31
|
database: (_d = options.database) === null || _d === void 0 ? void 0 : _d.database,
|
|
@@ -33,6 +35,10 @@ let LogService = LogService_1 = class LogService {
|
|
|
33
35
|
};
|
|
34
36
|
LogService_1.connection = new typeorm_1.DataSource(dataSourceOptions);
|
|
35
37
|
await LogService_1.connection.initialize();
|
|
38
|
+
if (LogService_1.timer) {
|
|
39
|
+
clearInterval(LogService_1.timer);
|
|
40
|
+
}
|
|
41
|
+
LogService_1.timer = (0, timers_1.setInterval)(this.checkRecords, 1000 * 60 * 60); // check one time per hour
|
|
36
42
|
return LogService_1.connection;
|
|
37
43
|
}
|
|
38
44
|
log(message, context) {
|
|
@@ -72,7 +78,17 @@ let LogService = LogService_1 = class LogService {
|
|
|
72
78
|
});
|
|
73
79
|
}
|
|
74
80
|
async getAll() {
|
|
75
|
-
return this.getConnection().find(LogService_1.Log
|
|
81
|
+
return this.getConnection().find(LogService_1.Log, {
|
|
82
|
+
select: [
|
|
83
|
+
"type",
|
|
84
|
+
"message",
|
|
85
|
+
"count",
|
|
86
|
+
"createdAt",
|
|
87
|
+
"updatedAt",
|
|
88
|
+
"context",
|
|
89
|
+
"trace",
|
|
90
|
+
],
|
|
91
|
+
});
|
|
76
92
|
}
|
|
77
93
|
async smartInsert(data) {
|
|
78
94
|
const currentDate = new Date();
|
|
@@ -84,8 +100,11 @@ let LogService = LogService_1 = class LogService {
|
|
|
84
100
|
message: data.message,
|
|
85
101
|
},
|
|
86
102
|
});
|
|
103
|
+
const context = data.context ? this.parseContext(data.context) : undefined;
|
|
87
104
|
if (log) {
|
|
88
105
|
return await connection.update(LogService_1.Log, log._id, {
|
|
106
|
+
context,
|
|
107
|
+
trace: data.trace,
|
|
89
108
|
count: log.count + 1,
|
|
90
109
|
updatedAt: currentDate,
|
|
91
110
|
});
|
|
@@ -93,6 +112,8 @@ let LogService = LogService_1 = class LogService {
|
|
|
93
112
|
return await connection.insert(LogService_1.Log, {
|
|
94
113
|
type: data.type,
|
|
95
114
|
message: data.message,
|
|
115
|
+
context,
|
|
116
|
+
trace: data.trace,
|
|
96
117
|
count: 1,
|
|
97
118
|
createdAt: currentDate,
|
|
98
119
|
updatedAt: currentDate,
|
|
@@ -101,6 +122,46 @@ let LogService = LogService_1 = class LogService {
|
|
|
101
122
|
getConnection() {
|
|
102
123
|
return LogService_1.connection.manager || this.memoryDbService;
|
|
103
124
|
}
|
|
125
|
+
parseContext(context) {
|
|
126
|
+
const res = {};
|
|
127
|
+
const args = context.getArgs();
|
|
128
|
+
for (const arg of args) {
|
|
129
|
+
if (arg.rawHeaders) {
|
|
130
|
+
res.rawHeaders = arg.rawHeaders;
|
|
131
|
+
}
|
|
132
|
+
if (arg.url) {
|
|
133
|
+
res.url = arg.url;
|
|
134
|
+
}
|
|
135
|
+
if (arg.method) {
|
|
136
|
+
res.method = arg.method;
|
|
137
|
+
}
|
|
138
|
+
if (arg.params) {
|
|
139
|
+
res.params = arg.params;
|
|
140
|
+
}
|
|
141
|
+
if (arg.body) {
|
|
142
|
+
res.body = arg.body;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return res;
|
|
146
|
+
}
|
|
147
|
+
async checkRecords() {
|
|
148
|
+
var _a, _b;
|
|
149
|
+
if ((_a = LogService_1.options) === null || _a === void 0 ? void 0 : _a.maxSize) {
|
|
150
|
+
const latest = await this.getConnection().find(LogService_1.Log, {
|
|
151
|
+
order: { updatedAt: "DESC" },
|
|
152
|
+
take: (_b = LogService_1.options) === null || _b === void 0 ? void 0 : _b.maxSize,
|
|
153
|
+
select: ["_id"],
|
|
154
|
+
});
|
|
155
|
+
const latestIds = latest.map((item) => item.id);
|
|
156
|
+
await LogService_1.connection
|
|
157
|
+
.getRepository(LogService_1.Log)
|
|
158
|
+
.createQueryBuilder()
|
|
159
|
+
.delete()
|
|
160
|
+
.from(LogService_1.Log)
|
|
161
|
+
.where("_id NOT IN (:...ids)", { ids: latestIds })
|
|
162
|
+
.execute();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
104
165
|
};
|
|
105
166
|
exports.LogService = LogService;
|
|
106
167
|
LogService.Log = (0, log_entity_1.createLogEntity)(defaults_1.defaultTable);
|
|
@@ -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;AAEzD,mCAAqC;AAG9B,IAAM,UAAU,kBAAhB,MAAM,UAAU;IAMrB,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,YAAU,CAAC,OAAO,GAAG,OAAO,CAAC;QAE7B,MAAM,iBAAiB,GAAG;YACxB,IAAI,EAAE,MAAA,OAAO,CAAC,QAAQ,0CAAE,IAAI;YAC5B,QAAQ,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,YAAU,CAAC,GAAG,EAAE;gBAC7D,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE;gBAC5B,IAAI,EAAE,MAAA,YAAU,CAAC,OAAO,0CAAE,OAAO;gBACjC,MAAM,EAAE,CAAC,KAAK,CAAC;aAChB,CAAC,CAAC;YAEH,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAEhD,MAAM,YAAU,CAAC,UAAU;iBACxB,aAAa,CAAC,YAAU,CAAC,GAAG,CAAC;iBAC7B,kBAAkB,EAAE;iBACpB,MAAM,EAAE;iBACR,IAAI,CAAC,YAAU,CAAC,GAAG,CAAC;iBACpB,KAAK,CAAC,sBAAsB,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;iBACjD,OAAO,EAAE,CAAC;QACf,CAAC;IACH,CAAC;;AAtLU,gCAAU;AAGd,cAAG,GAAiB,IAAA,4BAAe,EAAC,uBAAY,CAAC,AAA9C,CAA+C;qBAH9C,UAAU;IADtB,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,SAAS,EAAE,CAAC;qCAOS,mCAAe;GANlD,UAAU,CAuLtB"}
|
|
@@ -4,7 +4,9 @@ export declare class MemoryDbService {
|
|
|
4
4
|
constructor();
|
|
5
5
|
insert(entity: EntitySchema, data: any): string;
|
|
6
6
|
update(entity: EntitySchema, condition: any, data: any): Promise<string>;
|
|
7
|
-
find(entity: EntitySchema
|
|
7
|
+
find(entity: EntitySchema, options?: {
|
|
8
|
+
select?: string[];
|
|
9
|
+
}): Promise<any[]>;
|
|
8
10
|
getOneById(entity: EntitySchema, _id: string): Promise<any>;
|
|
9
11
|
findByProperty(entity: EntitySchema, field: string, value: string): any[];
|
|
10
12
|
findOne(entity: EntitySchema, condition: {
|
|
@@ -52,15 +52,19 @@ let MemoryDbService = class MemoryDbService {
|
|
|
52
52
|
}
|
|
53
53
|
return Promise.reject();
|
|
54
54
|
}
|
|
55
|
-
async find(entity) {
|
|
55
|
+
async find(entity, options) {
|
|
56
56
|
const table = this.getTableName(entity);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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));
|
|
64
68
|
}
|
|
65
69
|
async getOneById(entity, _id) {
|
|
66
70
|
const table = this.getTableName(entity);
|
|
@@ -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;AAG3C,MAAM,MAAM,GAAG,CAAC,uBAAY,CAAC,CAAC;AAGvB,IAAM,eAAe,GAArB,MAAM,eAAe;IAG1B;QAFQ,OAAE,GAA6B,EAAE,CAAC;
|
|
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,MA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M,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,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
|
@@ -12,6 +12,8 @@ export function createLogEntity(name: string) {
|
|
|
12
12
|
type: { type: String },
|
|
13
13
|
message: { type: String },
|
|
14
14
|
count: { type: Number, default: 1 },
|
|
15
|
+
context: { type: String, nullable: true },
|
|
16
|
+
trace: { type: String, nullable: true },
|
|
15
17
|
createdAt: { type: Date },
|
|
16
18
|
updatedAt: { type: Date },
|
|
17
19
|
},
|
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,9 +1,10 @@
|
|
|
1
|
-
import { Module, Global } from "@nestjs/common";
|
|
1
|
+
import { Module, Global, HttpException } from "@nestjs/common";
|
|
2
2
|
import { LogService } from "./services/log.service";
|
|
3
3
|
import { MemoryDbService } from "./services/memory-db.service";
|
|
4
4
|
import { LogInterceptor } from "./log.interceptor";
|
|
5
5
|
import { LogModuleOptions } from "./types";
|
|
6
6
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
|
7
|
+
import querystring from "node:querystring";
|
|
7
8
|
|
|
8
9
|
@Global()
|
|
9
10
|
@Module({
|
|
@@ -25,6 +26,14 @@ export class LogModule {
|
|
|
25
26
|
if (options?.path) {
|
|
26
27
|
const httpAdapter = app.getHttpAdapter();
|
|
27
28
|
httpAdapter.get(options.path, async (req: any, res: any) => {
|
|
29
|
+
if (LogService.options?.key) {
|
|
30
|
+
const params = querystring.parse(req.url.split("?")[1]);
|
|
31
|
+
|
|
32
|
+
if (params.key && params.key !== LogService.options.key) {
|
|
33
|
+
throw new HttpException("Unauthorized", 401);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
28
37
|
res.json(await logService.getAll());
|
|
29
38
|
});
|
|
30
39
|
}
|
|
@@ -1,12 +1,7 @@
|
|
|
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";
|
|
4
|
+
import { Context, LogModuleOptions, LogType } from "../types";
|
|
10
5
|
import {
|
|
11
6
|
DataSource,
|
|
12
7
|
DataSourceOptions,
|
|
@@ -14,11 +9,15 @@ import {
|
|
|
14
9
|
EntitySchema,
|
|
15
10
|
} from "typeorm";
|
|
16
11
|
import { createLogEntity } from "../entities/log.entity";
|
|
12
|
+
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
|
13
|
+
import { setInterval } from "timers";
|
|
17
14
|
|
|
18
15
|
@Injectable({ scope: Scope.TRANSIENT })
|
|
19
16
|
export class LogService implements LoggerService {
|
|
20
17
|
static connection: DataSource;
|
|
18
|
+
static options: LogModuleOptions;
|
|
21
19
|
static Log: EntitySchema = createLogEntity(defaultTable);
|
|
20
|
+
static timer: ReturnType<typeof setInterval>;
|
|
22
21
|
|
|
23
22
|
constructor(private readonly memoryDbService: MemoryDbService) {}
|
|
24
23
|
|
|
@@ -27,6 +26,8 @@ export class LogService implements LoggerService {
|
|
|
27
26
|
options.database?.collection || options.database?.table || defaultTable
|
|
28
27
|
);
|
|
29
28
|
|
|
29
|
+
LogService.options = options;
|
|
30
|
+
|
|
30
31
|
const dataSourceOptions = {
|
|
31
32
|
type: options.database?.type,
|
|
32
33
|
database: options.database?.database,
|
|
@@ -39,10 +40,16 @@ export class LogService implements LoggerService {
|
|
|
39
40
|
|
|
40
41
|
await LogService.connection.initialize();
|
|
41
42
|
|
|
43
|
+
if (LogService.timer) {
|
|
44
|
+
clearInterval(LogService.timer);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
LogService.timer = setInterval(this.checkRecords, 1000 * 60 * 60); // check one time per hour
|
|
48
|
+
|
|
42
49
|
return LogService.connection;
|
|
43
50
|
}
|
|
44
51
|
|
|
45
|
-
log(message: string, context?:
|
|
52
|
+
log(message: string, context?: ExecutionContextHost) {
|
|
46
53
|
this.smartInsert({
|
|
47
54
|
type: LogType.LOG,
|
|
48
55
|
message,
|
|
@@ -50,7 +57,7 @@ export class LogService implements LoggerService {
|
|
|
50
57
|
});
|
|
51
58
|
}
|
|
52
59
|
|
|
53
|
-
error(message: string, trace?: string, context?:
|
|
60
|
+
error(message: string, trace?: string, context?: ExecutionContextHost) {
|
|
54
61
|
this.smartInsert({
|
|
55
62
|
type: LogType.ERROR,
|
|
56
63
|
message,
|
|
@@ -59,7 +66,7 @@ export class LogService implements LoggerService {
|
|
|
59
66
|
});
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
warn(message: string, context?:
|
|
69
|
+
warn(message: string, context?: ExecutionContextHost) {
|
|
63
70
|
this.smartInsert({
|
|
64
71
|
type: LogType.WARN,
|
|
65
72
|
message,
|
|
@@ -67,7 +74,7 @@ export class LogService implements LoggerService {
|
|
|
67
74
|
});
|
|
68
75
|
}
|
|
69
76
|
|
|
70
|
-
debug(message: string, context?:
|
|
77
|
+
debug(message: string, context?: ExecutionContextHost) {
|
|
71
78
|
this.smartInsert({
|
|
72
79
|
type: LogType.DEBUG,
|
|
73
80
|
message,
|
|
@@ -75,7 +82,7 @@ export class LogService implements LoggerService {
|
|
|
75
82
|
});
|
|
76
83
|
}
|
|
77
84
|
|
|
78
|
-
verbose(message: string, context?:
|
|
85
|
+
verbose(message: string, context?: ExecutionContextHost) {
|
|
79
86
|
this.smartInsert({
|
|
80
87
|
type: LogType.VERBOSE,
|
|
81
88
|
message,
|
|
@@ -84,10 +91,25 @@ export class LogService implements LoggerService {
|
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
async getAll(): Promise<any[]> {
|
|
87
|
-
return this.getConnection().find(LogService.Log
|
|
94
|
+
return this.getConnection().find(LogService.Log, {
|
|
95
|
+
select: [
|
|
96
|
+
"type",
|
|
97
|
+
"message",
|
|
98
|
+
"count",
|
|
99
|
+
"createdAt",
|
|
100
|
+
"updatedAt",
|
|
101
|
+
"context",
|
|
102
|
+
"trace",
|
|
103
|
+
],
|
|
104
|
+
});
|
|
88
105
|
}
|
|
89
106
|
|
|
90
|
-
private async smartInsert(data:
|
|
107
|
+
private async smartInsert(data: {
|
|
108
|
+
type: LogType;
|
|
109
|
+
message: string;
|
|
110
|
+
context?: ExecutionContextHost;
|
|
111
|
+
trace?: any;
|
|
112
|
+
}): Promise<any> {
|
|
91
113
|
const currentDate = new Date();
|
|
92
114
|
|
|
93
115
|
const connection = this.getConnection();
|
|
@@ -100,8 +122,12 @@ export class LogService implements LoggerService {
|
|
|
100
122
|
},
|
|
101
123
|
});
|
|
102
124
|
|
|
125
|
+
const context = data.context ? this.parseContext(data.context) : undefined;
|
|
126
|
+
|
|
103
127
|
if (log) {
|
|
104
128
|
return await connection.update(LogService.Log, log._id, {
|
|
129
|
+
context,
|
|
130
|
+
trace: data.trace,
|
|
105
131
|
count: log.count + 1,
|
|
106
132
|
updatedAt: currentDate,
|
|
107
133
|
});
|
|
@@ -110,6 +136,8 @@ export class LogService implements LoggerService {
|
|
|
110
136
|
return await connection.insert(LogService.Log, {
|
|
111
137
|
type: data.type,
|
|
112
138
|
message: data.message,
|
|
139
|
+
context,
|
|
140
|
+
trace: data.trace,
|
|
113
141
|
count: 1,
|
|
114
142
|
createdAt: currentDate,
|
|
115
143
|
updatedAt: currentDate,
|
|
@@ -119,4 +147,53 @@ export class LogService implements LoggerService {
|
|
|
119
147
|
private getConnection(): EntityManager {
|
|
120
148
|
return LogService.connection.manager || this.memoryDbService;
|
|
121
149
|
}
|
|
150
|
+
|
|
151
|
+
private parseContext(context: ExecutionContextHost): Partial<Context> {
|
|
152
|
+
const res: Partial<Context> = {};
|
|
153
|
+
const args = context.getArgs();
|
|
154
|
+
|
|
155
|
+
for (const arg of args) {
|
|
156
|
+
if (arg.rawHeaders) {
|
|
157
|
+
res.rawHeaders = arg.rawHeaders;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (arg.url) {
|
|
161
|
+
res.url = arg.url;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (arg.method) {
|
|
165
|
+
res.method = arg.method;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (arg.params) {
|
|
169
|
+
res.params = arg.params;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (arg.body) {
|
|
173
|
+
res.body = arg.body;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return res;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private async checkRecords() {
|
|
181
|
+
if (LogService.options?.maxSize) {
|
|
182
|
+
const latest = await this.getConnection().find(LogService.Log, {
|
|
183
|
+
order: { updatedAt: "DESC" },
|
|
184
|
+
take: LogService.options?.maxSize,
|
|
185
|
+
select: ["_id"],
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const latestIds = latest.map((item) => item.id);
|
|
189
|
+
|
|
190
|
+
await LogService.connection
|
|
191
|
+
.getRepository(LogService.Log)
|
|
192
|
+
.createQueryBuilder()
|
|
193
|
+
.delete()
|
|
194
|
+
.from(LogService.Log)
|
|
195
|
+
.where("_id NOT IN (:...ids)", { ids: latestIds })
|
|
196
|
+
.execute();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
122
199
|
}
|
|
@@ -2,7 +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 } from "typeorm";
|
|
5
|
+
import { EntitySchema, FindManyOptions } from "typeorm";
|
|
6
6
|
|
|
7
7
|
const tables = [defaultTable];
|
|
8
8
|
|
|
@@ -59,18 +59,29 @@ export class MemoryDbService {
|
|
|
59
59
|
return Promise.reject();
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
public async find(
|
|
62
|
+
public async find(
|
|
63
|
+
entity: EntitySchema,
|
|
64
|
+
options?: {
|
|
65
|
+
select?: string[];
|
|
66
|
+
}
|
|
67
|
+
): Promise<any[]> {
|
|
63
68
|
const table = this.getTableName(entity);
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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));
|
|
74
85
|
}
|
|
75
86
|
|
|
76
87
|
public async getOneById(entity: EntitySchema, _id: string): Promise<any> {
|
package/src/types/index.ts
CHANGED
|
@@ -2,6 +2,10 @@ import { DataSourceOptions } from "typeorm";
|
|
|
2
2
|
|
|
3
3
|
export type LogModuleOptions = {
|
|
4
4
|
path?: string;
|
|
5
|
+
key?: string; // access key
|
|
6
|
+
maxRecords?: number; // max log records
|
|
7
|
+
maxAge?: number; // in days
|
|
8
|
+
maxSize?: number; // in megabytes
|
|
5
9
|
database?: DataSourceOptions & {
|
|
6
10
|
host?: string;
|
|
7
11
|
port?: string;
|