-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow registering admin entities inside their own module
- Loading branch information
1 parent
bb10ed2
commit a95fb70
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters