mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
init code-first mode by splitting module manager
This commit is contained in:
@@ -5,13 +5,14 @@ import type { em as prototypeEm } from "data/prototype";
|
||||
import { Connection } from "data/connection/Connection";
|
||||
import type { Hono } from "hono";
|
||||
import {
|
||||
ModuleManager,
|
||||
type InitialModuleConfigs,
|
||||
type ModuleBuildContext,
|
||||
type ModuleConfigs,
|
||||
type ModuleManagerOptions,
|
||||
type Modules,
|
||||
} from "modules/ModuleManager";
|
||||
ModuleManager,
|
||||
type ModuleBuildContext,
|
||||
type ModuleManagerOptions,
|
||||
} from "modules/manager/ModuleManager";
|
||||
import { DbModuleManager } from "modules/manager/DbModuleManager";
|
||||
import * as SystemPermissions from "modules/permissions";
|
||||
import { AdminController, type AdminControllerOptions } from "modules/server/AdminController";
|
||||
import { SystemController } from "modules/server/SystemController";
|
||||
@@ -93,13 +94,21 @@ export type AppOptions = {
|
||||
email?: IEmailDriver;
|
||||
cache?: ICacheDriver;
|
||||
};
|
||||
};
|
||||
mode?: "db" | "code";
|
||||
readonly?: boolean;
|
||||
} & (
|
||||
| {
|
||||
mode: "db";
|
||||
secrets?: Record<string, any>;
|
||||
}
|
||||
| { mode: "code" }
|
||||
);
|
||||
export type CreateAppConfig = {
|
||||
/**
|
||||
* bla
|
||||
*/
|
||||
connection?: Connection | { url: string };
|
||||
initialConfig?: InitialModuleConfigs;
|
||||
config?: InitialModuleConfigs;
|
||||
options?: AppOptions;
|
||||
};
|
||||
|
||||
@@ -121,8 +130,8 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
|
||||
|
||||
constructor(
|
||||
public connection: C,
|
||||
_initialConfig?: InitialModuleConfigs,
|
||||
private options?: Options,
|
||||
_config?: InitialModuleConfigs,
|
||||
public options?: Options,
|
||||
) {
|
||||
this.drivers = options?.drivers ?? {};
|
||||
|
||||
@@ -134,9 +143,13 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
|
||||
this.plugins.set(config.name, config);
|
||||
}
|
||||
this.runPlugins("onBoot");
|
||||
this.modules = new ModuleManager(connection, {
|
||||
|
||||
// use db manager by default
|
||||
const Manager = this.mode === "db" ? DbModuleManager : ModuleManager;
|
||||
|
||||
this.modules = new Manager(connection, {
|
||||
...(options?.manager ?? {}),
|
||||
initial: _initialConfig,
|
||||
initial: _config,
|
||||
onUpdated: this.onUpdated.bind(this),
|
||||
onFirstBoot: this.onFirstBoot.bind(this),
|
||||
onServerInit: this.onServerInit.bind(this),
|
||||
@@ -145,6 +158,14 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
|
||||
this.modules.ctx().emgr.registerEvents(AppEvents);
|
||||
}
|
||||
|
||||
get mode() {
|
||||
return this.options?.mode ?? "db";
|
||||
}
|
||||
|
||||
isReadOnly() {
|
||||
return this.mode === "code" || this.options?.readonly;
|
||||
}
|
||||
|
||||
get emgr() {
|
||||
return this.modules.ctx().emgr;
|
||||
}
|
||||
@@ -175,7 +196,7 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
|
||||
return results as any;
|
||||
}
|
||||
|
||||
async build(options?: { sync?: boolean; fetch?: boolean; forceBuild?: boolean }) {
|
||||
async build(options?: { sync?: boolean; forceBuild?: boolean; [key: string]: any }) {
|
||||
// prevent multiple concurrent builds
|
||||
if (this._building) {
|
||||
while (this._building) {
|
||||
@@ -188,7 +209,7 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
|
||||
this._building = true;
|
||||
|
||||
if (options?.sync) this.modules.ctx().flags.sync_required = true;
|
||||
await this.modules.build({ fetch: options?.fetch });
|
||||
await this.modules.build();
|
||||
|
||||
const { guard } = this.modules.ctx();
|
||||
|
||||
@@ -215,10 +236,6 @@ export class App<C extends Connection = Connection, Options extends AppOptions =
|
||||
this._building = false;
|
||||
}
|
||||
|
||||
mutateConfig<Module extends keyof Modules>(module: Module) {
|
||||
return this.modules.mutateConfigSafe(module);
|
||||
}
|
||||
|
||||
get server() {
|
||||
return this.modules.server;
|
||||
}
|
||||
@@ -377,5 +394,5 @@ export function createApp(config: CreateAppConfig = {}) {
|
||||
throw new Error("Invalid connection");
|
||||
}
|
||||
|
||||
return new App(config.connection, config.initialConfig, config.options);
|
||||
return new App(config.connection, config.config, config.options);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user