inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

탄탄한 백엔드 NestJS, 기초부터 심화까지

NestJS 관리자 페이지 개발 (Mongoose)

useFactory 사용시 resource 파일 분리 후 di 방법

389

지태관

작성한 질문수 3

1

먼저 너무 좋은 ADMIN 대시보드 라이브러리를 알려주셔서 감사합니다.

해당 라이브러리로 몇가지 테스트해보는 중인데 adminJsOptions가 너무 길어져 resources를 별도의 파일로 분리하려 하고 있습니다.

하다보니 이미 factory로 blogModel을 inject하고 있는데 resources를 별도의 파일로 분리하려고 보니 blogModel을 분리한 파일에서 어떻게 가져와야할지 막막하더라구요.

adminjs뿐아니라 다른 모듈을 사용할때도 useFactory를 통해 di를 해주는경우가 많은데 이럴때는 분리된 파일에는 어떻게 의존성을 주입해줘야 할까요???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as AdminJSMongoose from '@adminjs/mongoose';
import { AdminModule as AdminBroModule } from '@adminjs/nestjs';
import { Module } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose';
import AdminJS from 'adminjs';
import { Model } from 'mongoose';
import { UsersModule } from 'src/users/users.module';
import { User } from 'src/users/users.schema';
import { after, before } from './hooks/users.hooks';
 
AdminJS.registerAdapter(AdminJSMongoose);
 
@Module({
    imports: [
        AdminBroModule.createAdminAsync({
            imports: [UsersModule],
            inject: [getModelToken(User.name)],
            useFactory: (usersModel: Model<User>=> ({
                adminJsOptions: {
                    rootPath: '/admin',
                    resources: [
                        {
                            resource: usersModel,
                            options: {
                                navigation: {
                                    namenull,
                                    icon: '',
                                },
                                properties: {
                                    email: {
                                        isTitle: false,
                                        position: 1,
                                    },
                                    name: {
                                        isTitle: true,
                                    },
                                    _id: {
                                        isVisible: { list: false },
                                    },
                                    password: {
                                        isVisible: false,
                                    },
                                },
                                actions: {
                                    edit: { isAccessible: isAdmin },
                                    delete: { isAccessible: isAdmin },
                                    new: { isAccessible: isAdmin },
                                    show: {
                                        isAccessible: isAdmin,
                                    },
                                    list: { isAccessible: isAdmin },
                                    custom_delete: {
                                        actionType: 'record',
                                        icon: 'TrashCan',
                                        guard: 'doYouReallyWantToDoThis',
                                        variant: 'danger',
                                        before: before,
                                        after: after,
                                        handler: async (request, response, context) => {
                                            const user = context.record;
                                            await usersModel.findOne({_id: request.params.id});
                                            console.log('custom handler!!!');
                                            return {
                                                record: user.toJSON(context.currentAdmin),
                                            };
                                        }
                                    },
                                },
                            },
                        },
                    ],
                },
                auth: {
                    authenticate: async (email, password) => {
                        const user = await usersModel.findOne({ email: email });
                        if (user) {
                            const matched = user.password === password;
                            if (matched && user.role === 'ADMIN') {
                                return user.readOnlyData;
                            }
                        }
                        return null;
                    },
                    cookieName: 'cookie-test',
                    cookiePassword: 'test',
                },
            }),
        }),
    ],
})
export class AdminModule {}
 
const isAdmin = ({ currentAdmin }) => currentAdmin && currentAdmin.role === 'ADMIN';
 

resource 코드 분리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as AdminJSMongoose from '@adminjs/mongoose';
import { AdminModule as AdminBroModule } from '@adminjs/nestjs';
import { Module } from '@nestjs/common';
import { getModelToken } from '@nestjs/mongoose';
import AdminJS from 'adminjs';
import { Model } from 'mongoose';
import { UsersModule } from 'src/users/users.module';
import { User } from 'src/users/users.schema';
import { after, before } from './hooks/users.hooks';
import { userResource } from './resources/users.resource';
 
AdminJS.registerAdapter(AdminJSMongoose);
 
@Module({
    imports: [
        AdminBroModule.createAdminAsync({
            imports: [UsersModule],
            inject: [getModelToken(User.name)],
            useFactory: (usersModel: Model<User>=> ({
                adminJsOptions: {
                    rootPath: '/admin',
                    resources: [
                        userResource
                    ],
                },
                auth: {
                    authenticate: async (email, password) => {
                        const user = await usersModel.findOne({ email: email });
                        if (user) {
                            const matched = user.password === password;
                            if (matched && user.role === 'ADMIN') {
                                return user.readOnlyData;
                            }
                        }
                        return null;
                    },
                    cookieName: 'cookie-test',
                    cookiePassword: 'test',
                },
            }),
        }),
    ],
})
export class AdminModule {}
 
const isAdmin = ({ currentAdmin }) => currentAdmin && currentAdmin.role === 'ADMIN';
 

함수로 만들어 파라미터로 넘겨주는 방법밖에 없을까요???

 

 

 

NestJS ssr mongodb express nodejs

답변 1

1

윤상석

안녕하세요 :)

더 좋은 방법이 있을지 모르겠지만 저도 함수로 따로 분리해서 작업했었습니다!

 

0

지태관

넵 감사합니다!!!:)

프로젝트 환경 세팅할 때 최신 노드 버젼을 사용하시는 분들은 참고하셔도 좋을 것 같아요~

2

101

1

DTO에 대한 질문

1

97

2

백엔드 MVC에서 View의 역할은 무엇인가요?

1

111

2

추가 업데이트 관련 건

0

105

2

nest js 버전문제

0

95

2

mongdb 스키마 공식 문서와 형태가 다른 이유 궁금합니다.

0

111

1

라인 끝에 에러 표시(eslint) 때문에 구글 찾아 보니.

0

83

1

전체 고양이 조회 라우터 중 error.message 오류

0

79

1

캡슐화 추가 설명 중 단일책임원칙 관련 질문

0

115

0

42강 고양이끼리 소통 댓글 구현 중 Schema hasn't been registered for model 'comments' 에러 해결

0

86

1

채팅 이슈

0

137

1

모듈이 더 이상 지원하지 않는답니다

0

215

1

오류가 있습니다

0

114

1

import 에서 오류가 납니다

0

131

1

이런 오류가 나옵니다

0

106

1

에러가 발생합니다

0

117

1

프론트 에러 뜨는데 수정 안해주시나요

0

166

1

emit() broadcast.emit() 질문있습니다

0

107

1

서버연결이 안됩니다.

1

408

1

[PM2][ERROR] Command not found

0

528

1

S3에 업로드까지는 성공했는데 사진이 나오지 않습니다.

0

254

1

error_code : Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339)

0

607

1

jwt를 따로 연습하고 있는데 env를 못읽는 것 같습니다.

0

330

2

Ec2로 안하시는 이유가 있을까요?

0

345

1