Skip to content

Commit e99d978

Browse files
committed
feat: added async and sync, docs
- Changed name from createAdminModule to createAdminAsync - Fixed type for authentication in AdminModuleOptions - Improvements for jsdoc - Adjusted example for the change - Removed packages for express, as developer should add them on his own
1 parent 5044e98 commit e99d978

File tree

6 files changed

+102
-111
lines changed

6 files changed

+102
-111
lines changed

example-app/src/app.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Module } from '@nestjs/common';
22
import { MongooseModule, getModelToken } from '@nestjs/mongoose';
3-
import { AdminModule } from '@admin-bro/nestjs';
43
import { Model } from 'mongoose';
54

5+
import { AdminModule } from '../../src'; // lib
6+
67
import { AppController } from './app.controller';
78
import { AppService } from './app.service';
89
import { Admin } from './mongoose/admin-model';
@@ -11,7 +12,7 @@ import { MongooseSchemasModule } from './mongoose/mongoose.module';
1112
@Module({
1213
imports: [
1314
MongooseModule.forRoot('mongodb://localhost:27017/nest'),
14-
AdminModule.createAdminModule({
15+
AdminModule.createAdminAsync({
1516
imports: [
1617
MongooseSchemasModule,
1718
],

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
},
1919
"peerDependencies": {
20-
"admin-bro": ">=3.0.0-beta.7"
20+
"admin-bro": ">=3.0.0"
2121
},
2222
"devDependencies": {
2323
"@commitlint/cli": "^8.3.5",
@@ -41,12 +41,9 @@
4141
"typescript": "^3.9.7"
4242
},
4343
"dependencies": {
44-
"@admin-bro/express": "^3.0.0-beta.3",
4544
"@nestjs/common": "^7.4.2",
4645
"@nestjs/core": "^7.4.2",
4746
"@types/express-session": "^1.17.0",
48-
"express-formidable": "^1.2.0",
49-
"express-session": "^1.17.1",
5047
"reflect-metadata": "^0.1.13",
5148
"rxjs": "^6.5.3"
5249
}

src/admin.module.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ export class AdminModule implements OnModuleInit {
1818
@Inject(CONFIG_TOKEN)
1919
private readonly adminModuleOptions: AdminModuleOptions,
2020
) {}
21+
22+
public static createAdmin(options: AdminModuleOptions): DynamicModule {
23+
return {
24+
module: AdminModule,
25+
providers: [
26+
{
27+
provide: CONFIG_TOKEN,
28+
useValue: options,
29+
},
30+
],
31+
}
32+
}
2133

22-
public static createAdminModule(options: AdminModuleFactory): DynamicModule {
34+
public static createAdminAsync(options: AdminModuleFactory): DynamicModule {
2335
return {
2436
imports: options.imports,
2537
module: AdminModule,
@@ -41,6 +53,5 @@ export class AdminModule implements OnModuleInit {
4153
...this.adminModuleOptions,
4254
adminBroOptions: admin.options,
4355
});
44-
admin.watch()
4556
}
4657
}

src/index.ts

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,26 @@
1010
* 1. First of all, install the AdminBro along with the module:
1111
*
1212
* ```
13-
* yarn add admin-bro @admin-bro/nestjs @admin-bro/express
13+
* yarn add admin-bro @admin-bro/nestjs
1414
* ```
1515
*
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
1733
* into the root AppModule.
1834
*
1935
* ```
@@ -22,14 +38,10 @@
2238
*
2339
* @Module({
2440
* 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+
* }),
3345
* ],
3446
* })
3547
* export class AppModule {}
@@ -68,31 +80,28 @@
6880
* entities: [User],
6981
* synchronize: true,
7082
* }),
71-
* AdminModule.createAdminModule({
72-
* useFactory: () => ({
73-
* adminBroOptions: {
83+
* AdminModule.createAdmin({
84+
* adminBroOptions: {
7485
* rootPath: '/admin',
7586
* resources: [User],
76-
* },
77-
* }),
78-
* }),
87+
* }
88+
* }),
7989
* ],
8090
* })
8191
* export class AppModule {}
8292
* ```
8393
*
8494
* ## Authentication
8595
*
86-
* Apart from the `adminBroOptions` useFactory can return `auth` settings.
96+
* Apart from the `adminBroOptions` you can define `auth` settings.
8797
*
8898
* This is an example which always logs users in, since authenticate method
8999
* always returns a Promise resolving to {@link CurrentAdmin}. You may
90100
* want to compare the password against what what you have encrypted
91101
* in the database.
92102
*
93103
* ```
94-
* AdminModule.createAdminModule({
95-
* useFactory: () => ({
104+
* AdminModule.createAdmin({
96105
* adminBroOptions: {
97106
* rootPath: '/admin',
98107
* resources: [User],
@@ -102,11 +111,57 @@
102111
* cookieName: 'test',
103112
* cookiePassword: 'testPass',
104113
* },
105-
* }),
114+
* }),
106115
* ```
116+
* ## Advanced techniques
117+
* Sometimes some thing couldn't be provided synchronously, that's why there is also asynchronous way of providing options.
107118
*
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)
108164
*/
109-
110165
import * as NestJSPlugin from './admin.module'
111166

112167
export * from './admin.module';

src/interfaces/admin-module-options.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { AdminBroOptions } from 'admin-bro';
1+
import { AdminBroOptions, CurrentAdmin } from 'admin-bro';
22
import { SessionOptions } from 'express-session';
33

44
import { ExpressFormidableOptions } from './express-formidable-options.interface';
55

66
export interface AdminModuleOptions {
77
adminBroOptions: AdminBroOptions,
88
auth?: {
9-
authenticate: Function,
9+
authenticate: (email: string, password: string) => Promise<CurrentAdmin>,
1010
cookiePassword: string,
1111
cookieName: string,
1212
}

0 commit comments

Comments
 (0)