From e96233d00195afb2fea6fb84830eea1b32ee2f64 Mon Sep 17 00:00:00 2001 From: rob Date: Mon, 22 May 2023 18:19:40 -0400 Subject: [PATCH] change model loaders to accept the Mongoose connection instead of using global --- app/models/announcement.js | 4 +++- app/models/attachment.js | 4 +++- app/models/chat-message.js | 4 +++- app/models/chat-room-invite.js | 4 +++- app/models/chat-room.js | 4 +++- app/models/comment.js | 4 +++- app/models/connect-token.js | 4 +++- app/models/content-report.js | 4 +++- app/models/content-vote.js | 4 +++- app/models/core-node-connect.js | 4 +++- app/models/core-node-request.js | 4 +++- app/models/core-node.js | 4 +++- app/models/core-user.js | 4 +++- app/models/csrf-token.js | 4 +++- app/models/email-blacklist.js | 4 +++- app/models/email-body.js | 4 +++- app/models/email-log.js | 4 +++- app/models/email-verify.js | 4 +++- app/models/email.js | 4 +++- app/models/emoji-reaction.js | 4 +++- app/models/feed-entry.js | 4 +++- app/models/feed.js | 4 +++- app/models/image.js | 4 +++- app/models/kaleidoscope-event.js | 4 +++- app/models/log.js | 4 +++- app/models/media-router.js | 4 +++- app/models/media-worker.js | 4 +++- app/models/net-host-stats.js | 4 +++- app/models/net-host.js | 4 +++- app/models/newsletter-recipient.js | 4 +++- app/models/newsletter.js | 4 +++- app/models/oauth2-authorization-code.js | 4 +++- app/models/oauth2-client.js | 4 +++- app/models/oauth2-token.js | 4 +++- app/models/otp-account.js | 4 +++- app/models/resource-view.js | 4 +++- app/models/resource-visit.js | 4 +++- app/models/sticker.js | 4 +++- app/models/user-block.js | 4 +++- app/models/user-notification.js | 4 +++- app/models/user-subscription.js | 4 +++- app/models/user.js | 4 +++- lib/site-platform.js | 6 +++--- 43 files changed, 129 insertions(+), 45 deletions(-) diff --git a/app/models/announcement.js b/app/models/announcement.js index d7d8b51..7438408 100644 --- a/app/models/announcement.js +++ b/app/models/announcement.js @@ -29,4 +29,6 @@ const AnnouncementSchema = new Schema({ resourceStats: { type: ResourceStats, default: ResourceStatsDefaults, required: true }, }); -module.exports = mongoose.model('Announcement', AnnouncementSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Announcement', AnnouncementSchema); +}; \ No newline at end of file diff --git a/app/models/attachment.js b/app/models/attachment.js index 71c3b89..5ac8e3d 100644 --- a/app/models/attachment.js +++ b/app/models/attachment.js @@ -61,4 +61,6 @@ AttachmentSchema.index({ name: 'attachment_item_idx', }); -module.exports = mongoose.model('Attachment', AttachmentSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Attachment', AttachmentSchema); +}; \ No newline at end of file diff --git a/app/models/chat-message.js b/app/models/chat-message.js index ba963ca..7ccf67e 100644 --- a/app/models/chat-message.js +++ b/app/models/chat-message.js @@ -28,4 +28,6 @@ const ChatMessageSchema = new Schema({ attachments: { type: [Schema.ObjectId], ref: 'Attachment' }, }); -module.exports = mongoose.model('ChatMessage', ChatMessageSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ChatMessage', ChatMessageSchema); +}; \ No newline at end of file diff --git a/app/models/chat-room-invite.js b/app/models/chat-room-invite.js index f1d8fe6..dbe6eaf 100644 --- a/app/models/chat-room-invite.js +++ b/app/models/chat-room-invite.js @@ -27,4 +27,6 @@ ChatRoomInviteSchema.index({ name: 'chatroom_invite_unique_idx', }); -module.exports = mongoose.model('ChatRoomInvite', ChatRoomInviteSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ChatRoomInvite', ChatRoomInviteSchema); +}; \ No newline at end of file diff --git a/app/models/chat-room.js b/app/models/chat-room.js index 6d3e2ff..502647d 100644 --- a/app/models/chat-room.js +++ b/app/models/chat-room.js @@ -41,4 +41,6 @@ ChatRoomSchema.index({ name: 'chatroom_public_open_idx', }); -module.exports = mongoose.model('ChatRoom', ChatRoomSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ChatRoom', ChatRoomSchema); +}; \ No newline at end of file diff --git a/app/models/comment.js b/app/models/comment.js index 2aaa3de..8bd6558 100644 --- a/app/models/comment.js +++ b/app/models/comment.js @@ -72,4 +72,6 @@ CommentSchema.index({ name: 'comment_replies', }); -module.exports = mongoose.model('Comment', CommentSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Comment', CommentSchema); +}; \ No newline at end of file diff --git a/app/models/connect-token.js b/app/models/connect-token.js index 7f22340..c9ef76f 100644 --- a/app/models/connect-token.js +++ b/app/models/connect-token.js @@ -16,4 +16,6 @@ const ConnectTokenSchema = new Schema({ claimed: { type: Date }, }); -module.exports = mongoose.model('ConnectToken', ConnectTokenSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ConnectToken', ConnectTokenSchema); +}; \ No newline at end of file diff --git a/app/models/content-report.js b/app/models/content-report.js index ffad674..bb1860e 100644 --- a/app/models/content-report.js +++ b/app/models/content-report.js @@ -28,4 +28,6 @@ ContentReportSchema.index({ name: 'unique_user_content_report', }); -module.exports = mongoose.model('ContentReport', ContentReportSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ContentReport', ContentReportSchema); +}; \ No newline at end of file diff --git a/app/models/content-vote.js b/app/models/content-vote.js index 490b098..28b21df 100644 --- a/app/models/content-vote.js +++ b/app/models/content-vote.js @@ -23,4 +23,6 @@ ContentVoteSchema.index({ name: 'unique_user_content_vote', }); -module.exports = mongoose.model('ContentVote', ContentVoteSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ContentVote', ContentVoteSchema); +}; \ No newline at end of file diff --git a/app/models/core-node-connect.js b/app/models/core-node-connect.js index 599503e..355ca5b 100644 --- a/app/models/core-node-connect.js +++ b/app/models/core-node-connect.js @@ -30,4 +30,6 @@ const CoreNodeConnectSchema = new Schema({ }, }); -module.exports = mongoose.model('CoreNodeConnect', CoreNodeConnectSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('CoreNodeConnect', CoreNodeConnectSchema); +}; \ No newline at end of file diff --git a/app/models/core-node-request.js b/app/models/core-node-request.js index df526f4..730d117 100644 --- a/app/models/core-node-request.js +++ b/app/models/core-node-request.js @@ -35,4 +35,6 @@ const CoreNodeRequestSchema = new Schema({ }, }); -module.exports = mongoose.model('CoreNodeRequest', CoreNodeRequestSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('CoreNodeRequest', CoreNodeRequestSchema); +}; \ No newline at end of file diff --git a/app/models/core-node.js b/app/models/core-node.js index 81eda79..3a037af 100644 --- a/app/models/core-node.js +++ b/app/models/core-node.js @@ -45,4 +45,6 @@ CoreNodeSchema.index({ name: 'core_address_idx', }); -module.exports = mongoose.model('CoreNode', CoreNodeSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('CoreNode', CoreNodeSchema); +}; \ No newline at end of file diff --git a/app/models/core-user.js b/app/models/core-user.js index 04e9e46..f91a347 100644 --- a/app/models/core-user.js +++ b/app/models/core-user.js @@ -52,4 +52,6 @@ CoreUserSchema.index({ name: 'core_username_lc_unique', }); -module.exports = mongoose.model('CoreUser', CoreUserSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('CoreUser', CoreUserSchema); +}; \ No newline at end of file diff --git a/app/models/csrf-token.js b/app/models/csrf-token.js index 81d75df..2337094 100644 --- a/app/models/csrf-token.js +++ b/app/models/csrf-token.js @@ -17,4 +17,6 @@ const CsrfTokenSchema = new Schema({ ip: { type: String, required: true }, }); -module.exports = mongoose.model('CsrfToken', CsrfTokenSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('CsrfToken', CsrfTokenSchema); +}; \ No newline at end of file diff --git a/app/models/email-blacklist.js b/app/models/email-blacklist.js index ff03a3a..50281c6 100644 --- a/app/models/email-blacklist.js +++ b/app/models/email-blacklist.js @@ -31,4 +31,6 @@ EmailBlacklistSchema.index({ }, }); -module.exports = mongoose.model('EmailBlacklist', EmailBlacklistSchema); +module.exports = (conn) => { + return conn.model('EmailBlacklist', EmailBlacklistSchema); +}; \ No newline at end of file diff --git a/app/models/email-body.js b/app/models/email-body.js index e8e3824..c0ceb57 100644 --- a/app/models/email-body.js +++ b/app/models/email-body.js @@ -12,4 +12,6 @@ const EmailBodySchema = new Schema({ body: { type: String, required: true }, }); -module.exports = mongoose.model('EmailBody', EmailBodySchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('EmailBody', EmailBodySchema); +}; \ No newline at end of file diff --git a/app/models/email-log.js b/app/models/email-log.js index b15fdec..d196d85 100644 --- a/app/models/email-log.js +++ b/app/models/email-log.js @@ -16,4 +16,6 @@ const EmailLogSchema = new Schema({ messageId: { type: String }, }); -module.exports = mongoose.model('EmailLog', EmailLogSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('EmailLog', EmailLogSchema); +}; \ No newline at end of file diff --git a/app/models/email-verify.js b/app/models/email-verify.js index 9b86900..e1ebd49 100644 --- a/app/models/email-verify.js +++ b/app/models/email-verify.js @@ -15,4 +15,6 @@ const EmailVerifySchema = new Schema({ token: { type: String, required: true }, }); -module.exports = mongoose.model('EmailVerify', EmailVerifySchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('EmailVerify', EmailVerifySchema); +}; \ No newline at end of file diff --git a/app/models/email.js b/app/models/email.js index f76bcae..b745c5b 100644 --- a/app/models/email.js +++ b/app/models/email.js @@ -17,4 +17,6 @@ const EmailSchema = new Schema({ content: { type: Schema.ObjectId, required: true, index: true, refPath: 'contentType' }, }); -module.exports = mongoose.model('Email', EmailSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Email', EmailSchema); +}; \ No newline at end of file diff --git a/app/models/emoji-reaction.js b/app/models/emoji-reaction.js index 6ce84a2..ce87469 100644 --- a/app/models/emoji-reaction.js +++ b/app/models/emoji-reaction.js @@ -36,4 +36,6 @@ const EmojiReactionSchema = new Schema({ timestamp: { type: Number }, }); -module.exports = mongoose.model('EmojiReaction', EmojiReactionSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('EmojiReaction', EmojiReactionSchema); +}; \ No newline at end of file diff --git a/app/models/feed-entry.js b/app/models/feed-entry.js index 676888a..fa708f8 100644 --- a/app/models/feed-entry.js +++ b/app/models/feed-entry.js @@ -23,4 +23,6 @@ FeedEntrySchema.index({ name: 'feed_entry_by_feed_idx', }); -module.exports = mongoose.model('FeedEntry', FeedEntrySchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('FeedEntry', FeedEntrySchema); +}; \ No newline at end of file diff --git a/app/models/feed.js b/app/models/feed.js index 84bc39e..4d2524c 100644 --- a/app/models/feed.js +++ b/app/models/feed.js @@ -18,4 +18,6 @@ const FeedSchema = new Schema({ published: { type: Date }, }); -module.exports = mongoose.model('Feed', FeedSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Feed', FeedSchema); +}; \ No newline at end of file diff --git a/app/models/image.js b/app/models/image.js index d31e6da..c2511c1 100644 --- a/app/models/image.js +++ b/app/models/image.js @@ -32,4 +32,6 @@ const ImageSchema = new Schema({ }, }); -module.exports = mongoose.model('Image', ImageSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Image', ImageSchema); +}; \ No newline at end of file diff --git a/app/models/kaleidoscope-event.js b/app/models/kaleidoscope-event.js index b76f33b..949547f 100644 --- a/app/models/kaleidoscope-event.js +++ b/app/models/kaleidoscope-event.js @@ -58,4 +58,6 @@ KaleidoscopeEventSchema.index({ name: 'evtsrc_site_author_index', }); -module.exports = mongoose.model('KaleidoscopeEvent', KaleidoscopeEventSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('KaleidoscopeEvent', KaleidoscopeEventSchema); +}; \ No newline at end of file diff --git a/app/models/log.js b/app/models/log.js index b2462f6..b74d682 100644 --- a/app/models/log.js +++ b/app/models/log.js @@ -29,4 +29,6 @@ const LogSchema = new Schema({ metadata: { type: Schema.Types.Mixed }, }); -module.exports = mongoose.model('Log', LogSchema); +module.exports = (conn) => { + return conn.model('Log', LogSchema); +}; \ No newline at end of file diff --git a/app/models/media-router.js b/app/models/media-router.js index c8dc32c..0416002 100644 --- a/app/models/media-router.js +++ b/app/models/media-router.js @@ -50,4 +50,6 @@ const MediaRouterSchema = new Schema({ } }); -module.exports = mongoose.model('MediaRouter', MediaRouterSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('MediaRouter', MediaRouterSchema); +}; \ No newline at end of file diff --git a/app/models/media-worker.js b/app/models/media-worker.js index feebc87..51b5e4c 100644 --- a/app/models/media-worker.js +++ b/app/models/media-worker.js @@ -40,4 +40,6 @@ const MediaWorkerSchema = new Schema({ } }); -module.exports = mongoose.model('MediaWorker', MediaWorkerSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('MediaWorker', MediaWorkerSchema); +}; \ No newline at end of file diff --git a/app/models/net-host-stats.js b/app/models/net-host-stats.js index 084068e..76f315a 100644 --- a/app/models/net-host-stats.js +++ b/app/models/net-host-stats.js @@ -72,4 +72,6 @@ const NetHostStatsSchema = new Schema({ network: { type: [NetworkInterfaceStatsSchema], required: true }, }); -module.exports = mongoose.model('NetHostStats', NetHostStatsSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('NetHostStats', NetHostStatsSchema); +}; \ No newline at end of file diff --git a/app/models/net-host.js b/app/models/net-host.js index 0646b2e..c8d482c 100644 --- a/app/models/net-host.js +++ b/app/models/net-host.js @@ -42,4 +42,6 @@ const NetHostSchema = new Schema({ network: { type: [NetworkInterfaceSchema] }, }); -module.exports = mongoose.model('NetHost', NetHostSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('NetHost', NetHostSchema); +}; \ No newline at end of file diff --git a/app/models/newsletter-recipient.js b/app/models/newsletter-recipient.js index da76741..2acfb65 100644 --- a/app/models/newsletter-recipient.js +++ b/app/models/newsletter-recipient.js @@ -18,4 +18,6 @@ const NewsletterRecipientSchema = new Schema({ }, }); -module.exports = mongoose.model('NewsletterRecipient', NewsletterRecipientSchema); +module.exports = (conn) => { + return conn.model('NewsletterRecipient', NewsletterRecipientSchema); +}; \ No newline at end of file diff --git a/app/models/newsletter.js b/app/models/newsletter.js index b036fdb..0f11675 100644 --- a/app/models/newsletter.js +++ b/app/models/newsletter.js @@ -28,4 +28,6 @@ const NewsletterSchema = new Schema({ }, }); -module.exports = mongoose.model('Newsletter', NewsletterSchema); +module.exports = (conn) => { + return conn.model('Newsletter', NewsletterSchema); +}; \ No newline at end of file diff --git a/app/models/oauth2-authorization-code.js b/app/models/oauth2-authorization-code.js index 2017ea7..4278c90 100644 --- a/app/models/oauth2-authorization-code.js +++ b/app/models/oauth2-authorization-code.js @@ -16,4 +16,6 @@ const OAuth2AuthorizationCodeSchema = new Schema({ scopes: { type: [String], required: true }, }); -module.exports = mongoose.model('OAuth2AuthorizationCode', OAuth2AuthorizationCodeSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('OAuth2AuthorizationCode', OAuth2AuthorizationCodeSchema); +}; \ No newline at end of file diff --git a/app/models/oauth2-client.js b/app/models/oauth2-client.js index de56e24..e9d9877 100644 --- a/app/models/oauth2-client.js +++ b/app/models/oauth2-client.js @@ -40,4 +40,6 @@ OAuth2ClientSchema.index({ unique: true, }); -module.exports = mongoose.model('OAuth2Client', OAuth2ClientSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('OAuth2Client', OAuth2ClientSchema); +}; \ No newline at end of file diff --git a/app/models/oauth2-token.js b/app/models/oauth2-token.js index f8133dd..692e279 100644 --- a/app/models/oauth2-token.js +++ b/app/models/oauth2-token.js @@ -24,4 +24,6 @@ OAuth2TokenSchema.index({ name: 'oauth2_token_unique', }); -module.exports = mongoose.model('OAuth2Token', OAuth2TokenSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('OAuth2Token', OAuth2TokenSchema); +}; \ No newline at end of file diff --git a/app/models/otp-account.js b/app/models/otp-account.js index fcaea75..1bc1287 100644 --- a/app/models/otp-account.js +++ b/app/models/otp-account.js @@ -33,4 +33,6 @@ OtpAccountSchema.index({ name: 'otp_user_svc_uniq_idx', }); -module.exports = mongoose.model('OtpAccount', OtpAccountSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('OtpAccount', OtpAccountSchema); +}; \ No newline at end of file diff --git a/app/models/resource-view.js b/app/models/resource-view.js index d59ad0a..c2cfbd2 100644 --- a/app/models/resource-view.js +++ b/app/models/resource-view.js @@ -29,4 +29,6 @@ ResourceViewSchema.index({ name: 'res_view_daily_unique', }); -module.exports = mongoose.model('ResourceView', ResourceViewSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ResourceView', ResourceViewSchema); +}; \ No newline at end of file diff --git a/app/models/resource-visit.js b/app/models/resource-visit.js index 770ce64..1c36baf 100644 --- a/app/models/resource-visit.js +++ b/app/models/resource-visit.js @@ -26,4 +26,6 @@ ResourceVisitSchema.index({ name: 'resource_visits_for_user', }); -module.exports = mongoose.model('ResourceVisit', ResourceVisitSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('ResourceVisit', ResourceVisitSchema); +}; \ No newline at end of file diff --git a/app/models/sticker.js b/app/models/sticker.js index 5e33840..55dd4fe 100644 --- a/app/models/sticker.js +++ b/app/models/sticker.js @@ -43,4 +43,6 @@ const StickerSchema = new Schema({ encoded: { type: StickerMediaSchema }, }); -module.exports = mongoose.model('Sticker', StickerSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('Sticker', StickerSchema); +}; \ No newline at end of file diff --git a/app/models/user-block.js b/app/models/user-block.js index f6396e1..842c233 100644 --- a/app/models/user-block.js +++ b/app/models/user-block.js @@ -14,4 +14,6 @@ const UserBlockSchema = new Schema({ blockedMembers: { type: [DtpUserSchema] }, }); -module.exports = mongoose.model('UserBlock', UserBlockSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('UserBlock', UserBlockSchema); +}; \ No newline at end of file diff --git a/app/models/user-notification.js b/app/models/user-notification.js index 544de5f..fa5a0ff 100644 --- a/app/models/user-notification.js +++ b/app/models/user-notification.js @@ -24,4 +24,6 @@ const UserNotificationSchema = new Schema({ event: { type: Schema.ObjectId, required: true, ref: 'KaleidoscopeEvent' }, }); -module.exports = mongoose.model('UserNotification', UserNotificationSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('UserNotification', UserNotificationSchema); +}; \ No newline at end of file diff --git a/app/models/user-subscription.js b/app/models/user-subscription.js index 7200eea..d7bea99 100644 --- a/app/models/user-subscription.js +++ b/app/models/user-subscription.js @@ -24,4 +24,6 @@ const UserSubscriptionSchema = new Schema({ subscriptions: { type: [SubscriptionSchema] }, }); -module.exports = mongoose.model('UserSubscription', UserSubscriptionSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('UserSubscription', UserSubscriptionSchema); +}; \ No newline at end of file diff --git a/app/models/user.js b/app/models/user.js index 897e571..92f7949 100644 --- a/app/models/user.js +++ b/app/models/user.js @@ -43,4 +43,6 @@ const UserSchema = new Schema({ lastAnnouncement: { type: Date }, }); -module.exports = mongoose.model('User', UserSchema); \ No newline at end of file +module.exports = (conn) => { + return conn.model('User', UserSchema); +}; \ No newline at end of file diff --git a/lib/site-platform.js b/lib/site-platform.js index 8bb8994..cc6ba90 100644 --- a/lib/site-platform.js +++ b/lib/site-platform.js @@ -58,12 +58,12 @@ module.connectDatabase = async (/*dtp*/) => { } }; - module.loadModels = async (dtp) => { dtp.models = module.models = [ ]; const modelScripts = glob.sync(path.join(dtp.config.root, 'app', 'models', '*.js')); modelScripts.forEach((modelScript) => { - const model = require(modelScript); + const instance = require(modelScript); + const model = instance(module.db); if (module.models[model.modelName]) { module.log.error('model name collision', { name: model.modelName }); process.exit(-1); @@ -206,7 +206,7 @@ module.exports.startPlatform = async (dtp) => { await module.connectRedis(dtp); await module.loadModels(dtp); - SiteLog.setModel(mongoose.model('Log')); + SiteLog.setModel(module.db.model('Log')); await module.loadServices(dtp);