@vsaas/loopback-datasource-juggler 11.0.2 → 11.2.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/dist/lib/dao.js +93 -52
- package/dist/lib/model.js +11 -2
- package/dist/lib/observer.js +44 -1
- package/dist/lib/scope.js +3 -0
- package/dist/lib/utils.js +31 -2
- package/dist/package.js +4 -4
- package/package.json +6 -6
- package/types/common.d.ts +5 -0
- package/types/model.d.ts +21 -3
- package/types/observer-mixin.d.ts +24 -2
- package/types/persisted-model.d.ts +22 -1
- package/types/scope.d.ts +12 -1
package/dist/lib/dao.js
CHANGED
|
@@ -63,6 +63,12 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
63
63
|
function copyData(from, to) {
|
|
64
64
|
for (const key in from) to[key] = from[key];
|
|
65
65
|
}
|
|
66
|
+
function withoutLeanOption(options) {
|
|
67
|
+
if (!options || options.lean !== true) return options;
|
|
68
|
+
const opts = Object.create(options);
|
|
69
|
+
opts.lean = false;
|
|
70
|
+
return opts;
|
|
71
|
+
}
|
|
66
72
|
function once(fn) {
|
|
67
73
|
let called = false;
|
|
68
74
|
return function() {
|
|
@@ -596,7 +602,7 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
596
602
|
if (this.settings.forceId) {
|
|
597
603
|
options = Object.create(options);
|
|
598
604
|
options.validate = !!doValidate;
|
|
599
|
-
Model.findById(id, options, function(err, model) {
|
|
605
|
+
Model.findById(id, withoutLeanOption(options), function(err, model) {
|
|
600
606
|
if (err) return cb(err);
|
|
601
607
|
if (!model) return cb(errorModelNotFound(id));
|
|
602
608
|
model.updateAttributes(data, options, cb);
|
|
@@ -1129,7 +1135,7 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1129
1135
|
}, data, options);
|
|
1130
1136
|
});
|
|
1131
1137
|
});
|
|
1132
|
-
} else Model.findOne(query, options, function(err, record) {
|
|
1138
|
+
} else Model.findOne(query, withoutLeanOption(options), function(err, record) {
|
|
1133
1139
|
if (err) return cb(err);
|
|
1134
1140
|
if (record) if (cb.promise) return cb(null, [record, false]);
|
|
1135
1141
|
else return cb(null, record, false);
|
|
@@ -1194,6 +1200,9 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1194
1200
|
if (typeof filter === "function") {
|
|
1195
1201
|
cb = filter;
|
|
1196
1202
|
filter = {};
|
|
1203
|
+
} else if (typeof filter === "object" && filter && !(filter.include || filter.fields)) {
|
|
1204
|
+
options = filter;
|
|
1205
|
+
filter = {};
|
|
1197
1206
|
}
|
|
1198
1207
|
} else if (cb === void 0) {
|
|
1199
1208
|
if (typeof options === "function") {
|
|
@@ -1416,58 +1425,64 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1416
1425
|
includedRelationNames.push(utils.isPlainObject(inc) ? Object.keys(inc)[0] : inc);
|
|
1417
1426
|
}
|
|
1418
1427
|
}
|
|
1419
|
-
if (!err && Array.isArray(data))
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
const relationName = includedRelationNames[i];
|
|
1442
|
-
let included = obj.__cachedRelations[relationName];
|
|
1443
|
-
if (Array.isArray(included)) included = new List(included, null, obj);
|
|
1444
|
-
if (included) obj.__data[relationName] = included;
|
|
1428
|
+
if (!err && Array.isArray(data)) {
|
|
1429
|
+
if (options.lean === true) {
|
|
1430
|
+
let results = buildLeanResults(data, query, includedRelationNames);
|
|
1431
|
+
if (!supportsGeo && near) results = geo.filter(results, near);
|
|
1432
|
+
return cb(err, results);
|
|
1433
|
+
}
|
|
1434
|
+
mapParallel(data, function(item, next) {
|
|
1435
|
+
const Model = self.lookupModel(item);
|
|
1436
|
+
if (options.notify === false) buildResult(item, next);
|
|
1437
|
+
else withNotify(item, next);
|
|
1438
|
+
function buildResult(data, callback) {
|
|
1439
|
+
const ctorOpts = {
|
|
1440
|
+
fields: query.fields,
|
|
1441
|
+
applySetters: false,
|
|
1442
|
+
persisted: true,
|
|
1443
|
+
applyDefaultValues: false
|
|
1444
|
+
};
|
|
1445
|
+
let obj;
|
|
1446
|
+
try {
|
|
1447
|
+
obj = new Model(data, ctorOpts);
|
|
1448
|
+
} catch (err) {
|
|
1449
|
+
return callback(err);
|
|
1445
1450
|
}
|
|
1446
|
-
|
|
1451
|
+
if (query && query.include) if (query.collect) {
|
|
1452
|
+
obj = obj.__cachedRelations[query.collect];
|
|
1453
|
+
if (obj === null) obj = void 0;
|
|
1454
|
+
} else {
|
|
1455
|
+
for (let i = 0; i < includedRelationNames.length; i++) {
|
|
1456
|
+
const relationName = includedRelationNames[i];
|
|
1457
|
+
let included = obj.__cachedRelations[relationName];
|
|
1458
|
+
if (Array.isArray(included)) included = new List(included, null, obj);
|
|
1459
|
+
if (included) obj.__data[relationName] = included;
|
|
1460
|
+
}
|
|
1461
|
+
delete obj.__data.__cachedRelations;
|
|
1462
|
+
}
|
|
1463
|
+
callback(null, obj);
|
|
1447
1464
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
});
|
|
1470
|
-
else cb(err, data || []);
|
|
1465
|
+
function withNotify(data, callback) {
|
|
1466
|
+
const context = {
|
|
1467
|
+
Model,
|
|
1468
|
+
data,
|
|
1469
|
+
isNewInstance: false,
|
|
1470
|
+
hookState,
|
|
1471
|
+
options
|
|
1472
|
+
};
|
|
1473
|
+
Model.notifyObserversOf("loaded", context, function(err) {
|
|
1474
|
+
if (err) return callback(err);
|
|
1475
|
+
buildResult(context.data, callback);
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
}, function(err, results) {
|
|
1479
|
+
if (err) return cb(err);
|
|
1480
|
+
results = results.filter(isDefined);
|
|
1481
|
+
if (data && data.countBeforeLimit) results.countBeforeLimit = data.countBeforeLimit;
|
|
1482
|
+
if (!supportsGeo && near) results = geo.filter(results, near);
|
|
1483
|
+
cb(err, results);
|
|
1484
|
+
});
|
|
1485
|
+
} else cb(err, data || []);
|
|
1471
1486
|
}
|
|
1472
1487
|
if (options.notify === false) invokeConnectorMethod(connector, "all", self, [query], options, allCb);
|
|
1473
1488
|
else {
|
|
@@ -1487,6 +1502,32 @@ var require_dao = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, modul
|
|
|
1487
1502
|
function isDefined(value) {
|
|
1488
1503
|
return value !== void 0;
|
|
1489
1504
|
}
|
|
1505
|
+
function buildLeanResults(data, query, includedRelationNames) {
|
|
1506
|
+
const results = [];
|
|
1507
|
+
for (let i = 0; i < data.length; i++) {
|
|
1508
|
+
const result = buildLeanResult(data[i], query, includedRelationNames);
|
|
1509
|
+
if (isDefined(result)) results.push(result);
|
|
1510
|
+
}
|
|
1511
|
+
if (data && data.countBeforeLimit) results.countBeforeLimit = data.countBeforeLimit;
|
|
1512
|
+
return results;
|
|
1513
|
+
}
|
|
1514
|
+
function buildLeanResult(data, query, includedRelationNames) {
|
|
1515
|
+
if (!query || !query.include) return data;
|
|
1516
|
+
const cachedRelations = data && data.__cachedRelations;
|
|
1517
|
+
if (query.collect) {
|
|
1518
|
+
let collected = cachedRelations && cachedRelations[query.collect];
|
|
1519
|
+
if (collected === null) collected = void 0;
|
|
1520
|
+
return collected;
|
|
1521
|
+
}
|
|
1522
|
+
if (!cachedRelations || !includedRelationNames) return data;
|
|
1523
|
+
for (let i = 0; i < includedRelationNames.length; i++) {
|
|
1524
|
+
const relationName = includedRelationNames[i];
|
|
1525
|
+
const included = cachedRelations[relationName];
|
|
1526
|
+
if (included) data[relationName] = included;
|
|
1527
|
+
}
|
|
1528
|
+
delete data.__cachedRelations;
|
|
1529
|
+
return data;
|
|
1530
|
+
}
|
|
1490
1531
|
/**
|
|
1491
1532
|
* Find one record, same as `find`, but limited to one result. This function returns an object, not a collection.
|
|
1492
1533
|
*
|
package/dist/lib/model.js
CHANGED
|
@@ -36,6 +36,15 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
|
|
|
36
36
|
Text: true,
|
|
37
37
|
ObjectID: true
|
|
38
38
|
};
|
|
39
|
+
function isSelectedField(fields, key) {
|
|
40
|
+
if (fields.indexOf(key) !== -1) return true;
|
|
41
|
+
const prefix = key + ".";
|
|
42
|
+
for (let i = 0; i < fields.length; i++) {
|
|
43
|
+
const field = fields[i];
|
|
44
|
+
if (typeof field === "string" && field.indexOf(prefix) === 0) return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
39
48
|
/**
|
|
40
49
|
* Model class: base class for all persistent objects.
|
|
41
50
|
*
|
|
@@ -128,7 +137,7 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
|
|
|
128
137
|
if (data.__cachedRelations) this.__cachedRelations = data.__cachedRelations;
|
|
129
138
|
let keys = Object.keys(data);
|
|
130
139
|
if (Array.isArray(options.fields)) keys = keys.filter(function(k) {
|
|
131
|
-
return options.fields
|
|
140
|
+
return isSelectedField(options.fields, k);
|
|
132
141
|
});
|
|
133
142
|
let size = keys.length;
|
|
134
143
|
let p, propVal;
|
|
@@ -170,7 +179,7 @@ var require_model = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, mod
|
|
|
170
179
|
}
|
|
171
180
|
keys = Object.keys(properties);
|
|
172
181
|
if (Array.isArray(options.fields)) keys = keys.filter(function(k) {
|
|
173
|
-
return options.fields
|
|
182
|
+
return isSelectedField(options.fields, k);
|
|
174
183
|
});
|
|
175
184
|
size = keys.length;
|
|
176
185
|
for (let k = 0; k < size; k++) {
|
package/dist/lib/observer.js
CHANGED
|
@@ -21,6 +21,25 @@ var require_observer = /* @__PURE__ */ require_runtime.__commonJSMin(((exports,
|
|
|
21
21
|
fn.apply(this, arguments);
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
+
function completeSyncObserver(retval, next) {
|
|
25
|
+
if (retval && typeof retval.then === "function") {
|
|
26
|
+
retval.then(function() {
|
|
27
|
+
next();
|
|
28
|
+
return null;
|
|
29
|
+
}, next);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
next();
|
|
33
|
+
}
|
|
34
|
+
function findObserverIndex(observers, listener) {
|
|
35
|
+
let index = observers.indexOf(listener);
|
|
36
|
+
if (index !== -1) return index;
|
|
37
|
+
for (let i = 0; i < observers.length; i++) {
|
|
38
|
+
const observer = observers[i];
|
|
39
|
+
if (observer && observer._syncObserverListener === listener) return i;
|
|
40
|
+
}
|
|
41
|
+
return -1;
|
|
42
|
+
}
|
|
24
43
|
/**
|
|
25
44
|
* Register an asynchronous observer for the given operation (event).
|
|
26
45
|
*
|
|
@@ -55,6 +74,30 @@ var require_observer = /* @__PURE__ */ require_runtime.__commonJSMin(((exports,
|
|
|
55
74
|
this._observers[operation].push(listener);
|
|
56
75
|
};
|
|
57
76
|
/**
|
|
77
|
+
* Register an observer that completes when the listener returns.
|
|
78
|
+
*
|
|
79
|
+
* The listener does not receive a `next` callback. Returning without throwing
|
|
80
|
+
* completes the observer, and thrown errors or rejected promises stop the chain.
|
|
81
|
+
*
|
|
82
|
+
* @param {String} operation The operation name.
|
|
83
|
+
* @callback {function} listener The listener function.
|
|
84
|
+
* @end
|
|
85
|
+
*/
|
|
86
|
+
ObserverMixin.observeSync = function(operation, listener) {
|
|
87
|
+
const target = this;
|
|
88
|
+
function syncObserver(context, next) {
|
|
89
|
+
let retval;
|
|
90
|
+
try {
|
|
91
|
+
retval = listener.call(target, context);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
return next(err);
|
|
94
|
+
}
|
|
95
|
+
completeSyncObserver(retval, next);
|
|
96
|
+
}
|
|
97
|
+
syncObserver._syncObserverListener = listener;
|
|
98
|
+
this.observe(operation, syncObserver);
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
58
101
|
* Unregister an asynchronous observer for the given operation (event).
|
|
59
102
|
*
|
|
60
103
|
* Example:
|
|
@@ -72,7 +115,7 @@ var require_observer = /* @__PURE__ */ require_runtime.__commonJSMin(((exports,
|
|
|
72
115
|
*/
|
|
73
116
|
ObserverMixin.removeObserver = function(operation, listener) {
|
|
74
117
|
if (!(this._observers && this._observers[operation])) return;
|
|
75
|
-
const index = this._observers[operation]
|
|
118
|
+
const index = findObserverIndex(this._observers[operation], listener);
|
|
76
119
|
if (index !== -1) return this._observers[operation].splice(index, 1);
|
|
77
120
|
};
|
|
78
121
|
/**
|
package/dist/lib/scope.js
CHANGED
|
@@ -339,6 +339,9 @@ var require_scope = /* @__PURE__ */ require_runtime.__commonJSMin(((exports) =>
|
|
|
339
339
|
if (typeof filter === "function") {
|
|
340
340
|
cb = filter;
|
|
341
341
|
filter = {};
|
|
342
|
+
} else if (typeof filter === "object" && filter && !(filter.include || filter.fields)) {
|
|
343
|
+
options = filter;
|
|
344
|
+
filter = {};
|
|
342
345
|
}
|
|
343
346
|
} else if (cb === void 0) {
|
|
344
347
|
if (typeof options === "function") {
|
package/dist/lib/utils.js
CHANGED
|
@@ -196,21 +196,50 @@ var require_utils = /* @__PURE__ */ require_runtime.__commonJSMin(((exports) =>
|
|
|
196
196
|
}
|
|
197
197
|
let fieldArray = [];
|
|
198
198
|
if (excludeUnknown) {
|
|
199
|
-
for (i = 0, n = result.length; i < n; i++) if (
|
|
199
|
+
for (i = 0, n = result.length; i < n; i++) if (isKnownField(result[i], properties)) fieldArray.push(result[i]);
|
|
200
200
|
} else fieldArray = result;
|
|
201
201
|
return fieldArray;
|
|
202
202
|
}
|
|
203
|
+
function isKnownField(field, properties) {
|
|
204
|
+
if (properties.indexOf(field) !== -1) return true;
|
|
205
|
+
if (typeof field !== "string") return false;
|
|
206
|
+
const dotIndex = field.indexOf(".");
|
|
207
|
+
if (dotIndex <= 0) return false;
|
|
208
|
+
return properties.indexOf(field.slice(0, dotIndex)) !== -1;
|
|
209
|
+
}
|
|
203
210
|
function selectFields(fields) {
|
|
204
211
|
return function(obj) {
|
|
205
212
|
const result = {};
|
|
206
213
|
let key;
|
|
207
214
|
for (let i = 0; i < fields.length; i++) {
|
|
208
215
|
key = fields[i];
|
|
209
|
-
result
|
|
216
|
+
copySelectedField(obj, result, key);
|
|
210
217
|
}
|
|
211
218
|
return result;
|
|
212
219
|
};
|
|
213
220
|
}
|
|
221
|
+
function copySelectedField(source, target, field) {
|
|
222
|
+
if (typeof field !== "string" || field.indexOf(".") === -1 || Object.prototype.hasOwnProperty.call(source, field)) {
|
|
223
|
+
target[field] = source[field];
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const segments = field.split(".");
|
|
227
|
+
let value = source;
|
|
228
|
+
for (let i = 0; i < segments.length; i++) {
|
|
229
|
+
if (value == null || typeof value !== "object") {
|
|
230
|
+
value = void 0;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
value = value[segments[i]];
|
|
234
|
+
}
|
|
235
|
+
let cursor = target;
|
|
236
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
237
|
+
const segment = segments[i];
|
|
238
|
+
if (!cursor[segment] || typeof cursor[segment] !== "object") cursor[segment] = {};
|
|
239
|
+
cursor = cursor[segment];
|
|
240
|
+
}
|
|
241
|
+
cursor[segments[segments.length - 1]] = value;
|
|
242
|
+
}
|
|
214
243
|
function isProhibited(key, prohibitedKeys) {
|
|
215
244
|
if (!prohibitedKeys || !prohibitedKeys.length) return false;
|
|
216
245
|
if (typeof key !== "string") return false;
|
package/dist/package.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var require_package = /* @__PURE__ */ require("./_virtual/_rolldown/runtime.js").__commonJSMin(((exports, module) => {
|
|
3
3
|
module.exports = {
|
|
4
4
|
"name": "@vsaas/loopback-datasource-juggler",
|
|
5
|
-
"version": "11.0
|
|
5
|
+
"version": "11.2.0",
|
|
6
6
|
"description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"Connector",
|
|
@@ -63,12 +63,12 @@ var require_package = /* @__PURE__ */ require("./_virtual/_rolldown/runtime.js")
|
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@vsaas/loopback-connector": "workspace:^",
|
|
66
|
-
"debug": "
|
|
66
|
+
"debug": "catalog:",
|
|
67
67
|
"depd": "^2.0.0",
|
|
68
68
|
"inflection": "^3.0.2",
|
|
69
69
|
"minimatch": "10.2.4",
|
|
70
70
|
"neotraverse": "^0.6.18",
|
|
71
|
-
"qs": "
|
|
71
|
+
"qs": "catalog:"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@types/node": "catalog:",
|
|
@@ -76,7 +76,7 @@ var require_package = /* @__PURE__ */ require("./_virtual/_rolldown/runtime.js")
|
|
|
76
76
|
"loopback-connector-throwing": "workspace:*",
|
|
77
77
|
"oxfmt": "catalog:",
|
|
78
78
|
"oxlint": "catalog:",
|
|
79
|
-
"should": "
|
|
79
|
+
"should": "catalog:",
|
|
80
80
|
"tsdown": "catalog:",
|
|
81
81
|
"typescript": "catalog:",
|
|
82
82
|
"vitest": "catalog:"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vsaas/loopback-datasource-juggler",
|
|
3
|
-
"version": "11.0
|
|
3
|
+
"version": "11.2.0",
|
|
4
4
|
"description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Connector",
|
|
@@ -51,23 +51,23 @@
|
|
|
51
51
|
"registry": "https://registry.npmjs.org/"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"debug": "
|
|
54
|
+
"debug": "4.4.3",
|
|
55
55
|
"depd": "^2.0.0",
|
|
56
56
|
"inflection": "^3.0.2",
|
|
57
57
|
"minimatch": "10.2.4",
|
|
58
58
|
"neotraverse": "^0.6.18",
|
|
59
|
-
"qs": "
|
|
60
|
-
"@vsaas/loopback-connector": "^11.0.
|
|
59
|
+
"qs": "6.15.0",
|
|
60
|
+
"@vsaas/loopback-connector": "^11.0.3"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@types/node": "25.5.0",
|
|
64
64
|
"bson": "^4.7.2",
|
|
65
65
|
"oxfmt": "0.42.0",
|
|
66
66
|
"oxlint": "1.57.0",
|
|
67
|
-
"should": "
|
|
67
|
+
"should": "13.2.3",
|
|
68
68
|
"tsdown": "0.21.7",
|
|
69
69
|
"typescript": "6.0.2",
|
|
70
|
-
"vitest": "4.1.
|
|
70
|
+
"vitest": "4.1.4",
|
|
71
71
|
"loopback-connector-throwing": "1.0.0"
|
|
72
72
|
},
|
|
73
73
|
"engines": {
|
package/types/common.d.ts
CHANGED
|
@@ -15,6 +15,11 @@ export interface AnyObject<T = any> {
|
|
|
15
15
|
*/
|
|
16
16
|
export type Options = AnyObject<any>;
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Options for read operations that should return plain data instead of model instances.
|
|
20
|
+
*/
|
|
21
|
+
export type LeanOptions = Options & { lean: true };
|
|
22
|
+
|
|
18
23
|
/**
|
|
19
24
|
* Type alias for Node.js callback functions
|
|
20
25
|
*/
|
package/types/model.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { EventEmitter } from 'events';
|
|
7
7
|
import { AnyObject, Options } from './common';
|
|
8
8
|
import { DataSource } from './datasource';
|
|
9
|
-
import { Listener, OperationHookContext } from './observer-mixin';
|
|
9
|
+
import { Listener, OperationHookContext, SyncListener } from './observer-mixin';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Property types
|
|
@@ -279,6 +279,22 @@ export declare class ModelBase {
|
|
|
279
279
|
listener: Listener<OperationHookContext<T>>,
|
|
280
280
|
): void;
|
|
281
281
|
|
|
282
|
+
/**
|
|
283
|
+
* Register an observer that completes when the listener returns.
|
|
284
|
+
*
|
|
285
|
+
* The listener does not receive a `next` callback. Returning without throwing
|
|
286
|
+
* completes the observer, and thrown errors or rejected promises stop the chain.
|
|
287
|
+
*
|
|
288
|
+
* @param {String} operation The operation name.
|
|
289
|
+
* @callback {function} listener The listener function.
|
|
290
|
+
* @end
|
|
291
|
+
*/
|
|
292
|
+
static observeSync<T extends typeof ModelBase>(
|
|
293
|
+
this: T,
|
|
294
|
+
operation: string,
|
|
295
|
+
listener: SyncListener<OperationHookContext<T>>,
|
|
296
|
+
): void;
|
|
297
|
+
|
|
282
298
|
/**
|
|
283
299
|
* Unregister an asynchronous observer for the given operation (event).
|
|
284
300
|
*
|
|
@@ -298,8 +314,10 @@ export declare class ModelBase {
|
|
|
298
314
|
static removeObserver<T extends typeof ModelBase>(
|
|
299
315
|
this: T,
|
|
300
316
|
operation: string,
|
|
301
|
-
listener:
|
|
302
|
-
|
|
317
|
+
listener:
|
|
318
|
+
| Listener<OperationHookContext<T>>
|
|
319
|
+
| SyncListener<OperationHookContext<T>>,
|
|
320
|
+
): Listener<OperationHookContext<T>> | SyncListener<OperationHookContext<T>> | undefined;
|
|
303
321
|
|
|
304
322
|
/**
|
|
305
323
|
* Unregister all asynchronous observers for the given operation (event).
|
|
@@ -24,6 +24,10 @@ export type Listener<Ctx = OperationHookContext<typeof ModelBase>> = (
|
|
|
24
24
|
next: (err?: any) => void,
|
|
25
25
|
) => PromiseOrVoid<void>;
|
|
26
26
|
|
|
27
|
+
export type SyncListener<Ctx = OperationHookContext<typeof ModelBase>> = (
|
|
28
|
+
ctx: Ctx,
|
|
29
|
+
) => PromiseOrVoid<void>;
|
|
30
|
+
|
|
27
31
|
export interface ObserverMixin {
|
|
28
32
|
/**
|
|
29
33
|
* Register an asynchronous observer for the given operation (event).
|
|
@@ -59,6 +63,22 @@ export interface ObserverMixin {
|
|
|
59
63
|
listener: Listener<OperationHookContext<T>>,
|
|
60
64
|
): void;
|
|
61
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Register an observer that completes when the listener returns.
|
|
68
|
+
*
|
|
69
|
+
* The listener does not receive a `next` callback. Returning without throwing
|
|
70
|
+
* completes the observer, and thrown errors or rejected promises stop the chain.
|
|
71
|
+
*
|
|
72
|
+
* @param {String} operation The operation name.
|
|
73
|
+
* @callback {function} listener The listener function.
|
|
74
|
+
* @end
|
|
75
|
+
*/
|
|
76
|
+
observeSync<T extends typeof ModelBase>(
|
|
77
|
+
this: T,
|
|
78
|
+
operation: string,
|
|
79
|
+
listener: SyncListener<OperationHookContext<T>>,
|
|
80
|
+
): void;
|
|
81
|
+
|
|
62
82
|
/**
|
|
63
83
|
* Unregister an asynchronous observer for the given operation (event).
|
|
64
84
|
*
|
|
@@ -78,8 +98,10 @@ export interface ObserverMixin {
|
|
|
78
98
|
removeObserver<T extends typeof ModelBase>(
|
|
79
99
|
this: T,
|
|
80
100
|
operation: string,
|
|
81
|
-
listener:
|
|
82
|
-
|
|
101
|
+
listener:
|
|
102
|
+
| Listener<OperationHookContext<T>>
|
|
103
|
+
| SyncListener<OperationHookContext<T>>,
|
|
104
|
+
): Listener<OperationHookContext<T>> | SyncListener<OperationHookContext<T>> | undefined;
|
|
83
105
|
|
|
84
106
|
/**
|
|
85
107
|
* Unregister all asynchronous observers for the given operation (event).
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
-
import { Callback, Options, PromiseOrVoid } from './common';
|
|
6
|
+
import { Callback, LeanOptions, Options, PromiseOrVoid } from './common';
|
|
7
7
|
import { ModelBase, ModelData } from './model';
|
|
8
8
|
import { Filter, Where } from './query';
|
|
9
9
|
|
|
@@ -193,6 +193,17 @@ export declare class PersistedModel extends ModelBase {
|
|
|
193
193
|
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
|
|
194
194
|
* @param {Object} instance Model instance matching the specified ID or null if no instance matches.
|
|
195
195
|
*/
|
|
196
|
+
static findById(
|
|
197
|
+
id: any,
|
|
198
|
+
options: LeanOptions,
|
|
199
|
+
callback?: Callback<ModelData>,
|
|
200
|
+
): PromiseOrVoid<ModelData>;
|
|
201
|
+
static findById(
|
|
202
|
+
id: any,
|
|
203
|
+
filter: Filter | undefined,
|
|
204
|
+
options: LeanOptions,
|
|
205
|
+
callback?: Callback<ModelData>,
|
|
206
|
+
): PromiseOrVoid<ModelData>;
|
|
196
207
|
static findById(
|
|
197
208
|
id: any,
|
|
198
209
|
filter?: Filter,
|
|
@@ -227,6 +238,11 @@ export declare class PersistedModel extends ModelBase {
|
|
|
227
238
|
* @param {Array} models Model instances matching the filter, or null if none found.
|
|
228
239
|
*/
|
|
229
240
|
|
|
241
|
+
static find(
|
|
242
|
+
filter: Filter | undefined,
|
|
243
|
+
options: LeanOptions,
|
|
244
|
+
callback?: Callback<ModelData[]>,
|
|
245
|
+
): PromiseOrVoid<ModelData[]>;
|
|
230
246
|
static find(
|
|
231
247
|
filter?: Filter,
|
|
232
248
|
options?: Options,
|
|
@@ -258,6 +274,11 @@ export declare class PersistedModel extends ModelBase {
|
|
|
258
274
|
* @param {Error} err Error object; see [Error object](http://loopback.io/doc/en/lb2/Error-object.html).
|
|
259
275
|
* @param {Array} model First model instance that matches the filter or null if none found.
|
|
260
276
|
*/
|
|
277
|
+
static findOne(
|
|
278
|
+
filter: Filter | undefined,
|
|
279
|
+
options: LeanOptions,
|
|
280
|
+
callback?: Callback<ModelData>,
|
|
281
|
+
): PromiseOrVoid<ModelData>;
|
|
261
282
|
static findOne(
|
|
262
283
|
filter?: Filter,
|
|
263
284
|
options?: Options,
|
package/types/scope.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
-
import { AnyObject, Callback, Options, PromiseOrVoid } from './common';
|
|
6
|
+
import { AnyObject, Callback, LeanOptions, Options, PromiseOrVoid } from './common';
|
|
7
7
|
import { ModelBase, ModelBaseClass, ModelData } from './model';
|
|
8
8
|
import { Count } from './persisted-model';
|
|
9
9
|
import { Filter, Where } from './query';
|
|
@@ -84,8 +84,19 @@ export interface ScopedMethods<T extends ModelBase = ModelBase> {
|
|
|
84
84
|
callback?: Callback<Count>,
|
|
85
85
|
): PromiseOrVoid<Count>;
|
|
86
86
|
|
|
87
|
+
findById(
|
|
88
|
+
id: any,
|
|
89
|
+
filter: Filter | undefined,
|
|
90
|
+
options: LeanOptions,
|
|
91
|
+
callback?: Callback<ModelData<T>>,
|
|
92
|
+
): PromiseOrVoid<ModelData<T>>;
|
|
87
93
|
findById(id: any, filter: Filter, options?: Options, callback?: Callback<T>): PromiseOrVoid<T>;
|
|
88
94
|
|
|
95
|
+
findOne(
|
|
96
|
+
filter: Filter | undefined,
|
|
97
|
+
options: LeanOptions,
|
|
98
|
+
callback?: Callback<ModelData<T>>,
|
|
99
|
+
): PromiseOrVoid<ModelData<T>>;
|
|
89
100
|
findOne(filter: Filter, options?: Options, callback?: Callback<T>): PromiseOrVoid<T>;
|
|
90
101
|
|
|
91
102
|
count(where: Where, options?: Options, callback?: Callback<number>): PromiseOrVoid<number>;
|