mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-02 08:06:00 +00:00
mm: added diff event + plugin to write down diffs
This commit is contained in:
@@ -39,6 +39,7 @@ export {
|
||||
type ParseOptions,
|
||||
InvalidSchemaError,
|
||||
} from "./object/schema";
|
||||
export * as $diff from "./object/diff";
|
||||
|
||||
export * from "./drivers";
|
||||
export * from "./events";
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user