@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
127 lines • 7.31 kB
TypeScript
import type { OmittedSdkParameters } from "./OmittedSdkParameters.js";
import type { UnmarshalledExpressionAttributeValues, UnmarshalledKeysAndAttributes, UnmarshalledBatchWriteRequest, UnmarshalledTransactWriteItem, UnmarshalledItemCollectionMetrics } from "./unmarshalledSdkFieldTypes.js";
import type { BaseItem, ItemKeys, OverrideSharedProperties, FixPartialUndefined } from "../../types/index.js";
import type { AttributeValue, KeysAndAttributes, WriteRequest, ItemCollectionMetrics, TransactWriteItem, __MetadataBearer as MetadataBearer } from "@aws-sdk/client-dynamodb";
import type { Simplify } from "type-fest";
/**
* This type is used to organize the mapping between MARSHALLED and UNMARSHALLED
* types for two kinds of SDK fields:
*
* 1. SDK params that must be MARSHALLED before they can be used to create an SDK Command instance
* 2. SDK response fields that must be UNMARSHALLED after the SDK Command has been executed
*/
type MarshalledSdkFieldMeta<MarshalledTypeFromSDK = unknown, UnmarshalledType = unknown> = {
MARSHALLED: MarshalledTypeFromSDK;
UNMARSHALLED: UnmarshalledType;
};
/**
* ### SDK PARAMS THAT REQUIRE MARSHALLING
*
* This type is a dictionary of SDK params that must be **MARSHALLED** before they can be used to
* create an SDK Command instance. Each param is mapped to a {@link MarshalledSdkFieldMeta} type
* that defines its **MARSHALLED** and **UNMARSHALLED** types.
*/
type SdkParamsThatRequireMarshalling<SdkInput extends object> = {
Key: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
ItemKeys>;
Item: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
BaseItem>;
ExpressionAttributeValues?: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
UnmarshalledExpressionAttributeValues>;
ExclusiveStartKey?: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
ItemKeys>;
TransactItems: MarshalledSdkFieldMeta<Array<TransactWriteItem>, // internal marshalled type from SDK
Array<UnmarshalledTransactWriteItem>>;
RequestItems: "RequestItems" extends keyof SdkInput ? Record<string, Array<Record<keyof WriteRequest, any>>> extends SdkInput["RequestItems"] ? MarshalledSdkFieldMeta<{
[tableName: string]: Array<WriteRequest>;
}, // internal marshalled type from SDK
{
[tableName: string]: Array<UnmarshalledBatchWriteRequest>;
}> : Record<string, Record<keyof KeysAndAttributes, any>> extends SdkInput["RequestItems"] ? MarshalledSdkFieldMeta<{
[tableName: string]: FixPartialUndefined<KeysAndAttributes>;
}, // internal marshalled type from SDK
{
[tableName: string]: UnmarshalledKeysAndAttributes;
}> : never : never;
};
/**
* Union of all SDK parameters that must be MARSHALLED before they can be used to create an SDK Command instance.
*/
export type SdkParameterThatRequiresMarshalling = Simplify<keyof SdkParamsThatRequireMarshalling<any>>;
/**
* ### SDK RESPONSE-FIELDS THAT REQUIRE UNMARSHALLING
*
* This type is a dictionary of SDK response-fields that must be **UNMARSHALLED** after an SDK
* Command has been executed. Each response-field is mapped to a {@link MarshalledSdkFieldMeta}
* type that defines its respective **MARSHALLED** and **UNMARSHALLED** types.
*/
type SdkResponseFieldsThatRequireUnmarshalling<SdkOutput extends object> = {
Item?: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
BaseItem>;
Items?: MarshalledSdkFieldMeta<Array<Record<string, AttributeValue>>, // internal marshalled type from SDK
Array<BaseItem>>;
Attributes?: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
BaseItem>;
LastEvaluatedKey?: MarshalledSdkFieldMeta<Record<string, AttributeValue>, // internal marshalled type from SDK
ItemKeys>;
Responses?: MarshalledSdkFieldMeta<{
[tableName: string]: Array<Record<string, AttributeValue>>;
}, // internal marshalled type from SDK
{
[tableName: string]: Array<BaseItem>;
}>;
UnprocessedKeys?: MarshalledSdkFieldMeta<{
[tableName: string]: KeysAndAttributes;
}, // internal marshalled type from SDK
{
[tableName: string]: UnmarshalledKeysAndAttributes;
}>;
UnprocessedItems?: MarshalledSdkFieldMeta<{
[tableName: string]: Array<WriteRequest>;
}, // internal marshalled type from SDK
{
[tableName: string]: Array<UnmarshalledBatchWriteRequest>;
}>;
ItemCollectionMetrics?: "ItemCollectionMetrics" extends keyof SdkOutput ? Record<keyof ItemCollectionMetrics, any> extends keyof SdkOutput["ItemCollectionMetrics"] ? MarshalledSdkFieldMeta<ItemCollectionMetrics, // internal marshalled type from SDK
UnmarshalledItemCollectionMetrics> : MarshalledSdkFieldMeta<{
[tableName: string]: Array<ItemCollectionMetrics>;
}, // internal marshalled type from SDK
{
[tableName: string]: Array<UnmarshalledItemCollectionMetrics>;
}> : never;
};
/**
* Union of all SDK response-fields that must be UNMARSHALLED after an SDK Command has been executed.
*/
export type SdkResponseFieldThatRequiresUnmarshalling = Simplify<keyof SdkResponseFieldsThatRequireUnmarshalling<any>>;
/**
* This generic takes a ddb client Command input or output type and modifies it as follows:
* - Replaces _**marshalled**_ types with _**unmarshalled**_ types, and vice versa
* - Removes all {@link OmittedSdkParameters|deprecated legacy parameters}
* - Fixes the partiality of the resultant type by removing `undefined` from required fields
*/
type ModifySdkType<SdkType extends object, BaseFieldsDict extends {
[fieldName: string]: MarshalledSdkFieldMeta;
}, DictType extends keyof MarshalledSdkFieldMeta> = FixPartialUndefined<OverrideSharedProperties<Omit<SdkType, OmittedSdkParameters>, {
[Field in keyof BaseFieldsDict]: Exclude<BaseFieldsDict[Field], undefined>[DictType];
}>>;
/**
* This generic modifies the provided ddb Command-**input** type to use _**unmarshalled**_ values.
*/
export type ToUnmarshalledSdkInput<SdkInput extends object> = ModifySdkType<SdkInput, SdkParamsThatRequireMarshalling<SdkInput>, "UNMARSHALLED">;
/**
* This generic modifies the provided ddb Command-**input** type to use _**marshalled**_ values.
*/
export type ToMarshalledSdkInput<SdkInput extends object> = ModifySdkType<SdkInput, SdkParamsThatRequireMarshalling<SdkInput>, "MARSHALLED">;
/**
* This generic modifies the provided ddb Command-**output** type to use _**unmarshalled**_ values.
*/
export type ToUnmarshalledSdkOutput<SdkOutput extends object> = Simplify<ModifySdkType<SdkOutput, SdkResponseFieldsThatRequireUnmarshalling<SdkOutput>, "UNMARSHALLED"> & {
$metadata: MetadataBearer["$metadata"];
}>;
/**
* This generic modifies the provided ddb Command-**output** type to use _**marshalled**_ values.
*/
export type ToMarshalledSdkOutput<SdkOutput extends object> = ModifySdkType<SdkOutput, SdkResponseFieldsThatRequireUnmarshalling<SdkOutput>, "MARSHALLED">;
export {};
//# sourceMappingURL=sdkTypeModifiers.d.ts.map