Skip to content

Commit

Permalink
feat: allow registering admin entities inside their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
MathildeDuboille authored and dziraf committed Jan 3, 2023
1 parent bb10ed2 commit a95fb70
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/admin-resource.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Module, DynamicModule } from '@nestjs/common';
import { ResourceWithOptions } from 'adminjs'

import AdminResourceService from './admin-resource.service';

/**
* Nest module which is responsible for admin resources
*
* @summary Nest Module
*
* @class
* @name module:@adminjs/nestjs~AdminResourceModule
* @alias AdminResourceModule
* @memberof module:@adminjs/nestjs
*/
@Module({})
export class AdminResourceModule {
/**
* Register resources
*
* @param {(ResourceWithOptions | any)[]} resources
* @memberof module:@adminjs/nestjs~AdminResourceModule
* @method
* @name forFeature
* @example
* import { Module } from '@nestjs/common';
* import { AdminResourceModule } from '@adminjs/nestjs';
* import { User } from './user.entity';
*
* \@Module({
* imports: [
* AdminResourceModule.forFeature([User]),
* ],
* })
* export class UserModule {}
*
*/
public static forFeature(resources: (ResourceWithOptions | any)[]): DynamicModule {
AdminResourceService.add(resources)

return {
module: AdminResourceModule,
};
}
}
22 changes: 22 additions & 0 deletions src/admin-resource.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@nestjs/common';
import { ResourceWithOptions } from 'adminjs';

@Injectable()
class AdminResourceService {
private static resources: Set<ResourceWithOptions | any> = new Set();

public static add(resources: (ResourceWithOptions | any)[]) {
for (const resource of resources) {
if (AdminResourceService.resources.has(resource)) {
return;
}
AdminResourceService.resources.add(resource);
}
}

public static getResources() {
return Array.from(AdminResourceService.resources);
}
}

export default AdminResourceService;
9 changes: 8 additions & 1 deletion src/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AbstractLoader } from './loaders/abstract.loader'
import { AdminModuleOptions } from './interfaces/admin-module-options.interface'
import { AdminModuleFactory } from './interfaces/admin-module-factory.interface'
import { CustomLoader } from './interfaces/custom-loader.interface'
import AdminResourceService from './admin-resource.service'

/**
* Nest module which is responsible for an AdminJS integration
Expand Down Expand Up @@ -126,7 +127,13 @@ export class AdminModule implements OnModuleInit {
return;
}

const admin = new AdminJS(this.adminModuleOptions.adminJsOptions);
const forFeatureResources = AdminResourceService.getResources()

const adminJSOptions = forFeatureResources.length > 0
? { ...this.adminModuleOptions.adminJsOptions, resources: forFeatureResources }
: this.adminModuleOptions.adminJsOptions

const admin = new AdminJS(adminJSOptions);

const { httpAdapter } = this.httpAdapterHost;
this.loader.register(admin, httpAdapter, {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
import * as NestJSPlugin from './admin.module'

export * from './admin.module';
export * from './admin-resource.module';
export default NestJSPlugin;
export * from './interfaces/admin-module-factory.interface';
export * from './interfaces/admin-module-options.interface';
Expand Down

0 comments on commit a95fb70

Please sign in to comment.