-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from zhumeisongsong/feature/user-infrastructure
feat: ✨ mongoose of user infrastructure
- Loading branch information
Showing
14 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const baseConfig = require('../../../../eslint.config.js'); | ||
|
||
module.exports = [...baseConfig]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default { | ||
displayName: 'users-infrastructure-mongoose', | ||
preset: '../../../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../../coverage/libs/users/infrastructure/mongoose', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './lib/mongoose-user.repository'; | ||
export * from './lib/user.schema'; |
47 changes: 47 additions & 0 deletions
47
libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UserDocument>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
MongooseUserRepository, | ||
{ | ||
provide: getModelToken(UserDocument.name), | ||
useValue: { | ||
findById: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
repository = module.get<MongooseUserRepository>(MongooseUserRepository); | ||
userModel = module.get<Model<UserDocument>>(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(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
libs/users/infrastructure/mongoose/src/lib/mongoose-user.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UserDocument>, | ||
) {} | ||
|
||
async findById(id: string): Promise<User | null> { | ||
const userDoc = await this.userModel.findById(id).exec(); | ||
return userDoc ? new User(userDoc.id, userDoc.name) : null; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
libs/users/infrastructure/mongoose/src/lib/user.schema.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UserDocument>; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: getModelToken(UserDocument.name), | ||
useValue: Model, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
userModel = module.get<Model<UserDocument>>(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'); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters