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