Skip to content

Commit 7204334

Browse files
committed
feat(generators/with-mongo): refactor type def in types, controller & repository
1 parent bad9a93 commit 7204334

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

generators/with-mongo/templates/<%= compNameParamCasePlural %>/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ENTITY } from './constants';
77
import repository from './repository';
88

99
interface MyRequest extends Request {
10-
readonly [ENTITY]: Required<<%= compNamePascalCase %>>;
10+
readonly [ENTITY]: <%= compNamePascalCase %>;
1111
}
1212

1313
const getById = async (

generators/with-mongo/templates/<%= compNameParamCasePlural %>/repository.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const get = async (id: string): Promise<<%= compNamePascalCase %>> => {
2828
return transform(document);
2929
};
3030

31-
const updatePartial = async (id: string, object: <%= compNamePascalCase %>): Promise<<%= compNamePascalCase %>> => {
31+
const updatePartial = async (id: string, object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
3232
// get document
3333
const document = await Model.findById(id).exec();
3434
if (document === null) {
@@ -42,7 +42,7 @@ const updatePartial = async (id: string, object: <%= compNamePascalCase %>): Pro
4242
return transform(document);
4343
};
4444

45-
const update = async (id: string, object: <%= compNamePascalCase %>): Promise<<%= compNamePascalCase %>> => {
45+
const update = async (id: string, object: Omit<<%= compNamePascalCase %>, 'id'>): Promise<<%= compNamePascalCase %>> => {
4646
// get document
4747
const document = await Model.findById(id).exec();
4848
if (document === null) {
@@ -64,19 +64,18 @@ const del = async (id: string): Promise<<%= compNamePascalCase %>> => {
6464
}
6565

6666
// delete document
67-
await Model.deleteOne({ _id: id }).exec();
67+
await document.remove();
6868

6969
return transform(document);
7070
};
7171

7272
// transform document to json object
7373
const transform = (document: Document): <%= compNamePascalCase %> => {
74-
const { _id, ...restObject } = document.toObject({
75-
virtuals: true,
74+
const { _id: id, ...restObject } = document.toObject({
7675
versionKey: false,
7776
});
7877

79-
return restObject;
78+
return { ...restObject, id };
8079
};
8180

8281
export default { list, create, get, updatePartial, update, del };

generators/with-mongo/templates/<%= compNameParamCasePlural %>/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
interface <%= compNamePascalCase %> {
2-
readonly id?: string;
3-
readonly name?: string;
2+
readonly id: string;
3+
readonly name: string;
44
// TODO: add more props here
55
}
66

0 commit comments

Comments
 (0)