Last active
September 11, 2021 05:38
-
-
Save Romakita/7af8f0ecdca9b2b5a45db8f79f51242f to your computer and use it in GitHub Desktop.
AdminBro with Ts.ED
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
import {Module, Configuration, Inject, Constant, InjectorService} from "@tsed/di"; | |
import {PlatformApplication} from "@tsed/common"; | |
import {MongooseService} from "@tsed/mongoose"; | |
import {Logger} from "@tsed/logger"; | |
import {Type} from "@tsed/core"; | |
import AdminBro from "admin-bro"; | |
import AdminBroExpress from "@admin-bro/express"; | |
import AdminBroMongoose from "@admin-bro/mongoose"; | |
AdminBro.registerAdapter(AdminBroMongoose) | |
@Module() | |
export class AdminBroModule { | |
@Constant("adminBro") | |
private settings: any; | |
@Inject() | |
protected logger: Logger; | |
@Configuration() | |
protected configuration: Configuration; | |
@Inject() | |
protected app: PlatformApplication; | |
@Inject() | |
protected injector: InjectorService; | |
@Inject() | |
protected mongooseService: MongooseService; | |
#adminBro: AdminBro; | |
protected getMongooseModels() { | |
return this.settings.resources.map((resource) => { | |
if (isClass(resource)) { | |
resource = { | |
resource | |
} | |
} | |
return { | |
...resource, | |
resource: this.injector.get(resource.resource) | |
} | |
}) | |
} | |
$onInit() { | |
this.#adminBro = new AdminBro({ | |
...this.settings, | |
databases: [this.mongooseService.get()], | |
resources: this.getMongooseModels() | |
}); | |
} | |
$onRoutesInit() { // or $afterRoutesInit | |
const routes = AdminBroExpress.buildRouter(this.#adminBro); | |
this.app.use(this.#adminBro.options.rootPath, router); | |
} | |
$onReady() { | |
const {configuration, injector} = this; | |
const {httpsPort, httpPort} = configuration; | |
const displayLog = (host: any) => { | |
const url = typeof host.port === "number" ? `${host.protocol}://${host.address}:${host.port}` : ""; | |
this.logger.info(`AdminBro is available on ${url}${this.settings.rootPath}`); | |
}; | |
if (httpsPort) { | |
const host = configuration.getHttpsPort(); | |
displayLog({protocol: "https", ...host}); | |
} else if (httpPort) { | |
const host = configuration.getHttpPort(); | |
displayLog({protocol: "http", ...host}); | |
} | |
} | |
} |
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
import {Configuration} from "@tsed/di"; | |
import "./module/AdminBroModule"; | |
import {Model} from "@tsed/mongoose"; | |
@Model() | |
class User { | |
/// model declaration | |
} | |
@Configuration({ | |
mongoose: {}, | |
adminBro: { | |
resources: [ | |
User | |
] | |
} | |
}) | |
export class Server {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment