@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
66 lines • 3.14 kB
TypeScript
import type { BaseItem } from "./BaseItem.js";
import type { NestDepthMax32, IterateNestDepthMax32 } from "./NestDepth.js";
import type { SupportedAttributeValueType } from "./SupportedAttributeValueType.js";
import type { AnyValidAttributeConfig, ModelSchemaAttributeConfig } from "../Schema/types/index.js";
import type { Simplify, Except, IsAny, IsUnknown, IsNever, IsTuple, UnionToTuple } from "type-fest";
/**
* This generic creates a `Schema` type from the provided item-type `T`, the
* keys of which _**must**_ reflect attribute names — not aliases.
*/
export type ItemTypeToSchema<T extends BaseItem> = MapItemToSchema<T, 0>;
/**
* Maps the item-type `T` to a `Schema` type with corresponding attribute-configs.
*/
type MapItemToSchema<T extends BaseItem, NestDepth extends NestDepthMax32> = {
readonly [Key in keyof T]-?: ItemValueToAttrConfig<T[Key], NestDepth>;
};
/**
* Returns the attribute-config types for the given `Value`.
*/
type ItemValueToAttrConfig<Value, NestDepth extends NestDepthMax32> = CanAscertainValueSchemaType<Value> extends false ? never : Simplify<Except<NestDepth extends 0 ? AnyValidAttributeConfig : ModelSchemaAttributeConfig, "type" | "oneOf" | "schema" | "required" | "nullable"> & TypeAttrConfigForValue<NonNullable<Value>, NestDepth> & RequiredAttrConfigForValue<Value> & NullableAttrConfigForValue<Value>>;
/**
* Returns the `type`, `oneOf`, and `schema` attribute-config types for the given `Value`.
*/
type TypeAttrConfigForValue<Value extends NonNullable<SupportedAttributeValueType>, NestDepth extends NestDepthMax32> = Value extends string ? string extends Value ? {
readonly type: "string";
} : {
readonly type: "enum";
readonly oneOf: UnionToTuple<Value>;
} : Value extends number ? {
readonly type: "number";
} : Value extends boolean ? {
readonly type: "boolean";
} : Value extends Date ? {
readonly type: "Date";
} : Value extends Buffer ? {
readonly type: "Buffer";
} : NestDepth extends 32 ? never : Value extends Array<infer El> ? {
readonly type: IsTuple<Value> extends true ? "tuple" : "array";
readonly schema: [ItemValueToAttrConfig<El, IterateNestDepthMax32<NestDepth>>];
} : Value extends BaseItem ? {
readonly type: "map";
readonly schema: MapItemToSchema<Value, IterateNestDepthMax32<NestDepth>>;
} : never;
/**
* Returns the `required` attribute-config type for the given `Value`.
*/
type RequiredAttrConfigForValue<Value> = undefined extends Value ? {
readonly required?: false;
} : {
readonly required: true;
};
/**
* Returns the `nullable` attribute-config type for the given `Value`.
*/
type NullableAttrConfigForValue<Value> = null extends Value ? {
readonly nullable: true;
} : {
readonly nullable?: false;
};
/**
* A value's `Schema` cannot be ascertained if it is `any`, `unknown`, or `never`.
* This generic returns `false` if the type is one of those, and `true` otherwise.
*/
type CanAscertainValueSchemaType<T> = IsAny<T> extends true ? false : IsUnknown<T> extends true ? false : IsNever<T> extends true ? false : true;
export {};
//# sourceMappingURL=ItemTypeToSchema.d.ts.map