UNPKG

9.54 kBTypeScriptView Raw
1import { Filter, FilterExcludingWhere, Where } from '@loopback/filter';
2import { AnyObject, Command, Count, DataObject, NamedParameters, Options, PositionalParameters } from '../common-types';
3import { DataSource } from '../datasource';
4import { Entity, Model, ValueObject } from '../model';
5import { InclusionResolver } from '../relations/relation.types';
6import { IsolationLevel, Transaction } from '../transaction';
7export interface Repository<T extends Model> {
8}
9export interface ExecutableRepository<T extends Model> extends Repository<T> {
10 /**
11 * Execute a query with the given parameter object or an array of parameters
12 * @param command - The query string or command object
13 * @param parameters - The object with name/value pairs or an array of parameter
14 * values
15 * @param options - Options
16 */
17 execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise<AnyObject>;
18}
19/**
20 * A type for CRUD repositories that are backed by IDs and support
21 * Transactions
22 */
23export type TransactionalEntityRepository<T extends Entity, ID, Relations extends object = {}> = TransactionalRepository<T> & EntityCrudRepository<T, ID>;
24/**
25 * Repository Interface for Repositories that support Transactions
26 *
27 * @typeParam T Generic type for the Entity
28 */
29export interface TransactionalRepository<T extends Entity> extends Repository<T> {
30 /**
31 * Begin a new Transaction
32 * @param options - Options for the operations
33 * @returns Promise<Transaction> Promise that resolves to a new Transaction
34 * object
35 */
36 beginTransaction(options?: IsolationLevel | Options): Promise<Transaction>;
37}
38/**
39 * Basic CRUD operations for ValueObject and Entity. No ID is required.
40 */
41export interface CrudRepository<T extends ValueObject | Entity, Relations extends object = {}> extends Repository<T> {
42 /**
43 * Create a new record
44 * @param dataObject - The data to be created
45 * @param options - Options for the operations
46 * @returns A promise of record created
47 */
48 create(dataObject: DataObject<T>, options?: Options): Promise<T>;
49 /**
50 * Create all records
51 * @param dataObjects - An array of data to be created
52 * @param options - Options for the operations
53 * @returns A promise of an array of records created
54 */
55 createAll(dataObjects: DataObject<T>[], options?: Options): Promise<T[]>;
56 /**
57 * Find matching records
58 * @param filter - Query filter
59 * @param options - Options for the operations
60 * @returns A promise of an array of records found
61 */
62 find(filter?: Filter<T>, options?: Options): Promise<(T & Relations)[]>;
63 /**
64 * Updating matching records with attributes from the data object
65 * @param dataObject - The data to be updated
66 * @param where - Matching criteria
67 * @param options - Options for the operations
68 * @returns A promise of number of records updated
69 */
70 updateAll(dataObject: DataObject<T>, where?: Where<T>, options?: Options): Promise<Count>;
71 /**
72 * Delete matching records
73 * @param where - Matching criteria
74 * @param options - Options for the operations
75 * @returns A promise of number of records deleted
76 */
77 deleteAll(where?: Where<T>, options?: Options): Promise<Count>;
78 /**
79 * Count matching records
80 * @param where - Matching criteria
81 * @param options - Options for the operations
82 * @returns A promise of number of records matched
83 */
84 count(where?: Where<T>, options?: Options): Promise<Count>;
85}
86/**
87 * Base interface for a repository of entities
88 */
89export interface EntityRepository<T extends Entity, ID> extends ExecutableRepository<T> {
90}
91/**
92 * CRUD operations for a repository of entities
93 */
94export interface EntityCrudRepository<T extends Entity, ID, Relations extends object = {}> extends EntityRepository<T, ID>, CrudRepository<T, Relations> {
95 entityClass: typeof Entity & {
96 prototype: T;
97 };
98 inclusionResolvers: Map<string, InclusionResolver<T, Entity>>;
99 /**
100 * Save an entity. If no id is present, create a new entity
101 * @param entity - Entity to be saved
102 * @param options - Options for the operations
103 * @returns A promise that will be resolve if the operation succeeded or will
104 * be rejected if the entity was not found.
105 */
106 save(entity: DataObject<T>, options?: Options): Promise<T>;
107 /**
108 * Update an entity
109 * @param entity - Entity to be updated
110 * @param options - Options for the operations
111 * @returns A promise that will be resolve if the operation succeeded or will
112 * be rejected if the entity was not found.
113 */
114 update(entity: DataObject<T>, options?: Options): Promise<void>;
115 /**
116 * Delete an entity
117 * @param entity - Entity to be deleted
118 * @param options - Options for the operations
119 * @returns A promise that will be resolve if the operation succeeded or will
120 * be rejected if the entity was not found.
121 */
122 delete(entity: DataObject<T>, options?: Options): Promise<void>;
123 /**
124 * Find an entity by id, return a rejected promise if not found.
125 *
126 * @remarks
127 *
128 * The rationale behind findById is to find an instance by its primary key
129 * (id). No other search criteria than id should be used. If a client wants
130 * to use a `where` clause beyond id, use `find` or `findOne` instead.
131 *
132 * @param id - Value for the entity id
133 * @param filter - Additional query options. E.g. `filter.include` configures
134 * which related models to fetch as part of the database query (or queries).
135 * @param options - Options for the operations
136 * @returns A promise of an entity found for the id
137 */
138 findById(id: ID, filter?: FilterExcludingWhere<T>, options?: Options): Promise<T & Relations>;
139 /**
140 * Update an entity by id with property/value pairs in the data object
141 * @param id - Value for the entity id
142 * @param data - Data attributes to be updated
143 * @param options - Options for the operations
144 * @returns A promise that will be resolve if the operation succeeded or will
145 * be rejected if the entity was not found.
146 */
147 updateById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
148 /**
149 * Replace an entity by id
150 * @param id - Value for the entity id
151 * @param data - Data attributes to be replaced
152 * @param options - Options for the operations
153 * @returns A promise that will be resolve if the operation succeeded or will
154 * be rejected if the entity was not found.
155 */
156 replaceById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
157 /**
158 * Delete an entity by id
159 * @param id - Value for the entity id
160 * @param options - Options for the operations
161 * @returns A promise that will be resolve if the operation succeeded or will
162 * be rejected if the entity was not found.
163 */
164 deleteById(id: ID, options?: Options): Promise<void>;
165 /**
166 * Check if an entity exists for the given id
167 * @param id - Value for the entity id
168 * @param options - Options for the operations
169 * @returns Promise<true> if an entity exists for the id, otherwise
170 * Promise<false>
171 */
172 exists(id: ID, options?: Options): Promise<boolean>;
173}
174/**
175 * Repository implementation
176 *
177 * @example
178 *
179 * User can import `CrudRepositoryImpl` and call its functions like:
180 * `CrudRepositoryImpl.find(somefilters, someoptions)`
181 *
182 * Or extend class `CrudRepositoryImpl` and override its functions:
183 * ```ts
184 * export class TestRepository extends CrudRepositoryImpl<Test> {
185 * constructor(dataSource: DataSource, model: Test) {
186 * super(dataSource, Customer);
187 * }
188 *
189 * // Override `deleteAll` to disable the operation
190 * deleteAll(where?: Where, options?: Options) {
191 * return Promise.reject(new Error('deleteAll is disabled'));
192 * }
193 * }
194 * ```
195 */
196export declare class CrudRepositoryImpl<T extends Entity, ID> implements EntityCrudRepository<T, ID> {
197 dataSource: DataSource;
198 entityClass: typeof Entity & {
199 prototype: T;
200 };
201 private connector;
202 readonly inclusionResolvers: Map<string, InclusionResolver<T, Entity>>;
203 constructor(dataSource: DataSource, entityClass: typeof Entity & {
204 prototype: T;
205 });
206 private toModels;
207 private toModel;
208 create(entity: DataObject<T>, options?: Options): Promise<T>;
209 createAll(entities: DataObject<T>[], options?: Options): Promise<T[]>;
210 save(entity: DataObject<T>, options?: Options): Promise<T>;
211 find(filter?: Filter<T>, options?: Options): Promise<T[]>;
212 findById(id: ID, filter?: FilterExcludingWhere<T>, options?: Options): Promise<T>;
213 update(entity: DataObject<T>, options?: Options): Promise<void>;
214 delete(entity: DataObject<T>, options?: Options): Promise<void>;
215 updateAll(data: DataObject<T>, where?: Where<T>, options?: Options): Promise<Count>;
216 updateById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
217 replaceById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
218 deleteAll(where?: Where<T>, options?: Options): Promise<Count>;
219 deleteById(id: ID, options?: Options): Promise<void>;
220 count(where?: Where<T>, options?: Options): Promise<Count>;
221 exists(id: ID, options?: Options): Promise<boolean>;
222 execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise<AnyObject>;
223}