Skip to content

Commit

Permalink
feat: database transaction logging for all models (#1849)
Browse files Browse the repository at this point in the history
* Database transaction logging middleware

* Review changes

* updated .gitignore

* logging middlware for all models

* fix lint

* fix format

* fix failing tests
  • Loading branch information
lakshz authored Feb 15, 2024
1 parent a431418 commit bee07a9
Show file tree
Hide file tree
Showing 29 changed files with 97 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/models/Advertisement.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Types, Model, PopulatedDoc } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface, that represents database - (MongoDB) document for Advertisement.
*/
Expand Down Expand Up @@ -99,6 +100,8 @@ const advertisementSchema = new Schema(
}
);

createLoggingMiddleware(advertisementSchema, "Advertisement");

const advertisementModel = (): Model<InterfaceAdvertisement> =>
model<InterfaceAdvertisement>("Advertisement", advertisementSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/CheckIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type Model,
} from "mongoose";
import { type InterfaceEventAttendee } from "./EventAttendee";
import { createLoggingMiddleware } from "../libraries/dbLogger";

export interface InterfaceCheckIn {
_id: Types.ObjectId;
Expand Down Expand Up @@ -56,6 +57,8 @@ checkInSchema.index({
eventAttendeeId: 1,
});

createLoggingMiddleware(checkInSchema, "CheckIn");

const checkInModel = (): Model<InterfaceCheckIn> =>
model<InterfaceCheckIn>("CheckIn", checkInSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PopulatedDoc, Types, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceUser } from "./User";
import type { InterfacePost } from "./Post";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a comment in the database(MongoDB).
*/
Expand Down Expand Up @@ -65,6 +66,8 @@ const commentSchema = new Schema(
}
);

createLoggingMiddleware(commentSchema, "Comment");

const commentModel = (): Model<InterfaceComment> =>
model<InterfaceComment>("Comment", commentSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/DirectChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Schema, model, models } from "mongoose";
import type { InterfaceDirectChatMessage } from "./DirectChatMessage";
import type { InterfaceOrganization } from "./Organization";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for direct chat in the database(MongoDB).
*/
Expand Down Expand Up @@ -63,6 +64,8 @@ const directChatSchema = new Schema(
}
);

createLoggingMiddleware(directChatSchema, "DirectChat");

const directChatModel = (): Model<InterfaceDirectChat> =>
model<InterfaceDirectChat>("DirectChat", directChatSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/DirectChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PopulatedDoc, Types, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceDirectChat } from "./DirectChat";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a direct chat message in the database(MongoDB).
*/
Expand Down Expand Up @@ -58,6 +59,8 @@ const directChatMessageSchema = new Schema(
}
);

createLoggingMiddleware(directChatMessageSchema, "DirectChatMessage");

const directChatMessageModel = (): Model<InterfaceDirectChatMessage> =>
model<InterfaceDirectChatMessage>(
"DirectChatMessage",
Expand Down
3 changes: 3 additions & 0 deletions src/models/Donation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Types, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a donation in the database(MongoDB).
*/
Expand Down Expand Up @@ -56,6 +57,8 @@ const donationSchema = new Schema(
}
);

createLoggingMiddleware(donationSchema, "Donation");

const donationModel = (): Model<InterfaceDonation> =>
model<InterfaceDonation>("Donation", donationSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/EncodedImage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Types, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Encoded Image.
*/
Expand Down Expand Up @@ -31,6 +32,8 @@ const encodedImageSchema = new Schema({
},
});

createLoggingMiddleware(encodedImageSchema, "EncodedImage");

const encodedImageModel = (): Model<InterfaceEncodedImage> =>
model<InterfaceEncodedImage>("EncodedImage", encodedImageSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/EncodedVideo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Types, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Encoded Video.
*/
Expand Down Expand Up @@ -31,6 +32,8 @@ const encodedVideoSchema = new Schema({
},
});

createLoggingMiddleware(encodedVideoSchema, "EncodedVideo");

