import { CloneOptions } from '../stores/index.js'; import { AnyData, PatchParams } from '../types.js'; export interface BaseModelData { /** * Indicates if this instance is a clone.. It will be * * - `true` after calling `instance.clone()` * - `false` after calling `instance.commit()`. * * Should not be set manually when creating new instances. */ __isClone?: boolean; /** * If no idField is specified, `__tempId` can be manually specified, otherwise a random one will be added for you. * The automatically-added `__tempId` values are valid ObjectId strings. */ __tempId?: string; } export interface StoreInstanceProps { /** * The name of the Model function */ readonly __modelName: string; readonly __isClone: boolean; /** * The attribute on the data holding the unique id property. It will match whatever was provided in the Model options. * This value should match the API service. General `idField` values are as follows: * * - `id` for SQL databases * - `_id` for MongoDB */ readonly __idField: string; readonly __tempId: string; /** * A boolean indicating if the instance is a temp. Will be `true` if the instance does not have an idField. This is * the only reliable way to determine if a record is a temp or not, since after calling `temp.save`, the temp will * have both a `__tempId` and a real idField value. */ readonly __isTemp: boolean; /** * Returns the item's clone from the store, if one exists. */ hasClone(this: N): N | null; /** * Creates a copy of an item or temp record. The copy will have `__isClone` set to `true` and will be added to the * Model's clone storage. If not already stored, the original item will be added to the appropriate store. * @param data * @param options */ clone(this: N, data?: Partial, options?: CloneOptions): N; /** * Copies a clone's data onto the original item or temp record. * @param data * @param options */ commit(this: N, data?: Partial, options?: CloneOptions): N; /** * Resets a clone's data to match the original item or temp record. If additional properties were added to the clone, * they will be removed to exactly match the original. * @param data * @param options */ reset(this: N, data?: Partial, options?: CloneOptions): N; /** * Adds the current instance to the appropriate store. If the instance is a clone, it will be added to `clones`. If it * has an `idField`, it will be added to items, otherwise it will be added to temps. */ createInStore(this: N): N; /** * Removes the current instance from items, temps, and clones. */ removeFromStore(this: N): N; } export type ModelInstanceData = Partial; export type ModelInstance = ModelInstanceData & StoreInstanceProps; export interface ServiceInstanceProps = PatchParams> { readonly isSavePending: boolean; readonly isCreatePending: boolean; readonly isPatchPending: boolean; readonly isRemovePending: boolean; save: (this: N, params?: P) => Promise; create: (this: ModelInstance, params?: P) => Promise; patch: (this: ModelInstance, params?: P) => Promise; remove: (this: ModelInstance, params?: P) => Promise; } export type ServiceInstance = ModelInstanceData & StoreInstanceProps & ServiceInstanceProps;