Skip to content

Commit 0d6e9ff

Browse files
committed
feat: initialize admin bro the nest way
1 parent 3a3b942 commit 0d6e9ff

File tree

11 files changed

+143
-52
lines changed

11 files changed

+143
-52
lines changed

example-app/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"test:e2e": "jest --config ./test/jest-e2e.json"
1919
},
2020
"dependencies": {
21+
"admin-bro": "^3.0.0-beta.12",
22+
"@admin-bro/mongoose": "^1.0.0-beta.3",
2123
"@nestjs/common": "^6.7.2",
2224
"@nestjs/core": "^6.7.2",
2325
"@nestjs/mongoose": "^7.0.2",
@@ -33,6 +35,7 @@
3335
"@nestjs/testing": "^6.7.1",
3436
"@types/express": "^4.17.1",
3537
"@types/jest": "^24.0.18",
38+
"@types/mongoose": "^5.7.36",
3639
"@types/node": "^12.7.5",
3740
"@types/supertest": "^2.0.8",
3841
"cypress": "^4.11.0",

example-app/src/app.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Controller, Get } from '@nestjs/common';
2+
import mongoose from 'mongoose';
3+
24
import { AppService } from './app.service';
35

46
@Controller()

example-app/src/app.module.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
import { Module } from '@nestjs/common';
2+
import { MongooseModule, getModelToken } from '@nestjs/mongoose';
3+
import mongoose, { Model } from 'mongoose';
24

35
import { AdminModule } from '../../src/index';
46

57
import { AppController } from './app.controller';
68
import { AppService } from './app.service';
9+
import { Admin } from './mongoose/admin-model';
10+
import { MongooseSchemasModule } from './mongoose/mongoose.module';
711

812
@Module({
9-
imports: [AdminModule.createAdminModule({
10-
adminBroOptions: {
11-
rootPath: '/admin',
12-
},
13-
// auth: {
14-
// authenticate: async () => new Promise(() => ({ email: 'mordeczka' })),
15-
// cookieName: 'test',
16-
// cookiePassword: 'testPass',
17-
// },
18-
})],
13+
imports: [
14+
MongooseModule.forRoot('mongodb://localhost:27017/nest'),
15+
AdminModule.createAdminModule({
16+
imports: [
17+
MongooseSchemasModule,
18+
],
19+
inject: [
20+
getModelToken('Admin'),
21+
],
22+
useFactory: (adminModel: Model<Admin>) => ({
23+
adminBroOptions: {
24+
rootPath: '/admin',
25+
resources: [
26+
{ resource: adminModel },
27+
],
28+
},
29+
auth: {
30+
authenticate: async () => new Promise(() => ({ email: 'mordeczka' })),
31+
cookieName: 'test',
32+
cookiePassword: 'testPass',
33+
},
34+
}),
35+
}),
36+
MongooseSchemasModule,
37+
],
1938
controllers: [AppController],
2039
providers: [AppService],
2140
})
22-
export class AppModule {}
41+
export class AppModule { }

example-app/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import AdminBro from 'admin-bro';
2+
import AdminBroMongoose from '@admin-bro/mongoose';
13
import { NestFactory } from '@nestjs/core';
4+
25
import { AppModule } from './app.module';
36

