interface Root {} interface Database { _db: any } interface Collection { _coll: any } interface Document { _doc: any } interface TablesDB { _tdb: any } interface Table { _tbl: any } interface Row { _row: any } interface Bucket { _bkt: any } interface File { _file: any } interface Func { _fn: any } interface Execution { _exec: any } interface Team { _team: any } interface Membership { _mem: any } interface Resolved { _res: any } type Actionable = Document | Row | File | Team | Membership; function normalize(id: string): string { if (id === undefined || id === null) { throw new Error("Channel ID is required"); } const trimmed = String(id).trim(); if (trimmed === "") { throw new Error("Channel ID is required"); } return trimmed; } export class Channel { declare _type: T; private constructor(private readonly segments: string[]) {} private next(segment: string, id?: string): Channel { const segments = id === undefined ? [...this.segments, segment] : [...this.segments, segment, normalize(id)]; return new Channel(segments) as any; } private resolve(action: string): Channel { return new Channel([...this.segments, action]) as any; } toString(): string { return this.segments.join("."); } // --- DATABASE ROUTE --- // Only available on Channel collection(this: Channel, id: string): Channel { return this.next("collections", id); } // Only available on Channel document(this: Channel, id?: string): Channel { // Default: no document ID segment return this.next("documents", id); } // --- TABLESDB ROUTE --- table(this: Channel, id: string): Channel { return this.next
("tables", id); } row(this: Channel
, id?: string): Channel { // Default: no row ID segment return this.next("rows", id); } // --- BUCKET ROUTE --- file(this: Channel, id?: string): Channel { // Default: no file ID segment return this.next("files", id); } // --- TERMINAL ACTIONS --- // Restricted to the Actionable union create(this: Channel): Channel { return this.resolve("create"); } upsert(this: Channel): Channel { return this.resolve("upsert"); } update(this: Channel): Channel { return this.resolve("update"); } delete(this: Channel): Channel { return this.resolve("delete"); } // --- ROOT FACTORIES --- static database(id: string) { return new Channel(["databases", normalize(id)]); } static execution(id: string) { return new Channel(["executions", normalize(id)]); } static tablesdb(id: string) { return new Channel(["tablesdb", normalize(id)]); } static bucket(id: string) { return new Channel(["buckets", normalize(id)]); } static function(id: string) { return new Channel(["functions", normalize(id)]); } static team(id: string) { return new Channel(["teams", normalize(id)]); } static membership(id: string) { return new Channel(["memberships", normalize(id)]); } static account(): string { return "account"; } // Global events static documents(): string { return "documents"; } static rows(): string { return "rows"; } static files(): string { return "files"; } static executions(): string { return "executions"; } static teams(): string { return "teams"; } static memberships(): string { return "memberships"; } } // Export types for backward compatibility with realtime export type ActionableChannel = Channel | Channel | Channel | Channel | Channel | Channel; export type ResolvedChannel = Channel;