import * as z from "zod"; import type {Duration} from "dayjs/plugin/duration.js"; import {durationValueSchema} from "../Atomic.ts"; export const retentionPlayTypeSchema = z.enum(['failed', 'completed', 'duped']); export type RetentionPlayType = z.infer; export const retentionPlayTypes: RetentionPlayType[] = ['failed','completed','duped']; const durationSchema = z.custom( (val) => val !== null && typeof val === 'object' && typeof (val as Duration).asMilliseconds === 'function', {message: 'Expected a dayjs Duration instance'} ); export const retentionValueUnparsedSchema = z.union([durationValueSchema, z.literal(false)]); export type RetentionValueUnparsed = z.infer; export const retentionValueSchema = z.union([durationSchema, z.literal(false)]); export type RetentionValue = z.infer; export const rententionGranularDurationValueSchema = z.object({ failed: durationValueSchema.optional(), completed: durationValueSchema.optional(), duped: durationValueSchema.optional() }).meta({ description: 'Individual duration policies based on the state of the Play', title: 'Duration Value per Play Type' }); export type RententionGranularDurationValue = z.infer; export const rententionGranularDurationSchema = z.object({ failed: durationSchema.optional(), completed: durationSchema.optional(), duped: durationSchema.optional(), }); export type RententionGranularDuration = z.infer; export const rententionGranularRetentionValueSchema = z.object({ failed: retentionValueSchema.optional(), completed: retentionValueSchema.optional(), duped: retentionValueSchema.optional(), }); export type RententionGranularRetentionValue = z.infer; export const rententionGranularSchema = z.union([ rententionGranularDurationValueSchema, rententionGranularDurationSchema, rententionGranularRetentionValueSchema, ]); export type RententionGranular = z.infer; export const retentionConfigValueDurationValueSchema = z.union([durationValueSchema, rententionGranularDurationValueSchema]); export type RetentionConfigValueDurationValue = z.infer; export const retentionConfigValueDurationSchema = z.union([durationSchema, rententionGranularDurationSchema]); export type RetentionConfigValueDuration = z.infer; export const retentionConfigValueRetentionValueSchema = z.union([retentionValueSchema, rententionGranularRetentionValueSchema]); export type RetentionConfigValueRetentionValue = z.infer; export const retentionConfigValueSchema = z.union([ retentionConfigValueDurationValueSchema, retentionConfigValueDurationSchema, retentionConfigValueRetentionValueSchema, ]); export type RetentionConfigValue = z.infer; export const compactablePropertySchema = z.enum(['transform', 'input']).meta({ description: 'The type of data to compact on the Play' }); export type CompactableProperty = z.infer; export const COMPACTABLE = { transform: 'transform', input: 'input' } as const satisfies Record; export const compactableProperties: CompactableProperty[] = [COMPACTABLE.transform, COMPACTABLE.input]; export const retentionOptionDurationSchema = z.object({ failed: durationSchema, completed: durationSchema, duped: durationSchema, }); export type RetentionOptionDuration = z.infer; export const retentionOptionRetentionValueSchema = z.object({ failed: retentionValueSchema, completed: retentionValueSchema, duped: retentionValueSchema, }); export type RetentionOptionRetentionValue = z.infer; export const retentionOptionSchema = z.union([retentionOptionDurationSchema, retentionOptionRetentionValueSchema]); export type RetentionOption = z.infer; export const retentionConfigDurationValueSchema = z.object({ deleteAfter: retentionConfigValueDurationValueSchema.optional().meta({description: 'Delete Plays using these retention policies'}), compactAfter: retentionConfigValueDurationValueSchema.optional().meta({description: 'Compact Plays using these retention policies'}), compact: z.array(compactablePropertySchema).optional().meta({ description: 'The type of data to compact on the Play'}) }).meta({description: 'Retention policy for Plays stored in the database'}); export type RetentionConfigDurationValue = z.infer; export const retentionConfigDurationSchema = z.object({ deleteAfter: retentionConfigValueDurationSchema.optional(), compactAfter: retentionConfigValueDurationSchema.optional(), compact: z.array(compactablePropertySchema).optional(), }); export type RetentionConfigDuration = z.infer; export const retentionConfigRetentionValueSchema = z.object({ deleteAfter: retentionConfigValueRetentionValueSchema.optional(), compactAfter: retentionConfigValueRetentionValueSchema.optional(), compact: z.array(compactablePropertySchema).optional(), }); export type RetentionConfigRetentionValue = z.infer; export const retentionConfigSchema = z.union([ retentionConfigDurationValueSchema, retentionConfigDurationSchema, retentionConfigRetentionValueSchema, ]); export type RetentionConfig = z.infer; export const retentionOptionsSchema = z.object({ deleteAfter: retentionOptionDurationSchema, compactAfter: retentionOptionRetentionValueSchema, compact: z.array(compactablePropertySchema), }); export type RetentionOptions = z.infer; export const DEFAULT_RETENTION_DELETE_AFTER = 604800; // 7 days export const DEFAULT_RETENTION_COMPACT_AFTER = 259200; // 3 days