rspace-online/modules/rswag/schemas.ts

69 lines
1.5 KiB
TypeScript

/**
* rSwag Automerge document schemas.
*
* Stores shared design metadata for collaborative artifact management.
* Actual image files remain server-side; this doc syncs titles, product
* types, status, and artifact references across space members.
*
* DocId format: {space}:swag:designs
*/
import type { DocSchema } from '../../shared/local-first/document';
// ── Document types ──
export interface SwagDesign {
id: string;
title: string;
productType: 'sticker' | 'poster' | 'tee' | 'hoodie';
/** Server artifact ID (if generated) */
artifactId: string | null;
createdBy: string | null;
createdAt: number;
updatedAt: number;
}
export interface SwagDoc {
meta: {
module: string;
collection: string;
version: number;
spaceSlug: string;
createdAt: number;
};
designs: Record<string, SwagDesign>;
/** Currently active design ID for the space */
activeDesignId: string | null;
}
// ── Schema registration ──
export const swagSchema: DocSchema<SwagDoc> = {
module: 'swag',
collection: 'designs',
version: 1,
init: (): SwagDoc => ({
meta: {
module: 'swag',
collection: 'designs',
version: 1,
spaceSlug: '',
createdAt: Date.now(),
},
designs: {},
activeDesignId: null,
}),
migrate: (doc: any, _fromVersion: number) => {
if (!doc.designs) doc.designs = {};
if (!('activeDesignId' in doc)) doc.activeDesignId = null;
doc.meta.version = 1;
return doc;
},
};
// ── Helpers ──
export function swagDocId(space: string) {
return `${space}:swag:designs` as const;
}