Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/storage/ReadOnlyTabularStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license
* Copyright 2026 Steven Roussey <sroussey@gmail.com>
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, expect, it } from "vitest";
import { InMemoryTabularStorage } from "workglow";
import { CikNamePrimaryKeyNames, CikNameSchema } from "./entity/CikNameSchema";
import { ReadOnlyTabularStorage } from "./ReadOnlyTabularStorage";

describe("ReadOnlyTabularStorage", () => {
it("putByUniqueKey writes nothing and reports an overwrite", async () => {
const inner = new InMemoryTabularStorage(CikNameSchema, CikNamePrimaryKeyNames, []);
await inner.put({ cik: 1, name: "Before" });
const readOnly = new ReadOnlyTabularStorage(inner);

const result = await readOnly.putByUniqueKey({ cik: 1, name: "After" }, ["cik"]);

expect(result).toEqual({ entity: { cik: 1, name: "After" }, inserted: false });
expect(await inner.get({ cik: 1 })).toEqual({ cik: 1, name: "Before" });
expect(await readOnly.size()).toBe(1);
});
});
13 changes: 13 additions & 0 deletions src/storage/ReadOnlyTabularStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
TabularEventParameters,
TabularSubscribeOptions,
TypedArraySchemaOptions,
UniqueKeyPutResult,
} from "workglow";

/**
Expand All @@ -45,6 +46,18 @@ export class ReadOnlyTabularStorage<
return _value as unknown as Entity;
}

/**
* Reports an overwrite rather than an insert: `inserted: true` tells a caller
* nothing can be keyed by the row yet, so it would skip reading back rows that
* the underlying storage does hold.
*/
async putByUniqueKey(
value: InsertType,
_uniqueKey: ReadonlyArray<keyof Entity>
): Promise<UniqueKeyPutResult<Entity>> {
return { entity: value as unknown as Entity, inserted: false };
}

async putBulk(values: InsertType[]): Promise<Entity[]> {
return values as unknown as Entity[];
}
Expand Down
Loading