1 | import { Getter } from '@loopback/core';
|
2 | import { Filter, FilterExcludingWhere, InclusionFilter, Where } from '@loopback/filter';
|
3 | import legacy from 'loopback-datasource-juggler';
|
4 | import { AnyObject, Command, Count, DataObject, NamedParameters, Options, PositionalParameters } from '../common-types';
|
5 | import { Entity, Model } from '../model';
|
6 | import { BelongsToAccessor, HasManyRepositoryFactory, HasManyThroughRepositoryFactory, HasOneRepositoryFactory, InclusionResolver, ReferencesManyAccessor } from '../relations';
|
7 | import { IsolationLevel, Transaction } from '../transaction';
|
8 | import { EntityCrudRepository, TransactionalEntityRepository } from './repository';
|
9 | export declare namespace juggler {
|
10 | export import DataSource = legacy.DataSource;
|
11 | export import ModelBase = legacy.ModelBase;
|
12 | export import ModelBaseClass = legacy.ModelBaseClass;
|
13 | export import PersistedModel = legacy.PersistedModel;
|
14 | export import KeyValueModel = legacy.KeyValueModel;
|
15 | export import PersistedModelClass = legacy.PersistedModelClass;
|
16 | export import Transaction = legacy.Transaction;
|
17 | export import IsolationLevel = legacy.IsolationLevel;
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export declare function bindModel<T extends juggler.ModelBaseClass>(modelClass: T, ds: juggler.DataSource): T;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export declare function ensurePromise<T>(p: legacy.PromiseOrVoid<T>): Promise<T>;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export declare class DefaultCrudRepository<T extends Entity, ID, Relations extends object = {}> implements EntityCrudRepository<T, ID, Relations> {
|
37 | entityClass: typeof Entity & {
|
38 | prototype: T;
|
39 | };
|
40 | dataSource: juggler.DataSource;
|
41 | modelClass: juggler.PersistedModelClass;
|
42 | readonly inclusionResolvers: Map<string, InclusionResolver<T, Entity>>;
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | constructor(entityClass: typeof Entity & {
|
49 | prototype: T;
|
50 | }, dataSource: juggler.DataSource);
|
51 | private ensurePersistedModel;
|
52 | /**
|
53 | * Creates a legacy persisted model class, attaches it to the datasource and
|
54 | * returns it. This method can be overridden in sub-classes to acess methods
|
55 | * and properties in the generated model class.
|
56 | * @param entityClass - LB4 Entity constructor
|
57 | */
|
58 | protected definePersistedModel(entityClass: typeof Model): typeof juggler.PersistedModel;
|
59 | private resolvePropertyType;
|
60 | /**
|
61 | * @deprecated
|
62 | * Function to create a constrained relation repository factory
|
63 | *
|
64 | * Use `this.createHasManyRepositoryFactoryFor()` instead
|
65 | *
|
66 | * @param relationName - Name of the relation defined on the source model
|
67 | * @param targetRepo - Target repository instance
|
68 | */
|
69 | protected _createHasManyRepositoryFactoryFor<Target extends Entity, TargetID, ForeignKeyType>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>): HasManyRepositoryFactory<Target, ForeignKeyType>;
|
70 | /**
|
71 | * Function to create a constrained relation repository factory
|
72 | *
|
73 | * @example
|
74 | * ```ts
|
75 | * class CustomerRepository extends DefaultCrudRepository<
|
76 | * Customer,
|
77 | * typeof Customer.prototype.id,
|
78 | * CustomerRelations
|
79 | * > {
|
80 | * public readonly orders: HasManyRepositoryFactory<Order, typeof Customer.prototype.id>;
|
81 | *
|
82 | * constructor(
|
83 | * protected db: juggler.DataSource,
|
84 | * orderRepository: EntityCrudRepository<Order, typeof Order.prototype.id>,
|
85 | * ) {
|
86 | * super(Customer, db);
|
87 | * this.orders = this._createHasManyRepositoryFactoryFor(
|
88 | * 'orders',
|
89 | * orderRepository,
|
90 | * );
|
91 | * }
|
92 | * }
|
93 | * ```
|
94 | *
|
95 | * @param relationName - Name of the relation defined on the source model
|
96 | * @param targetRepo - Target repository instance
|
97 | */
|
98 | protected createHasManyRepositoryFactoryFor<Target extends Entity, TargetID, ForeignKeyType>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>): HasManyRepositoryFactory<Target, ForeignKeyType>;
|
99 | /**
|
100 | * Function to create a constrained hasManyThrough relation repository factory
|
101 | *
|
102 | * @example
|
103 | * ```ts
|
104 | * class CustomerRepository extends DefaultCrudRepository<
|
105 | * Customer,
|
106 | * typeof Customer.prototype.id,
|
107 | * CustomerRelations
|
108 | * > {
|
109 | * public readonly cartItems: HasManyRepositoryFactory<CartItem, typeof Customer.prototype.id>;
|
110 | *
|
111 | * constructor(
|
112 | * protected db: juggler.DataSource,
|
113 | * cartItemRepository: EntityCrudRepository<CartItem, typeof, CartItem.prototype.id>,
|
114 | * throughRepository: EntityCrudRepository<Through, typeof Through.prototype.id>,
|
115 | * ) {
|
116 | * super(Customer, db);
|
117 | * this.cartItems = this.createHasManyThroughRepositoryFactoryFor(
|
118 | * 'cartItems',
|
119 | * cartItemRepository,
|
120 | * );
|
121 | * }
|
122 | * }
|
123 | * ```
|
124 | *
|
125 | * @param relationName - Name of the relation defined on the source model
|
126 | * @param targetRepo - Target repository instance
|
127 | * @param throughRepo - Through repository instance
|
128 | */
|
129 | protected createHasManyThroughRepositoryFactoryFor<Target extends Entity, TargetID, Through extends Entity, ThroughID, ForeignKeyType>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>> | {
|
130 | [repoType: string]: Getter<EntityCrudRepository<Target, TargetID>>;
|
131 | }, throughRepositoryGetter: Getter<EntityCrudRepository<Through, ThroughID>>): HasManyThroughRepositoryFactory<Target, TargetID, Through, ForeignKeyType>;
|
132 | /**
|
133 | * @deprecated
|
134 | * Function to create a belongs to accessor
|
135 | *
|
136 | * Use `this.createBelongsToAccessorFor()` instead
|
137 | *
|
138 | * @param relationName - Name of the relation defined on the source model
|
139 | * @param targetRepo - Target repository instance
|
140 | */
|
141 | protected _createBelongsToAccessorFor<Target extends Entity, TargetId>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetId>> | {
|
142 | [repoType: string]: Getter<EntityCrudRepository<Target, TargetId>>;
|
143 | }): BelongsToAccessor<Target, ID>;
|
144 | /**
|
145 | * Function to create a belongs to accessor
|
146 | *
|
147 | * @param relationName - Name of the relation defined on the source model
|
148 | * @param targetRepo - Target repository instance
|
149 | */
|
150 | protected createBelongsToAccessorFor<Target extends Entity, TargetId>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetId>> | {
|
151 | [repoType: string]: Getter<EntityCrudRepository<Target, TargetId>>;
|
152 | }): BelongsToAccessor<Target, ID>;
|
153 | /**
|
154 | * @deprecated
|
155 | * Function to create a constrained hasOne relation repository factory
|
156 | *
|
157 | * @param relationName - Name of the relation defined on the source model
|
158 | * @param targetRepo - Target repository instance
|
159 | */
|
160 | protected _createHasOneRepositoryFactoryFor<Target extends Entity, TargetID, ForeignKeyType>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>> | {
|
161 | [repoType: string]: Getter<EntityCrudRepository<Target, TargetID>>;
|
162 | }): HasOneRepositoryFactory<Target, ForeignKeyType>;
|
163 | /**
|
164 | * Function to create a constrained hasOne relation repository factory
|
165 | *
|
166 | * @param relationName - Name of the relation defined on the source model
|
167 | * @param targetRepo - Target repository instance
|
168 | */
|
169 | protected createHasOneRepositoryFactoryFor<Target extends Entity, TargetID, ForeignKeyType>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>> | {
|
170 | [repoType: string]: Getter<EntityCrudRepository<Target, TargetID>>;
|
171 | }): HasOneRepositoryFactory<Target, ForeignKeyType>;
|
172 | /**
|
173 | * @deprecated
|
174 | * Function to create a references many accessor
|
175 | *
|
176 | * Use `this.createReferencesManyAccessorFor()` instead
|
177 | *
|
178 | * @param relationName - Name of the relation defined on the source model
|
179 | * @param targetRepo - Target repository instance
|
180 | */
|
181 | protected _createReferencesManyAccessorFor<Target extends Entity, TargetId>(relationName: string, targetRepoGetter: Getter<EntityCrudRepository<Target, TargetId>>): ReferencesManyAccessor<Target, ID>;
|
182 | /**
|
183 | * Function to create a references many accessor
|
184 | *
|
185 | * @param relationName - Name of the relation defined on the source model
|
186 | * @param targetRepo - Target repository instance
|
187 | */
|
188 | protected createReferencesManyAccessorFor<Target extends Entity, TargetId>(relationName: string, targetRepoGetter: Getter<EntityCrudRepository<Target, TargetId>>): ReferencesManyAccessor<Target, ID>;
|
189 | create(entity: DataObject<T>, options?: Options): Promise<T>;
|
190 | createAll(entities: DataObject<T>[], options?: Options): Promise<T[]>;
|
191 | save(entity: T, options?: Options): Promise<T>;
|
192 | find(filter?: Filter<T>, options?: Options): Promise<(T & Relations)[]>;
|
193 | findOne(filter?: Filter<T>, options?: Options): Promise<(T & Relations) | null>;
|
194 | findById(id: ID, filter?: FilterExcludingWhere<T>, options?: Options): Promise<T & Relations>;
|
195 | update(entity: T, options?: Options): Promise<void>;
|
196 | delete(entity: T, options?: Options): Promise<void>;
|
197 | updateAll(data: DataObject<T>, where?: Where<T>, options?: Options): Promise<Count>;
|
198 | updateById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
|
199 | replaceById(id: ID, data: DataObject<T>, options?: Options): Promise<void>;
|
200 | deleteAll(where?: Where<T>, options?: Options): Promise<Count>;
|
201 | deleteById(id: ID, options?: Options): Promise<void>;
|
202 | count(where?: Where<T>, options?: Options): Promise<Count>;
|
203 | exists(id: ID, options?: Options): Promise<boolean>;
|
204 | /**
|
205 | * Execute a SQL command.
|
206 | *
|
207 | * **WARNING:** In general, it is always better to perform database actions
|
208 | * through repository methods. Directly executing SQL may lead to unexpected
|
209 | * results, corrupted data, security vulnerabilities and other issues.
|
210 | *
|
211 | * @example
|
212 | *
|
213 | * ```ts
|
214 | *
|
215 | * const result = await repo.execute(
|
216 | * 'SELECT * FROM Products WHERE size > ?',
|
217 | * [42]
|
218 | * );
|
219 | *
|
220 | *
|
221 | * const result = await repo.execute(
|
222 | * 'SELECT * FROM Products WHERE size > $1',
|
223 | * [42]
|
224 | * );
|
225 | * ```
|
226 | *
|
227 | * @param command A parameterized SQL command or query.
|
228 | * Check your database documentation for information on which characters to
|
229 | * use as parameter placeholders.
|
230 | * @param parameters List of parameter values to use.
|
231 | * @param options Additional options, for example `transaction`.
|
232 | * @returns A promise which resolves to the command output as returned by the
|
233 | * database driver. The output type (data structure) is database specific and
|
234 | * often depends on the command executed.
|
235 | */
|
236 | execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise<AnyObject>;
|
237 | /**
|
238 | * Execute a MongoDB command.
|
239 | *
|
240 | * **WARNING:** In general, it is always better to perform database actions
|
241 | * through repository methods. Directly executing MongoDB commands may lead
|
242 | * to unexpected results and other issues.
|
243 | *
|
244 | * @example
|
245 | *
|
246 | * ```ts
|
247 | * const result = await repo.execute('MyCollection', 'aggregate', [
|
248 | * {$lookup: {
|
249 | *
|
250 | * }},
|
251 | * {$unwind: '$data'},
|
252 | * {$out: 'tempData'}
|
253 | * ]);
|
254 | * ```
|
255 | *
|
256 | * @param collectionName The name of the collection to execute the command on.
|
257 | * @param command The command name. See
|
258 | * [Collection API docs](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html)
|
259 | * for the list of commands supported by the MongoDB client.
|
260 | * @param parameters Command parameters (arguments), as described in MongoDB API
|
261 | * docs for individual collection methods.
|
262 | * @returns A promise which resolves to the command output as returned by the
|
263 | * database driver.
|
264 | */
|
265 | execute(collectionName: string, command: string, ...parameters: PositionalParameters): Promise<AnyObject>;
|
266 | /**
|
267 | * Execute a raw database command using a connector that's not described
|
268 | * by LoopBack's `execute` API yet.
|
269 | *
|
270 | * **WARNING:** In general, it is always better to perform database actions
|
271 | * through repository methods. Directly executing database commands may lead
|
272 | * to unexpected results and other issues.
|
273 | *
|
274 | * @param args Command and parameters, please consult your connector's
|
275 | * documentation to learn about supported commands and their parameters.
|
276 | * @returns A promise which resolves to the command output as returned by the
|
277 | * database driver.
|
278 | */
|
279 | execute(...args: PositionalParameters): Promise<AnyObject>;
|
280 | protected toEntity<R extends T>(model: juggler.PersistedModel): R;
|
281 | protected toEntities<R extends T>(models: juggler.PersistedModel[]): R[];
|
282 | /**
|
283 | * Register an inclusion resolver for the related model name.
|
284 | *
|
285 | * @param relationName - Name of the relation defined on the source model
|
286 | * @param resolver - Resolver function for getting related model entities
|
287 | */
|
288 | registerInclusionResolver(relationName: string, resolver: InclusionResolver<T, Entity>): void;
|
289 | /**
|
290 | * Returns model instances that include related models of this repository
|
291 | * that have a registered resolver.
|
292 | *
|
293 | * @param entities - An array of entity instances or data
|
294 | * @param include -Inclusion filter
|
295 | * @param options - Options for the operations
|
296 | */
|
297 | protected includeRelatedModels(entities: T[], include?: InclusionFilter[], options?: Options): Promise<(T & Relations)[]>;
|
298 | /**
|
299 | * This function works as a persist hook.
|
300 | * It converts an entity from the CRUD operations' caller
|
301 | * to a persistable data that can will be stored in the
|
302 | * back-end database.
|
303 | *
|
304 | * User can extend `DefaultCrudRepository` then override this
|
305 | * function to execute custom persist hook.
|
306 | * @param entity The entity passed from CRUD operations' caller.
|
307 | * @param options
|
308 | */
|
309 | protected entityToData<R extends T>(entity: R | DataObject<R>, options?: {}): Promise<legacy.ModelData<legacy.PersistedModel>>;
|
310 | /** Converts an entity object to a JSON object to check if it contains navigational property.
|
311 | * Throws an error if `entity` contains navigational property.
|
312 | *
|
313 | * @param entity The entity passed from CRUD operations' caller.
|
314 | * @param options
|
315 | */
|
316 | protected ensurePersistable<R extends T>(entity: R | DataObject<R>, options?: {}): legacy.ModelData<legacy.PersistedModel>;
|
317 | /**
|
318 | * Removes juggler's "include" filter as it does not apply to LoopBack 4
|
319 | * relations.
|
320 | *
|
321 | * @param filter - Query filter
|
322 | */
|
323 | protected normalizeFilter(filter?: Filter<T>): legacy.Filter | undefined;
|
324 | }
|
325 | /**
|
326 | * Default implementation of CRUD repository using legacy juggler model
|
327 | * and data source with beginTransaction() method for connectors which
|
328 | * support Transactions
|
329 | */
|
330 | export declare class DefaultTransactionalRepository<T extends Entity, ID, Relations extends object = {}> extends DefaultCrudRepository<T, ID, Relations> implements TransactionalEntityRepository<T, ID, Relations> {
|
331 | beginTransaction(options?: IsolationLevel | Options): Promise<Transaction>;
|
332 | }
|
333 |
|
\ | No newline at end of file |