import type { Attributes } from "./entity.js"; import type t from "type-fest"; export type KeyValue = string | number | bigint; /** * Composite Key - Whole key used to get and set an entity, made up of partition and sort key parts containing one or more attribute. * Key Part - partition or sort key of the composite key, each made up of one or more key attribute. * Key Attribute - A single attribute used as a segment of a key part. */ /** * Any attribute name considered to be a valid key attribute. */ export type KeyAttribute = { [K in keyof Attr]: K extends string ? Attr[K] extends Values ? K : never : never; }[keyof Attr]; /** * An at least one tuple of attribute keys. Attributes can refer to any {@link KeyValue}. * * Used to define a key in an Entity, where all key attributes must not be optional. */ export type EntityCompositeKeyPart = readonly [ KeyAttribute, ...KeyAttribute[] ]; /** * An at least one tuple of attribute keys. Attributes can refer to any {@link KeyValue} and may be optional. * * Used to define a key in an {@link EntityIndex}, where key attributes may refer to optional fields. This is called a sparse index. */ export type IndexCompositeKeyPart = readonly [ KeyAttribute, ...KeyAttribute[] ] | EntityCompositeKeyPart; /** * An at least one tuple of attribute keys. Does not include attribute type constraints, but the keys must be string. * * Should match either {@link IndexCompositeKeyPart} or {@link EntityCompositeKeyPart}. */ export type CompositeKeyPart = readonly [ KeyAttribute, ...(readonly KeyAttribute[]) ]; /** * All attributes of the composite key as an object. * * ```ts * { * partitionAttribute1: "", * partitionAttribute2: "", * sortAttribute1: "", * sortAttribute2: "" * } * ``` */ export type KeyMap = CompositeKeyPart, Sort extends CompositeKeyPart | undefined = CompositeKeyPart | undefined> = { [k in Partition[number]]: Exclude; } & (Sort extends CompositeKeyPart ? { [k in Sort[number]]: Exclude; } : {}); export type KeyPartialTuple = Attrs extends [] ? readonly [] : Attrs extends readonly [ infer Head extends keyof Attr, ...infer Rest extends readonly (keyof Attr)[] ] ? readonly [Extract, ...KeyPartialTuple] : readonly []; /** * All attributes of the composite key as a in order tuple. * * ```ts * [partitionAttribute1, partitionAttribute2, sortAttribute1, sortAttribute2] * ``` */ export type KeyTuple = CompositeKeyPart, Sort extends CompositeKeyPart | undefined = CompositeKeyPart | undefined> = Sort extends undefined ? KeyPartialTuple : readonly [ ...KeyPartialTuple, ...KeyPartialTuple> ]; /** * All attributes in either the partition key and the sort key (when present). */ export type CompositeKey = CompositeKeyPart, Sort extends CompositeKeyPart | undefined = CompositeKeyPart | undefined> = KeyMap | KeyTuple; /** * Matches if the key attribute is between the start and end value, inclusive. * * start <= value <= end * * Note: numeric multi-attribute key parts are treated as strings. */ export type QueryKeyCondition = BetweenQueryKeyCondition | LessThanQueryKeyCondition | LessThanEqualsQueryKeyCondition | GreaterThanQueryKeyCondition | GreaterThanEqualsQueryKeyCondition | BeginsWithQueryKeyCondition; /** * Matches if the key attribute is between the start and end value, inclusive. * * start <= value <= end * * Note: numeric multi-attribute key parts are treated as strings. */ export interface BetweenQueryKeyCondition { $between: [t.LiteralToPrimitive, t.LiteralToPrimitive]; } /** * Matches if the key attribute starts with the given value. * * Can only be used with string fields. * * Note: numeric multi-attribute key parts are treated as strings. */ export interface BeginsWithQueryKeyCondition { $beginsWith: Extract, string>; } /** * Matches if the key attribute is less than the given value. * * Note: numeric multi-attribute key parts are treated as strings. */ export interface LessThanQueryKeyCondition { $lt: t.LiteralToPrimitive; } /** * Matches if the key attribute is less than or equal to the given value. * * Note: numeric multi-attribute key parts are treated as strings. */ export interface LessThanEqualsQueryKeyCondition { $lte: t.LiteralToPrimitive; } /** * Matches if the key attribute is greater than the given value. * * Note: numeric multi-attribute key parts are treated as strings. */ export interface GreaterThanQueryKeyCondition { $gt: t.LiteralToPrimitive; } /** * Matches if the key attribute is greater than or equal to the given value. * * Note: numeric multi-attribute key parts are treated as strings. */ export interface GreaterThanEqualsQueryKeyCondition { $gte: t.LiteralToPrimitive; } export type ProgressiveTupleQueryKey = Sort extends readonly [] ? Accum : Sort extends readonly [ infer k extends keyof Attr, ...infer ks extends readonly (keyof Attr)[] ] ? Accum | [...Accum, QueryKeyCondition>] | ProgressiveTupleQueryKey ]> : never; export type ProgressiveQueryKey = Sort extends readonly [ infer k extends keyof Attr, ...infer ks extends readonly (keyof Attr)[] ] ? { [sk in Sort[number]]?: never; } | { [sk in k]: QueryKeyCondition>; } | BetweenProgressiveKeyCondition | ({ [sk in k]: Extract; } & ProgressiveQueryKey) : {}; /** * Supports betweens condition using multiple sort attribute parts. * * At least one attribute must be present in the left and right side. * * BETWEEN "a" and "c#b" * { * $between: [{sort1: "a"}, {sort1: "c", sort2: "b"}] * } * * BETWEEN "a" and "c" * { * sort1: { $between: ["a", "c"] } * } */ export type BetweenProgressiveKeyCondition = { $between: Sort extends readonly [ infer k extends keyof Attr, ...infer ks extends readonly (keyof Attr)[] ] ? [ ProgressiveKey & { [sk in k]: Extract; }, ProgressiveKey & { [sk in k]: Extract; } ] : never; }; export type ProgressiveKey = Sort extends readonly [ infer k extends keyof Attr, ...infer ks extends readonly (keyof Attr)[] ] ? { [sk in Sort[number]]?: never; } | ({ [sk in k]: Extract; } & ProgressiveKey) : {}; export type QueryKeyMap = CompositeKeyPart, Sort extends CompositeKeyPart | undefined = CompositeKeyPart | undefined> = { [pk in Partition[number]]: Extract; } & (Sort extends undefined ? {} : ProgressiveQueryKey]>); /** * A partial key that can be used to query an entity. * * ```ts * entity.query({ part1: "val", part2: "val2", sort1: "val" }); * ``` */ export type QueryKey, Sort extends CompositeKeyPart | undefined> = QueryKeyMap | [ ...KeyTuple, ...(Sort extends undefined ? [] : ProgressiveTupleQueryKey>) ]; /** * A stream query can contain partial sort keys and partial partition keys. */ export type StreamQueryKey = CompositeKeyPart, Sort extends CompositeKeyPart | undefined = CompositeKeyPart | undefined> = ProgressiveKey & (Sort extends undefined ? {} : ProgressiveKey>); export type KeyAttributes = IndexCompositeKeyPart, Sort extends IndexCompositeKeyPart | undefined = IndexCompositeKeyPart | undefined> = Sort extends undefined ? Partition[number] : [...Partition, ...Exclude][number]; //# sourceMappingURL=key.d.ts.map