Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing metadata and reused timestamp #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ export interface PrismaTransporterOptions extends TransportStreamOptions {
tableName?: string;
}

// JSONValue is a type that represents all valid JSON values
type JSONValue =
| string
| number
| boolean
| { [x: string]: JSONValue }
| Array<JSONValue>;

export interface ILogInfo {
level: string;
message: string;
meta?: Record<string, unknown>;
timestamp: Date;
meta?: JSONValue;
}

/**
Expand All @@ -19,9 +28,9 @@ export interface ILogInfo {
* @param {String} options.prisma Prisma client
* @param {String} **Optional** options.tableName Name of the table to log to
* @param {Function} **Optional** options.log Custom log function
*
*
* @returns {Object} Winston transport
*
*
* Create Primsa Transport plugin for Winston
*/
export class PrismaWinstonTransporter extends Transport {
Expand All @@ -42,18 +51,17 @@ export class PrismaWinstonTransporter extends Transport {
* @msg {string} Message to log
* @meta {Object} **Optional** Additional metadata to attach
* @callback {function} Continuation to respond to when complete.
*
*
* @returns {undefined}
*
*
* Core logging method exposed to Winston. Metadata is optional.
*/
log(
info: ILogInfo,
callback?: (error?: Error, value?: unknown) => void
): void {

// get log content
const { level, message, meta } = info;
const { timestamp, level, message, ...meta } = info;

process.nextTick(() => {
// protect
Expand All @@ -62,14 +70,15 @@ export class PrismaWinstonTransporter extends Transport {
callback = () => {};
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
this.prisma[this.tableName]
.create({
data: {
level,
message,
timestamp: new Date(),
meta,
},
timestamp: new Date(timestamp),
meta
}
})
.then(() => {
setImmediate(() => {
Expand Down