@webtypen/webframez-core 0.2.40 → 0.2.42

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/README.md CHANGED
@@ -141,6 +141,7 @@ Models are usually placed under `app/Models`. Each model class inherits from `Mo
141
141
  import { Model, hasMany, QueryBuilder } from "@webtypen/webframez-core";
142
142
  import { File } from "./File";
143
143
  import { Session } from "./Session";
144
+ import { CustomerGroups } from "./CustomerGroups";
144
145
 
145
146
  export class User extends Model {
146
147
  __table = "users";
@@ -157,13 +158,22 @@ export class User extends Model {
157
158
  avatar!: () => File;
158
159
 
159
160
  /**
160
- * @param File Dependency Model
161
+ * @param Session Dependency Model
161
162
  * @param foreignKey
162
163
  * @param localKey (optional)
163
164
  * @param queryBuilderFunction (optional) customize the query
164
165
  **/
165
166
  @hasMany(() => Session, "_user")
166
167
  sessions!: () => Session[];
168
+
169
+ /**
170
+ * @param CustomerGroups Dependency Model
171
+ * @param foreignKey
172
+ * @param localKey (optional)
173
+ * @param queryBuilderFunction (optional) customize the query
174
+ **/
175
+ @hasManyArray(() => CustomerGroups, "_groups")
176
+ groups!: () => CustomerGroups[];
167
177
  }
168
178
  ```
169
179
 
@@ -181,6 +191,7 @@ const testUser = await User.where("email", "=", "test@test.de").where("is_active
181
191
  ```ts
182
192
  const sessions = await testUser.session();
183
193
  const avatarUrl = await testUser.avatar()?.url;
194
+ const customerGroups = await testUser.groups();
184
195
  ```
185
196
 
186
197
  ##### Disable cache / force dependency-reload:
@@ -16,7 +16,7 @@ export type DataBuilderSchema = {
16
16
  [key: string]: any;
17
17
  };
18
18
  newDataHandler?: Function;
19
- canDelete: any;
19
+ canDelete?: any;
20
20
  };
