Skip to content

Commit

Permalink
Merge pull request #9 from numengames/fix/mongoose-model-overwrite
Browse files Browse the repository at this point in the history
fix: prevent mongoose model overwrite by checking existing models
  • Loading branch information
DevStarlight authored Oct 21, 2024
2 parents 481fbce + 05693dd commit db4b87f
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [6.0.2] - 2024-10-21

### Fixed

- Prevented Mongoose models from being redefined, avoiding `OverwriteModelError` by adding checks to use existing models if they are already defined.

## [6.0.1] - 2024-10-17

### Changed
Expand Down
5 changes: 3 additions & 2 deletions src/models/assistants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

export interface AssistantAttributes {
name: string;
Expand All @@ -18,6 +18,7 @@ const schema = new Schema<AssistantAttributes>(
{ versionKey: false, timestamps: true },
);

export const AssistantModel = model<AssistantAttributes>('Assistant', schema);
export const AssistantModel =
mongoose.models.Assistant || model<AssistantAttributes>('Assistant', schema);

export type AssistantDocument = ReturnType<(typeof AssistantModel)['hydrate']>;
9 changes: 4 additions & 5 deletions src/models/conversation-chunks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

export interface ConversationChunkAttributes {
role: string;
Expand All @@ -24,9 +24,8 @@ const schema = new Schema<ConversationChunkAttributes>(
{ versionKey: false, timestamps: true },
);

export const ConversationChunkModel = model<ConversationChunkAttributes>(
'ConversationChunk',
schema,
);
export const ConversationChunkModel =
mongoose.models.ConversationChunk ||
model<ConversationChunkAttributes>('ConversationChunk', schema);

export type ConversationChunkDocument = ReturnType<(typeof ConversationChunkModel)['hydrate']>;
5 changes: 3 additions & 2 deletions src/models/conversations/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

import { validateModelOrAssistant } from './validations';

Expand Down Expand Up @@ -49,6 +49,7 @@ schema.pre('validate', validateModelOrAssistant);

schema.index({ conversationId: 1 });

export const ConversationModel = model<ConversationAttributes>('Conversation', schema);
export const ConversationModel =
mongoose.models.Conversation || model<ConversationAttributes>('Conversation', schema);

export type ConversationDocument = ReturnType<(typeof ConversationModel)['hydrate']>;
4 changes: 2 additions & 2 deletions src/models/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

/**
* Attributes for logging in-game events in the Numinia platform.
Expand Down Expand Up @@ -49,6 +49,6 @@ const schema = new Schema<EventAttributes>(
{ versionKey: false },
);

export const EventModel = model<EventAttributes>('Event', schema);
export const EventModel = mongoose.models.Event || model<EventAttributes>('Event', schema);

export type EventDocument = ReturnType<(typeof EventModel)['hydrate']>;
5 changes: 3 additions & 2 deletions src/models/game-scores.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

export interface GameScoreAttributes {
timer: number;
Expand All @@ -23,6 +23,7 @@ const schema = new Schema<GameScoreAttributes>(
schema.index({ game: 1 });
schema.index({ player: 1 }, { sparse: true });

export const GameScoreModel = model<GameScoreAttributes>('GameScore', schema);
export const GameScoreModel =
mongoose.models.GameScore || model<GameScoreAttributes>('GameScore', schema);

export type GameScoreDocument = ReturnType<(typeof GameScoreModel)['hydrate']>;
4 changes: 2 additions & 2 deletions src/models/games.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

export interface GameAttributes {
name: string;
Expand All @@ -24,6 +24,6 @@ const schema = new Schema<GameAttributes>(
{ versionKey: false, timestamps: true },
);

export const GameModel = model<GameAttributes>('Game', schema);
export const GameModel = mongoose.models.Game || model<GameAttributes>('Game', schema);

export type GameDocument = ReturnType<(typeof GameModel)['hydrate']>;
5 changes: 3 additions & 2 deletions src/models/player-rewards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

import { RewardDocument } from './rewards';
import { PlayerDocument } from './players';
Expand All @@ -19,6 +19,7 @@ const schema = new Schema<PlayerRewardAttributes>(
{ versionKey: false, timestamps: true },
);

export const PlayerRewardModel = model<PlayerRewardAttributes>('PlayerReward', schema);
export const PlayerRewardModel =
mongoose.models.PlayerReward || model<PlayerRewardAttributes>('PlayerReward', schema);

export type PlayerRewardDocument = ReturnType<(typeof PlayerRewardModel)['hydrate']>;
5 changes: 3 additions & 2 deletions src/models/player-sessions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

/**
* Attributes for defining a session in the Numinia platform.
Expand Down Expand Up @@ -58,6 +58,7 @@ const schema = new Schema<PlayerSessionAttributes>(
{ versionKey: false },
);

export const PlayerSessionModel = model<PlayerSessionAttributes>('PlayerSession', schema);
export const PlayerSessionModel =
mongoose.models.PlayerSession || model<PlayerSessionAttributes>('PlayerSession', schema);

export type PlayerSessionDocument = ReturnType<(typeof PlayerSessionModel)['hydrate']>;
4 changes: 2 additions & 2 deletions src/models/players.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

/**
* Attributes for defining a player in the Numinia platform.
Expand Down Expand Up @@ -65,6 +65,6 @@ const schema = new Schema<PlayerAttributes>(
schema.index({ oncyberId: 1 }, { unique: true, sparse: true });
schema.index({ hyperfyId: 1 }, { unique: true, sparse: true });

export const PlayerModel = model<PlayerAttributes>('Player', schema);
export const PlayerModel = mongoose.models.Player || model<PlayerAttributes>('Player', schema);

export type PlayerDocument = ReturnType<(typeof PlayerModel)['hydrate']>;
5 changes: 3 additions & 2 deletions src/models/rewards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types, Document } from 'mongoose';
import mongoose, { Schema, model, Types, Document } from 'mongoose';

import RewardTypes from '../constants/reward-types';

Expand Down Expand Up @@ -38,7 +38,8 @@ const rewardSchema = new Schema<RewardAttributes>(
{ versionKey: false, timestamps: true, discriminatorKey: 'type' },
);

export const RewardModel = model<RewardAttributes>('Reward', rewardSchema);
export const RewardModel =
mongoose.models.Reward || model<RewardAttributes>('Reward', rewardSchema);

export const DigitalAssetRewardModel = RewardModel.discriminator<DigitalAssetRewardDocument>(
RewardTypes.DIGITAL_ASSET,
Expand Down
4 changes: 2 additions & 2 deletions src/models/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema, model, Types } from 'mongoose';
import mongoose, { Schema, model, Types } from 'mongoose';

/**
* Attributes for defining a wallet associated with a player in the Numinia platform.
Expand Down Expand Up @@ -40,6 +40,6 @@ const schema = new Schema<WalletAttributes>(

schema.index({ walletAddress: 1 }, { unique: true });

export const WalletModel = model<WalletAttributes>('Wallet', schema);
export const WalletModel = mongoose.models.Wallet || model<WalletAttributes>('Wallet', schema);

export type WalletDocument = ReturnType<(typeof WalletModel)['hydrate']>;

0 comments on commit db4b87f

Please sign in to comment.