@vsaas/loopback-datasource-juggler 11.1.0 → 11.2.2

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/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.indexOf(k) != -1;
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.indexOf(k) != -1;
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/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 (properties.indexOf(result[i]) !== -1) fieldArray.push(result[i]);
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[key] = obj[key];
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;
@@ -194,6 +194,9 @@ var require_validations = /* @__PURE__ */ require_runtime.__commonJSMin(((export
194
194
  /**
195
195
  * Validate using custom asynchronous validation function.
196
196
  *
197
+ * The validator uses the callback-style signature `(err, done)` or
198
+ * `(err, options, done)`.
199
+ *
197
200
  * Example:
198
201
  *```js
199
202
  * User.validateAsync('name', customValidator, {message: 'Bad name'});
@@ -223,6 +226,40 @@ var require_validations = /* @__PURE__ */ require_runtime.__commonJSMin(((export
223
226
  */
224
227
  Validatable.validateAsync = getConfigurator("custom", { async: true });
225
228
  /**
229
+ * Validate using a custom promise-based validation function.
230
+ *
231
+ * The validator receives validation options as its first argument and should
232
+ * either resolve when valid or throw/reject when invalid.
233
+ *
234
+ * Example:
235
+ *```js
236
+ * User.validatePromise('name', async function(options) {
237
+ * await Promise.resolve();
238
+ * if (this.name === 'bad') {
239
+ * throw new Error('Bad name');
240
+ * }
241
+ * });
242
+ * var user = new User({name: 'Peter'});
243
+ * user.isValid(function (isValid) {
244
+ * isValid; // true
245
+ * })
246
+ * user.name = 'bad';
247
+ * user.isValid(function (isValid) {
248
+ * isValid; // false
249
+ * })
250
+ * ```
251
+ *
252
+ * @param {String} propertyName Property name to validate.
253
+ * @param {Function} validatorFn Custom validation function.
254
+ * @options {Object} options Configuration parameters; see below.
255
+ * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
256
+ * @property {Boolean} allowNull Whether null values are allowed.
257
+ */
258
+ Validatable.validatePromise = getConfigurator("custom", {
259
+ async: true,
260
+ promise: true
261
+ });
262
+ /**
226
263
  * Validate uniqueness of the value for a property in the collection of models.
227
264
  *
228
265
  * Not available for all connectors. Currently supported with these connectors:
@@ -324,9 +361,49 @@ var require_validations = /* @__PURE__ */ require_runtime.__commonJSMin(((export
324
361
  done = options;
325
362
  options = {};
326
363
  }
327
- if (!done) conf.customValidator.call(this, err, options);
328
- else if (conf.customValidator.length === 3) conf.customValidator.call(this, err, options, done);
329
- else conf.customValidator.call(this, err, done);
364
+ if (!done) return conf.customValidator.call(this, err, options);
365
+ if (conf.options && conf.options.promise) return validatePromiseCustom.call(this, attr, conf, err, options, done);
366
+ if (conf.customValidator.length === 3) return conf.customValidator.call(this, err, options, done);
367
+ return conf.customValidator.call(this, err, done);
368
+ }
369
+ function validatePromiseCustom(attr, conf, err, options, done) {
370
+ let result;
371
+ try {
372
+ result = conf.customValidator.call(this, options);
373
+ } catch (reason) {
374
+ handleCustomValidatorRejection.call(this, attr, conf, err, reason);
375
+ done();
376
+ return;
377
+ }
378
+ if (!isPromiseLike(result)) {
379
+ done();
380
+ return result;
381
+ }
382
+ result.then(function() {
383
+ done();
384
+ }, function(reason) {
385
+ handleCustomValidatorRejection.call(this, attr, conf, err, reason);
386
+ done();
387
+ }.bind(this));
388
+ return result;
389
+ }
390
+ function handleCustomValidatorRejection(attr, conf, err, reason) {
391
+ const message = getCustomValidatorRejectionMessage(conf, reason);
392
+ if (!message) {
393
+ err();
394
+ return;
395
+ }
396
+ err(false);
397
+ this.errors.add(attr, message, conf.code || conf.validation);
398
+ }
399
+ function getCustomValidatorRejectionMessage(conf, reason) {
400
+ if (typeof conf.message === "string") return conf.message;
401
+ if (reason instanceof Error && reason.message) return reason.message;
402
+ if (typeof reason === "string") return reason;
403
+ return null;
404
+ }
405
+ function isPromiseLike(value) {
406
+ return value != null && typeof value.then === "function";
330
407
  }
331
408
  function escapeStringRegexp(str) {
332
409
  if (typeof str !== "string") throw new TypeError("Expected a string");
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.1.0",
5
+ "version": "11.2.2",
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": "^4.4.3",
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": "^6.15.0"
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": "^13.2.3",
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.1.0",
3
+ "version": "11.2.2",
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": "^4.4.3",
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": "^6.15.0",
60
- "@vsaas/loopback-connector": "^11.0.2"
59
+ "qs": "^6.15.2",
60
+ "@vsaas/loopback-connector": "^11.0.5"
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": "^13.2.3",
67
+ "should": "13.2.3",
68
68
  "tsdown": "0.21.7",
69
69
  "typescript": "6.0.2",
70
- "vitest": "4.1.2",
70
+ "vitest": "4.1.4",
71
71
  "loopback-connector-throwing": "1.0.0"
72
72
  },
73
73
  "engines": {
@@ -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, Options, PromiseOrVoid } from "./common";
7
7
 
8
8
  /**
9
9
  * This class provides methods that add validation cababilities to models.
@@ -196,11 +196,18 @@ export interface Validatable {
196
196
  * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
197
197
  * @property {Boolean} allowNull Whether null values are allowed.
198
198
  */
199
- validate(propertyName: string, validatorFn: Function, options?: Options): void;
199
+ validate(
200
+ propertyName: string,
201
+ validatorFn: Function,
202
+ options?: Options,
203
+ ): void;
200
204
 
201
205
  /**
202
206
  * Validate using custom asynchronous validation function.
203
207
  *
208
+ * The validator uses the callback-style signature `(err, done)` or
209
+ * `(err, options, done)`.
210
+ *
204
211
  * Example:
205
212
  *```js
206
213
  * User.validateAsync('name', customValidator, {message: 'Bad name'});
@@ -228,7 +235,47 @@ export interface Validatable {
228
235
  * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
229
236
  * @property {Boolean} allowNull Whether null values are allowed.
230
237
  */
231
- validateAsync(propertyName: string, validatorFn: Function, options?: Options): void;
238
+ validateAsync(
239
+ propertyName: string,
240
+ validatorFn: Function,
241
+ options?: Options,
242
+ ): void;
243
+
244
+ /**
245
+ * Validate using a custom promise-based validation function.
246
+ *
247
+ * The validator receives validation options as its first argument and should
248
+ * either resolve when valid or throw/reject when invalid.
249
+ *
250
+ * Example:
251
+ *```js
252
+ * User.validatePromise('name', async function(options) {
253
+ * await Promise.resolve();
254
+ * if (this.name === 'bad') {
255
+ * throw new Error('Bad name');
256
+ * }
257
+ * });
258
+ * var user = new User({name: 'Peter'});
259
+ * user.isValid(function (isValid) {
260
+ * isValid; // true
261
+ * })
262
+ * user.name = 'bad';
263
+ * user.isValid(function (isValid) {
264
+ * isValid; // false
265
+ * })
266
+ * ```
267
+ *
268
+ * @param {String} propertyName Property name to validate.
269
+ * @param {Function} validatorFn Custom validation function.
270
+ * @options {Object} options Configuration parameters; see below.
271
+ * @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
272
+ * @property {Boolean} allowNull Whether null values are allowed.
273
+ */
274
+ validatePromise(
275
+ propertyName: string,
276
+ validatorFn: Function,
277
+ options?: Options,
278
+ ): void;
232
279
 
233
280
  /**
234
281
  * Validate uniqueness of the value for a property in the collection of models.
@@ -257,7 +304,11 @@ export interface Validatable {
257
304
  * @property {String} if Validate only if `if` exists.
258
305
  * @property {String} unless Validate only if `unless` exists.
259
306
  */
260
- validatesUniquenessOf(propertyName: string, validatorFn: Function, options?: Options): void;
307
+ validatesUniquenessOf(
308
+ propertyName: string,
309
+ validatorFn: Function,
310
+ options?: Options,
311
+ ): void;
261
312
 
262
313
  /**
263
314
  * Validate if a value for a property is a Date.
@@ -271,7 +322,11 @@ export interface Validatable {
271
322
  * @options {Object} options Configuration parameters; see below.
272
323
  * @property {String} message Error message to use instead of default.
273
324
  */
274
- validatesDateOf(propertyName: string, validatorFn: Function, options?: Options): void;
325
+ validatesDateOf(
326
+ propertyName: string,
327
+ validatorFn: Function,
328
+ options?: Options,
329
+ ): void;
275
330
  }
276
331
 
277
332
  /**