|
| 1 | +import type { |
| 2 | + ITabularStorage, |
| 3 | + TabularEventName, |
| 4 | + TabularEventListener, |
| 5 | + TabularEventParameters, |
| 6 | + TabularChangePayload, |
| 7 | + TabularSubscribeOptions, |
| 8 | + DeleteSearchCriteria, |
| 9 | + SearchCriteria, |
| 10 | + QueryOptions, |
| 11 | +} from "@workglow/storage"; |
| 12 | +import type { DataPortSchemaObject, FromSchema, TypedArraySchemaOptions } from "@workglow/util"; |
| 13 | +import type { |
| 14 | + AutoGeneratedKeys, |
| 15 | + InsertEntity, |
| 16 | + SimplifyPrimaryKey, |
| 17 | +} from "@workglow/storage"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Wraps an {@link ITabularStorage} and silently no-ops all write operations. |
| 21 | + * Read operations (get, getAll, query, getBulk, records, pages, size) are |
| 22 | + * forwarded to the underlying storage so that dry-run commands can still |
| 23 | + * inspect existing data. |
| 24 | + */ |
| 25 | +export class ReadOnlyTabularStorage< |
| 26 | + Schema extends DataPortSchemaObject, |
| 27 | + PrimaryKeyNames extends ReadonlyArray<keyof Schema["properties"]>, |
| 28 | + Entity = FromSchema<Schema, TypedArraySchemaOptions>, |
| 29 | + PrimaryKey = SimplifyPrimaryKey<Entity, PrimaryKeyNames>, |
| 30 | + InsertType = InsertEntity<Entity, AutoGeneratedKeys<Schema>>, |
| 31 | +> implements ITabularStorage<Schema, PrimaryKeyNames, Entity, PrimaryKey, InsertType> |
| 32 | +{ |
| 33 | + constructor( |
| 34 | + private readonly inner: ITabularStorage<Schema, PrimaryKeyNames, Entity, PrimaryKey, InsertType> |
| 35 | + ) {} |
| 36 | + |
| 37 | + // ── writes (no-op) ───────────────────────────────────────────────── |
| 38 | + |
| 39 | + async put(_value: InsertType): Promise<Entity> { |
| 40 | + return _value as unknown as Entity; |
| 41 | + } |
| 42 | + |
| 43 | + async putBulk(values: InsertType[]): Promise<Entity[]> { |
| 44 | + return values as unknown as Entity[]; |
| 45 | + } |
| 46 | + |
| 47 | + async delete(_key: PrimaryKey | Entity): Promise<void> {} |
| 48 | + |
| 49 | + async deleteAll(): Promise<void> {} |
| 50 | + |
| 51 | + async deleteSearch(_criteria: DeleteSearchCriteria<Entity>): Promise<void> {} |
| 52 | + |
| 53 | + async setupDatabase(): Promise<void> {} |
| 54 | + |
| 55 | + // ── reads (forwarded) ────────────────────────────────────────────── |
| 56 | + |
| 57 | + get(key: PrimaryKey): Promise<Entity | undefined> { |
| 58 | + return this.inner.get(key); |
| 59 | + } |
| 60 | + |
| 61 | + getAll(options?: QueryOptions<Entity>): Promise<Entity[] | undefined> { |
| 62 | + return this.inner.getAll(options); |
| 63 | + } |
| 64 | + |
| 65 | + size(): Promise<number> { |
| 66 | + return this.inner.size(); |
| 67 | + } |
| 68 | + |
| 69 | + getBulk(offset: number, limit: number): Promise<Entity[] | undefined> { |
| 70 | + return this.inner.getBulk(offset, limit); |
| 71 | + } |
| 72 | + |
| 73 | + records(pageSize?: number): AsyncGenerator<Entity, void, undefined> { |
| 74 | + return this.inner.records(pageSize); |
| 75 | + } |
| 76 | + |
| 77 | + pages(pageSize?: number): AsyncGenerator<Entity[], void, undefined> { |
| 78 | + return this.inner.pages(pageSize); |
| 79 | + } |
| 80 | + |
| 81 | + query( |
| 82 | + criteria: SearchCriteria<Entity>, |
| 83 | + options?: QueryOptions<Entity> |
| 84 | + ): Promise<Entity[] | undefined> { |
| 85 | + return this.inner.query(criteria, options); |
| 86 | + } |
| 87 | + |
| 88 | + // ── events (forwarded) ──────────────────────────────────────────── |
| 89 | + |
| 90 | + on<Event extends TabularEventName>( |
| 91 | + name: Event, |
| 92 | + fn: TabularEventListener<Event, PrimaryKey, Entity> |
| 93 | + ): void { |
| 94 | + this.inner.on(name, fn); |
| 95 | + } |
| 96 | + |
| 97 | + off<Event extends TabularEventName>( |
| 98 | + name: Event, |
| 99 | + fn: TabularEventListener<Event, PrimaryKey, Entity> |
| 100 | + ): void { |
| 101 | + this.inner.off(name, fn); |
| 102 | + } |
| 103 | + |
| 104 | + emit<Event extends TabularEventName>( |
| 105 | + name: Event, |
| 106 | + ...args: TabularEventParameters<Event, PrimaryKey, Entity> |
| 107 | + ): void { |
| 108 | + this.inner.emit(name, ...args); |
| 109 | + } |
| 110 | + |
| 111 | + once<Event extends TabularEventName>( |
| 112 | + name: Event, |
| 113 | + fn: TabularEventListener<Event, PrimaryKey, Entity> |
| 114 | + ): void { |
| 115 | + this.inner.once(name, fn); |
| 116 | + } |
| 117 | + |
| 118 | + waitOn<Event extends TabularEventName>( |
| 119 | + name: Event |
| 120 | + ): Promise<TabularEventParameters<Event, PrimaryKey, Entity>> { |
| 121 | + return this.inner.waitOn(name); |
| 122 | + } |
| 123 | + |
| 124 | + subscribeToChanges( |
| 125 | + callback: (change: TabularChangePayload<Entity>) => void, |
| 126 | + options?: TabularSubscribeOptions |
| 127 | + ): () => void { |
| 128 | + return this.inner.subscribeToChanges(callback, options); |
| 129 | + } |
| 130 | + |
| 131 | + // ── lifecycle ───────────────────────────────────────────────────── |
| 132 | + |
| 133 | + destroy(): void { |
| 134 | + this.inner.destroy(); |
| 135 | + } |
| 136 | + |
| 137 | + [Symbol.dispose](): void { |
| 138 | + this.inner[Symbol.dispose](); |
| 139 | + } |
| 140 | + |
| 141 | + [Symbol.asyncDispose](): Promise<void> { |
| 142 | + return this.inner[Symbol.asyncDispose](); |
| 143 | + } |
| 144 | +} |
0 commit comments