Compare commits

...

1 Commits

Author SHA1 Message Date
dswbx 7e25d54847 mm: added diff event + plugin to write down diffs 2025-07-08 09:54:56 +02:00
4 changed files with 43 additions and 0 deletions
+1
View File
@@ -39,6 +39,7 @@ export {
type ParseOptions,
InvalidSchemaError,
} from "./object/schema";
export * as $diff from "./object/diff";
export * from "./drivers";
export * from "./events";
+15
View File
@@ -128,8 +128,16 @@ export class ModuleManagerConfigUpdateEvent<
}> {
static override slug = "mm-config-update";
}
export class ModuleManagerConfigDiffEvent extends ModuleManagerEvent<{
diffs: $diff.DiffEntry[];
}> {
static override slug = "mm-config-diff";
}
export const ModuleManagerEvents = {
ModuleManagerConfigUpdateEvent,
ModuleManagerConfigDiffEvent,
};
// @todo: cleanup old diffs on upgrade
@@ -349,6 +357,13 @@ export class ModuleManager {
// validate diffs, it'll throw on invalid
this.validateDiffs(diffs);
await this.emgr.emit(
new ModuleManagerConfigDiffEvent({
ctx: this.ctx(),
diffs: $diff.clone(diffs),
}),
);
const date = new Date();
// store diff
await this.mutator().insertOne({
+26
View File
@@ -0,0 +1,26 @@
import { type App, ModuleManagerEvents, type AppPlugin } from "bknd";
import type { $diff } from "bknd/core";
export type SyncDiffsOptions = {
enabled?: boolean;
write: (name: string, diffs: $diff.DiffEntry[]) => Promise<void>;
};
export function syncDiffs({ enabled = true, write }: SyncDiffsOptions): AppPlugin {
return (app: App) => ({
name: "bknd-sync-diffs",
onBuilt: async () => {
if (!enabled) return;
app.emgr.onEvent(
ModuleManagerEvents.ModuleManagerConfigDiffEvent,
async (event) => {
const name = `${new Date().getTime()}.diff.json`;
await write?.(name, event.params.diffs);
},
{
id: "sync-config",
},
);
},
});
}
+1
View File
@@ -5,3 +5,4 @@ export {
export { showRoutes, type ShowRoutesOptions } from "./dev/show-routes.plugin";
export { syncConfig, type SyncConfigOptions } from "./dev/sync-config.plugin";
export { syncTypes, type SyncTypesOptions } from "./dev/sync-types.plugin";
export { syncDiffs, type SyncDiffsOptions } from "./dev/sync-diffs.plugin";