/// import { z } from "zod"; import { KeyDefinition } from "../internal/entity.js"; import { EntityIndexSpec, EntitySpec, EntityStreamOperation, EntityStreamOptions } from "../internal/service-spec.js"; import { SetOptionalFields } from "../type-utils.js"; import type { CompositeKey, EntityCompositeKeyPart, IndexCompositeKeyPart, KeyAttributes, QueryKey } from "./key.js"; import type { EntityBatchStream, EntityBatchStreamHandler, EntityStream, EntityStreamHandler } from "./stream.js"; export type AttributeBinaryValue = ArrayBuffer | Blob | Buffer | DataView | File | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; type AttributeScalarValue = null | undefined | bigint | string | number | boolean | AttributeBinaryValue; export type AttributeValue = Attributes | AttributeScalarValue | Set | AttributeValue[]; export interface Attributes { [key: string]: AttributeValue; } /** * Turns a {@link Attributes} type into a Zod {@link z.ZodRawShape}. */ export type EntityZodShape = { [key in keyof Attr]: z.ZodType; }; /** * An eventual entity. * * @see entity */ export interface Entity = EntityCompositeKeyPart, Sort extends EntityCompositeKeyPart | undefined = EntityCompositeKeyPart | undefined> extends Omit, "attributes" | "streams" | "partition" | "sort" | "indices"> { kind: "Entity"; key: KeyDefinition; attributes: ZodAttributesObject; indices: EntityIndex[]; streams: (EntityBatchStream | EntityStream)[]; /** * Get a value. * * @param key - {@link CompositeKey} of the value to retrieve. */ get(key: CompositeKey, options?: EntityReadOptions): Promise; /** * Get a value and metadata like version. * * @param key - {@link CompositeKey} of the value to retrieve. */ getWithMetadata(key: CompositeKey, options?: EntityReadOptions): Promise | undefined>; /** * Puts a value into an entity at a key. */ put(entity: SetOptionalFields, options?: EntityPutOptions): Promise<{ version: number; }>; /** * Deletes a single entry within an entity. */ delete(key: CompositeKey, options?: EntityConsistencyOptions): Promise; /** * Query the entity using the partition key and optionally part of the sort key. */ query | undefined = undefined>(key: QueryKey, request?: EntityQueryOptions): Promise>; /** * Returns all items in the table, up to the limit given or 1MB (on AWS). * * In general, scan is an expensive operation and should be avoided in favor of query * unless it is necessary to get all items in a table across all or most partitions. */ scan | undefined = undefined>(request?: EntityScanOptions): Promise>; index | undefined = undefined, const IndexSort extends IndexCompositeKeyPart | undefined = undefined>(name: Name, options: EntityIndexOptions): EntityIndexMapper; stream(name: Name, options: EntityStreamOptions, handler: EntityStreamHandler): EntityStream; stream(name: string, handler: EntityStreamHandler): EntityStream; batchStream(name: Name, options: EntityStreamOptions, handler: EntityBatchStreamHandler): EntityBatchStream; batchStream(name: string, handler: EntityBatchStreamHandler): EntityBatchStream; } export declare const Entity: { transactWrite: (items: EntityTransactItem[]) => Promise; }; /** * Tries the {@link Attributes} type to the computed output of the object. * * TODO: extend this type to support intersection and union. */ export type ZodAttributesObject = z.ZodObject; export interface EntityOptions, Sort extends EntityCompositeKeyPart | undefined = undefined> { attributes: ZodAttributesObject | EntityZodShape; partition: Partition; sort?: Sort; } /** * Creates an entity which holds data. * * An entity's keys are made up of one or more attributes in the entity. * When an entity's key is made up of more than one attribute, it is considered to be a composite key. * * Each attribute of the composite key is considered to be either a partition key or a sort key, which we consider a composite key part. * Each entity is required to at least have one partition key attribute, but may have may partition and or sort key attributes. * To retrieve a single value with an entity, the entire composite key must be used, until the query operation is used to return multiple entities (within a partition). * * A partition key separates data within an entity. When using the Query operation, data can only be queried within * a single partition. * * A sort key determines the order of the values when running a query. It also allows for ranges of values to be queried * using only some of the sort key attributes (in order). * * ```ts * // lets take an example where we have posts for a user, separated by forum. * const userComments = entity("userComments", { * attributes: { * forum: z.string(), * userId: z.string(), * postId: z.string(), * commentId: z.string(), * message: z.string() * }, * partition: ["forum", "userId"], * sort: ["postId", "id"], * }); * * // add a new post comment * await userComments.put({ * forum: "games", * userId: "1", * postId: "100", * commentId: "abc", * message: "I love games" * }); * * // get all comments for a user in a forum * await userComments.query({ * forum: "games", // required in the query * userId: "1", // required in the query * }); * * // get all comments for a user in a forum and a post * await userComments.query({ * forum: "games", // required in the query * userId: "1", // required in the query * post: "100", // optional in the query * }); * * // get a single post * await userComments.get({ * forum: "games", * userId: "1", * postId: "100", * commentId: "abc" * }); * ``` */ export declare function entity, const Sort extends EntityCompositeKeyPart | undefined = undefined>(name: Name, options: EntityOptions): Entity; export type EntityIndexOptions | undefined = undefined, Sort extends IndexCompositeKeyPart | undefined = undefined> = { partition: Partition; sort?: Sort; } | { sort: Sort; }; export type EntityIndexMapper = EntityCompositeKeyPart, IndexPartition extends IndexCompositeKeyPart | undefined = undefined, Sort extends IndexCompositeKeyPart | undefined = undefined> = IndexPartition extends undefined ? EntityIndex : EntityIndex, Sort>; /** * An index's key attributes are never undefined. */ export type EntityIndexAttributes = IndexCompositeKeyPart, Sort extends IndexCompositeKeyPart | undefined = IndexCompositeKeyPart | undefined> = { [k in keyof Attr]: k extends KeyAttributes ? Exclude : Attr[k]; }; export interface EntityIndex = IndexCompositeKeyPart, Sort extends IndexCompositeKeyPart | undefined = IndexCompositeKeyPart | undefined, IndexAttr extends EntityIndexAttributes = EntityIndexAttributes> extends EntityIndexSpec { kind: "EntityIndex"; query | undefined = undefined>(request?: EntityScanOptions): Promise>; } export interface EntityQueryResult | undefined = undefined> { entries?: EntityWithMetadata>[]; /** * Returned when there are more values than the limit allowed to return. */ nextToken?: string; } export interface EntityReadOptions { /** * when consistent read is false or undefined, a query or scan may not include the latest changes to the entity values. * Setting consistent read to true will increase the cost of the read and may take longer to return. */ consistentRead?: boolean; } export type EntityQuerySelect = readonly (keyof Attr)[]; export interface EntityScanOptions | undefined = undefined> extends EntityReadOptions { /** * Number of items to retrieve * @default 100 */ limit?: number; nextToken?: string; /** * Attributes to return in the query or scan. * * @default - all attributes are returned for each item in the query/scan. */ select?: Select; } export interface EntityQueryOptions | undefined = undefined> extends EntityScanOptions { /** * Determines the direction of the items returned in the query based on the sort key. * * @default ASC - ascending order */ direction?: "ASC" | "DESC"; } export interface EntityConsistencyOptions { /** * The expected version of the entity in the entity. * * Used to support consistent writes and deletes. * A value of 0 will only pass if the item is new. */ expectedVersion?: number; } export interface EntityPutOptions extends EntityConsistencyOptions { /** * Whether or not to update the version on change. * If this is the first time the value has been set, it will be set to 1. * * @default true - version will be incremented. */ incrementVersion?: boolean; } export interface EntityWithMetadata { value: Attr; version: number; } export type SelectedAttributes | undefined> = Select extends EntityQuerySelect ? Pick : Attr; interface EntityTransactItemBase, Sort extends EntityCompositeKeyPart | undefined> { entity: Entity | string; } export type EntityTransactItem = EntityCompositeKeyPart, Sort extends EntityCompositeKeyPart | undefined = EntityCompositeKeyPart | undefined> = EntityTransactPutOperation | EntityTransactDeleteOperation | EntityTransactConditionalOperation; export interface EntityTransactPutOperation = EntityCompositeKeyPart, Sort extends EntityCompositeKeyPart | undefined = EntityCompositeKeyPart | undefined> extends EntityTransactItemBase { operation: "put"; value: Attr; options?: EntityPutOptions; } export interface EntityTransactDeleteOperation = EntityCompositeKeyPart, Sort extends EntityCompositeKeyPart | undefined = EntityCompositeKeyPart | undefined> extends EntityTransactItemBase { operation: "delete"; key: CompositeKey; options?: EntityPutOptions; } /** * Used in transactions, cancels the transaction if the key's version does not match. */ export interface EntityTransactConditionalOperation = EntityCompositeKeyPart, Sort extends EntityCompositeKeyPart | undefined = EntityCompositeKeyPart | undefined> extends EntityTransactItemBase { operation: "condition"; key: CompositeKey; version?: number; } export {}; //# sourceMappingURL=entity.d.ts.map