-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
2,693 additions
and
6 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,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 { | ||
} |
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 @@ | ||
export class BaseEntity { | ||
|
||
// tslint:disable-next-line:variable-name | ||
_id: any; | ||
|
||
createdAt?: Date; | ||
|
||
updatedAt?: Date; | ||
} |
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,6 @@ | ||
import { Document } from 'mongoose'; | ||
import { Comment } from './comment.entity'; | ||
|
||
export interface CommentDocument extends Comment, Document { | ||
|
||
} |
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,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; | ||
|
||
} |
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,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 }); |
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,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) { | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CommentsController } from './comments.controller'; | ||
import { CommentsService } from './comments.service'; | ||
|
||
@Module({ | ||
controllers: [ | ||
CommentsController | ||
], | ||
providers: [ | ||
CommentsService | ||
] | ||
}) | ||
export class CommentsModule {} |
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,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); | ||
} | ||
} |
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,5 @@ | ||
export * from './comment.document'; | ||
export * from './comment.entity'; | ||
export * from './comment.schema'; | ||
export * from './comments.module'; | ||
export * from './comments.service'; |
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 @@ | ||
export const USER_REQUEST_KEY = 'user'; |
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,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(); |
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,5 @@ | ||
import { MongooseModuleOptions } from '@nestjs/mongoose/dist/interfaces/mongoose-options.interface'; | ||
|
||
export const MONGO_URI = 'mongodb://localhost:27017/test'; | ||
|
||
export const MONGO_CONFIG: MongooseModuleOptions = {}; |
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,4 @@ | ||
export * from './post.document'; | ||
export * from './post.entity'; | ||
export * from './post.schema'; | ||
export * from './posts.service'; |
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,6 @@ | ||
import { Document } from 'mongoose'; | ||
import { Post } from './post.entity'; | ||
|
||
export interface PostDocument extends Post, Document { | ||
|
||
} |
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,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; | ||
} |
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,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, | ||
}); |
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,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) { | ||
} | ||
} |
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 { Module } from '@nestjs/common'; | ||
import { PostsController } from './posts.controller'; | ||
import { PostsService } from './posts.service'; | ||
|
||
@Module({ | ||
controllers: [PostsController], | ||
providers: [PostsService], | ||
}) | ||
export class PostsModule { | ||
} |
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 @@ | ||
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); | ||
} | ||
|
||
} |
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,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', | ||
}, | ||
]; |
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 @@ | ||
export * from './user.entity'; |
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,6 @@ | ||
import { Document } from 'mongoose'; | ||
import { User } from './user.entity'; | ||
|
||
export interface UserDocument extends User, Document { | ||
|
||
} |
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,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; | ||
} |
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,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, | ||
}); |
Oops, something went wrong.