UNPKG

5.01 kBTypeScriptView Raw
1import { Getter } from '@loopback/core';
2import { Filter } from '@loopback/filter';
3import { TypeResolver } from '../../';
4import { Count, DataObject, Options } from '../../common-types';
5import { Entity } from '../../model';
6import { EntityCrudRepository } from '../../repositories';
7/**
8 * CRUD operations for a target repository of a HasMany relation
9 */
10export interface HasOneRepository<Target extends Entity> {
11 /**
12 * Create a target model instance
13 * @param targetModelData - The target model data
14 * @param options - Options for the operation
15 * options.polymorphicType - If polymorphic target model,
16 * specify of which concrete model the created instance should be
17 * @returns A promise which resolves to the newly created target model instance
18 */
19 create(targetModelData: DataObject<Target>, options?: Options & {
20 polymorphicType?: string;
21 }): Promise<Target>;
22 /**
23 * Find the only target model instance that belongs to the declaring model.
24 * @param filter - Query filter without a Where condition
25 * @param options - Options for the operations
26 * options.polymorphicType - a string or a string array of polymorphic type names
27 * to specify which repositories should are expected to be searched
28 * It is highly recommended to contain this param especially for
29 * datasources using deplicated ids across tables
30 * @returns A promise resolved with the target object or rejected
31 * with an EntityNotFoundError when target model instance was not found.
32 */
33 get(filter?: Pick<Filter<Target>, Exclude<keyof Filter<Target>, 'where'>>, options?: Options & {
34 polymorphicType?: string | string[];
35 }): Promise<Target>;
36 /**
37 * Delete the related target model instance
38 * @param options
39 * options.polymorphicType - a string or a string array of polymorphic type names
40 * to specify which repositories should are expected to be searched
41 * It is highly recommended to contain this param especially for
42 * datasources using deplicated ids across tables
43 * @returns A promise which resolves the deleted target model instances
44 */
45 delete(options?: Options & {
46 polymorphicType?: string | string[];
47 }): Promise<Count>;
48 /**
49 * Patch the related target model instance
50 * @param dataObject - The target model fields and their new values to patch
51 * If the target models are of different types, this should be a dictionary
52 * @param options
53 * options.isPolymorphic - whether dataObject is a dictionary
54 * @returns A promise which resolves the patched target model instances
55 */
56 patch(dataObject: DataObject<Target> | {
57 [polymorphicType: string]: DataObject<Target>;
58 }, options?: Options & {
59 isPolymorphic?: boolean;
60 }): Promise<Count>;
61}
62export declare class DefaultHasOneRepository<TargetEntity extends Entity, TargetID, TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>> implements HasOneRepository<TargetEntity> {
63 getTargetRepository: Getter<TargetRepository> | {
64 [repoType: string]: Getter<TargetRepository>;
65 };
66 constraint: DataObject<TargetEntity>;
67 targetResolver: TypeResolver<Entity, typeof Entity>;
68 /**
69 * Constructor of DefaultHasOneEntityCrudRepository
70 * @param getTargetRepository - either a dictionary of target model type - target repository instance
71 * or a single target repository instance
72 * e.g. if the target is of a non-polymorphic type "Student", put the studentRepositoryGetterInstance
73 * if the target is of a polymorphic type "Person" which can be either a "Student" or a "Teacher"
74 * then put "{Student: studentRepositoryGetterInstance, Teacher: teacherRepositoryGetterInstance}"
75 * @param constraint - the key value pair representing foreign key name to constrain
76 * the target repository instance
77 * @param targetResolver - () => Target to resolve the target class
78 * e.g. if the target is of type "Student", then put "() => Student"
79 */
80 constructor(getTargetRepository: Getter<TargetRepository> | {
81 [repoType: string]: Getter<TargetRepository>;
82 }, constraint: DataObject<TargetEntity>, targetResolver: TypeResolver<Entity, typeof Entity>);
83 getTargetRepositoryDict: {
84 [repoType: string]: Getter<TargetRepository>;
85 };
86 create(targetModelData: DataObject<TargetEntity>, options?: Options & {
87 polymorphicType?: string;
88 }): Promise<TargetEntity>;
89 get(filter?: Pick<Filter<TargetEntity>, Exclude<keyof Filter<TargetEntity>, 'where'>>, options?: Options & {
90 polymorphicType?: string | string[];
91 }): Promise<TargetEntity>;
92 delete(options?: Options & {
93 polymorphicType?: string | string[];
94 }): Promise<Count>;
95 patch(dataObject: DataObject<TargetEntity> | {
96 [polymorphicType: string]: DataObject<TargetEntity>;
97 }, options?: Options & {
98 isPolymorphic?: boolean;
99 }): Promise<Count>;
100}