import type { CommandOptions } from "./command"; import { Command } from "./command"; type XAddCommandOptions = { nomkStream?: boolean; trim?: ( | { type: "MAXLEN" | "maxlen"; threshold: number; } | { type: "MINID" | "minid"; threshold: string; } ) & ( | { comparison: "~"; limit?: number; } | { comparison: "="; limit?: never; } ); }; /** * @see https://redis.io/commands/xadd * @see node_modules/@upstash/redis/docs/commands/stream/xadd.mdx * * Stream ID formats: * - "*" - Fully automatic ID generation * - "-" - Explicit ID (e.g., "1526919030474-55") * - "-*" - Auto-generate sequence number for the given millisecond timestamp (Redis 8+) */ export class XAddCommand extends Command { constructor( [key, id, entries, opts]: [ key: string, id: "*" | `${number}-*` | string, entries: Record, opts?: XAddCommandOptions, ], commandOptions?: CommandOptions ) { const command: unknown[] = ["XADD", key]; if (opts) { if (opts.nomkStream) { command.push("NOMKSTREAM"); } if (opts.trim) { command.push(opts.trim.type, opts.trim.comparison, opts.trim.threshold); if (opts.trim.limit !== undefined) { command.push("LIMIT", opts.trim.limit); } } } command.push(id); // entries for (const [k, v] of Object.entries(entries)) { command.push(k, v); } super(command, commandOptions); } }