@vsaas/loopback-datasource-juggler 11.0.2 → 11.1.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 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)) mapParallel(data, function(item, next) {
1420
- const Model = self.lookupModel(item);
1421
- if (options.notify === false) buildResult(item, next);
1422
- else withNotify(item, next);
1423
- function buildResult(data, callback) {
1424
- const ctorOpts = {
1425
- fields: query.fields,
1426
- applySetters: false,
1427
- persisted: true,
1428
- applyDefaultValues: false
1429
- };
1430
- let obj;
1431
- try {
1432
- obj = new Model(data, ctorOpts);
1433
- } catch (err) {
1434
- return callback(err);
1435
- }
1436
- if (query && query.include) if (query.collect) {
1437
- obj = obj.__cachedRelations[query.collect];
1438
- if (obj === null) obj = void 0;
1439
- } else {
1440
- for (let i = 0; i < includedRelationNames.length; i++) {
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
- delete obj.__data.__cachedRelations;
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
- callback(null, obj);
1449
- }
1450
- function withNotify(data, callback) {
1451
- const context = {
1452
- Model,
1453
- data,
1454
- isNewInstance: false,
1455
- hookState,
1456
- options
1457
- };
1458
- Model.notifyObserversOf("loaded", context, function(err) {
1459
- if (err) return callback(err);
1460
- buildResult(context.data, callback);
1461
- });
1462
- }
1463
- }, function(err, results) {
1464
- if (err) return cb(err);
1465
- results = results.filter(isDefined);
1466
- if (data && data.countBeforeLimit) results.countBeforeLimit = data.countBeforeLimit;
1467
- if (!supportsGeo && near) results = geo.filter(results, near);
1468
- cb(err, results);
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
  *
@@ -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].indexOf(listener);
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/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.2",
5
+ "version": "11.1.0",
6
6
  "description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
7
7
  "keywords": [
8
8
  "Connector",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vsaas/loopback-datasource-juggler",
3
- "version": "11.0.2",
3
+ "version": "11.1.0",
4
4
  "description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
5
5
  "keywords": [
6
6
  "Connector",
@@ -67,7 +67,7 @@
67
67
  "should": "^13.2.3",
68
68
  "tsdown": "0.21.7",
69
69
  "typescript": "6.0.2",
70
- "vitest": "4.1.1",
70
+ "vitest": "4.1.2",
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: Listener<OperationHookContext<T>>,
302
- ): Listener<OperationHookContext<T>> | undefined;
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: Listener<OperationHookContext<T>>,
82
- ): Listener<OperationHookContext<T>> | undefined;
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>;