const encodedVideoModel = (): Model<InterfaceEncodedVideo> =>
model<InterfaceEncodedVideo>("EncodedVideo", encodedVideoSchema);

Expand Down
15 changes: 7 additions & 8 deletions src/models/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Schema, model, models } from "mongoose";
import type { InterfaceOrganization } from "./Organization";
import type { InterfaceUser } from "./User";
import type { InterfaceRecurrenceRule } from "./RecurrenceRule";
import { createLoggingMiddleware } from "../libraries/dbLogger";

/**
* This is an interface representing a document for an event in the database(MongoDB).
Expand Down Expand Up @@ -125,29 +126,25 @@ const eventSchema = new Schema(
},
endDate: {
type: Date,
required: function (): () => boolean {
// @ts-expect-error Suppressing typescript error for conditional required field
required: function (this: InterfaceEvent): boolean {
return !this.allDay;
},
},
startTime: {
type: Date,
required: function (): () => boolean {
// @ts-expect-error Suppressing typescript error for conditional required field
required: function (this: InterfaceEvent): boolean {
return !this.allDay;
},
},
endTime: {
type: Date,
required: function (): () => boolean {
// @ts-expect-error Suppressing typescript error for conditional required field
required: function (this: InterfaceEvent): boolean {
return !this.allDay;
},
},
recurrance: {
type: String,
required: function (): () => boolean {
// @ts-expect-error Suppressing typescript error for conditional required field
required: function (this: InterfaceEvent): boolean {
return this.recurring;
},
enum: ["ONCE", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"],
Expand Down Expand Up @@ -190,6 +187,8 @@ const eventSchema = new Schema(
}
);

createLoggingMiddleware(eventSchema, "Event");

const eventModel = (): Model<InterfaceEvent> =>
model<InterfaceEvent>("Event", eventSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/EventAttendee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Schema, model, models } from "mongoose";
import type { InterfaceUser } from "./User";
import type { InterfaceEvent } from "./Event";
import type { InterfaceCheckIn } from "./CheckIn";
import { createLoggingMiddleware } from "../libraries/dbLogger";

export interface InterfaceEventAttendee {
_id: Schema.Types.ObjectId;
Expand Down Expand Up @@ -32,6 +33,8 @@ const eventAttendeeSchema = new Schema({

eventAttendeeSchema.index({ userId: 1, eventId: 1 }, { unique: true });

createLoggingMiddleware(eventAttendeeSchema, "EventAttendee");

const eventAttendeeModel = (): Model<InterfaceEventAttendee> =>
model<InterfaceEventAttendee>("EventAttendee", eventAttendeeSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/Feedback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Types, PopulatedDoc, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceEvent } from "./Event";
import { createLoggingMiddleware } from "../libraries/dbLogger";

export interface InterfaceFeedback {
_id: Types.ObjectId;
Expand Down Expand Up @@ -39,6 +40,8 @@ feedbackSchema.index({
eventId: 1,
});

createLoggingMiddleware(feedbackSchema, "Feedback");

const feedbackModel = (): Model<InterfaceFeedback> =>
model<InterfaceFeedback>("Feedback", feedbackSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/File.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Types, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import { v4 as uuidv4 } from "uuid";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a file in the database(MongoDB).
*/
Expand Down Expand Up @@ -58,6 +59,8 @@ const fileSchema = new Schema(
}
);

createLoggingMiddleware(fileSchema, "File");

const fileModel = (): Model<InterfaceFile> =>
model<InterfaceFile>("File", fileSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PopulatedDoc, Types, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceOrganization } from "./Organization";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a group in the database(MongoDB).
*/
Expand Down Expand Up @@ -57,6 +58,8 @@ const groupSchema = new Schema(
}
);

createLoggingMiddleware(groupSchema, "Group");

