@vsaas/loopback-datasource-juggler 12.1.2 → 12.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.
@@ -20,6 +20,7 @@ var require_relation_definition = /* @__PURE__ */ require_runtime.__commonJSMin(
20
20
  const mergeQuery = utils.mergeQuery;
21
21
  const idEquals = utils.idEquals;
22
22
  const idsHaveDuplicates = utils.idsHaveDuplicates;
23
+ const uniq = utils.uniq;
23
24
  const ModelBaseClass = require_lib_model;
24
25
  const applyFilter = require_lib_connectors_memory.applyFilter;
25
26
  const ValidationError = require_lib_validations.ValidationError;
@@ -487,6 +488,10 @@ var require_relation_definition = /* @__PURE__ */ require_runtime.__commonJSMin(
487
488
  * @property {String} as Name of the property in the referring model that corresponds to the foreign key field in the related model.
488
489
  * @property {String} foreignKey Property name of foreign key field.
489
490
  * @property {Object} model Model object
491
+ * @property {Object} options Extra relation options such as `disableInclude`
492
+ * and `countRelated`. When `countRelated` is true on a hasManyThrough
493
+ * relation, `count()` counts existing related target instances instead of
494
+ * through-model rows (orphan through records are excluded).
490
495
  */
491
496
  RelationDefinition.hasMany = function hasMany(modelFrom, modelToRef, params) {
492
497
  const thisClassName = modelFrom.modelName;
@@ -546,6 +551,7 @@ var require_relation_definition = /* @__PURE__ */ require_runtime.__commonJSMin(
546
551
  scopeMethods.create = scopeMethod(definition, "create");
547
552
  scopeMethods.add = scopeMethod(definition, "add");
548
553
  scopeMethods.remove = scopeMethod(definition, "remove");
554
+ scopeMethods.count = scopeMethod(definition, "count");
549
555
  const addFunc = scopeMethods.add;
550
556
  modelFrom.prototype["__link__" + relationName] = addFunc;
551
557
  const removeFunc = scopeMethods.remove;
@@ -916,6 +922,65 @@ var require_relation_definition = /* @__PURE__ */ require_runtime.__commonJSMin(
916
922
  return cb.promise;
917
923
  };
918
924
  /**
925
+ * Count related instances for a hasManyThrough relation.
926
+ *
927
+ * By default this counts through-model rows, including orphans and duplicate
928
+ * links. Set `options.countRelated` on the relation to count unique existing
929
+ * related target instances instead. The optional `where` is applied to the
930
+ * through model by default, or to the target model when `countRelated` is true.
931
+ *
932
+ * @param {Object} [where]
933
+ * @param {Object} [options]
934
+ * @param {Function} [cb]
935
+ */
936
+ HasManyThrough.prototype.count = function(where, options, cb) {
937
+ if (typeof where === "function") {
938
+ cb = where;
939
+ where = {};
940
+ options = {};
941
+ } else if (typeof options === "function" && cb === void 0) {
942
+ cb = options;
943
+ options = {};
944
+ }
945
+ options = options || {};
946
+ where = where || {};
947
+ const definition = this.definition;
948
+ const modelThrough = definition.modelThrough;
949
+ const pk1 = definition.keyFrom;
950
+ cb = cb || utils.createPromiseCallback();
951
+ if (!definition.options || !definition.options.countRelated) {
952
+ const filter = { where: {} };
953
+ filter.where[definition.keyTo] = this.modelInstance[pk1];
954
+ definition.applyScope(this.modelInstance, filter);
955
+ mergeQuery(filter, { where });
956
+ modelThrough.count(filter.where, options, cb);
957
+ return cb.promise;
958
+ }
959
+ const keys = throughKeys(definition);
960
+ const fk1 = keys[0];
961
+ const fk2 = keys[1];
962
+ const query = {};
963
+ query[fk1] = this.modelInstance[pk1];
964
+ const filter = { where: query };
965
+ definition.applyScope(this.modelInstance, filter);
966
+ const modelTo = definition.modelTo;
967
+ const idName = modelTo.definition.idName();
968
+ modelThrough.find({ where: filter.where }, options, function(err, throughs) {
969
+ if (err) return cb(err);
970
+ const ids = uniq((throughs || []).map(function(row) {
971
+ return row[fk2];
972
+ }).filter(function(id) {
973
+ return id != null;
974
+ }));
975
+ if (ids.length === 0) return cb(null, 0);
976
+ const targetFilter = { where: {} };
977
+ targetFilter.where[idName] = { inq: ids };
978
+ mergeQuery(targetFilter, { where });
979
+ modelTo.count(targetFilter.where, options, cb);
980
+ });
981
+ return cb.promise;
982
+ };
983
+ /**
919
984
  * Remove the target model instance from the 'hasMany' relation
920
985
  * @param {Object|ID) acInst The actual instance or id value
921
986
  */
@@ -71,6 +71,10 @@ var require_relations = /* @__PURE__ */ require_runtime.__commonJSMin(((exports,
71
71
  * @property {Object|Function} scope Explicitly define additional scopes.
72
72
  * @property {Boolean} invert Specify if the relation is inverted.
73
73
  * @property {Object} model The model object.
74
+ * @property {Object} options Extra relation options such as `disableInclude`
75
+ * and `countRelated`. When `countRelated` is true on a hasManyThrough
76
+ * relation, `count()` counts existing related target instances instead of
77
+ * through-model rows (orphan through records are excluded).
74
78
  */
75
79
  RelationMixin.hasMany = function hasMany(modelTo, params) {
76
80
  return RelationDefinition.hasMany(this, modelTo, params);
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": "12.1.2",
5
+ "version": "12.2.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": "12.1.2",
3
+ "version": "12.2.0",
4
4
  "description": "Fork of loopback-datasource-juggler for LoopBack 3 style applications",
5
5
  "keywords": [
6
6
  "Connector",
@@ -137,6 +137,8 @@ export declare class HasManyThrough<
137
137
  exists(acInst: any, options?: Options, callback?: Callback<boolean>): PromiseOrVoid<boolean>;
138
138
 
139
139
  remove(acInst: any, options?: Options, callback?: Callback<boolean>): PromiseOrVoid<boolean>;
140
+
141
+ count(where?: Where, options?: Options, callback?: Callback<number>): PromiseOrVoid<number>;
140
142
  }
141
143
 
142
144
  export declare class HasOne<