@webtypen/webframez-core 0.2.33 → 0.2.35

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.
@@ -50,6 +50,7 @@ export declare class DataBuilder {
50
50
  getFieldsFrontend(fields: any, payload?: any): Promise<any>;
51
51
  getTypeFromRequest(req: any): DataBuilderType;
52
52
  validateFields(db: any, type: any, fields: any, req: Request, errors?: any, path?: string): Promise<any>;
53
+ handleUnique(db: any, req: any, field: any, type: any): Promise<boolean>;
53
54
  typeForFrontend(type: any, req: any): Promise<any>;
54
55
  loadType(req: Request): Promise<{
55
56
  status: string;
@@ -126,12 +126,12 @@ class DataBuilder {
126
126
  if (fields[key].unique) {
127
127
  let isUnique = false;
128
128
  if (typeof fields[key].unique === "function") {
129
- yield fields[key].unique(req.body.data, req);
129
+ isUnique = yield fields[key].unique(req.body.data, req);
130
130
  }
131
- else {
132
- throw new Error("Missing unique-function ...");
131
+ else if (typeof fields[key].unique === "object") {
132
+ isUnique = yield this.handleUnique(db, req, fields[key], type);
133
133
  }
134
- if (isUnique) {
134
+ if (!isUnique) {
135
135
  errors[fieldPath] = "Es gibt bereits einen anderen Datensatz mit diesem Wert.";
136
136
  continue;
137
137
  }
@@ -151,6 +151,40 @@ class DataBuilder {
151
151
  return errors;
152
152
  });
153
153
  }
154
+ handleUnique(db, req, field, type) {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ const match = typeof field.unique.match === "function"
157
+ ? yield field.unique.match(req)
158
+ : typeof field.unique.match === "object"
159
+ ? field.unique.match
160
+ : {};
161
+ const check = yield db
162
+ .collection(typeof field.unique.collection === "string" && field.unique.collection.trim() !== ""
163
+ ? field.unique.collection
164
+ : type.collection)
165
+ .aggregate([
166
+ { $match: match },
167
+ ...(typeof field.unique.aggregation === "function"
168
+ ? yield field.aggregation(req)
169
+ : field.aggregation && Array.isArray(field.aggregation)
170
+ ? field.aggregation
171
+ : []),
172
+ ])
173
+ .toArray();
174
+ if (!check || check.length < 1) {
175
+ return true;
176
+ }
177
+ if (!req.body.__builder_id || req.body.__builder_id === "new") {
178
+ return false;
179
+ }
180
+ for (let el of check) {
181
+ if (el && el._id && el._id.toString() !== req.body.__builder_id) {
182
+ return true;
183
+ }
184
+ }
185
+ return true;
186
+ });
187
+ }
154
188
  typeForFrontend(type, req) {
155
189
  return __awaiter(this, void 0, void 0, function* () {
156
190
  const newData = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.33",
3
+ "version": "0.2.35",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -171,12 +171,12 @@ export class DataBuilder {
171
171
  if (fields[key].unique) {
172
172
  let isUnique = false;
173
173
  if (typeof fields[key].unique === "function") {
174
- await fields[key].unique(req.body.data, req);
175
- } else {
176
- throw new Error("Missing unique-function ...");
174
+ isUnique = await fields[key].unique(req.body.data, req);
175
+ } else if (typeof fields[key].unique === "object") {
176
+ isUnique = await this.handleUnique(db, req, fields[key], type);
177
177
  }
178
178
 
179
- if (isUnique) {
179
+ if (!isUnique) {
180
180
  errors[fieldPath] = "Es gibt bereits einen anderen Datensatz mit diesem Wert.";
181
181
  continue;
182
182
  }
@@ -199,6 +199,45 @@ export class DataBuilder {
199
199
  return errors;
200
200
  }
201
201
 
202
+ async handleUnique(db: any, req: any, field: any, type: any) {
203
+ const match: any =
204
+ typeof field.unique.match === "function"
205
+ ? await field.unique.match(req)
206
+ : typeof field.unique.match === "object"
207
+ ? field.unique.match
208
+ : {};
209
+ const check = await db
210
+ .collection(
211
+ typeof field.unique.collection === "string" && field.unique.collection.trim() !== ""
212
+ ? field.unique.collection
213
+ : type.collection
214
+ )
215
+ .aggregate([
216
+ { $match: match },
217
+ ...(typeof field.unique.aggregation === "function"
218
+ ? await field.aggregation(req)
219
+ : field.aggregation && Array.isArray(field.aggregation)
220
+ ? field.aggregation
221
+ : []),
222
+ ])
223
+ .toArray();
224
+
225
+ if (!check || check.length < 1) {
226
+ return true;
227
+ }
228
+
229
+ if (!req.body.__builder_id || req.body.__builder_id === "new") {
230
+ return false;
231
+ }
232
+
233
+ for (let el of check) {
234
+ if (el && el._id && el._id.toString() !== req.body.__builder_id) {
235
+ return true;
236
+ }
237
+ }
238
+ return true;
239
+ }
240
+
202
241
  async typeForFrontend(type: any, req: any) {
203
242
  const newData: any = {};
204
243
  for (let key in type) {