Skip to content

Commit

Permalink
Merge a2ba203 into 115ee2b
Browse files Browse the repository at this point in the history
  • Loading branch information
yharaskrik authored May 17, 2020
2 parents 115ee2b + a2ba203 commit f07267f
Show file tree
Hide file tree
Showing 45 changed files with 2,693 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ services:
command: redis-server
networks:
- nestjsx_crud

mongo:
image: mongo
ports:
- 27017:27017
networks:
- nestjsx_crud
22 changes: 22 additions & 0 deletions integration/crud-mongoose/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { MONGO_CONFIG, MONGO_URI } from './mongoose.config';
import { postSchema } from './posts';
import { PostsModule } from './posts/posts.module';
import { userSchema } from './users/user.schema';
import { UsersModule } from './users/users.module';

@Module({
imports: [
MongooseModule.forRoot(MONGO_URI, MONGO_CONFIG),
MongooseModule.forFeature([
{ name: 'User', schema: userSchema },
{ name: 'Post', schema: postSchema },
]),
UsersModule,
PostsModule
],
providers: [],
})
export class AppModule {
}
9 changes: 9 additions & 0 deletions integration/crud-mongoose/base-entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class BaseEntity {

// tslint:disable-next-line:variable-name
_id: any;

createdAt?: Date;

updatedAt?: Date;
}
6 changes: 6 additions & 0 deletions integration/crud-mongoose/comments/comment.document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Document } from 'mongoose';
import { Comment } from './comment.entity';

export interface CommentDocument extends Comment, Document {

}
19 changes: 19 additions & 0 deletions integration/crud-mongoose/comments/comment.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CrudValidationGroups } from '@nestjsx/crud';
import { IsOptional, IsString } from 'class-validator';
import { BaseEntity } from '../base-entity';

const { CREATE, UPDATE } = CrudValidationGroups;

export class Comment extends BaseEntity {

@IsString({ groups: [CREATE, UPDATE] })
@IsOptional({ groups: [CREATE, UPDATE] })
title?: string;

@IsString({ groups: [CREATE] })
userId?: string;

@IsString({ groups: [CREATE] })
postId?: string;

}
8 changes: 8 additions & 0 deletions integration/crud-mongoose/comments/comment.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Schema } from 'mongoose';
import { CommentDocument } from './comment.document';

export const commentSchema: Schema = new Schema<CommentDocument>({
title: String,
userId: String,
postId: String,
}, { timestamps: true });
25 changes: 25 additions & 0 deletions integration/crud-mongoose/comments/comments.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller } from '@nestjs/common';
import { Crud, CrudController } from '@nestjsx/crud';
import { Comment } from './comment.entity';
import { CommentsService } from './comments.service';

@Crud({
model: {
type: Comment,
},
serialize: {
get: false,
getMany: false,
createMany: false,
create: false,
update: false,
replace: false,
delete: false
}
})
@Controller('comments')
export class CommentsController implements CrudController<Comment> {

constructor(public service: CommentsService) {
}
}
13 changes: 13 additions & 0 deletions integration/crud-mongoose/comments/comments.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { CommentsController } from './comments.controller';
import { CommentsService } from './comments.service';

@Module({
controllers: [
CommentsController
],
providers: [
CommentsService
]
})
export class CommentsModule {}
12 changes: 12 additions & 0 deletions integration/crud-mongoose/comments/comments.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { MongooseCrudService } from 'nest-crud-mongoose';
import { Model } from 'mongoose';
import { CommentDocument } from './comment.document';

@Injectable()
export class CommentsService extends MongooseCrudService<CommentDocument> {
constructor(@InjectModel('Comment') model: Model<CommentDocument>) {
super(model);
}
}
5 changes: 5 additions & 0 deletions integration/crud-mongoose/comments/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './comment.document';
export * from './comment.entity';
export * from './comment.schema';
export * from './comments.module';
export * from './comments.service';
1 change: 1 addition & 0 deletions integration/crud-mongoose/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const USER_REQUEST_KEY = 'user';
37 changes: 37 additions & 0 deletions integration/crud-mongoose/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { CrudConfigService } from '@nestjsx/crud';
import { HttpExceptionFilter } from '../shared/https-exception.filter';
import { AppModule } from './app.module';
import { USER_REQUEST_KEY } from './constants';

// Important: load config before (!!!) you import AppModule
// https://github.com/nestjsx/crud/wiki/Controllers#global-options
CrudConfigService.load({
auth: {
property: USER_REQUEST_KEY,
},
routes: {
exclude: ['createManyBase'],
},
});

async function bootstrap() {
const app = await NestFactory.create(AppModule);

app.useGlobalPipes(new ValidationPipe());
app.useGlobalFilters(new HttpExceptionFilter());

const options = new DocumentBuilder()
.setTitle('@nestjsx/crud-typeorm')
.setDescription('@nestjsx/crud-typeorm')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('docs', app, document);

await app.listen(process.env.PORT || 3000);
}

bootstrap();
5 changes: 5 additions & 0 deletions integration/crud-mongoose/mongoose.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MongooseModuleOptions } from '@nestjs/mongoose/dist/interfaces/mongoose-options.interface';

export const MONGO_URI = 'mongodb://localhost:27017/test';

export const MONGO_CONFIG: MongooseModuleOptions = {};
4 changes: 4 additions & 0 deletions integration/crud-mongoose/posts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './post.document';
export * from './post.entity';
export * from './post.schema';
export * from './posts.service';
6 changes: 6 additions & 0 deletions integration/crud-mongoose/posts/post.document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Document } from 'mongoose';
import { Post } from './post.entity';

