drapcode-utility 1.0.1 → 1.0.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/build/format-fields/index.d.ts +1 -0
- package/build/format-fields/index.js +68 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/utils/check-error.js +7 -0
- package/build/utils/query-parser.js +16 -2
- package/build/utils/query-paser-new.js +0 -8
- package/build/utils/util.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const formatFieldsOfItem: (item: any, fields: any) => any;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatFieldsOfItem = void 0;
|
|
4
|
+
var drapcode_constant_1 = require("drapcode-constant");
|
|
5
|
+
var formatFieldsOfItem = function (item, fields) {
|
|
6
|
+
if (!item)
|
|
7
|
+
return item;
|
|
8
|
+
if (!fields && !fields.length)
|
|
9
|
+
return item;
|
|
10
|
+
if (Array.isArray(item)) {
|
|
11
|
+
item = item.map(function (itemData) { return formatField(itemData, fields); });
|
|
12
|
+
}
|
|
13
|
+
else
|
|
14
|
+
item = formatField(item, fields);
|
|
15
|
+
return item;
|
|
16
|
+
};
|
|
17
|
+
exports.formatFieldsOfItem = formatFieldsOfItem;
|
|
18
|
+
var formatField = function (itemData, fields) {
|
|
19
|
+
var newItemData = {};
|
|
20
|
+
Object.keys(itemData).forEach(function (key) {
|
|
21
|
+
var field = fields.find(function (field) { return field.fieldName === key; });
|
|
22
|
+
newItemData[key] = field
|
|
23
|
+
? getFormatFieldData(itemData[key], field.type)
|
|
24
|
+
: itemData[key];
|
|
25
|
+
});
|
|
26
|
+
return newItemData;
|
|
27
|
+
};
|
|
28
|
+
var getFormatFieldData = function (fieldData, fieldType) {
|
|
29
|
+
var createdAt = drapcode_constant_1.FieldTypes.createdAt, text = drapcode_constant_1.FieldTypes.text, large_text = drapcode_constant_1.FieldTypes.large_text, date = drapcode_constant_1.FieldTypes.date, password = drapcode_constant_1.FieldTypes.password, uuid = drapcode_constant_1.FieldTypes.uuid, custom_uuid = drapcode_constant_1.FieldTypes.custom_uuid, email = drapcode_constant_1.FieldTypes.email, url = drapcode_constant_1.FieldTypes.url, number = drapcode_constant_1.FieldTypes.number, unix_timestamp = drapcode_constant_1.FieldTypes.unix_timestamp, reference = drapcode_constant_1.FieldTypes.reference, belongsTo = drapcode_constant_1.FieldTypes.belongsTo, multi_reference = drapcode_constant_1.FieldTypes.multi_reference, dynamic_option = drapcode_constant_1.FieldTypes.dynamic_option, static_option = drapcode_constant_1.FieldTypes.static_option, boolean = drapcode_constant_1.FieldTypes.boolean, time_slot = drapcode_constant_1.FieldTypes.time_slot;
|
|
30
|
+
var stringType = [
|
|
31
|
+
createdAt.id,
|
|
32
|
+
text.id,
|
|
33
|
+
large_text.id,
|
|
34
|
+
date.id,
|
|
35
|
+
password.id,
|
|
36
|
+
uuid.id,
|
|
37
|
+
custom_uuid.id,
|
|
38
|
+
email.id,
|
|
39
|
+
url.id,
|
|
40
|
+
time_slot.id,
|
|
41
|
+
];
|
|
42
|
+
var numberType = [number.id, unix_timestamp.id];
|
|
43
|
+
var arrayType = [
|
|
44
|
+
reference.id,
|
|
45
|
+
belongsTo.id,
|
|
46
|
+
multi_reference.id,
|
|
47
|
+
dynamic_option.id,
|
|
48
|
+
static_option.id,
|
|
49
|
+
];
|
|
50
|
+
if (stringType.includes(fieldType)) {
|
|
51
|
+
if (typeof fieldData !== "string")
|
|
52
|
+
fieldData = String(fieldData);
|
|
53
|
+
}
|
|
54
|
+
if (numberType.includes(fieldType)) {
|
|
55
|
+
if (typeof fieldData !== "number")
|
|
56
|
+
fieldData = Number(fieldData);
|
|
57
|
+
}
|
|
58
|
+
if (fieldType === boolean.id) {
|
|
59
|
+
if (typeof fieldData !== "boolean")
|
|
60
|
+
fieldData = Boolean(fieldData);
|
|
61
|
+
}
|
|
62
|
+
if (arrayType.includes(fieldType)) {
|
|
63
|
+
if (!Array.isArray(fieldData)) {
|
|
64
|
+
fieldData = fieldData ? [fieldData] : [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return fieldData;
|
|
68
|
+
};
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -27,3 +27,4 @@ __exportStar(require("./utils/uuid-generator"), exports);
|
|
|
27
27
|
__exportStar(require("./utils/check-error"), exports);
|
|
28
28
|
__exportStar(require("./utils/prepare-query"), exports);
|
|
29
29
|
__exportStar(require("./encryption/index"), exports);
|
|
30
|
+
__exportStar(require("./format-fields/index"), exports);
|
|
@@ -117,6 +117,7 @@ var handleMultErrorConfig = function (error, result, status) {
|
|
|
117
117
|
console.log("handleMultErrorConfig result :>> " + Object.keys(result));
|
|
118
118
|
apiErrorMessage = lodash_1.default.get(result, message);
|
|
119
119
|
if (apiErrorMessage) {
|
|
120
|
+
console.log("*** utility 3 ***");
|
|
120
121
|
if (typeof apiErrorMessage == "string") {
|
|
121
122
|
console.log("It's string so leave");
|
|
122
123
|
}
|
|
@@ -133,13 +134,16 @@ var handleMultErrorConfig = function (error, result, status) {
|
|
|
133
134
|
apiErrorValue = lodash_1.default.get(result, path);
|
|
134
135
|
}
|
|
135
136
|
else {
|
|
137
|
+
console.log("*** utility 7 ***");
|
|
136
138
|
apiErrorValue = status;
|
|
137
139
|
}
|
|
140
|
+
console.log("*** utility 8 ***");
|
|
138
141
|
}
|
|
139
142
|
else {
|
|
140
143
|
console.log("result", result);
|
|
141
144
|
apiErrorValue = status;
|
|
142
145
|
}
|
|
146
|
+
console.log("apiErrorValue", apiErrorValue);
|
|
143
147
|
apiErrorValue = "" + apiErrorValue;
|
|
144
148
|
console.log("apiErrorMessage", apiErrorMessage);
|
|
145
149
|
console.log("apiErrorValue", apiErrorValue);
|
|
@@ -150,6 +154,7 @@ var handleMultErrorConfig = function (error, result, status) {
|
|
|
150
154
|
};
|
|
151
155
|
}
|
|
152
156
|
else if (["false", "error", "fail", "failed"].includes(apiErrorValue.toLowerCase())) {
|
|
157
|
+
console.log("*** utility 9 ***");
|
|
153
158
|
return {
|
|
154
159
|
status: 400,
|
|
155
160
|
message: apiErrorMessage,
|
|
@@ -158,6 +163,8 @@ var handleMultErrorConfig = function (error, result, status) {
|
|
|
158
163
|
else if (voca_1.default.startsWith(apiErrorValue, "3") ||
|
|
159
164
|
voca_1.default.startsWith(apiErrorValue, "4") ||
|
|
160
165
|
voca_1.default.startsWith(apiErrorValue, "5")) {
|
|
166
|
+
console.log("*** utility 10 ***");
|
|
167
|
+
console.log("Compare 4");
|
|
161
168
|
return {
|
|
162
169
|
status: apiErrorValue,
|
|
163
170
|
message: apiErrorMessage,
|
|
@@ -66,10 +66,17 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
|
|
|
66
66
|
if (!query)
|
|
67
67
|
return [2 /*return*/, ""];
|
|
68
68
|
console.log("***1*** queryParser ***");
|
|
69
|
+
console.log("%%%%% refCollectionFieldsInItems", JSON.stringify(refCollectionFieldsInItems));
|
|
70
|
+
console.log("%%%% timezone", timezone);
|
|
71
|
+
console.log("collectionName", collectionName);
|
|
72
|
+
console.log("query", JSON.stringify(query));
|
|
73
|
+
console.log("constants", JSON.stringify(constants));
|
|
74
|
+
console.log("externalParams", JSON.stringify(externalParams));
|
|
75
|
+
console.log("currentUser", JSON.stringify(currentUser));
|
|
76
|
+
console.log("searchObj", JSON.stringify(searchObj));
|
|
77
|
+
console.log("searchQueryTypeObj", JSON.stringify(searchQueryTypeObj));
|
|
69
78
|
filterQuery = mongoFilterQuery(query, externalParams, constants, currentUser, currentTenant, timezone);
|
|
70
|
-
console.log("**************************");
|
|
71
79
|
console.log("filterQuery", JSON.stringify(filterQuery));
|
|
72
|
-
console.log("==> searchQueryTypeObj :>>", searchQueryTypeObj);
|
|
73
80
|
aggregateQuery = [];
|
|
74
81
|
queryStr = ".aggregate([])";
|
|
75
82
|
if (filterQuery.length &&
|
|
@@ -79,17 +86,22 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
|
|
|
79
86
|
if (searchObj) {
|
|
80
87
|
_a = getSearchObjQuery(searchObj, searchQueryTypeObj, timezone), searchAggregateQuery = _a.searchAggregateQuery, likeQuery = _a.likeQuery;
|
|
81
88
|
aggregateQuery = __spreadArray(__spreadArray([], aggregateQuery), searchAggregateQuery);
|
|
89
|
+
console.log("likeQuery after getSearchObjQuery", JSON.stringify(likeQuery));
|
|
82
90
|
if (likeQuery && likeQuery.length > 0) {
|
|
83
91
|
searchQueryObj = likeQuery;
|
|
84
92
|
}
|
|
85
93
|
}
|
|
94
|
+
console.log("aggregateQuery After search object condition", JSON.stringify(aggregateQuery));
|
|
95
|
+
console.log("searchQueryObj", JSON.stringify(searchQueryObj));
|
|
86
96
|
finalQuery = { $and: filterQuery };
|
|
87
97
|
if (searchQueryObj) {
|
|
88
98
|
(_c = finalQuery.$and).push.apply(_c, searchQueryObj);
|
|
89
99
|
}
|
|
100
|
+
console.log("finalQuery after", JSON.stringify(finalQuery));
|
|
90
101
|
aggregateQuery.push({ $match: finalQuery });
|
|
91
102
|
}
|
|
92
103
|
else if (searchObj) {
|
|
104
|
+
console.log("I donot have filter query");
|
|
93
105
|
_b = getSearchObjQuery(searchObj, searchQueryTypeObj, timezone), searchAggregateQuery = _b.searchAggregateQuery, likeQuery = _b.likeQuery;
|
|
94
106
|
aggregateQuery = __spreadArray(__spreadArray([], aggregateQuery), searchAggregateQuery);
|
|
95
107
|
if (likeQuery.length) {
|
|
@@ -97,6 +109,7 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
|
|
|
97
109
|
aggregateQuery.push({ $match: { $and: likeQuery } });
|
|
98
110
|
}
|
|
99
111
|
}
|
|
112
|
+
console.log("aggregateQuery", JSON.stringify(aggregateQuery));
|
|
100
113
|
console.log("==> query", query);
|
|
101
114
|
projection = mongoSelectQuery(query);
|
|
102
115
|
finder = query.finder, sortBy = query.sortBy, orderBy = query.orderBy;
|
|
@@ -208,6 +221,7 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
|
|
|
208
221
|
aggregateQuery.push({ $skip: +offset }, { $limit: +limit });
|
|
209
222
|
}
|
|
210
223
|
console.log("==> aggregateQuery", aggregateQuery);
|
|
224
|
+
console.log("==> aggregateQuery", JSON.stringify(aggregateQuery));
|
|
211
225
|
queryStr = ".aggregate(" + JSON.stringify(aggregateQuery) + ", { collation: { locale: \"en\" } })";
|
|
212
226
|
str = "req.db.collection('" + collectionName + "')" + queryStr;
|
|
213
227
|
str += ".toArray()";
|
|
@@ -191,14 +191,11 @@ var queryParserNew = function (collectionName, query, constants, externalParams,
|
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
});
|
|
194
|
-
console.log("==> aggregateQuery", aggregateQuery);
|
|
195
|
-
console.log("==> likeQuery", likeQuery_1);
|
|
196
194
|
if (likeQuery_1.length) {
|
|
197
195
|
// aggregateQuery.push({ $match: { $or: likeQuery } });
|
|
198
196
|
aggregateQuery.push({ $match: { $and: likeQuery_1 } });
|
|
199
197
|
}
|
|
200
198
|
}
|
|
201
|
-
console.log("==> query", query);
|
|
202
199
|
projection = mongoSelectQuery(query);
|
|
203
200
|
finder = query.finder, sortBy = query.sortBy, orderBy = query.orderBy, aggregateFunctionField = query.aggregateFunctionField;
|
|
204
201
|
if (!(finder != "COUNT" && refCollectionFieldsInItems)) return [3 /*break*/, 2];
|
|
@@ -329,7 +326,6 @@ var queryParserNew = function (collectionName, query, constants, externalParams,
|
|
|
329
326
|
if (finder != "COUNT" && limit) {
|
|
330
327
|
aggregateQuery.push({ $skip: +offset }, { $limit: +limit });
|
|
331
328
|
}
|
|
332
|
-
console.log("==> aggregateQuery", JSON.stringify(aggregateQuery, null, 2));
|
|
333
329
|
queryStr = ".aggregate(" + JSON.stringify(aggregateQuery) + ", { collation: { locale: \"en\" } })";
|
|
334
330
|
str = "req.db.collection('" + collectionName + "')" + queryStr;
|
|
335
331
|
str += ".toArray()";
|
|
@@ -601,20 +597,16 @@ var replaceExternalParams = function (query, requiredExternal, externalParams, c
|
|
|
601
597
|
else
|
|
602
598
|
return startValue_1 + "---" + endValue_1;
|
|
603
599
|
}
|
|
604
|
-
console.log("==> value", value);
|
|
605
|
-
console.log("==> currentUser", currentUser);
|
|
606
600
|
if (value.includes(drapcode_constant_1.CURRENT_USER)) {
|
|
607
601
|
if (value === drapcode_constant_1.CURRENT_USER) {
|
|
608
602
|
return currentUser["uuid"];
|
|
609
603
|
}
|
|
610
604
|
var splitValues = value.split("::");
|
|
611
|
-
console.log("splitValues", splitValues);
|
|
612
605
|
var refCollection = splitValues[1];
|
|
613
606
|
var refCollectionField = splitValues[2];
|
|
614
607
|
if (refCollection && refCollectionField) {
|
|
615
608
|
console.log("I have refCollection and Field");
|
|
616
609
|
var refCollectionData = currentUser[refCollection];
|
|
617
|
-
console.log("refCollectionData", refCollectionData);
|
|
618
610
|
if (!refCollectionData) {
|
|
619
611
|
return "";
|
|
620
612
|
}
|
package/build/utils/util.js
CHANGED
|
@@ -263,6 +263,8 @@ var replaceDataValueIntoExpression = function (expression, data, user, sessionVa
|
|
|
263
263
|
else {
|
|
264
264
|
if (Object.keys(data).length > 0) {
|
|
265
265
|
dataOfItem = exports.parseValueFromData(data, prop);
|
|
266
|
+
//TODO: Need better way
|
|
267
|
+
delete data[prop];
|
|
266
268
|
}
|
|
267
269
|
}
|
|
268
270
|
expression = replaceValueInExpression(needle, dataOfItem, expression);
|