-
-
Notifications
You must be signed in to change notification settings - Fork 573
[WIP] mongoose implementation #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0fd52ae
feat(Mongoose): basic mongoose implementation for CRUD
yharaskrik 9ec6428
feat(Mongoose): allow joins via populate
yharaskrik 4e0eddc
feat(Mongoose): allow nested joins via virtuals
yharaskrik ec1e583
fix(Mongoose): will throw bad request if join is invalid
yharaskrik 8a9527b
refactor(mongoose): publish nest-crud-mongoose as separate package fo…
yharaskrik d920292
fix(getmany): use lean to coerce mongoose documents to JSON
yharaskrik 67b06cd
fix(mongoose): remove class validation from interceptor for mongoose
yharaskrik 4d7e5ee
fix(mongoose): configure mongoose to serialize ObjectID and return pl…
yharaskrik d9dd947
chore: release 0.0.5
yharaskrik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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') | ||
yharaskrik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export class PostsController implements CrudController<Post> { | ||
|
||
constructor(public service: PostsService) { | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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' }, | ||
{ name: 'john', _id: '5de34417cd5e475f96a46584', password: '1' }, | ||
{ name: 'jim', _id: '5de34417cd5e475f96a46585', password: '1' }, | ||
{ name: 'jay1', _id: '5de34417cd5e475f96a46586', password: '1' }, | ||
{ name: 'john1', _id: '5de34417cd5e475f96a46587', password: '1' }, | ||
{ name: 'jim1', _id: '5de34417cd5e475f96a46588', password: '1' }, | ||
{ name: 'jay2', _id: '5de34417cd5e475f96a46589', password: '1' }, | ||
{ name: 'john2', _id: '5de34417cd5e475f96a46590', password: '1' }, | ||
{ name: 'jim2', _id: '5de34417cd5e475f96a46591', password: '1' } | ||
]; | ||
|
||
export const seedPosts = [ | ||
{ | ||
id: '1', | ||
title: 'Title 1', | ||
userId: '5de34417cd5e475f96a46583', | ||
_id: '5de34417cd5e475f96a46583' | ||
}, | ||
]; | ||
|
||
export const seedComments = [ | ||
{ | ||
_id: '5de34417cd5e475f96a46584', | ||
postId: '5de34417cd5e475f96a46583', | ||
userId: '5de34417cd5e475f96a46583', | ||
}, | ||
]; |
This file contains hidden or 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 hidden or 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 hidden or 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 { CrudValidationGroups } from '@nestjsx/crud'; | ||
import { IsOptional, IsString } from 'class-validator'; | ||
import { BaseEntity } from '../base-entity'; | ||
|
||
const { CREATE, UPDATE } = CrudValidationGroups; | ||
|
||
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 | ||
} |
This file contains hidden or 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,27 @@ | ||
import { Schema } from 'mongoose'; | ||
import { User } from './user.entity'; | ||
|
||
export const userSchema: Schema = new Schema<User>({ | ||
name: String, | ||
email: String, | ||
password: String | ||
}, { | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.