|
10 | 10 | * 1. First of all, install the AdminBro along with the module:
|
11 | 11 | *
|
12 | 12 | * ```
|
13 |
| - * yarn add admin-bro @admin-bro/nestjs @admin-bro/express |
| 13 | + * yarn add admin-bro @admin-bro/nestjs |
14 | 14 | * ```
|
15 | 15 | *
|
16 |
| - * 2. Once the installation process is complete, we can import the AdminBroModule |
| 16 | + * ### Express: |
| 17 | + * You have to additionally add admin-bro express plugin along with packages it's using, express and express formidable: |
| 18 | + * |
| 19 | + * ``` |
| 20 | + * yarn add express @admin-bro/express express-formidable |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * If you are passing `authenticate` object you have to also add express-session: |
| 24 | + * |
| 25 | + * ``` |
| 26 | + * yarn add express-session |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * ### Fastify: |
| 30 | + * Work in progress - currently not available |
| 31 | + * |
| 32 | + * 2. Once the installation process is complete, we can import the AdminModule |
17 | 33 | * into the root AppModule.
|
18 | 34 | *
|
19 | 35 | * ```
|
|
22 | 38 | *
|
23 | 39 | * @Module({
|
24 | 40 | * imports: [
|
25 |
| - * AdminModule.createAdminModule({ |
26 |
| - * useFactory: () => ({ |
27 |
| - * adminBroOptions: { |
28 |
| - * rootPath: '/admin', |
29 |
| - * resources: [], |
30 |
| - * }, |
31 |
| - * }), |
32 |
| - * }), |
| 41 | + * AdminModule.createAdmin({ |
| 42 | + * rootPath: '/admin', |
| 43 | + * resources: [], |
| 44 | + * }), |
33 | 45 | * ],
|
34 | 46 | * })
|
35 | 47 | * export class AppModule {}
|
|
68 | 80 | * entities: [User],
|
69 | 81 | * synchronize: true,
|
70 | 82 | * }),
|
71 |
| - * AdminModule.createAdminModule({ |
72 |
| - * useFactory: () => ({ |
73 |
| - * adminBroOptions: { |
| 83 | + * AdminModule.createAdmin({ |
| 84 | + * adminBroOptions: { |
74 | 85 | * rootPath: '/admin',
|
75 | 86 | * resources: [User],
|
76 |
| - * }, |
77 |
| - * }), |
78 |
| - * }), |
| 87 | + * } |
| 88 | + * }), |
79 | 89 | * ],
|
80 | 90 | * })
|
81 | 91 | * export class AppModule {}
|
82 | 92 | * ```
|
83 | 93 | *
|
84 | 94 | * ## Authentication
|
85 | 95 | *
|
86 |
| - * Apart from the `adminBroOptions` useFactory can return `auth` settings. |
| 96 | + * Apart from the `adminBroOptions` you can define `auth` settings. |
87 | 97 | *
|
88 | 98 | * This is an example which always logs users in, since authenticate method
|
89 | 99 | * always returns a Promise resolving to {@link CurrentAdmin}. You may
|
90 | 100 | * want to compare the password against what what you have encrypted
|
91 | 101 | * in the database.
|
92 | 102 | *
|
93 | 103 | * ```
|
94 |
| - * AdminModule.createAdminModule({ |
95 |
| - * useFactory: () => ({ |
| 104 | + * AdminModule.createAdmin({ |
96 | 105 | * adminBroOptions: {
|
97 | 106 | * rootPath: '/admin',
|
98 | 107 | * resources: [User],
|
|
102 | 111 | * cookieName: 'test',
|
103 | 112 | * cookiePassword: 'testPass',
|
104 | 113 | * },
|
105 |
| - * }), |
| 114 | + * }), |
106 | 115 | * ```
|
| 116 | + * ## Advanced techniques |
| 117 | + * Sometimes some thing couldn't be provided synchronously, that's why there is also asynchronous way of providing options. |
107 | 118 | *
|
| 119 | + * Let's say you use @nestjs/mongoose module, which could define models in modules that fit the best contextually. |
| 120 | + * This creates a problem that we don't have model instance available yet when we are creating AdminModule synchronously. |
| 121 | + * |
| 122 | + * We can take advantage of nestjs dependency injection using `AdminModule.createAdminAsync()`. |
| 123 | + * This method alows us to import modules that have necessary dependencies and then inject them to admin bro config. |
| 124 | + * |
| 125 | + * For example: |
| 126 | + * - we have MongooseSchemasModule which defines Admin model and exports it: |
| 127 | + * ``` |
| 128 | + * @Module({ |
| 129 | + * imports: [ |
| 130 | + * MongooseModule.forFeature([{ name: 'Admin', schema: AdminSchema }]), |
| 131 | + * ], |
| 132 | + * exports: [MongooseModule], |
| 133 | + * }) |
| 134 | + * export class MongooseSchemasModule {} |
| 135 | + * ``` |
| 136 | + * - we want to use Admin model in admin-bro panel, to be displayed as the resource |
| 137 | + * ``` |
| 138 | + * @Module({ |
| 139 | + * imports: [ |
| 140 | + * MongooseModule.forRoot('mongodb://localhost:27017/test'), |
| 141 | + * AdminModule.createAdminAsync({ |
| 142 | + * imports: [ |
| 143 | + * MongooseSchemasModule, // importing module that exported model we want to inject |
| 144 | + * ], |
| 145 | + * inject: [ |
| 146 | + * getModelToken('Admin'), // using mongoose function to inject dependency |
| 147 | + * ], |
| 148 | + * useFactory: (adminModel: Model<Admin>) => ({ // injected dependecy will appear as an argument |
| 149 | + * adminBroOptions: { |
| 150 | + * rootPath: '/admin', |
| 151 | + * resources: [ |
| 152 | + * { resource: adminModel }, |
| 153 | + * ], |
| 154 | + * }, |
| 155 | + * }), |
| 156 | + * }), |
| 157 | + * MongooseSchemasModule, |
| 158 | + * ], |
| 159 | + * }) |
| 160 | + * export class AppModule { } |
| 161 | + * ``` |
| 162 | + * |
| 163 | + * There is a working example [here](https://github.com/SoftwareBrothers/admin-bro-nestjs/tree/master/example-app) |
108 | 164 | */
|
109 |
| - |
110 | 165 | import * as NestJSPlugin from './admin.module'
|
111 | 166 |
|
112 | 167 | export * from './admin.module';
|
|
0 commit comments