@webtypen/webframez-core 0.2.12 → 0.2.14

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.
@@ -2,12 +2,15 @@ import { QueryBuilder } from "./QueryBuilder";
2
2
  type ObjectIDType = {
3
3
  noExceptions?: Boolean;
4
4
  };
5
+ export declare function hasOne(modelGetter: () => any, foreignKey: string, localKey?: string, queryFunction?: Function): (target: any, propertyKey: string, v2?: any) => void;
6
+ export declare function hasMany(modelGetter: () => any, foreignKey: string, localKey?: string, queryFunction?: Function): (target: any, propertyKey: string) => void;
5
7
  export declare class Model {
6
8
  [key: string]: any | undefined;
7
9
  __primaryKey: string;
8
10
  __table: string;
9
11
  __connection: string | undefined;
10
12
  __hidden: string[];
13
+ __dependencies: any;
11
14
  __unmapped: string[];
12
15
  __unmappedSystem: string[];
13
16
  static objectId(val?: any, options?: ObjectIDType): Promise<any>;
@@ -52,6 +55,7 @@ export declare class Model {
52
55
  * @returns any
53
56
  */
54
57
  static aggregate(aggregation: any, options?: any, collection?: string): Promise<any>;
58
+ buildRelationship(model: any, foreignKey: string, localKey?: string): any;
55
59
  /**
56
60
  * Returns the model-data without system- and unmapped-fields (new js-object)
57
61
  * @returns object
@@ -9,17 +9,91 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Model = void 0;
12
+ exports.Model = exports.hasMany = exports.hasOne = void 0;
13
13
  const DBConnection_1 = require("./DBConnection");
14
14
  const QueryBuilder_1 = require("./QueryBuilder");
15
+ function hasOne(modelGetter, foreignKey, localKey, queryFunction) {
16
+ return function (target, propertyKey, v2) {
17
+ Object.defineProperty(target, propertyKey, {
18
+ value: function (options) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ if ((!options || !options.force) && this.__dependencies && this.__dependencies[propertyKey] !== undefined) {
21
+ return this.__dependencies[propertyKey];
22
+ }
23
+ const model = modelGetter();
24
+ if (!model) {
25
+ throw new Error(`Model for foreignKey "${foreignKey}" konnte nicht aufgelöst werden.`);
26
+ }
27
+ if (!this.__dependencies) {
28
+ this.__dependencies = {};
29
+ }
30
+ const query = this.buildRelationship(model, localKey !== null && localKey !== void 0 ? localKey : "_id", foreignKey);
31
+ if (queryFunction && typeof queryFunction === "function") {
32
+ queryFunction(query);
33
+ }
34
+ if (options && options.query) {
35
+ return query;
36
+ }
37
+ this.__dependencies[propertyKey] = yield query.first();
38
+ return this.__dependencies[propertyKey];
39
+ });
40
+ },
41
+ writable: false,
42
+ configurable: true,
43
+ });
44
+ };
45
+ }
46
+ exports.hasOne = hasOne;
47
+ function hasMany(modelGetter, foreignKey, localKey, queryFunction) {
48
+ return function (target, propertyKey) {
49
+ Object.defineProperty(target, propertyKey, {
50
+ value: function (options) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ if ((!options || !options.force) && this.__dependencies && this.__dependencies[propertyKey] !== undefined) {
53
+ return this.__dependencies[propertyKey];
54
+ }
55
+ const model = modelGetter();
56
+ if (!model) {
57
+ throw new Error(`Model for foreignKey "${foreignKey}" konnte nicht aufgelöst werden.`);
58
+ }
59
+ if (!this.__dependencies) {
60
+ this.__dependencies = {};
61
+ }
62
+ const query = this.buildRelationship(model, foreignKey, localKey);
63
+ if (queryFunction && typeof queryFunction === "function") {
64
+ queryFunction(query);
65
+ }
66
+ if (options && options.query) {
67
+ return query;
68
+ }
69
+ this.__dependencies[propertyKey] = yield query.get();
70
+ return this.__dependencies[propertyKey];
71
+ });
72
+ },
73
+ writable: false,
74
+ configurable: true,
75
+ });
76
+ };
77
+ }
78
+ exports.hasMany = hasMany;
15
79
  class Model {
16
80
  constructor() {
17
81
  this.__primaryKey = "_id";
18
82
  this.__table = "";
19
83
  this.__connection = undefined;
20
84
  this.__hidden = [];
85
+ this.__dependencies = {};
21
86
  this.__unmapped = [];
22
- this.__unmappedSystem = ["__primaryKey", "__table", "__connection", "__unmapped", "__unmappedSystem", "__is_deleted", "__hidden"];
87
+ this.__unmappedSystem = [
88
+ "__primaryKey",
89
+ "__table",
90
+ "__connection",
91
+ "__unmapped",
92
+ "__unmappedSystem",
93
+ "__is_deleted",
94
+ "__hidden",
95
+ "__dependencies",
96
+ ];
23
97
  }
24
98
  static objectId(val, options) {
25
99
  return __awaiter(this, void 0, void 0, function* () {
@@ -141,9 +215,12 @@ class Model {
141
215
  type: "aggregation",
142
216
  table: collection ? collection : model ? model.__table : undefined,
143
217
  aggregation: aggregation,
144
- }, model.__connection, options);
218
+ }, model.__connection);
145
219
  });
146
220
  }
221
+ buildRelationship(model, foreignKey, localKey) {
222
+ return model.where(foreignKey, "=", this[localKey !== null && localKey !== void 0 ? localKey : "_id"]);
223
+ }
147
224
  /**
148
225
  * Returns the model-data without system- and unmapped-fields (new js-object)
149
226
  * @returns object
@@ -318,7 +318,7 @@ class Datatable {
318
318
  break;
319
319
  }
320
320
  if (value !== undefined) {
321
- out[key] = value;
321
+ out[filterEl && filterEl.mapping && filterEl.mapping.trim() !== "" ? filterEl.mapping : key] = value;
322
322
  }
323
323
  }
324
324
  return out;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
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",
@@ -5,14 +5,92 @@ type ObjectIDType = {
5
5
  noExceptions?: Boolean;
6
6
  };
7
7
 
8
+ export function hasOne(modelGetter: () => any, foreignKey: string, localKey?: string, queryFunction?: Function) {
9
+ return function (target: any, propertyKey: string, v2?: any) {
10
+ Object.defineProperty(target, propertyKey, {
11
+ value: async function (options?: any) {
12
+ if ((!options || !options.force) && this.__dependencies && this.__dependencies[propertyKey] !== undefined) {
13
+ return this.__dependencies[propertyKey];
14
+ }
15
+
16
+ const model = modelGetter();
17
+ if (!model) {
18
+ throw new Error(`Model for foreignKey "${foreignKey}" konnte nicht aufgelöst werden.`);
19
+ }
20
+
21
+ if (!this.__dependencies) {
22
+ this.__dependencies = {};
23
+ }
24
+
25
+ const query = this.buildRelationship(model, localKey ?? "_id", foreignKey);
26
+ if (queryFunction && typeof queryFunction === "function") {
27
+ queryFunction(query);
28
+ }
29
+
30
+ if (options && options.query) {
31
+ return query;
32
+ }
33
+ this.__dependencies[propertyKey] = await query.first();
34
+ return this.__dependencies[propertyKey];
35
+ },
36
+ writable: false,
37
+ configurable: true,
38
+ });
39
+ };
40
+ }
41
+
42
+ export function hasMany(modelGetter: () => any, foreignKey: string, localKey?: string, queryFunction?: Function) {
43
+ return function (target: any, propertyKey: string) {
44
+ Object.defineProperty(target, propertyKey, {
45
+ value: async function (options?: any) {
46
+ if ((!options || !options.force) && this.__dependencies && this.__dependencies[propertyKey] !== undefined) {
47
+ return this.__dependencies[propertyKey];
48
+ }
49
+
50
+ const model = modelGetter();
51
+ if (!model) {
52
+ throw new Error(`Model for foreignKey "${foreignKey}" konnte nicht aufgelöst werden.`);
53
+ }
54
+
55
+ if (!this.__dependencies) {
56
+ this.__dependencies = {};
57
+ }
58
+
59
+ const query = this.buildRelationship(model, foreignKey, localKey);
60
+ if (queryFunction && typeof queryFunction === "function") {
61
+ queryFunction(query);
62
+ }
63
+
64
+ if (options && options.query) {
65
+ return query;
66
+ }
67
+ this.__dependencies[propertyKey] = await query.get();
68
+ return this.__dependencies[propertyKey];
69
+ },
70
+ writable: false,
71
+ configurable: true,
72
+ });
73
+ };
74
+ }
75
+
8
76
  export class Model {
9
77
  [key: string]: any | undefined; // allow-custom-properties
10
78
  __primaryKey = "_id";
11
79
  __table: string = "";
12
80
  __connection: string | undefined = undefined;
13
81
  __hidden: string[] = [];
82
+ __dependencies: any = {};
14
83
  __unmapped: string[] = [];
15
- __unmappedSystem: string[] = ["__primaryKey", "__table", "__connection", "__unmapped", "__unmappedSystem", "__is_deleted", "__hidden"];
84
+ __unmappedSystem: string[] = [
85
+ "__primaryKey",
86
+ "__table",
87
+ "__connection",
88
+ "__unmapped",
89
+ "__unmappedSystem",
90
+ "__is_deleted",
91
+ "__hidden",
92
+ "__dependencies",
93
+ ];
16
94
 
17
95
  static async objectId(val?: any, options?: ObjectIDType) {
18
96
  const model = new this();
@@ -124,7 +202,7 @@ export class Model {
124
202
  * @param aggregation
125
203
  * @returns any
126
204
  */
127
- static async aggregate(aggregation: any, options?: any,collection?: string) {
205
+ static async aggregate(aggregation: any, options?: any, collection?: string) {
128
206
  const model = new this();
129
207
  return await DBConnection.execute(
130
208
  {
@@ -132,11 +210,14 @@ export class Model {
132
210
  table: collection ? collection : model ? model.__table : undefined,
133
211
  aggregation: aggregation,
134
212
  },
135
- model.__connection,
136
- options
213
+ model.__connection
137
214
  );
138
215
  }
139
216
 
217
+ buildRelationship(model: any, foreignKey: string, localKey?: string): any {
218
+ return model.where(foreignKey, "=", this[localKey ?? "_id"]);
219
+ }
220
+
140
221
  /**
141
222
  * Returns the model-data without system- and unmapped-fields (new js-object)
142
223
  * @returns object
@@ -332,7 +332,7 @@ export class Datatable {
332
332
  }
333
333
 
334
334
  if (value !== undefined) {
335
- out[key] = value;
335
+ out[filterEl && filterEl.mapping && filterEl.mapping.trim() !== "" ? filterEl.mapping : key] = value;
336
336
  }
337
337
  }
338
338
  return out;
package/tsconfig.json CHANGED
@@ -10,7 +10,8 @@
10
10
  "declaration": true,
11
11
  "outDir": "dist",
12
12
  "rootDir": "src",
13
- "strict": true
13
+ "strict": true,
14
+ "experimentalDecorators": true
14
15
  },
15
16
  "exclude": ["dist", "node_modules", "src/**/*.spec.ts"],
16
17
  "include": ["src"]