mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
module manager: allow initial config without config as fallback
This commit is contained in:
@@ -148,50 +148,58 @@ describe("ModuleManager", async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// @todo: check what happens here
|
test("partial config given", async () => {
|
||||||
/*test("blank app, modify deep config", async () => {
|
|
||||||
const { dummyConnection } = getDummyConnection();
|
const { dummyConnection } = getDummyConnection();
|
||||||
|
|
||||||
const mm = new ModuleManager(dummyConnection);
|
const partial = {
|
||||||
await mm.build();
|
auth: {
|
||||||
|
enabled: true
|
||||||
/!* await mm
|
|
||||||
.get("data")
|
|
||||||
.schema()
|
|
||||||
.patch("entities.test", {
|
|
||||||
fields: {
|
|
||||||
content: {
|
|
||||||
type: "text"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
const mm = new ModuleManager(dummyConnection, {
|
||||||
|
initial: partial
|
||||||
});
|
});
|
||||||
await mm.build();
|
await mm.build();
|
||||||
|
|
||||||
expect(mm.configs().data.entities?.users?.fields?.email.type).toBe("text");
|
expect(mm.version()).toBe(CURRENT_VERSION);
|
||||||
|
expect(mm.built()).toBe(true);
|
||||||
|
expect(mm.configs().auth.enabled).toBe(true);
|
||||||
|
expect(mm.configs().data.entities.users).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
expect(
|
test("partial config given, but db version exists", async () => {
|
||||||
mm.get("data").schema().patch("desc", "entities.users.config.sort_dir")
|
const c = getDummyConnection();
|
||||||
).rejects.toThrow();
|
const mm = new ModuleManager(c.dummyConnection);
|
||||||
await mm.build();*!/
|
|
||||||
expect(mm.configs().data.entities?.users?.fields?.email.type).toBe("text");
|
|
||||||
console.log("here", mm.configs());
|
|
||||||
await mm
|
|
||||||
.get("data")
|
|
||||||
.schema()
|
|
||||||
.patch("entities.users", { config: { sort_dir: "desc" } });
|
|
||||||
await mm.build();
|
await mm.build();
|
||||||
expect(mm.toJSON());
|
const json = mm.configs();
|
||||||
|
|
||||||
//console.log(_jsonp(mm.toJSON().data));
|
const c2 = getDummyConnection();
|
||||||
/!*expect(mm.configs().data.entities!.test!.fields!.content.type).toBe("text");
|
const db = c2.dummyConnection.kysely;
|
||||||
expect(mm.configs().data.entities!.users!.config!.sort_dir).toBe("desc");*!/
|
await migrateSchema(CURRENT_VERSION, { db });
|
||||||
});*/
|
const payload = {
|
||||||
|
...json,
|
||||||
|
auth: {
|
||||||
|
...json.auth,
|
||||||
|
enabled: true,
|
||||||
|
basepath: "/api/auth2"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
await db
|
||||||
|
.updateTable(TABLE_NAME)
|
||||||
|
.set({
|
||||||
|
json: JSON.stringify(payload),
|
||||||
|
version: CURRENT_VERSION
|
||||||
|
})
|
||||||
|
.execute();
|
||||||
|
|
||||||
/*test("accessing modules", async () => {
|
const mm2 = new ModuleManager(c2.dummyConnection, {
|
||||||
const { dummyConnection } = getDummyConnection();
|
initial: {
|
||||||
|
auth: {
|
||||||
const mm = new ModuleManager(dummyConnection);
|
basepath: "/shouldnt/take/this"
|
||||||
|
}
|
||||||
//mm.get("auth").mutate().set({});
|
}
|
||||||
});*/
|
});
|
||||||
|
await mm2.build();
|
||||||
|
expect(mm2.configs().auth.basepath).toBe("/api/auth2");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { Default, type Static, objectEach, transformObject } from "core/utils";
|
|||||||
import { type Connection, EntityManager } from "data";
|
import { type Connection, EntityManager } from "data";
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { type Kysely, sql } from "kysely";
|
import { type Kysely, sql } from "kysely";
|
||||||
|
import { mergeWith } from "lodash-es";
|
||||||
import { CURRENT_VERSION, TABLE_NAME, migrate, migrateSchema } from "modules/migrations";
|
import { CURRENT_VERSION, TABLE_NAME, migrate, migrateSchema } from "modules/migrations";
|
||||||
import { AppServer } from "modules/server/AppServer";
|
import { AppServer } from "modules/server/AppServer";
|
||||||
import { AppAuth } from "../auth/AppAuth";
|
import { AppAuth } from "../auth/AppAuth";
|
||||||
@@ -38,9 +39,11 @@ export type ModuleConfigs = {
|
|||||||
[K in keyof ModuleSchemas]: Static<ModuleSchemas[K]>;
|
[K in keyof ModuleSchemas]: Static<ModuleSchemas[K]>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type InitialModuleConfigs = {
|
export type InitialModuleConfigs =
|
||||||
|
| ({
|
||||||
version: number;
|
version: number;
|
||||||
} & Partial<ModuleConfigs>;
|
} & ModuleConfigs)
|
||||||
|
| Partial<ModuleConfigs>;
|
||||||
|
|
||||||
export type ModuleManagerOptions = {
|
export type ModuleManagerOptions = {
|
||||||
initial?: InitialModuleConfigs;
|
initial?: InitialModuleConfigs;
|
||||||
@@ -71,7 +74,9 @@ export class ModuleManager {
|
|||||||
private _version: number = 0;
|
private _version: number = 0;
|
||||||
private _built = false;
|
private _built = false;
|
||||||
private _fetched = false;
|
private _fetched = false;
|
||||||
private readonly _provided;
|
|
||||||
|
// @todo: keep? not doing anything with it
|
||||||
|
private readonly _booted_with?: "provided" | "partial";
|
||||||
|
|
||||||
private logger = new DebugLogger(isDebug() && false);
|
private logger = new DebugLogger(isDebug() && false);
|
||||||
|
|
||||||
@@ -85,14 +90,15 @@ export class ModuleManager {
|
|||||||
let initial = {} as Partial<ModuleConfigs>;
|
let initial = {} as Partial<ModuleConfigs>;
|
||||||
|
|
||||||
if (options?.initial) {
|
if (options?.initial) {
|
||||||
|
if ("version" in options.initial) {
|
||||||
const { version, ...initialConfig } = options.initial;
|
const { version, ...initialConfig } = options.initial;
|
||||||
if (version && initialConfig) {
|
|
||||||
this._version = version;
|
this._version = version;
|
||||||
initial = initialConfig;
|
initial = initialConfig;
|
||||||
|
|
||||||
this._provided = true;
|
this._booted_with = "provided";
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Initial was provided, but it needs a version!");
|
initial = mergeWith(getDefaultConfig(), options.initial);
|
||||||
|
this._booted_with = "partial";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,10 +343,11 @@ export class ModuleManager {
|
|||||||
|
|
||||||
async build() {
|
async build() {
|
||||||
this.logger.context("build").log("version", this.version());
|
this.logger.context("build").log("version", this.version());
|
||||||
|
this.logger.log("booted with", this._booted_with);
|
||||||
|
|
||||||
// if no config provided, try fetch from db
|
// if no config provided, try fetch from db
|
||||||
if (this.version() === 0) {
|
if (this.version() === 0) {
|
||||||
this.logger.context("build no config").log("version is 0");
|
this.logger.context("no version").log("version is 0");
|
||||||
try {
|
try {
|
||||||
const result = await this.fetch();
|
const result = await this.fetch();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user