7+
AdminBro.registerAdapter(AdminBroMongoose);
8+
49
async function bootstrap() {
510
const app = await NestFactory.create(AppModule);
611
await app.listen(3000);

example-app/src/mongoose/admin-model.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mongoose from 'mongoose';
2+
3+
const { Schema } = mongoose
4+
5+
export interface Admin extends mongoose.Document {
6+
email: string,
7+
password: string,
8+
}
9+
10+
export const AdminSchema = new Schema({
11+
email: {
12+
type: String,
13+
required: true,
14+
},
15+
password: {
16+
type: String,
17+
required: true,
18+
},
19+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Module } from '@nestjs/common';
2+
import { MongooseModule } from '@nestjs/mongoose';
3+
4+
import { AdminSchema } from './admin-model';
5+
6+
@Module({
7+
imports: [
8+
MongooseModule.forFeature([{ name: 'Admin', schema: AdminSchema }]),
9+
],
10+
exports: [MongooseModule],
11+
})
12+
export class MongooseSchemasModule {}

example-app/yarn.lock

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
# yarn lockfile v1
33

44

5+
"@admin-bro/mongoose@^1.0.0-beta.3":
6+
version "1.0.0-beta.3"
7+
resolved "https://registry.yarnpkg.com/@admin-bro/mongoose/-/mongoose-1.0.0-beta.3.tgz#2c86b953b186d6490f6d40b7b18775834630c684"
8+
integrity sha512-bPdJRIHszwBNmBu5cH6Dl+tN9Fq8dPAbKmw1chbf49csdhGem2lIxLFqlbwA+kF1r98o9Hk4kYHekaOJBnUdrw==
9+
dependencies:
10+
escape-regexp "0.0.1"
11+
flat "^4.1.0"
12+
lodash "^4.17.11"
13+
514
"@angular-devkit/core@7.3.8":
615
version "7.3.8"
716
resolved "https://registry.yarnpkg.com/@angular-devkit/core/-/core-7.3.8.tgz#702b0944a69c71cce3a1492e0d62de18df22a993"
@@ -573,6 +582,13 @@
573582
"@types/connect" "*"
574583
"@types/node" "*"
575584

585+
"@types/bson@*":
586+
version "4.0.2"
587+
resolved "https://registry.yarnpkg.com/@types/bson/-/bson-4.0.2.tgz#7accb85942fc39bbdb7515d4de437c04f698115f"
588+
integrity sha512-+uWmsejEHfmSjyyM/LkrP0orfE2m5Mx9Xel4tXNeqi1ldK5XMQcDsFkBmLDtuyKUbxj2jGDo0H240fbCRJZo7Q==
589+
dependencies:
590+
"@types/node" "*"
591+
576592
"@types/color-name@^1.1.1":
577593
version "1.1.1"
578594
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
@@ -651,6 +667,22 @@
651667
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.3.tgz#c893b73721db73699943bfc3653b1deb7faa4a3a"
652668
integrity sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q==
653669

670+
"@types/mongodb@*":
671+
version "3.5.25"
672+
resolved "https://registry.yarnpkg.com/@types/mongodb/-/mongodb-3.5.25.tgz#ab187db04d79f8e3f15af236327dc9139d9d4736"
673+
integrity sha512-2H/Owt+pHCl9YmBOYnXc3VdnxejJEjVdH+QCWL5ZAfPehEn3evygKBX3/vKRv7aTwfNbUd0E5vjJdQklH/9a6w==
674+
dependencies:
675+
"@types/bson" "*"
676+
"@types/node" "*"
677+
678+
"@types/mongoose@^5.7.36":
679+
version "5.7.36"
680+
resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.7.36.tgz#2dae28c63041c6afba8a83ea02969f463b3f1021"
681+
integrity sha512-ggFXgvkHgCNlT35B9d/heDYfSqOSwTmQjkRoR32sObGV5Xjd0N0WWuYlLzqeCg94j4hYN/OZxZ1VNNLltX/IVQ==
682+
dependencies:
683+
"@types/mongodb" "*"
684+
"@types/node" "*"
685+
654686
"@types/node@*":
655687
version "14.0.27"
656688
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.27.tgz#a151873af5a5e851b51b3b065c9e63390a9e0eb1"
@@ -2535,6 +2567,11 @@ escape-html@~1.0.3:
25352567
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
25362568
integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=
25372569

2570+
escape-regexp@0.0.1:
2571+
version "0.0.1"
2572+
resolved "https://registry.yarnpkg.com/escape-regexp/-/escape-regexp-0.0.1.tgz#f44bda12d45bbdf9cb7f862ee7e4827b3dd32254"
2573+
integrity sha1-9EvaEtRbvfnLf4Yu5+SCez3TIlQ=
2574+
25382575
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
25392576
version "1.0.5"
25402577
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
@@ -3080,6 +3117,13 @@ flat-cache@^2.0.1:
30803117
rimraf "2.6.3"
30813118
write "1.0.3"
30823119

3120+
flat@^4.1.0:
3121+
version "4.1.0"
3122+
resolved "https://registry.yarnpkg.com/flat/-/flat-4.1.0.tgz#090bec8b05e39cba309747f1d588f04dbaf98db2"
3123+
integrity sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==
3124+
dependencies:
3125+
is-buffer "~2.0.3"
3126+
30833127
flatted@^2.0.0:
30843128
version "2.0.2"
30853129
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138"
@@ -3636,6 +3680,11 @@ is-buffer@^1.1.5:
36363680
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
36373681
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
36383682

3683+
is-buffer@~2.0.3:
3684+
version "2.0.4"
3685+
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.4.tgz#3e572f23c8411a5cfd9557c849e3665e0b290623"
3686+
integrity sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==
3687+
36393688
is-callable@^1.1.4, is-callable@^1.2.0:
36403689
version "1.2.0"
36413690
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.0.tgz#83336560b54a38e35e3a2df7afd0454d691468bb"
@@ -4567,7 +4616,7 @@ lodash.toarray@^4.4.0:
45674616
resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561"
45684617
integrity sha1-JMS/zWsvuji/0FlNsRedjptlZWE=
45694618

4570-
lodash@^4.17.10, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19:
4619+
lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19:
45714620
version "4.17.19"
45724621
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
45734622
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==

src/admin.module.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import AdminBro, { AdminBroOptions } from 'admin-bro'
1+
import AdminBro from 'admin-bro'
22
import { Module, DynamicModule, OnModuleInit, Inject } from '@nestjs/common'
33
import { HttpAdapterHost } from '@nestjs/core'
44

55
import { serveStaticProvider } from './serve-static.provider'
6-
import { ADMIN_BRO_TOKEN, CONFIG_TOKEN } from './token.constants'
6+
import { CONFIG_TOKEN } from './token.constants'
77
import { AbstractLoader } from './loaders/abstract.loader'
88
import { AdminModuleOptions } from './interfaces/admin-module-options.interface'
9+
import { AdminModuleFactory } from './interfaces/admin-module-factory.interface'
910

1011
@Module({
1112
providers: [serveStaticProvider],
@@ -14,41 +15,32 @@ export class AdminModule implements OnModuleInit {
1415
constructor(
1516
private readonly httpAdapterHost: HttpAdapterHost,
1617
private readonly loader: AbstractLoader,
17-
@Inject(ADMIN_BRO_TOKEN)
18-
private readonly adminBroInstance: AdminBro,
1918
@Inject(CONFIG_TOKEN)
2019
private readonly adminModuleOptions: AdminModuleOptions,
2120
) {}
2221

23-
public static createAdminModule(adminModuleOptions: AdminModuleOptions): DynamicModule {
22+
public static createAdminModule(options: AdminModuleFactory): DynamicModule {
2423
return {
24+
imports: options.imports,
2525
module: AdminModule,
2626
providers: [
27-
{
28-
provide: ADMIN_BRO_TOKEN,
29-
useFactory: async () => {
30-
const admin = new AdminBro(adminModuleOptions.adminBroOptions);
31-
await admin.initialize();
32-
33-
return admin;
34-
},
35-
},
3627
{
3728
provide: CONFIG_TOKEN,
38-
useFactory: () => adminModuleOptions,
29+
useFactory: options.useFactory,
30+
inject: options.inject,
3931
},
4032
],
41-
exports: [
42-
ADMIN_BRO_TOKEN,
43-
],
4433
}
4534
}
4635

47-
public onModuleInit() {
36+
public async onModuleInit() {
37+
const admin = new AdminBro(this.adminModuleOptions.adminBroOptions);
38+
await admin.initialize();
39+
4840
const { httpAdapter } = this.httpAdapterHost;
49-
this.loader.register(this.adminBroInstance, httpAdapter, {
41+
this.loader.register(admin, httpAdapter, {
5042
...this.adminModuleOptions,
51-
adminBroOptions: this.adminBroInstance.options,
43+
adminBroOptions: admin.options,
5244
});
5345
}
5446
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { ModuleMetadata } from '@nestjs/common';
3+
4+
import { AdminModuleOptions } from './admin-module-options.interface';
5+
6+
export interface AdminModuleFactory extends Pick<ModuleMetadata, 'imports'> {
7+
inject?: any[],
8+
useFactory: (...args: any[]) => Promise<AdminModuleOptions> | AdminModuleOptions,
9+
}

0 commit comments

Comments
 (0)