Skip to content

Commit f8e394d

Browse files
committed
add: mongoose schemas & notifications and messages file structure
1 parent 5d0da18 commit f8e394d

17 files changed

+604
-49
lines changed

package-lock.json

+440-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"dependencies": {
2424
"@nestjs/common": "^8.0.0",
2525
"@nestjs/core": "^8.0.0",
26+
"@nestjs/mongoose": "^9.0.3",
2627
"@nestjs/platform-express": "^8.0.0",
28+
"class-transformer": "^0.5.1",
29+
"class-validator": "^0.13.2",
30+
"mongoose": "^6.2.7",
2731
"reflect-metadata": "^0.1.13",
2832
"rimraf": "^3.0.2",
2933
"rxjs": "^7.2.0"

src/app.module.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import { Module } from '@nestjs/common';
22
import { AppController } from './app.controller';
33
import { AppService } from './app.service';
44
import { UsersModule } from './users/users.module';
5+
import { MongooseModule } from '@nestjs/mongoose';
6+
import { NotificationsModule } from './notifications/notifications.module';
7+
import { MessagesModule } from './messages/messages.module';
58

69
@Module({
7-
imports: [UsersModule],
10+
imports: [
11+
UsersModule,
12+
NotificationsModule,
13+
MessagesModule,
14+
MongooseModule.forRoot('mongodb://localhost:27017/mcfly-nestjs'),
15+
],
816
controllers: [AppController],
917
providers: [AppService],
1018
})

src/messages/messages.controller.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Controller } from '@nestjs/common';
2+
3+
@Controller('messages')
4+
export class MessagesController {}

src/messages/messages.module.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { MessagesController } from './messages.controller';
3+
import { MessagesService } from './messages.service';
4+
5+
@Module({
6+
controllers: [MessagesController],
7+
providers: [MessagesService]
8+
})
9+
export class MessagesModule {}

src/messages/messages.service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class MessagesService {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Controller } from '@nestjs/common';
2+
3+
@Controller('notifications')
4+
export class NotificationsController {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { NotificationsController } from './notifications.controller';
3+
import { NotificationsService } from './notifications.service';
4+
5+
@Module({
6+
controllers: [NotificationsController],
7+
providers: [NotificationsService]
8+
})
9+
export class NotificationsModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class NotificationsService {}

src/schemas/message.schema.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
2+
import { Transform } from "class-transformer";
3+
// import { Document, Types } from 'mongoose'
4+
5+
@Schema()
6+
export class Message {
7+
8+
@Transform(({ value }) => value.toString()) // To manage type ID to type string
9+
_id: string;
10+
11+
@Prop()
12+
message: string;
13+
14+
@Prop({ default: Date })
15+
sent: Date;
16+
17+
@Prop()
18+
from: string;
19+
20+
@Prop()
21+
to: string;
22+
}
23+
24+
export const MessageSchema = SchemaFactory.createForClass(Message);

src/schemas/notification.schema.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
2+
import { Transform } from "class-transformer";
3+
import { Document, Types } from 'mongoose'
4+
import { Message } from "./message.schema";
5+
6+
@Schema()
7+
export class Notification {
8+
9+
@Transform(({ value }) => value.toString()) // To manage type ID to type string
10+
_id: string;
11+
12+
@Prop({type: [Types.ObjectId], ref: Message.name, default: []})
13+
messages: Message[];
14+
}
15+
16+
export const NotificationSchema = SchemaFactory.createForClass(Notification);

src/schemas/users.schema.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
2+
import { Exclude, Transform } from "class-transformer";
3+
import { Document, Types } from 'mongoose'; //to manage relations
4+
import { Message } from "./message.schema";
5+
6+
@Schema()
7+
export class User {
8+
9+
@Transform(({ value }) => value.toString()) // To manage type ID to type string
10+
_id: string;
11+
12+
@Prop()
13+
name: string;
14+
15+
@Prop({ unique: true })
16+
email: string;
17+
18+
@Prop()
19+
@Exclude()
20+
password: string;
21+
22+
@Prop({default: true})
23+
available: boolean;
24+
25+
@Prop({type: [Types.ObjectId], ref: Message.name, default: []})
26+
messages: Message[];
27+
28+
@Prop({type: Types.ObjectId, ref: Notification.name})
29+
notifications: Notification;
30+
};
31+
32+
export const UserSchema = SchemaFactory.createForClass(User);

src/users/dto/create-user.dto.ts

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
import { IsEmail, IsNotEmpty, IsNumber, IsString } from "class-validator"; // import dependency if you believe in this with npm install class-validator --save
2-
// or $ npm i --save class-validator class-transformer for ValidationPipeOptions, recommended for nest doc
1+
import { IsEmail, IsNotEmpty, IsString } from "class-validator"; // https://docs.nestjs.com/techniques/validation
32

43
export class createUserDto {
54

65
@IsString()
7-
@IsNotEmpty()
8-
readonly fullname: string;
9-
10-
@IsString()
11-
@IsNotEmpty()
12-
readonly nickname: string;
13-
14-
@IsEmail()
15-
@IsNotEmpty()
16-
readonly email: string;
17-
18-
@IsString()
19-
@IsNotEmpty()
20-
password: string;
6+
@IsNotEmpty()
7+
readonly name: string;
8+
9+
@IsEmail()
10+
@IsNotEmpty()
11+
readonly email: string;
12+
13+
@IsString()
14+
@IsNotEmpty()
15+
password: string;
2116

2217
}

src/users/interfaces/user.interface.ts

Whitespace-only changes.

src/users/users.controller.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
import { Controller } from '@nestjs/common';
1+
import {
2+
Controller,
3+
Get, Post, Put, Patch,
4+
Req, Res,
5+
HttpStatus,
6+
Body
7+
} from '@nestjs/common';
8+
import { createUserDto } from './dto/create-user.dto';
29

310
@Controller('users')
4-
export class UsersController {}
11+
export class UsersController {
12+
13+
@Post('/create')
14+
createUser(@Res() res, @Body() createUserDto: createUserDto){
15+
16+
console.log(createUserDto);
17+
18+
return res.status(HttpStatus.OK).json({
19+
message: 'received'
20+
})
21+
}
22+
23+
}

src/users/users.module.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { Module } from '@nestjs/common';
1+
import { Module, forwardRef } from '@nestjs/common';
2+
import { MongooseModule } from '@nestjs/mongoose';
3+
import { NotificationsModule } from 'src/notifications/notifications.module';
4+
25
import { UsersController } from './users.controller';
36
import { UsersService } from './users.service';
7+
import { User, UserSchema} from '../schemas/users.schema'
48

59
@Module({
10+
imports: [
11+
12+
MongooseModule.forFeature([ { name: User.name, schema: UserSchema } ]),
13+
forwardRef(() => NotificationsModule),
14+
15+
],
616
controllers: [UsersController],
7-
providers: [UsersService]
17+
providers: [UsersService],
818
})
919
export class UsersModule {}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"noImplicitAny": false,
1717
"strictBindCallApply": false,
1818
"forceConsistentCasingInFileNames": false,
19-
"noFallthroughCasesInSwitch": false
19+
"noFallthroughCasesInSwitch": false,
2020
}
2121
}

0 commit comments

Comments
 (0)