const groupModel = (): Model<InterfaceGroup> =>
model<InterfaceGroup>("Group", groupSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/GroupChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Schema, model, models } from "mongoose";
import type { InterfaceGroupChatMessage } from "./GroupChatMessage";
import type { InterfaceOrganization } from "./Organization";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a group chat in the database(MongoDB).
*/
Expand Down Expand Up @@ -69,6 +70,8 @@ const groupChatSchema = new Schema(
}
);

createLoggingMiddleware(groupChatSchema, "GroupChat");

const groupChatModel = (): Model<InterfaceGroupChat> =>
model<InterfaceGroupChat>("GroupChat", groupChatSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/GroupChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Types, PopulatedDoc, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceGroupChat } from "./GroupChat";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Group Chat Message.
*/
Expand Down Expand Up @@ -51,6 +52,8 @@ const groupChatMessageSchema = new Schema(
}
);

createLoggingMiddleware(groupChatMessageSchema, "GroupChatMessage");

const groupChatMessageModel = (): Model<InterfaceGroupChatMessage> =>
model<InterfaceGroupChatMessage>("GroupChatMessage", groupChatMessageSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/ImageHash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Types, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Image Hash.
*/
Expand Down Expand Up @@ -39,6 +40,8 @@ const imageHashSchema = new Schema({
},
});

createLoggingMiddleware(imageHashSchema, "ImageHash");

const imageHashModel = (): Model<InterfaceImageHash> =>
model<InterfaceImageHash>("ImageHash", imageHashSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/Language.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Types, Document, PopulatedDoc, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database document.
*/
Expand Down Expand Up @@ -70,6 +71,8 @@ const languageSchema = new Schema({
},
});

createLoggingMiddleware(languageSchema, "Language");

const languageModel = (): Model<InterfaceLanguage> =>
model<InterfaceLanguage>("Language", languageSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/MembershipRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PopulatedDoc, Types, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceOrganization } from "./Organization";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Membership Request.
*/
Expand Down Expand Up @@ -36,6 +37,8 @@ const membershipRequestSchema = new Schema({
},
});

createLoggingMiddleware(membershipRequestSchema, "MembershipRequest");

const membershipRequestModel = (): Model<InterfaceMembershipRequest> =>
model<InterfaceMembershipRequest>(
"MembershipRequest",
Expand Down
3 changes: 3 additions & 0 deletions src/models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PopulatedDoc, Types, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceGroup } from "./Group";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Message.
*/
Expand Down Expand Up @@ -63,6 +64,8 @@ const messageSchema = new Schema(
}
);

createLoggingMiddleware(messageSchema, "Message");

const messageModel = (): Model<InterfaceMessage> =>
model<InterfaceMessage>("Message", messageSchema);

Expand Down
3 changes: 3 additions & 0 deletions src/models/MessageChat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PopulatedDoc, Types, Document, Model } from "mongoose";
import { Schema, model, models } from "mongoose";
import type { InterfaceUser } from "./User";
import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface representing a document for a chat in the database(MongoDB).
*/
Expand Down Expand Up @@ -49,6 +50,8 @@ const messageChatSchema = new Schema(
}
);

createLoggingMiddleware(messageChatSchema, "MessageChat");

const messageChatModel = (): Model<InterfaceMessageChat> =>
model<InterfaceMessageChat>("MessageChat", messageChatSchema);

Expand Down
4 changes: 3 additions & 1 deletion src/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { InterfaceMessage } from "./Message";
import type { InterfaceOrganizationCustomField } from "./OrganizationCustomField";
import type { InterfacePost } from "./Post";
import type { InterfaceUser } from "./User";

import { createLoggingMiddleware } from "../libraries/dbLogger";
/**
* This is an interface that represents a database(MongoDB) document for Organization.
*/
Expand Down Expand Up @@ -178,6 +178,8 @@ const organizationSchema = new Schema(
}
);

createLoggingMiddleware(organizationSchema, "Organization");

const organizationModel = (): Model<InterfaceOrganization> =>
model<InterfaceOrganization>("Organization", organizationSchema);

Expand Down
Loading

0 comments on commit bee07a9

Please sign in to comment.