Skip to content

Conversation

KerollosMelad
Copy link

refactor: adopt DDD with rich domain models, migrate to MikroORM, and replace ioctopus with tsyringe

  • Restructured the codebase to follow Domain-Driven Design (DDD) using rich domain models instead of anemic ones.
  • Replaced Drizzle ORM with MikroORM to enable the Unit of Work pattern and automatic change tracking.
  • Updated dependency injection: replaced ioctopus with tsyringe.
  • Modified the DI setup to resolve repositories per request, enabling Unit of Work handling through MikroORM's EntityManager.

… replace ioctopus with tsyringe

- Restructured the codebase to follow Domain-Driven Design (DDD) using rich domain models instead of anemic ones.
- Replaced Drizzle ORM with MikroORM to enable the Unit of Work pattern and automatic change tracking.
- Updated dependency injection: replaced ioctopus with tsyringe.
- Modified the DI setup to resolve repositories per request, enabling Unit of Work handling through MikroORM's EntityManager.
@vercel
Copy link

vercel bot commented Jul 2, 2025

@KerollosMelad is attempting to deploy a commit to the Lazar Nikolov's projects Team on Vercel.

A member of the Team first needs to authorize it.

…ntication

- Replaced previous action-based authentication with fetch API calls for sign-in and sign-up processes.
- Added validation to ensure required fields are filled before submission.
- Improved error handling for network issues and server responses.
- Updated UI text for clarity in both forms.
… and reliability

- Removed server actions for creating, toggling, and bulk updating todos, replacing them with API route implementations.
- Updated client components to use fetch API for todo operations, ensuring a consistent approach across authentication and todo management.
- Enhanced error handling and user feedback for todo operations.
- Cleaned up unused imports and code related to previous action-based implementations.
- Added MemoryCacheAdapter for improved caching in serverless environments.
- Updated metadataProvider to use ReflectMetadataProvider for better serverless support.
- Removed unused closeDatabase function and related code from the DI setup.
…oORM

- Changed ManyToOne and OneToMany decorators to use object notation for better clarity and consistency.
- Updated imports in user.entity.ts to use type imports for improved performance and clarity.
…pe imports

- Updated @mikro-orm/reflection dependency to version 6.4.16 in package.json and package-lock.json.
- Introduced a new types.ts file to centralize entity type exports, avoiding circular dependencies in entity models.
- Modified user, session, and todo entity imports to reference types from the new types.ts file for better clarity and maintainability.
- Updated MikroORM configuration to use glob patterns for entity imports, improving compatibility with Vercel.
- Simplified entity imports across application modules by consolidating them into a single import from the entities directory.
- Enhanced clarity and maintainability of the codebase by reducing the number of direct entity imports.
… discovery settings

- Replaced glob patterns with direct imports for User, Todo, and Session entities to enhance reliability with Vercel.
- Added explicit discovery settings to improve entity management and compatibility in serverless environments.
- Removed lazy loading from ManyToOne relationships in User, Session, and Todo entities for improved clarity and performance.
- Added reflect-metadata import in instrumentation.ts for enhanced metadata handling.
- Added support for production-ready metadata caching in mikro-orm.config.ts, utilizing GeneratedCacheAdapter for production environments.
- Updated .gitignore to include MikroORM metadata cache directory.
- Configured Next.js to treat MikroORM packages as external in server components for improved compatibility.
…less deployment

- Removed unused reflect-metadata import from instrumentation.ts.
- Updated mikro-orm.config.ts to disable dynamic file access for Vercel compatibility.
- Enhanced documentation in mikro-orm-deployment.md with root causes and solutions for entity discovery issues.
- Clarified comments in session.entity.ts and todo.entity.ts regarding string references to avoid circular dependencies.
…cumentation

- Added new npm scripts for cache clearing and clean builds in package.json.
- Updated mikro-orm-deployment.md to include critical cache clearing steps and improved deployment workflow for better entity discovery.
…rences

- Updated ManyToOne relationships in session.entity.ts and todo.entity.ts to include explicit fieldName for foreign key references, improving clarity and avoiding circular dependencies.
- Modified cache:clear script in package.json to use PowerShell for better compatibility across environments.
… handling

- Consolidated entity imports in mikro-orm.config.ts for better organization.
- Added detailed logging for cache status and entity imports to aid in debugging.
- Updated metadata cache handling to check for cache file existence and log relevant information.
- Introduced new npm script for Vercel builds using cross-env for environment variable management.
…improvements

- Added forceDisableCache option to mikro-orm.config.ts for better cache management.
- Improved logging to include forceDisableCache status during MikroORM configuration loading.
- Updated Next.js webpack configuration to prevent minification of entity class names for better compatibility.
- Modified package.json script for Vercel builds to set NODE_ENV to production.
- Expanded Vercel debugging guide with emergency cache disable instructions for entity discovery issues.
…umentation

- Integrated MikroORM initialization directly within the instrumentation function for improved performance.
- Added error handling to ensure DI container initialization proceeds even if MikroORM fails to initialize.
- Updated database module to utilize the globally stored ORM instance if already initialized, reducing redundant initialization.
- Adjusted mikro-orm.config.ts to always disable cache on Vercel for better compatibility.
- Removed complex cache handling and disabled metadata cache for improved compatibility with Vercel.
- Updated entity imports to use direct references, ensuring consistent bundling and preventing tree-shaking.
- Streamlined TypeScript configuration for better compatibility and performance.
- Cleaned up package.json scripts by removing unnecessary cache generation commands.
- Updated entity imports to streamline references and prevent tree-shaking.
- Disabled force entity constructor to resolve prototype issues and improve compatibility.
- Refactored user creation logic to include validation and proper entity registration using EntityManager.
- Modified constructors in User, Todo, and Session entities for MikroORM compatibility, ensuring proper initialization.
- Improved session creation logic to utilize EntityManager for consistent entity handling.
- Changed private properties to public in Session, Todo, and User entities to enhance accessibility and maintain consistency across entity definitions.
- Updated session creation logic in AuthenticationService to include user reference for improved session management.
- Updated entity imports in mikro-orm.config.ts to use specific model paths for better clarity and organization.
- Rearranged the order of entities in the configuration for consistency.
- Changed entity imports in session.entity.ts and todo.entity.ts to use direct class references, improving clarity and preventing minification issues.
- Updated ManyToOne and OneToMany relationships in User, Session, and Todo entities to utilize class references for better compatibility with MikroORM.
@nikolovlazar
Copy link
Owner

Thank you for working on this @KerollosMelad 🙌 I definitely appreciate you refactoring the entities to proper DDD. I shared this PR with my community because I think it could be an interesting learning resource, but I wouldn't merge it to main because this repo is tied to the YouTube video I recorded, so people coming from the video would expect to see more or less the same code they saw on the video. This repo is "technically" frozen - can't deteriorate too much from the current state. I would encourage you to either leave this PR open, or to push it on your own repo and I'll add link to it in this one as a different "flavour" of this one. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants