diff --git a/README.md b/README.md index 1f10923..06b49ea 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,14 @@ You can use `npx nx list` to get a list of installed plugins. Then, run `npx nx ## Architecture -| Layer | Description | -| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| dto(presentation) | Define DTOs for GraphQL schema. | -| resolver(presentation) | Define GraphQL schema and resolver. | -| use-case(application) | Define business use cases and encapsulate business logic. | -| entity(domain) | Define core business entities and business rules.
Maintain entity independence from database and framework. | -| repository(domain) | Interfaces (or abstract classes), which define methods for manipulating data without concern for specific database implementations.
By defining this interface, we can decouple database access: the specific details of data access will be done by implementation classes, such as specific implementations using tools like Mongoose, TypeORM, Prisma, and so on. | +| Layer | Description | +| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| dto(presentation) | Define DTOs for GraphQL schema. | +| resolver(presentation) | Define GraphQL schema and resolver. | +| mongoose(infrastructure) | Implements the repository interfaces defined in the domain layer using Mongoose as the ODM (Object Document Mapper).
Includes Mongoose Schema definitions, database connection management, and concrete implementations of repository interfaces (e.g., MongooseUserRepository). | +| use-case(application) | Define business use cases and encapsulate business logic. | +| entity(domain) | Define core business entities and business rules.
Maintain entity independence from database and framework. | +| repository(domain) | Interfaces (or abstract classes), which define methods for manipulating data without concern for specific database implementations.
By defining this interface, we can decouple database access: the specific details of data access will be done by implementation classes, such as specific implementations using tools like Mongoose, TypeORM, Prisma, and so on. | ## Useful links diff --git a/libs/users/infrastructure/mongoose/README.md b/libs/users/infrastructure/mongoose/README.md new file mode 100644 index 0000000..14cbad6 --- /dev/null +++ b/libs/users/infrastructure/mongoose/README.md @@ -0,0 +1,7 @@ +# users-infrastructure-mongoose + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test users-infrastructure-mongoose` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/libs/users/infrastructure/mongoose/eslint.config.js b/libs/users/infrastructure/mongoose/eslint.config.js new file mode 100644 index 0000000..cdd3cba --- /dev/null +++ b/libs/users/infrastructure/mongoose/eslint.config.js @@ -0,0 +1,3 @@ +const baseConfig = require('../../../../eslint.config.js'); + +module.exports = [...baseConfig]; diff --git a/libs/users/infrastructure/mongoose/jest.config.ts b/libs/users/infrastructure/mongoose/jest.config.ts new file mode 100644 index 0000000..53af5c5 --- /dev/null +++ b/libs/users/infrastructure/mongoose/jest.config.ts @@ -0,0 +1,10 @@ +export default { + displayName: 'users-infrastructure-mongoose', + preset: '../../../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../../../coverage/libs/users/infrastructure/mongoose', +}; diff --git a/libs/users/infrastructure/mongoose/project.json b/libs/users/infrastructure/mongoose/project.json new file mode 100644 index 0000000..2787315 --- /dev/null +++ b/libs/users/infrastructure/mongoose/project.json @@ -0,0 +1,9 @@ +{ + "name": "users-infrastructure-mongoose", + "$schema": "../../../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/users/infrastructure/mongoose/src", + "projectType": "library", + "tags": [], + "// targets": "to see all targets run: nx show project users-infrastructure-mongoose --web", + "targets": {} +} diff --git a/libs/users/infrastructure/mongoose/src/index.ts b/libs/users/infrastructure/mongoose/src/index.ts new file mode 100644 index 0000000..ac4244f --- /dev/null +++ b/libs/users/infrastructure/mongoose/src/index.ts @@ -0,0 +1,2 @@ +export * from './lib/mongoose-user.repository'; +export * from './lib/user.schema'; \ No newline at end of file diff --git a/libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.spec.ts b/libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.spec.ts new file mode 100644 index 0000000..20a9466 --- /dev/null +++ b/libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.spec.ts @@ -0,0 +1,47 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { getModelToken } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import { MongooseUserRepository } from './mongoose-user.repository'; +import { UserDocument } from './user.schema'; +import { User } from '@user/domain'; + +describe('MongooseUserRepository', () => { + let repository: MongooseUserRepository; + let userModel: Model; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + MongooseUserRepository, + { + provide: getModelToken(UserDocument.name), + useValue: { + findById: jest.fn(), + }, + }, + ], + }).compile(); + + repository = module.get(MongooseUserRepository); + userModel = module.get>(getModelToken(UserDocument.name)); + }); + + it('should return a user when found', async () => { + const userDoc = { id: '1', name: 'John Doe' }; + jest.spyOn(userModel, 'findById').mockReturnValue({ + exec: jest.fn().mockResolvedValue(userDoc), + } as any); + + const user = await repository.findById('1'); + expect(user).toEqual(new User('1', 'John Doe')); + }); + + it('should return null when user is not found', async () => { + jest.spyOn(userModel, 'findById').mockReturnValue({ + exec: jest.fn().mockResolvedValue(null), + } as any); + + const user = await repository.findById('1'); + expect(user).toBeNull(); + }); +}); \ No newline at end of file diff --git a/libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.ts b/libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.ts new file mode 100644 index 0000000..61af61d --- /dev/null +++ b/libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.ts @@ -0,0 +1,16 @@ +import { InjectModel } from '@nestjs/mongoose'; +import { User, UserRepository } from '@user/domain'; +import { Model } from 'mongoose'; + +import { UserDocument } from './user.schema'; + +export class MongooseUserRepository implements UserRepository { + constructor( + @InjectModel(UserDocument.name) private userModel: Model, + ) {} + + async findById(id: string): Promise { + const userDoc = await this.userModel.findById(id).exec(); + return userDoc ? new User(userDoc.id, userDoc.name) : null; + } +} diff --git a/libs/users/infrastructure/mongoose/src/lib/user.schema.spec.ts b/libs/users/infrastructure/mongoose/src/lib/user.schema.spec.ts new file mode 100644 index 0000000..848c5e9 --- /dev/null +++ b/libs/users/infrastructure/mongoose/src/lib/user.schema.spec.ts @@ -0,0 +1,36 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { getModelToken } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import { UserDocument } from './user.schema'; + +describe('UserDocument', () => { + let userModel: Model; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: getModelToken(UserDocument.name), + useValue: Model, + }, + ], + }).compile(); + + userModel = module.get>(getModelToken(UserDocument.name)); + }); + + it('should be defined', () => { + expect(userModel).toBeDefined(); + }); + + // it('should require name field', () => { + // const user = new userModel(); + // const error = user.validateSync(); + // expect(error.errors['name']).toBeDefined(); + // }); + + // it('should create a user with a name', () => { + // const user = new userModel({ name: 'John Doe' }); + // expect(user.name).toBe('John Doe'); + // }); +}); \ No newline at end of file diff --git a/libs/users/infrastructure/mongoose/src/lib/user.schema.ts b/libs/users/infrastructure/mongoose/src/lib/user.schema.ts new file mode 100644 index 0000000..ff6527f --- /dev/null +++ b/libs/users/infrastructure/mongoose/src/lib/user.schema.ts @@ -0,0 +1,10 @@ +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; + +@Schema() +export class UserDocument extends Document { + @Prop({ required: true }) + name!: string; +} + +export const UserSchema = SchemaFactory.createForClass(UserDocument); \ No newline at end of file diff --git a/libs/users/infrastructure/mongoose/tsconfig.json b/libs/users/infrastructure/mongoose/tsconfig.json new file mode 100644 index 0000000..07e0ec6 --- /dev/null +++ b/libs/users/infrastructure/mongoose/tsconfig.json @@ -0,0 +1,22 @@ +{ + "extends": "../../../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noPropertyAccessFromIndexSignature": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/users/infrastructure/mongoose/tsconfig.lib.json b/libs/users/infrastructure/mongoose/tsconfig.lib.json new file mode 100644 index 0000000..28369ef --- /dev/null +++ b/libs/users/infrastructure/mongoose/tsconfig.lib.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "../../../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], + "include": ["src/**/*.ts"] +} diff --git a/libs/users/infrastructure/mongoose/tsconfig.spec.json b/libs/users/infrastructure/mongoose/tsconfig.spec.json new file mode 100644 index 0000000..6668655 --- /dev/null +++ b/libs/users/infrastructure/mongoose/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index 5180e51..ee04d91 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -22,6 +22,9 @@ ], "@user/domain": ["libs/user/domain/src/index.ts"], "@users/application": ["libs/users/application/src/index.ts"], + "@users/infrastructure-mongoose": [ + "libs/users/infrastructure/mongoose/src/index.ts" + ], "@users/presentation-dto": ["libs/users/presentation/dto/src/index.ts"], "@users/presentation-resolver": [ "libs/users/presentation/resolver/src/index.ts"