c2-mongoose 1.0.3 → 1.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.
package/dist/model/Search.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ declare abstract class Search {
|
|
|
13
13
|
buildPath: (target: string, nested?: string) => any;
|
|
14
14
|
buildOrdenation: () => any;
|
|
15
15
|
private isPageable;
|
|
16
|
-
|
|
16
|
+
diacriticSensitiveRegex: (string?: string) => string;
|
|
17
17
|
search: (model: mongoose.Model<any>) => Promise<SearchResponse<any>>;
|
|
18
18
|
private searchPageable;
|
|
19
19
|
private searchNoPageable;
|
package/dist/model/Search.js
CHANGED
|
@@ -84,6 +84,9 @@ var Search = /** @class */ (function () {
|
|
|
84
84
|
return order;
|
|
85
85
|
};
|
|
86
86
|
this.isPageable = function () {
|
|
87
|
+
if ((0, Utils_1.isEmpty)(_this.page)) {
|
|
88
|
+
_this.page = 1;
|
|
89
|
+
}
|
|
87
90
|
if (_this.page >= 0) {
|
|
88
91
|
return true;
|
|
89
92
|
}
|
|
@@ -251,7 +254,7 @@ var Search = /** @class */ (function () {
|
|
|
251
254
|
this.orderBy = params.orderBy || "_id";
|
|
252
255
|
this.select = params.select;
|
|
253
256
|
this.populate = params.populate;
|
|
254
|
-
this.page = params.page ||
|
|
257
|
+
this.page = params.page || undefined;
|
|
255
258
|
this.limit = params.limit || 25;
|
|
256
259
|
this.buildPopulate();
|
|
257
260
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-mongoose",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Lib to make any search in database mongoose and use as basic crud",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc"
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"preversion": "tsc",
|
|
10
|
+
"postversion": "git push && git push --tags && npm publish",
|
|
11
|
+
"publish-patch": "npm version patch -m 'Publishing a patch'",
|
|
12
|
+
"publish-minor": "npm version minor --force -m 'Publishing a minor'",
|
|
13
|
+
"publish-major": "npm version major --force -m 'Publishing a major'"
|
|
9
14
|
},
|
|
10
15
|
"repository": {
|
|
11
16
|
"type": "git",
|
package/src/model/Search.ts
CHANGED
|
@@ -18,7 +18,7 @@ abstract class Search {
|
|
|
18
18
|
this.orderBy = params.orderBy || "_id"
|
|
19
19
|
this.select = params.select
|
|
20
20
|
this.populate = params.populate
|
|
21
|
-
this.page = params.page ||
|
|
21
|
+
this.page = params.page || undefined
|
|
22
22
|
this.limit = params.limit || 25
|
|
23
23
|
this.buildPopulate()
|
|
24
24
|
}
|
|
@@ -57,6 +57,10 @@ abstract class Search {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
private isPageable = () => {
|
|
60
|
+
if (isEmpty(this.page)){
|
|
61
|
+
this.page = 1
|
|
62
|
+
}
|
|
63
|
+
|
|
60
64
|
if (this.page >= 0) {
|
|
61
65
|
return true
|
|
62
66
|
}
|
package/dist/src/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const Search_1 = __importDefault(require("./model/Search"));
|
|
7
|
-
exports.default = Search_1.default;
|
package/dist/src/model/Search.js
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const Environment_1 = require("../configuration/Environment");
|
|
13
|
-
const Utils_1 = require("../utils/Utils");
|
|
14
|
-
class Search {
|
|
15
|
-
constructor(params) {
|
|
16
|
-
this.buildPopulate = () => {
|
|
17
|
-
if ((0, Utils_1.isEmpty)(this.populate)) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
var propertiesArray = this.populate.split(',');
|
|
21
|
-
var populates = [];
|
|
22
|
-
for (var property of propertiesArray) {
|
|
23
|
-
let [first, ...rest] = property.split('.');
|
|
24
|
-
let nested = rest.join('.');
|
|
25
|
-
populates.push(this.buildPath(first, nested));
|
|
26
|
-
}
|
|
27
|
-
this.populate = populates;
|
|
28
|
-
};
|
|
29
|
-
this.buildPath = (target, nested = "") => {
|
|
30
|
-
var populate = {};
|
|
31
|
-
populate.path = target;
|
|
32
|
-
if ((0, Utils_1.isNotEmpty)(nested)) {
|
|
33
|
-
let [first, ...rest] = nested.split('.');
|
|
34
|
-
let nested2 = rest.join('.');
|
|
35
|
-
populate.populate = this.buildPath(first, nested2);
|
|
36
|
-
}
|
|
37
|
-
return populate;
|
|
38
|
-
};
|
|
39
|
-
this.buildOrdenation = () => {
|
|
40
|
-
let order = {};
|
|
41
|
-
this.orderBy = this.orderBy || "_id";
|
|
42
|
-
order[this.orderBy] = this.order === "desc" ? -1 : 1;
|
|
43
|
-
return order;
|
|
44
|
-
};
|
|
45
|
-
this.isPageable = () => {
|
|
46
|
-
if (this.page >= 0) {
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
return false;
|
|
50
|
-
};
|
|
51
|
-
this.diacriticSensitiveRegex = (string = "") => {
|
|
52
|
-
return string
|
|
53
|
-
.replace(/[a|á|à|ä|â|A|Á|Â|Ã|Ä]/g, '[a,á,à,ä,â,A,Á,Â,Ã,Ä]')
|
|
54
|
-
.replace(/[e|é|ë|è|E|É|Ë|È]/g, '[e,é,ë,è,E,É,Ë,È]')
|
|
55
|
-
.replace(/[i|í|ï|ì|I|Í|Ï|Ì]/g, '[i,í,ï,ì,I,Í,Ï,Ì]')
|
|
56
|
-
.replace(/[o|ó|ö|ò|õ|O|Ó|Ö|Ô|Õ]/g, '[o,ó,ö,ò,õ,O,Ó,Ö,Ô,Õ]')
|
|
57
|
-
.replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
|
|
58
|
-
.replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]');
|
|
59
|
-
};
|
|
60
|
-
this.search = (model) => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
-
if (this.isPageable()) {
|
|
62
|
-
return yield this.searchPageable(model);
|
|
63
|
-
}
|
|
64
|
-
return yield this.searchNoPageable(model);
|
|
65
|
-
});
|
|
66
|
-
this.searchPageable = (model) => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
const sort = this.buildOrdenation();
|
|
68
|
-
var items = yield model
|
|
69
|
-
.find(this.filters, this.select)
|
|
70
|
-
.populate(this.populate)
|
|
71
|
-
.skip(this.page * this.limit)
|
|
72
|
-
.limit(this.limit)
|
|
73
|
-
.sort(sort)
|
|
74
|
-
.collation({
|
|
75
|
-
locale: Environment_1.dbCollation || "pt"
|
|
76
|
-
});
|
|
77
|
-
return this.result(model, items);
|
|
78
|
-
});
|
|
79
|
-
this.searchNoPageable = (model) => __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
const sort = this.buildOrdenation();
|
|
81
|
-
var items = yield model
|
|
82
|
-
.find(this.filters, this.select)
|
|
83
|
-
.populate(this.populate)
|
|
84
|
-
.sort(sort)
|
|
85
|
-
.collation({
|
|
86
|
-
locale: Environment_1.dbCollation || "pt"
|
|
87
|
-
});
|
|
88
|
-
return this.result(model, items);
|
|
89
|
-
});
|
|
90
|
-
this.result = (model, items) => __awaiter(this, void 0, void 0, function* () {
|
|
91
|
-
var total = yield this.count(model);
|
|
92
|
-
var paging = {};
|
|
93
|
-
paging.total = total;
|
|
94
|
-
paging.page = this.page;
|
|
95
|
-
paging.limit = this.limit;
|
|
96
|
-
var searchResponse = {};
|
|
97
|
-
searchResponse.items = items;
|
|
98
|
-
searchResponse.paging = paging;
|
|
99
|
-
return searchResponse;
|
|
100
|
-
});
|
|
101
|
-
this.count = (model) => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
return yield model.countDocuments(this.filters).exec();
|
|
103
|
-
});
|
|
104
|
-
this.sumBy = (model, _sum, _by) => __awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
const ret = yield model.aggregate([
|
|
106
|
-
{
|
|
107
|
-
'$match': this.filters
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
$group: {
|
|
111
|
-
_id: _by,
|
|
112
|
-
totalValue: { "$sum": _sum },
|
|
113
|
-
count: { "$sum": 1 }
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
]);
|
|
117
|
-
return ret;
|
|
118
|
-
});
|
|
119
|
-
this.buildDefaultFilters = (objectSearch) => {
|
|
120
|
-
let filters = { $and: [] };
|
|
121
|
-
Object.entries(objectSearch).forEach(([key, value]) => {
|
|
122
|
-
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
123
|
-
let condition = {};
|
|
124
|
-
if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'searchText'].includes(key)) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
if (key.endsWith('DateRange')) {
|
|
128
|
-
var keyAux = key.replace('Range', '');
|
|
129
|
-
var values = value;
|
|
130
|
-
if ((0, Utils_1.isNotEmpty)(values[0])) {
|
|
131
|
-
condition[keyAux] = Object.assign(Object.assign({}, condition[keyAux]), { $gte: new Date(values[0]) });
|
|
132
|
-
}
|
|
133
|
-
if ((0, Utils_1.isNotEmpty)(values[1])) {
|
|
134
|
-
condition[keyAux] = Object.assign(Object.assign({}, condition[keyAux]), { $lte: new Date(values[1]) });
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
if (!Array.isArray(value)) {
|
|
139
|
-
condition[key] = value;
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
condition[key] = { $in: value };
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
filters.$and.push(condition);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
if ((0, Utils_1.isNotEmpty)(objectSearch.model)) {
|
|
149
|
-
this.addFilterModel(objectSearch.model, filters);
|
|
150
|
-
}
|
|
151
|
-
if (filters.$and.length === 0)
|
|
152
|
-
delete filters['$and'];
|
|
153
|
-
return filters;
|
|
154
|
-
};
|
|
155
|
-
this.addFilterModel = (model, filters) => {
|
|
156
|
-
Object.entries(model).forEach(([key, value]) => {
|
|
157
|
-
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
158
|
-
let condition = {};
|
|
159
|
-
condition[key] = value;
|
|
160
|
-
filters.$and.push(condition);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
this.searchText = params.searchText || "";
|
|
165
|
-
this.order = params.order || "asc";
|
|
166
|
-
this.orderBy = params.orderBy || "_id";
|
|
167
|
-
this.select = params.select;
|
|
168
|
-
this.populate = params.populate;
|
|
169
|
-
this.page = params.page || -1;
|
|
170
|
-
this.limit = params.limit || 25;
|
|
171
|
-
this.buildPopulate();
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
exports.default = Search;
|
package/dist/src/utils/Utils.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.compareDesc = exports.compareAsc = exports.isNotIterable = exports.isIterable = exports.isEmpty = exports.isNotEmpty = void 0;
|
|
4
|
-
const isNotEmpty = (obj) => {
|
|
5
|
-
return !(0, exports.isEmpty)(obj);
|
|
6
|
-
};
|
|
7
|
-
exports.isNotEmpty = isNotEmpty;
|
|
8
|
-
const isEmpty = (obj) => {
|
|
9
|
-
if (!obj) {
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
12
|
-
if (Array.isArray(obj)) {
|
|
13
|
-
if (obj.length == 0) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
if (obj === "") {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
return false;
|
|
21
|
-
};
|
|
22
|
-
exports.isEmpty = isEmpty;
|
|
23
|
-
const isIterable = (input) => {
|
|
24
|
-
if (input === null || input === undefined) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
if ('string' === typeof input) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
return typeof input[Symbol.iterator] === 'function';
|
|
31
|
-
};
|
|
32
|
-
exports.isIterable = isIterable;
|
|
33
|
-
const isNotIterable = (input) => {
|
|
34
|
-
return !(0, exports.isIterable)(input);
|
|
35
|
-
};
|
|
36
|
-
exports.isNotIterable = isNotIterable;
|
|
37
|
-
const compareAsc = (a, b) => {
|
|
38
|
-
if (a._id < b._id) {
|
|
39
|
-
return -1;
|
|
40
|
-
}
|
|
41
|
-
if (a._id > b._id) {
|
|
42
|
-
return 1;
|
|
43
|
-
}
|
|
44
|
-
return 0;
|
|
45
|
-
};
|
|
46
|
-
exports.compareAsc = compareAsc;
|
|
47
|
-
const compareDesc = (a, b) => {
|
|
48
|
-
if (a._id > b._id) {
|
|
49
|
-
return -1;
|
|
50
|
-
}
|
|
51
|
-
if (a._id < b._id) {
|
|
52
|
-
return 1;
|
|
53
|
-
}
|
|
54
|
-
return 0;
|
|
55
|
-
};
|
|
56
|
-
exports.compareDesc = compareDesc;
|