@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,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
3
|
+
const require_lib_utils = require("./utils.js");
|
|
4
|
+
//#region src/lib/observer.ts
|
|
5
|
+
var require_observer = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
|
|
6
|
+
const utils = require_lib_utils;
|
|
7
|
+
const debug = require("debug")("loopback:observer");
|
|
8
|
+
module.exports = ObserverMixin;
|
|
9
|
+
/**
|
|
10
|
+
* ObserverMixin class. Use to add observe/notifyObserversOf APIs to other
|
|
11
|
+
* classes.
|
|
12
|
+
*
|
|
13
|
+
* @class ObserverMixin
|
|
14
|
+
*/
|
|
15
|
+
function ObserverMixin() {}
|
|
16
|
+
function once(fn) {
|
|
17
|
+
let called = false;
|
|
18
|
+
return function() {
|
|
19
|
+
if (called) return;
|
|
20
|
+
called = true;
|
|
21
|
+
fn.apply(this, arguments);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Register an asynchronous observer for the given operation (event).
|
|
26
|
+
*
|
|
27
|
+
* Example:
|
|
28
|
+
*
|
|
29
|
+
* Registers a `before save` observer for a given model.
|
|
30
|
+
*
|
|
31
|
+
* ```javascript
|
|
32
|
+
* MyModel.observe('before save', function filterProperties(ctx, next) {
|
|
33
|
+
if (ctx.options && ctx.options.skipPropertyFilter) return next();
|
|
34
|
+
if (ctx.instance) {
|
|
35
|
+
FILTERED_PROPERTIES.forEach(function(p) {
|
|
36
|
+
ctx.instance.unsetAttribute(p);
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
FILTERED_PROPERTIES.forEach(function(p) {
|
|
40
|
+
delete ctx.data[p];
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
next();
|
|
44
|
+
});
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @param {String} operation The operation name.
|
|
48
|
+
* @callback {function} listener The listener function. It will be invoked with
|
|
49
|
+
* `this` set to the model constructor, e.g. `User`.
|
|
50
|
+
* @end
|
|
51
|
+
*/
|
|
52
|
+
ObserverMixin.observe = function(operation, listener) {
|
|
53
|
+
this._observers = this._observers || {};
|
|
54
|
+
if (!this._observers[operation]) this._observers[operation] = [];
|
|
55
|
+
this._observers[operation].push(listener);
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Unregister an asynchronous observer for the given operation (event).
|
|
59
|
+
*
|
|
60
|
+
* Example:
|
|
61
|
+
*
|
|
62
|
+
* ```javascript
|
|
63
|
+
* MyModel.removeObserver('before save', function removedObserver(ctx, next) {
|
|
64
|
+
// some logic user want to apply to the removed observer...
|
|
65
|
+
next();
|
|
66
|
+
});
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param {String} operation The operation name.
|
|
70
|
+
* @callback {function} listener The listener function.
|
|
71
|
+
* @end
|
|
72
|
+
*/
|
|
73
|
+
ObserverMixin.removeObserver = function(operation, listener) {
|
|
74
|
+
if (!(this._observers && this._observers[operation])) return;
|
|
75
|
+
const index = this._observers[operation].indexOf(listener);
|
|
76
|
+
if (index !== -1) return this._observers[operation].splice(index, 1);
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Unregister all asynchronous observers for the given operation (event).
|
|
80
|
+
*
|
|
81
|
+
* Example:
|
|
82
|
+
*
|
|
83
|
+
* Remove all observers connected to the `before save` operation.
|
|
84
|
+
*
|
|
85
|
+
* ```javascript
|
|
86
|
+
* MyModel.clearObservers('before save');
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @param {String} operation The operation name.
|
|
90
|
+
* @end
|
|
91
|
+
*/
|
|
92
|
+
ObserverMixin.clearObservers = function(operation) {
|
|
93
|
+
if (!(this._observers && this._observers[operation])) return;
|
|
94
|
+
this._observers[operation].length = 0;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Invoke all async observers for the given operation(s).
|
|
98
|
+
*
|
|
99
|
+
* Example:
|
|
100
|
+
*
|
|
101
|
+
* Notify all async observers for the `before save` operation.
|
|
102
|
+
*
|
|
103
|
+
* ```javascript
|
|
104
|
+
* var context = {
|
|
105
|
+
Model: Model,
|
|
106
|
+
instance: obj,
|
|
107
|
+
isNewInstance: true,
|
|
108
|
+
hookState: hookState,
|
|
109
|
+
options: options,
|
|
110
|
+
};
|
|
111
|
+
* Model.notifyObserversOf('before save', context, function(err) {
|
|
112
|
+
if (err) return cb(err);
|
|
113
|
+
// user can specify the logic after the observers have been notified
|
|
114
|
+
});
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @param {String|String[]} operation The operation name(s).
|
|
118
|
+
* @param {Object} context Operation-specific context.
|
|
119
|
+
* @callback {function(Error=)} callback The callback to call when all observers
|
|
120
|
+
* have finished.
|
|
121
|
+
*/
|
|
122
|
+
ObserverMixin.notifyObserversOf = function(operation, context, callback) {
|
|
123
|
+
const self = this;
|
|
124
|
+
if (!callback) callback = utils.createPromiseCallback();
|
|
125
|
+
if (Array.isArray(operation)) {
|
|
126
|
+
runOperations(0);
|
|
127
|
+
return callback.promise;
|
|
128
|
+
}
|
|
129
|
+
const observers = this._observers && this._observers[operation];
|
|
130
|
+
this._notifyBaseObservers(operation, context, function doNotify(err) {
|
|
131
|
+
if (err) return callback(err, context);
|
|
132
|
+
if (!observers || !observers.length) return callback(null, context);
|
|
133
|
+
notifyObserverAt(0);
|
|
134
|
+
});
|
|
135
|
+
return callback.promise;
|
|
136
|
+
function runOperations(index) {
|
|
137
|
+
if (index >= operation.length) return callback(null, context);
|
|
138
|
+
self.notifyObserversOf(operation[index], context, function(err) {
|
|
139
|
+
if (err) return callback(err, context);
|
|
140
|
+
runOperations(index + 1);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function notifyObserverAt(index) {
|
|
144
|
+
if (index >= observers.length) return callback(null, context);
|
|
145
|
+
const next = once(function(err) {
|
|
146
|
+
if (err) return callback(err, context);
|
|
147
|
+
notifyObserverAt(index + 1);
|
|
148
|
+
});
|
|
149
|
+
let retval;
|
|
150
|
+
try {
|
|
151
|
+
retval = observers[index](context, next);
|
|
152
|
+
} catch (err) {
|
|
153
|
+
return next(err);
|
|
154
|
+
}
|
|
155
|
+
if (retval && typeof retval.then === "function") retval.then(function() {
|
|
156
|
+
next();
|
|
157
|
+
return null;
|
|
158
|
+
}, next);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
ObserverMixin._notifyBaseObservers = function(operation, context, callback) {
|
|
162
|
+
if (this.base && this.base.notifyObserversOf) this.base.notifyObserversOf(operation, context, callback);
|
|
163
|
+
else callback();
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Run the given function with before/after observers.
|
|
167
|
+
*
|
|
168
|
+
* It's done in three serial asynchronous steps:
|
|
169
|
+
*
|
|
170
|
+
* - Notify the registered observers under 'before ' + operation
|
|
171
|
+
* - Execute the function
|
|
172
|
+
* - Notify the registered observers under 'after ' + operation
|
|
173
|
+
*
|
|
174
|
+
* If an error happens, it fails first and calls the callback with err.
|
|
175
|
+
*
|
|
176
|
+
* Example:
|
|
177
|
+
*
|
|
178
|
+
* ```javascript
|
|
179
|
+
* var context = {
|
|
180
|
+
Model: Model,
|
|
181
|
+
instance: obj,
|
|
182
|
+
isNewInstance: true,
|
|
183
|
+
hookState: hookState,
|
|
184
|
+
options: options,
|
|
185
|
+
};
|
|
186
|
+
* function work(done) {
|
|
187
|
+
process.nextTick(function() {
|
|
188
|
+
done(null, 1);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
* Model.notifyObserversAround('execute', context, work, function(err) {
|
|
192
|
+
if (err) return cb(err);
|
|
193
|
+
// user can specify the logic after the observers have been notified
|
|
194
|
+
});
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @param {String} operation The operation name
|
|
198
|
+
* @param {Context} context The context object
|
|
199
|
+
* @param {Function} fn The task to be invoked as fn(done) or fn(context, done)
|
|
200
|
+
* @callback {Function} callback The callback function
|
|
201
|
+
* @returns {*}
|
|
202
|
+
*/
|
|
203
|
+
ObserverMixin.notifyObserversAround = function(operation, context, fn, callback) {
|
|
204
|
+
const self = this;
|
|
205
|
+
context = context || {};
|
|
206
|
+
if (context.end === void 0) context.end = callback;
|
|
207
|
+
return self.notifyObserversOf("before " + operation, context, function(err, context) {
|
|
208
|
+
if (err) return callback(err);
|
|
209
|
+
function cbForWork(err) {
|
|
210
|
+
const args = [].slice.call(arguments, 0);
|
|
211
|
+
if (err) {
|
|
212
|
+
context.error = err;
|
|
213
|
+
self.notifyObserversOf("after " + operation + " error", context, function(_err, context) {
|
|
214
|
+
if (_err && err) debug("Operation %j failed and \"after %s error\" hook returned an error too. Calling back with the hook error only.\nOriginal error: %s\nHook error: %s\n", err.stack || err, _err.stack || _err);
|
|
215
|
+
callback.call(null, _err || err, context);
|
|
216
|
+
});
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const returnedArgs = args.slice(1);
|
|
220
|
+
context.results = returnedArgs;
|
|
221
|
+
self.notifyObserversOf("after " + operation, context, function(err, context) {
|
|
222
|
+
if (err) return callback(err, context);
|
|
223
|
+
let results = returnedArgs;
|
|
224
|
+
if (context && Array.isArray(context.results)) results = context.results;
|
|
225
|
+
const args = [err].concat(results);
|
|
226
|
+
callback.apply(null, args);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (fn.length === 1) fn(cbForWork);
|
|
230
|
+
else fn(context, cbForWork);
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
}));
|
|
234
|
+
//#endregion
|
|
235
|
+
module.exports = require_observer();
|