21
21
  export type DataBuilderType = {
22
22
  key: string;
@@ -4,6 +4,7 @@ type ObjectIDType = {
4
4
  };
5
5
  export declare function hasOne(modelGetter: () => any, foreignKey: string, localKey?: string, queryFunction?: Function): (target: any, propertyKey: string, v2?: any) => void;
6
6
  export declare function hasMany(modelGetter: () => any, foreignKey: string, localKey?: string, queryFunction?: Function): (target: any, propertyKey: string) => void;
7
+ export declare function hasManyArray(modelGetter: () => any, localArrayKey: string, foreignKey?: string, queryFunction?: Function): (target: any, propertyKey: string) => void;
7
8
  export declare class Model {
8
9
  [key: string]: any | undefined;
9
10
  __primaryKey: string;
@@ -56,6 +57,7 @@ export declare class Model {
56
57
  */
57
58
  static aggregate(aggregation: any, options?: any, collection?: string): Promise<any>;
58
59
  buildRelationship(model: any, foreignKey: string, localKey?: string): any;
60
+ buildArrayRelationship(model: any, localArrayKey: string, foreignKey?: string): any;
59
61
  /**
60
62
  * Returns the model-data without system- and unmapped-fields (new js-object)
61
63
  * @returns object
@@ -9,7 +9,7 @@ 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 = exports.hasMany = exports.hasOne = void 0;
12
+ exports.Model = exports.hasManyArray = exports.hasMany = exports.hasOne = void 0;
13
13
  const DBConnection_1 = require("./DBConnection");
14
14
  const QueryBuilder_1 = require("./QueryBuilder");
15
15
  function hasOne(modelGetter, foreignKey, localKey, queryFunction) {
@@ -76,6 +76,38 @@ function hasMany(modelGetter, foreignKey, localKey, queryFunction) {
76
76
  };
77
77
  }
78
78
  exports.hasMany = hasMany;
79
+ function hasManyArray(modelGetter, localArrayKey, foreignKey, queryFunction) {
80
+ return function (target, propertyKey) {
81
+ Object.defineProperty(target, propertyKey, {
82
+ value: function (options) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ if ((!options || !options.force) && this.__dependencies && this.__dependencies[propertyKey] !== undefined) {
85
+ return this.__dependencies[propertyKey];
86
+ }
87
+ const model = modelGetter();
88
+ if (!model) {
89
+ throw new Error(`Model for foreignKey "${foreignKey}" konnte nicht aufgelöst werden.`);
90
+ }
91
+ if (!this.__dependencies) {
92
+ this.__dependencies = {};
93
+ }
94
+ const query = this.buildArrayRelationship(model, localArrayKey, foreignKey);
95
+ if (queryFunction && typeof queryFunction === "function") {
96
+ queryFunction(query);
97
+ }
98
+ if (options && options.query) {
99
+ return query;
100
+ }
101
+ this.__dependencies[propertyKey] = yield query.get();
102
+ return this.__dependencies[propertyKey];
103
+ });
104
+ },
105
+ writable: false,
106
+ configurable: true,
107
+ });
108
+ };
109
+ }
110
+ exports.hasManyArray = hasManyArray;
79
111
  class Model {
80
112
  constructor() {
81
113
  this.__primaryKey = "_id";
@@ -221,6 +253,9 @@ class Model {
221
253
  buildRelationship(model, foreignKey, localKey) {
222
254
  return model.where(foreignKey, "=", this[localKey !== null && localKey !== void 0 ? localKey : "_id"]);
223
255
  }
256
+ buildArrayRelationship(model, localArrayKey, foreignKey) {
257
+ return model.where(foreignKey !== null && foreignKey !== void 0 ? foreignKey : "_id", "=", { $in: this[localArrayKey] });
258
+ }
224
259
  /**
225
260
  * Returns the model-data without system- and unmapped-fields (new js-object)
226
261
  * @returns object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.2.40",
3
+ "version": "0.2.42",
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",
@@ -15,7 +15,7 @@ export type DataBuilderSchema = {
15
15
  events?: { [key: string]: any };
16
16
  fields: { [key: string]: any };
17
17
  newDataHandler?: Function;
18
- canDelete: any;
18
+ canDelete?: any;
19
19
  };
20
20
 
21
21
  export type DataBuilderType = {
@@ -73,6 +73,40 @@ export function hasMany(modelGetter: () => any, foreignKey: string, localKey?: s
73
73
  };
74
74
  }
75
75
 
76
+ export function hasManyArray(modelGetter: () => any, localArrayKey: string, foreignKey?: string, queryFunction?: Function) {
77
+ return function (target: any, propertyKey: string) {
78
+ Object.defineProperty(target, propertyKey, {
79
+ value: async function (options?: any) {
80
+ if ((!options || !options.force) && this.__dependencies && this.__dependencies[propertyKey] !== undefined) {
81
+ return this.__dependencies[propertyKey];
82
+ }
83
+
84
+ const model = modelGetter();
85
+ if (!model) {
86
+ throw new Error(`Model for foreignKey "${foreignKey}" konnte nicht aufgelöst werden.`);
87
+ }
88
+
89
+ if (!this.__dependencies) {
90
+ this.__dependencies = {};
91
+ }
92
+
93
+ const query = this.buildArrayRelationship(model, localArrayKey, foreignKey);
94
+ if (queryFunction && typeof queryFunction === "function") {
95
+ queryFunction(query);
96
+ }
97
+
98
+ if (options && options.query) {
99
+ return query;
100
+ }
101
+ this.__dependencies[propertyKey] = await query.get();
102
+ return this.__dependencies[propertyKey];
103
+ },
104
+ writable: false,
105
+ configurable: true,
106
+ });
107
+ };
108
+ }
109
+
76
110
  export class Model {
77
111
  [key: string]: any | undefined; // allow-custom-properties
78
112
  __primaryKey = "_id";
@@ -218,6 +252,10 @@ export class Model {
218
252
  return model.where(foreignKey, "=", this[localKey ?? "_id"]);
219
253
  }
220
254
 
255
+ buildArrayRelationship(model: any, localArrayKey: string, foreignKey?: string): any {
256
+ return model.where(foreignKey ?? "_id", "=", { $in: this[localArrayKey] });
257
+ }
258
+
221
259
  /**
222
260
  * Returns the model-data without system- and unmapped-fields (new js-object)
223
261
  * @returns object