export interface PostDocument extends Post, Document {

}
21 changes: 21 additions & 0 deletions integration/crud-mongoose/posts/post.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CrudValidationGroups } from '@nestjsx/crud';
import { IsOptional, IsString } from 'class-validator';
import { BaseEntity } from '../base-entity';

const { CREATE, UPDATE } = CrudValidationGroups;

export class Post extends BaseEntity {

@IsString({ groups: [CREATE] })
@IsOptional({ groups: [CREATE] })
id?: string;

@IsString({ groups: [CREATE, UPDATE] })
@IsOptional({ groups: [CREATE, UPDATE] })
title?: string;


@IsString({ groups: [CREATE] })
@IsOptional({ groups: [CREATE] })
userId?: string;
}
20 changes: 20 additions & 0 deletions integration/crud-mongoose/posts/post.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Schema } from 'mongoose';
import { Post } from './post.entity';

export const postSchema: Schema = new Schema<Post>({
id: String,
title: String,
userId: String,
}, {
timestamps: true,
toJSON: {
virtuals: true,
},
});

postSchema.virtual('comments', {
ref: 'Comment',
localField: '_id',
foreignField: 'postId',
justOne: false,
});
25 changes: 25 additions & 0 deletions integration/crud-mongoose/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller } from '@nestjs/common';
import { Crud, CrudController } from '@nestjsx/crud';
import { Post } from './post.entity';
import { PostsService } from './posts.service';

@Crud({
model: {
type: Post,
},
serialize: {
get: false,
getMany: false,
createMany: false,
create: false,
update: false,
replace: false,
delete: false
}
})
@Controller('posts')
export class PostsController implements CrudController<Post> {

constructor(public service: PostsService) {
}
}
10 changes: 10 additions & 0 deletions integration/crud-mongoose/posts/posts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { PostsController } from './posts.controller';
import { PostsService } from './posts.service';

@Module({
controllers: [PostsController],
providers: [PostsService],
})
export class PostsModule {
}
14 changes: 14 additions & 0 deletions integration/crud-mongoose/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { MongooseCrudService } from 'nest-crud-mongoose';
import { Model } from 'mongoose';
import { PostDocument } from './post.document';

@Injectable()
export class PostsService extends MongooseCrudService<PostDocument> {

constructor(@InjectModel('Post') model: Model<PostDocument>) {
super(model);
}

}
28 changes: 28 additions & 0 deletions integration/crud-mongoose/seeds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const seedUsers = [
{ name: 'jay', _id: '5de34417cd5e475f96a46583', password: '1', age: 40, title: 'ceo' },
{ name: 'john', _id: '5de34417cd5e475f96a46584', password: '1', age: 20, title: 'coo' },
{ name: 'jim', _id: '5de34417cd5e475f96a46585', password: '1', age: 50, title: 'ceo' },
{ name: 'jay1', _id: '5de34417cd5e475f96a46586', password: '1', age: 24, title: 'cfo' },
{ name: 'john1', _id: '5de34417cd5e475f96a46587', password: '1', age: 65, title: 'cto' },
{ name: 'jim1', _id: '5de34417cd5e475f96a46588', password: '1', age: 34, title: 'ceo' },
{ name: 'jay2', _id: '5de34417cd5e475f96a46589', password: '1', age: 32, title: 'cfo' },
{ name: 'john2', _id: '5de34417cd5e475f96a46590', password: '1', age: 76, title: 'coo' },
{ name: 'jim2', _id: '5de34417cd5e475f96a46591', password: '1', age: 50, title: 'ceo' }
];

export const seedPosts = [
{
id: '1',
title: 'Title 1',
userId: '5de34417cd5e475f96a46583',
_id: '5de34417cd5e475f96a46583'
},
];

export const seedComments = [
{
_id: '5de34417cd5e475f96a46584',
postId: '5de34417cd5e475f96a46583',
userId: '5de34417cd5e475f96a46583',
},
];
1 change: 1 addition & 0 deletions integration/crud-mongoose/users/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user.entity';
6 changes: 6 additions & 0 deletions integration/crud-mongoose/users/user.document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Document } from 'mongoose';
import { User } from './user.entity';

export interface UserDocument extends User, Document {

}
35 changes: 35 additions & 0 deletions integration/crud-mongoose/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CrudValidationGroups } from '@nestjsx/crud';
import { IsOptional, IsString } from 'class-validator';
import { BaseEntity } from '../base-entity';

const { CREATE, UPDATE } = CrudValidationGroups;

export enum ETitle {
CEO = 'ceo',
CTO = 'cto',
COO = 'coo',
CFO = 'cfo'
}

export class User extends BaseEntity {

@IsString({ groups: [CREATE, UPDATE] })
@IsOptional({ groups: [CREATE, UPDATE] })
name?: string;

@IsString({ groups: [CREATE, UPDATE] })
@IsOptional({ groups: [CREATE, UPDATE] })
email?: string;

@IsString()
@IsOptional()
password?: string;

@IsString()
@IsOptional()
title?: ETitle;

@IsString()
@IsOptional()
age?: number;
}
29 changes: 29 additions & 0 deletions integration/crud-mongoose/users/user.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Schema } from 'mongoose';
import { User } from './user.entity';

export const userSchema: Schema = new Schema<User>({
name: String,
email: String,
password: String,
title: String,
age: Number
}, {
timestamps: true,
toJSON: {
virtuals: true,
},
});

userSchema.virtual('posts', {
ref: 'Post',
localField: '_id',
foreignField: 'userId',
justOne: false,
});

userSchema.virtual('comments', {
ref: 'Comment',
localField: '_id',
foreignField: 'userId',
justOne: false,
});
Loading

0 comments on commit f07267f

Please sign in to comment.