rspace-online/modules/rdata/schemas.ts

71 lines
1.5 KiB
TypeScript

/**
* rData Automerge document schemas.
*
* Lightweight sync for shared analytics dashboard configuration.
* The actual analytics data comes from the Umami API; this doc
* syncs which apps/metrics space members want to track together.
*
* DocId format: {space}:data:config
*/
import type { DocSchema } from '../../shared/local-first/document';
// ── Document types ──
export interface TrackedApp {
id: string;
name: string;
url: string;
addedBy: string | null;
addedAt: number;
}
export interface DataDoc {
meta: {
module: string;
collection: string;
version: number;
spaceSlug: string;
createdAt: number;
};
trackedApps: Record<string, TrackedApp>;
dashboardConfig: {
refreshInterval: number;
defaultDateRange: string;
};
}
// ── Schema registration ──
export const dataSchema: DocSchema<DataDoc> = {
module: 'data',
collection: 'config',
version: 1,
init: (): DataDoc => ({
meta: {
module: 'data',
collection: 'config',
version: 1,
spaceSlug: '',
createdAt: Date.now(),
},
trackedApps: {},
dashboardConfig: {
refreshInterval: 30000,
defaultDateRange: '7d',
},
}),
migrate: (doc: any, _fromVersion: number) => {
if (!doc.trackedApps) doc.trackedApps = {};
if (!doc.dashboardConfig) doc.dashboardConfig = { refreshInterval: 30000, defaultDateRange: '7d' };
doc.meta.version = 1;
return doc;
},
};
// ── Helpers ──
export function dataDocId(space: string) {
return `${space}:data:config` as const;
}