UNPKG

3.88 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/repository
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.constrainDataObjects = exports.constrainDataObject = exports.constrainWhereOr = exports.constrainWhere = exports.constrainFilter = void 0;
8const filter_1 = require("@loopback/filter");
9const lodash_1 = require("lodash");
10/**
11 * A utility function which takes a filter and enforces constraint(s)
12 * on it
13 * @param originalFilter - the filter to apply the constrain(s) to
14 * @param constraint - the constraint which is to be applied on the filter
15 * @returns Filter the modified filter with the constraint, otherwise
16 * the original filter
17 */
18function constrainFilter(originalFilter, constraint) {
19 const filter = (0, lodash_1.cloneDeep)(originalFilter);
20 const builder = new filter_1.FilterBuilder(filter);
21 return builder.impose(constraint).build();
22}
23exports.constrainFilter = constrainFilter;
24/**
25 * A utility function which takes a where filter and enforces constraint(s)
26 * on it
27 * @param originalWhere - the where filter to apply the constrain(s) to
28 * @param constraint - the constraint which is to be applied on the filter
29 * @returns Filter the modified filter with the constraint, otherwise
30 * the original filter
31 */
32function constrainWhere(originalWhere, constraint) {
33 const where = (0, lodash_1.cloneDeep)(originalWhere);
34 const builder = new filter_1.WhereBuilder(where);
35 return builder.impose(constraint).build();
36}
37exports.constrainWhere = constrainWhere;
38/**
39 * A utility function which takes a where filter and enforces constraint(s)
40 * on it with OR clause
41 * @param originalWhere - the where filter to apply the constrain(s) to
42 * @param constraint - the constraint which is to be applied on the filter with
43 * or clause
44 * @returns Filter the modified filter with the constraint, otherwise
45 * the original filter
46 */
47function constrainWhereOr(originalWhere, constraint) {
48 const where = (0, lodash_1.cloneDeep)(originalWhere);
49 const builder = new filter_1.WhereBuilder(where);
50 return builder.or(constraint).build();
51}
52exports.constrainWhereOr = constrainWhereOr;
53/**
54 * A utility function which takes a model instance data and enforces constraint(s)
55 * on it
56 * @param originalData - the model data to apply the constrain(s) to
57 * @param constraint - the constraint which is to be applied on the data object
58 * @returns the modified data with the constraint, otherwise
59 * the original instance data
60 */
61function constrainDataObject(originalData, constraint) {
62 const constrainedData = (0, lodash_1.cloneDeep)(originalData);
63 for (const c in constraint) {
64 if (Object.prototype.hasOwnProperty.call(constrainedData, c)) {
65 // Known limitation: === does not work for objects such as ObjectId
66 if (originalData[c] === constraint[c])
67 continue;
68 throw new Error(`Property "${c}" cannot be changed!`);
69 }
70 constrainedData[c] = constraint[c];
71 }
72 return constrainedData;
73}
74exports.constrainDataObject = constrainDataObject;
75/**
76 * A utility function which takes an array of model instance data and
77 * enforces constraint(s) on it
78 * @param originalData - the array of model data to apply the constrain(s) to
79 * @param constraint - the constraint which is to be applied on the data objects
80 * @returns an array of the modified data with the constraint, otherwise
81 * the original instance data array
82 */
83function constrainDataObjects(originalData, constraint) {
84 return originalData.map(obj => constrainDataObject(obj, constraint));
85}
86exports.constrainDataObjects = constrainDataObjects;
87//# sourceMappingURL=constraint-utils.js.map
\No newline at end of file