diff --git a/src/staged-query/selection.ts b/src/staged-query/selection.ts index b7b0981..93aca21 100644 --- a/src/staged-query/selection.ts +++ b/src/staged-query/selection.ts @@ -10,6 +10,7 @@ import { type IndexBound, type IndexKeys, type InsertOptions, + type SelectionKey, } from "./common"; import { AtomicMutationQuery, @@ -193,6 +194,20 @@ export class Table extends Selection { * @param index Secondary index, will use the primary key if undefined * @returns Multiple document selection */ + public getAll(keys: IndexKeys, index?: string): Selection; + /** + * Gets multiple documents using a secondary index, keyed by values or + * proxies of one key type, such as a row field typed `string` + * + * @typeParam K Type of a single key + * @param keys Key value(s) + * @param index Secondary index, will use the primary key if undefined + * @returns Multiple document selection + */ + public getAll( + keys: IndexKeys, + index?: string, + ): Selection; public getAll(keys: IndexKeys, index?: string) { return this.stage(Selection, QueryStage("getAll", { index }, keys)); } diff --git a/src/staged-query/stages.ts b/src/staged-query/stages.ts index 8bec816..9580e39 100644 --- a/src/staged-query/stages.ts +++ b/src/staged-query/stages.ts @@ -24,11 +24,13 @@ export type IndexBound = IndexValue | IndexValue[]; /** * The keys `getAll` matches: one key, a list of keys, or a list of compound - * keys, each holding a compound index's values in its field order. + * keys, each holding a compound index's values in its field order. `K` is the + * type of a single key, so a proxy of any `SelectionKey` member, such as a row + * field typed `string`, is a key. */ -export type IndexKeys = - | ValueProxyOrValue - | ValueProxyOrValue[] +export type IndexKeys = + | ValueProxyOrValue + | ValueProxyOrValue[] | SelectionKey[][]; type Func = QueryStage<"func">; diff --git a/src/tests/do_operations.test.ts b/src/tests/do_operations.test.ts index 46f8111..fe903cf 100644 --- a/src/tests/do_operations.test.ts +++ b/src/tests/do_operations.test.ts @@ -32,6 +32,10 @@ describe("Do Operations", () => { it("Do with Array Operations", DoWithArrayOperations); it("Do with Nested Object Operations", DoWithNestedObjectOperations); it("Do with Subquery Field Reference", DoWithSubqueryFieldReference); + it( + "Do with Subquery Keyed on a Typed Field", + DoWithSubqueryKeyedOnTypedField, + ); it( "Do with Unresolved Subquery Returns Null", DoWithUnresolvedSubqueryReturnsNull, @@ -264,6 +268,18 @@ async function DoWithSubqueryFieldReference() { expect(result.lookedUpUser).to.have.property("age", 25); } +async function DoWithSubqueryKeyedOnTypedField() { + const result = await table + .get(insertedKeys[0]) + .do((doc) => { + const name = doc.key("name"); + return { name, namesakes: table.getAll(name, "name").count() }; + }) + .run(); + + expect(result).to.deep.equal({ name: "Antoine", namesakes: 1 }); +} + async function DoWithUnresolvedSubqueryReturnsNull() { const result = await table .get(insertedKeys[0])