import type { SetTypeSubArg } from '@aws-amplify/data-schema-types'; import { type PrimaryIndexIrShape, type SecondaryIndexIrShape } from './util'; import type { InternalField, BaseModelField } from './ModelField'; import type { ModelRelationshipField, InternalRelationshipField, ModelRelationshipFieldParamShape } from './ModelRelationshipField'; import { type Authorization, type BaseAllowModifier, type AnyAuthorization } from './Authorization'; import type { RefType, RefTypeParamShape } from './RefType'; import type { EnumType } from './EnumType'; import type { CustomType, CustomTypeParamShape } from './CustomType'; import { type ModelIndexType, type InternalModelIndexType } from './ModelIndex'; import type { PrimaryIndexFieldsToIR, SecondaryIndexToIR } from './MappedTypes/MapIndexes'; import type { brandSymbol } from './util/Brand.js'; import type { methodKeyOf } from './util/usedMethods.js'; declare const brandName = "modelType"; export type deferredRefResolvingPrefix = 'deferredRefResolving:'; type ModelFields = Record | RefType | EnumType | CustomType>; type InternalModelFields = Record; export type DisableOperationsOptions = 'queries' | 'mutations' | 'subscriptions' | 'list' | 'get' | 'create' | 'update' | 'delete' | 'onCreate' | 'onUpdate' | 'onDelete'; type ModelData = { fields: ModelFields; identifier: ReadonlyArray; secondaryIndexes: ReadonlyArray>; authorization: Authorization[]; disabledOperations: ReadonlyArray; }; type InternalModelData = ModelData & { fields: InternalModelFields; identifier: ReadonlyArray; secondaryIndexes: ReadonlyArray; authorization: Authorization[]; disabledOperations: ReadonlyArray; originalName?: string; }; export type ModelTypeParamShape = { fields: ModelFields; identifier: PrimaryIndexIrShape; secondaryIndexes: ReadonlyArray; authorization: Authorization[]; disabledOperations: ReadonlyArray; }; /** * Extract fields that are eligible to be PK or SK fields with their resolved type. * * Eligible fields include: * 1. ModelField that contains string or number * 2. inline EnumType * 3. RefType that refers to a top level defined EnumType (this is enforced by * validation that happens in the Schema Processor) * * NOTE: at this point, there is no way to resolve the type from a RefType as * we don't have access to the NonModelType at this location. So we generate am * indicator string, and resolve its corresponding type later in * packages/data-schema/src/runtime/client/index.ts */ export type ExtractSecondaryIndexIRFields = { [FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends BaseModelField ? NonNullable extends string | number ? FieldProp : never : T['fields'][FieldProp] extends EnumType | RefType ? FieldProp : never]: T['fields'][FieldProp] extends BaseModelField ? R : T['fields'][FieldProp] extends EnumType ? values[number] : T['fields'][FieldProp] extends RefType ? `${deferredRefResolvingPrefix}${R['link']}` : never; }; export type AddRelationshipFieldsToModelTypeFields>> = Model extends ModelType ? ModelType, HiddenKeys> : never; export type BaseModelType = ModelType; export type UsableModelTypeKey = methodKeyOf; /** * Model type definition interface * * @param T - The shape of the model type * @param UsedMethod - The method keys already defined */ export type ModelType = Omit<{ [brandSymbol]: typeof brandName; /** * Defines single-field or composite identifiers, the fields must be marked required * * @param identifier A list of field names used as identifiers for the data model * @returns A ModelType instance with updated identifiers * * @example * a.model({ * name: a.string().required(), * email: a.string().required(), * age: a.integer(), * }).identifier(['name', 'email']) */ identifier, PrimaryIndexPool extends string = keyof PrimaryIndexFields & string, const ID extends ReadonlyArray = readonly [], const PrimaryIndexIR extends PrimaryIndexIrShape = PrimaryIndexFieldsToIR>(identifier: ID): ModelType, UsedMethod | 'identifier'>; /** * Adds secondary index for a model, secondary index consists of a "hash key" and optionally, a "sort key" * * @param callback A function that specifies "hash key" and "sort key" * @returns A ModelType instance with updated secondary index * * @example * a.model().secondaryIndexes((index) => [index('type').sortKeys(['sort'])]) * * @see [Amplify documentation for secondary indexes](https://docs.amplify.aws/react/build-a-backend/data/data-modeling/secondary-index/) * @see [Amazon DynamoDB documentation for secondary indexes](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html) */ secondaryIndexes, const SecondaryIndexPKPool extends string = keyof SecondaryIndexFields & string, const Indexes extends readonly ModelIndexType[] = readonly [], const IndexesIR extends readonly any[] = SecondaryIndexToIR>(callback: (index: (pk: PK) => ModelIndexType>>) => Indexes): ModelType, UsedMethod | 'secondaryIndexes'>; /** * Disables the specified operations for the model * * @param ops A list of operations to be disabled * @returns A ModelType instance with updated disabled operations * * @example * a.model().disableOperations(['delete', 'update', 'queries', 'subscriptions']) * * @see [Amplify Data documentation for supported operations](https://docs.amplify.aws/react/build-a-backend/data/) */ disableOperations>(ops: Ops): ModelType, UsedMethod | 'disableOperations'>; /** * Configures authorization rules for public, signed-in user, per user, and per user group data access * * @param callback A function that receives an allow modifier to define authorization rules * @returns A ModelType instance with updated authorization rules * * @example * a.model().authorization((allow) => [ * allow.guest(), * allow.publicApiKey(), * allow.authenticated(), * ]) */ authorization(callback: (allow: BaseAllowModifier) => AuthRuleType | AuthRuleType[]): ModelType, UsedMethod | 'authorization'>; }, UsedMethod>; /** * External representation of Model Type that exposes the `relationships` modifier. * Used on the complete schema object. */ export type SchemaModelType, ModelName extends string = string, IsRDS extends boolean = false> = IsRDS extends true ? T & { relationships> = Record>(relationships: Param): Record; fields: T extends ModelType ? R['fields'] : never; } : T; /** * Internal representation of Model Type that exposes the `data` property. * Used at buildtime. */ export type InternalModel = SchemaModelType, string, true> & { data: InternalModelData; }; /** * Model Type type guard * @param modelType - api-next ModelType * @returns true if the given value is a ModelSchema */ export declare const isSchemaModelType: (modelType: any | SchemaModelType) => modelType is SchemaModelType; /** * Model default identifier * * @param pk - primary key * @param sk - secondary key * @param compositeSk - composite secondary key */ export type ModelDefaultIdentifier = { pk: { readonly id: string; }; sk: never; compositeSk: never; }; /** * A data model that creates a matching Amazon DynamoDB table and provides create, read (list and get), update, * delete, and subscription APIs. * * @param fields database table fields. Supports scalar types and relationship types. * @returns a data model definition */ export declare function model(fields: T): ModelType<{ fields: T; identifier: ModelDefaultIdentifier; secondaryIndexes: []; authorization: []; disabledOperations: []; }>; export {};