/
persisted-model.d.ts
524 lines (481 loc) · 19.8 KB
/
persisted-model.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Callback, Options, PromiseOrVoid} from './common';
import {ModelBase, ModelData} from './model';
import {Filter, Where} from './query';
/**
* Data object for persisted models
*/
export type PersistedData<
T extends PersistedModel = PersistedModel
> = ModelData<T>;
/**
* Type of affected count
*/
export interface Count {
count: number;
}
/**
* Persisted model
*/
export declare class PersistedModel extends ModelBase {
/**
* Create new instance of Model, and save to database.
*
* @param {Object|Object[]} [data] Optional data argument. Can be either a
* single model instance or an array of instances.
*
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} models Model instances or null.
*/
static create(
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
static create(
data: PersistedData[],
options?: Options,
callback?: Callback<PersistedModel[]>,
): PromiseOrVoid<PersistedModel[]>;
/**
* Update or insert a model instance
* @param {Object} data The model instance data to insert.
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} model Updated model instance.
*/
static upsert(
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
static updateOrCreate(
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
static patchOrCreate(
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
/**
* Update or insert a model instance based on the search criteria.
* If there is a single instance retrieved, update the retrieved model.
* Creates a new model if no model instances were found.
* Returns an error if multiple instances are found.
* @param {Object} [where] `where` filter, like
* ```
* { key: val, key2: {gt: 'val2'}, ...}
* ```
* <br/>see
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-other-methods).
* @param {Object} data The model instance data to insert.
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} model Updated model instance.
*/
static upsertWithWhere(
where: Where,
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
static patchOrCreateWithWhere(
where: Where,
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
/**
* Replace or insert a model instance; replace existing record if one is found,
* such that parameter `data.id` matches `id` of model instance; otherwise,
* insert a new record.
* @param {Object} data The model instance data.
* @options {Object} [options] Options for replaceOrCreate
* @property {Boolean} validate Perform validation before saving. Default is true.
* @callback {Function} callback Callback function called with `cb(err, obj)` signature.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} model Replaced model instance.
*/
static replaceOrCreate(
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
/**
* Finds one record matching the optional filter object. If not found, creates
* the object using the data provided as second argument. In this sense it is
* the same as `find`, but limited to one object. Returns an object, not
* collection. If you don't provide the filter object argument, it tries to
* locate an existing object that matches the `data` argument.
*
* @options {Object} [filter] Optional Filter object; see below.
* @property {String|Object|Array} fields Identify fields to include in return result.
* <br/>See [Fields filter](http://loopback.io/doc/en/lb2/Fields-filter.html).
* @property {String|Object|Array} include See PersistedModel.include documentation.
* <br/>See [Include filter](http://loopback.io/doc/en/lb2/Include-filter.html).
* @property {Number} limit Maximum number of instances to return.
* <br/>See [Limit filter](http://loopback.io/doc/en/lb2/Limit-filter.html).
* @property {String} order Sort order: either "ASC" for ascending or "DESC" for descending.
* <br/>See [Order filter](http://loopback.io/doc/en/lb2/Order-filter.html).
* @property {Number} skip Number of results to skip.
* <br/>See [Skip filter](http://loopback.io/doc/en/lb2/Skip-filter.html).
* @property {Object} where Where clause, like
* ```
* {where: {key: val, key2: {gt: val2}, ...}}
* ```
* <br/>See
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-queries).
* @param {Object} data Data to insert if object matching the `where` filter is not found.
* @callback {Function} callback Callback function called with `cb(err, instance, created)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Model instance matching the `where` filter, if found.
* @param {Boolean} created True if the instance does not exist and gets created.
*/
static findOrCreate(
filter: Filter,
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<[PersistedModel, boolean]>;
/**
* Check whether a model instance exists in database.
*
* @param {id} id Identifier of object (primary key value).
*
* @callback {Function} callback Callback function called with `(err, exists)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Boolean} exists True if the instance with the specified ID exists; false otherwise.
*/
static exists(
id: any,
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
/**
* Find object by ID with an optional filter for include/fields.
*
* @param {*} id Primary key value
* @options {Object} [filter] Optional Filter JSON object; see below.
* @property {String|Object|Array} fields Identify fields to include in return result.
* <br/>See [Fields filter](http://loopback.io/doc/en/lb2/Fields-filter.html).
* @property {String|Object|Array} include See PersistedModel.include documentation.
* <br/>See [Include filter](http://loopback.io/doc/en/lb2/Include-filter.html).
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Model instance matching the specified ID or null if no instance matches.
*/
static findById(
id: any,
filter?: Filter,
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<PersistedModel>;
/**
* Find all model instances that match `filter` specification.
* See [Querying models](http://loopback.io/doc/en/lb2/Querying-data.html).
*
* @options {Object} [filter] Optional Filter JSON object; see below.
* @property {String|Object|Array} fields Identify fields to include in return result.
* <br/>See [Fields filter](http://loopback.io/doc/en/lb2/Fields-filter.html).
* @property {String|Object|Array} include See PersistedModel.include documentation.
* <br/>See [Include filter](http://loopback.io/doc/en/lb2/Include-filter.html).
* @property {Number} limit Maximum number of instances to return.
* <br/>See [Limit filter](http://loopback.io/doc/en/lb2/Limit-filter.html).
* @property {String} order Sort order: either "ASC" for ascending or "DESC" for descending.
* <br/>See [Order filter](http://loopback.io/doc/en/lb2/Order-filter.html).
* @property {Number} skip Number of results to skip.
* <br/>See [Skip filter](http://loopback.io/doc/en/lb2/Skip-filter.html).
* @property {Object} where Where clause, like
* ```
* { where: { key: val, key2: {gt: 'val2'}, ...} }
* ```
* <br/>See
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-queries).
*
* @callback {Function} callback Callback function called with `(err, returned-instances)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Array} models Model instances matching the filter, or null if none found.
*/
static find(
filter?: Filter,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel[]>;
/**
* Find one model instance that matches `filter` specification.
* Same as `find`, but limited to one result;
* Returns object, not collection.
*
* @options {Object} [filter] Optional Filter JSON object; see below.
* @property {String|Object|Array} fields Identify fields to include in return result.
* <br/>See [Fields filter](http://loopback.io/doc/en/lb2/Fields-filter.html).
* @property {String|Object|Array} include See PersistedModel.include documentation.
* <br/>See [Include filter](http://loopback.io/doc/en/lb2/Include-filter.html).
* @property {String} order Sort order: either "ASC" for ascending or "DESC" for descending.
* <br/>See [Order filter](http://loopback.io/doc/en/lb2/Order-filter.html).
* @property {Number} skip Number of results to skip.
* <br/>See [Skip filter](http://loopback.io/doc/en/lb2/Skip-filter.html).
* @property {Object} where Where clause, like
* ```
* {where: { key: val, key2: {gt: 'val2'}, ...} }
* ```
* <br/>See
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-queries).
*
* @callback {Function} callback Callback function called with `(err, returned-instance)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Array} model First model instance that matches the filter or null if none found.
*/
static findOne(
filter?: Filter,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
/**
* Destroy all model instances that match the optional `where` specification.
*
* @param {Object} [where] Optional where filter, like:
* ```
* {key: val, key2: {gt: 'val2'}, ...}
* ```
* <br/>See
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-other-methods).
*
* @callback {Function} callback Optional callback function called with `(err, info)` arguments.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} info Additional information about the command outcome.
* @param {Number} info.count Number of instances (rows, documents) destroyed.
*/
static destroyAll(
where?: Where,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
static remove(
where?: Where,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
static deleteAll(
where?: Where,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
/**
* Update multiple instances that match the where clause.
*
* Example:
*
*```js
* Employee.updateAll({managerId: 'x001'}, {managerId: 'x002'}, function(err, info) {
* ...
* });
* ```
*
* @param {Object} [where] Optional `where` filter, like
* ```
* { key: val, key2: {gt: 'val2'}, ...}
* ```
* <br/>see
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-other-methods).
* @param {Object} data Object containing data to replace matching instances, if AnyType.
*
* @callback {Function} callback Callback function called with `(err, info)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} info Additional information about the command outcome.
* @param {Number} info.count Number of instances (rows, documents) updated.
*
*/
static updateAll(
where?: Where,
data?: PersistedData,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
static update(
where?: Where,
data?: PersistedData,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
/**
* Destroy model instance with the specified ID.
* @param {*} id The ID value of model instance to delete.
* @callback {Function} callback Callback function called with `(err)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
*/
static destroyById(
id: any,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
static removeById(
id: any,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
static deleteById(
id: any,
options?: Options,
callback?: Callback<Count>,
): PromiseOrVoid<Count>;
/**
* Replace attributes for a model instance whose id is the first input
* argument and persist it into the datasource.
* Performs validation before replacing.
*
* @param {*} id The ID value of model instance to replace.
* @param {Object} data Data to replace.
* @options {Object} [options] Options for replace
* @property {Boolean} validate Perform validation before saving. Default is true.
* @callback {Function} callback Callback function called with `(err, instance)` arguments.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Replaced instance.
*/
static replaceById(
id: any,
data: PersistedData,
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
/**
* Return the number of records that match the optional "where" filter.
* @param {Object} [where] Optional where filter, like
* ```
* { key: val, key2: {gt: 'val2'}, ...}
* ```
* <br/>See
* [Where filter](http://loopback.io/doc/en/lb2/Where-filter.html#where-clause-for-other-methods).
* @callback {Function} callback Callback function called with `(err, count)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Number} count Number of instances.
*/
static count(
where?: Where,
options?: Options,
callback?: Callback<number>,
): PromiseOrVoid<number>;
/**
* Save model instance. If the instance doesn't have an ID, then calls [create](#persistedmodelcreatedata-cb) instead.
* Triggers: validate, save, update, or create.
* @options {Object} [options] See below.
* @property {Boolean} validate Perform validation before saving. Default is true.
* @property {Boolean} throws If true, throw a validation error; WARNING: This can crash Node.
* If false, report the error via callback. Default is false.
* @callback {Function} callback Optional callback function called with `(err, obj)` arguments.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Model instance saved or created.
*/
save(options?: Options, callback?: Callback<boolean>): PromiseOrVoid<boolean>;
/**
* Determine if the data model is new.
* @returns {Boolean} Returns true if the data model is new; false otherwise.
*/
isNewRecord(): boolean;
/**
* Deletes the model from persistence.
* Triggers `destroy` hook (async) before and after destroying object.
* @param {Function} callback Callback function.
*/
destroy(
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
remove(
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
delete(
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
/**
* Update a single attribute.
* Equivalent to `updateAttributes({name: 'value'}, cb)`
*
* @param {String} name Name of property.
* @param {Mixed} value Value of property.
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Updated instance.
*/
updateAttribute(
name: string,
value: any,
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
/**
* Update set of attributes. Performs validation before updating.
*
* Triggers: `validation`, `save` and `update` hooks
* @param {Object} data Data to update.
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Updated instance.
*/
updateAttributes(
data: PersistedData,
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
/**
* Replace attributes for a model instance and persist it into the datasource.
* Performs validation before replacing.
*
* @param {Object} data Data to replace.
* @options {Object} [options] Options for replace
* @property {Boolean} validate Perform validation before saving. Default is true.
* @callback {Function} callback Callback function called with `(err, instance)` arguments.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Replaced instance.
*/
replaceAttributes(
data: PersistedData,
options?: Options,
callback?: Callback<boolean>,
): PromiseOrVoid<boolean>;
/**
* Reload object from persistence. Requires `id` member of `object` to be able to call `find`.
* @callback {Function} callback Callback function called with `(err, instance)` arguments. Required.
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
* @param {Object} instance Model instance.
*/
reload(
options?: Options,
callback?: Callback<PersistedModel>,
): PromiseOrVoid<PersistedModel>;
/**
* Set the correct `id` property for the `PersistedModel`. Uses the `setId` method if the model is attached to
* connector that defines it. Otherwise, uses the default lookup.
* Override this method to handle complex IDs.
*
* @param {*} val The `id` value. Will be converted to the export type that the `id` property specifies.
*/
setId(val: any): void;
/**
* Get the `id` value for the `PersistedModel`.
*
* @returns {*} The `id` value
*/
getId(): any;
/**
* Get the `id` property name of the constructor.
*
* @returns {String} The `id` property name
*/
getIdName(): string;
/**
* Get the `id` property name of the constructor.
*
* @returns {String} The `id` property name
*/
static getIdName(): string;
}
export type PersistedModelClass = typeof PersistedModel;