@webtypen/webframez-core 0.3.64 → 0.3.65
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.
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { Request } from "../Router/Request";
|
|
2
|
+
export interface DatatableModelSelectorConfig {
|
|
3
|
+
value?: string;
|
|
4
|
+
label?: string;
|
|
5
|
+
subtitle?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface NormalizedDatatableModelSelectorConfig {
|
|
8
|
+
value: string;
|
|
9
|
+
label: string;
|
|
10
|
+
subtitle?: string;
|
|
11
|
+
}
|
|
2
12
|
export declare class Datatable {
|
|
3
13
|
collection: string | Function;
|
|
4
14
|
aggregation: {
|
|
@@ -26,6 +36,7 @@ export declare class Datatable {
|
|
|
26
36
|
defaultUnwind?: string | null;
|
|
27
37
|
disablePerPageConfig: boolean;
|
|
28
38
|
selectable: boolean;
|
|
39
|
+
modelSelector: boolean | DatatableModelSelectorConfig;
|
|
29
40
|
selectableFunctions: {
|
|
30
41
|
[key: string]: {
|
|
31
42
|
label: string;
|
|
@@ -38,6 +49,8 @@ export declare class Datatable {
|
|
|
38
49
|
};
|
|
39
50
|
} | null;
|
|
40
51
|
getInit(req: Request): Promise<any>;
|
|
52
|
+
getModelSelectorConfig(): NormalizedDatatableModelSelectorConfig | null;
|
|
53
|
+
getModelSelectorPreview(req: Request): Promise<any>;
|
|
41
54
|
getSelectableFunctionsDef(req: Request): Promise<{
|
|
42
55
|
label: string;
|
|
43
56
|
icon: string | undefined;
|
|
@@ -8,12 +8,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
11
22
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
24
|
};
|
|
14
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
26
|
exports.Datatable = void 0;
|
|
16
27
|
const moment_1 = __importDefault(require("moment"));
|
|
28
|
+
const mongodb_1 = require("mongodb");
|
|
17
29
|
const DBConnection_1 = require("../Database/DBConnection");
|
|
18
30
|
const NumericFunctions_1 = require("../Functions/NumericFunctions");
|
|
19
31
|
class Datatable {
|
|
@@ -32,6 +44,7 @@ class Datatable {
|
|
|
32
44
|
this.defaultUnwind = null;
|
|
33
45
|
this.disablePerPageConfig = false;
|
|
34
46
|
this.selectable = false;
|
|
47
|
+
this.modelSelector = false;
|
|
35
48
|
this.selectableFunctions = null;
|
|
36
49
|
}
|
|
37
50
|
getInit(req) {
|
|
@@ -41,6 +54,10 @@ class Datatable {
|
|
|
41
54
|
selectable: this.selectable ? true : false,
|
|
42
55
|
selectableFunctions: yield this.getSelectableFunctionsDef(req),
|
|
43
56
|
};
|
|
57
|
+
const modelSelector = this.getModelSelectorConfig();
|
|
58
|
+
if (modelSelector) {
|
|
59
|
+
out.modelSelector = modelSelector;
|
|
60
|
+
}
|
|
44
61
|
const promises = [];
|
|
45
62
|
promises.push(new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
46
63
|
out.filter = yield this.getFilter(req);
|
|
@@ -78,6 +95,73 @@ class Datatable {
|
|
|
78
95
|
return out;
|
|
79
96
|
});
|
|
80
97
|
}
|
|
98
|
+
getModelSelectorConfig() {
|
|
99
|
+
if (!this.modelSelector) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const config = this.modelSelector === true ? {} : this.modelSelector;
|
|
103
|
+
const subtitle = typeof config.subtitle === "string" && config.subtitle.trim() !== "" ? config.subtitle : undefined;
|
|
104
|
+
return Object.assign({ value: typeof config.value === "string" && config.value.trim() !== "" ? config.value.trim() : "_id", label: typeof config.label === "string" && config.label.trim() !== "" ? config.label.trim() : "_id" }, (subtitle ? { subtitle: subtitle.trim() } : {}));
|
|
105
|
+
}
|
|
106
|
+
getModelSelectorPreview(req) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const config = this.getModelSelectorConfig();
|
|
109
|
+
if (!config) {
|
|
110
|
+
throw new Error("Datatable is not enabled for model selection.");
|
|
111
|
+
}
|
|
112
|
+
const rawValue = req.body.value;
|
|
113
|
+
if (rawValue === undefined || rawValue === null || rawValue === "") {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
let value = rawValue;
|
|
117
|
+
if (config.value === "_id") {
|
|
118
|
+
if (rawValue instanceof mongodb_1.ObjectId) {
|
|
119
|
+
value = rawValue;
|
|
120
|
+
}
|
|
121
|
+
else if (typeof rawValue === "string" && mongodb_1.ObjectId.isValid(rawValue)) {
|
|
122
|
+
value = new mongodb_1.ObjectId(rawValue);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const normalizeStages = (stages) => (stages !== null && stages !== void 0 ? stages : []).map((stage) => {
|
|
129
|
+
if (!stage || typeof stage !== "object" || !Object.prototype.hasOwnProperty.call(stage, "skipStats")) {
|
|
130
|
+
return stage;
|
|
131
|
+
}
|
|
132
|
+
const { skipStats: _skipStats } = stage, normalized = __rest(stage, ["skipStats"]);
|
|
133
|
+
return normalized;
|
|
134
|
+
});
|
|
135
|
+
const aggregation = [
|
|
136
|
+
...normalizeStages(yield this.getAggregation(req)),
|
|
137
|
+
...normalizeStages(yield this.getSubAggregation(req)),
|
|
138
|
+
{ $match: { [config.value]: value } },
|
|
139
|
+
{ $limit: 1 },
|
|
140
|
+
];
|
|
141
|
+
const connection = yield DBConnection_1.DBConnection.getConnection();
|
|
142
|
+
const results = yield connection.client
|
|
143
|
+
.db(null)
|
|
144
|
+
.collection(yield this.getCollection(req))
|
|
145
|
+
.aggregate(aggregation, {
|
|
146
|
+
collation: {
|
|
147
|
+
locale: "de",
|
|
148
|
+
strength: 2,
|
|
149
|
+
},
|
|
150
|
+
})
|
|
151
|
+
.toArray();
|
|
152
|
+
if (!results[0]) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
let result = results[0];
|
|
156
|
+
if (this.onRow) {
|
|
157
|
+
result = yield this.onRow(result);
|
|
158
|
+
}
|
|
159
|
+
if (this.onAttributes) {
|
|
160
|
+
result.__cellattr = yield this.onAttributes(result);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
81
165
|
getSelectableFunctionsDef(req) {
|
|
82
166
|
return __awaiter(this, void 0, void 0, function* () {
|
|
83
167
|
if (!this.selectableFunctions) {
|
|
@@ -43,6 +43,18 @@ class DatatableController extends Controller_1.Controller {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
const table = new tableClass();
|
|
46
|
+
if (req.body.mode === "model-selector-preview") {
|
|
47
|
+
if (!table.getModelSelectorConfig()) {
|
|
48
|
+
return res.status(403).send({
|
|
49
|
+
status: "error",
|
|
50
|
+
message: "Datatable is not enabled for model selection.",
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return res.send({
|
|
54
|
+
status: "success",
|
|
55
|
+
data: yield table.getModelSelectorPreview(req),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
46
58
|
if (req.body.type === "stats" || req.body.mode === "stats") {
|
|
47
59
|
return res.send({
|
|
48
60
|
status: "success",
|
package/package.json
CHANGED