@webtypen/webframez-core 0.2.41 → 0.2.45
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/.github/workflows/npm-publish.yml +4 -0
- package/README.md +12 -1
- package/dist/DataBuilder/DataBuilder.js +5 -0
- package/dist/Database/Model.d.ts +2 -0
- package/dist/Database/Model.js +38 -1
- package/package.json +1 -1
- package/src/DataBuilder/DataBuilder.ts +6 -1
- package/src/Database/Model.ts +40 -0
|
@@ -28,6 +28,10 @@ jobs:
|
|
|
28
28
|
registry-url: https://registry.npmjs.org/
|
|
29
29
|
- run: npm install
|
|
30
30
|
- run: npm run build
|
|
31
|
+
- run: npm whoami
|
|
32
|
+
env:
|
|
33
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
34
|
+
- run: npm config get registry
|
|
31
35
|
- run: npm publish --access public
|
|
32
36
|
env:
|
|
33
37
|
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
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
|
|
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:
|
|
@@ -220,6 +220,11 @@ class DataBuilder {
|
|
|
220
220
|
else if (type.forms[form].fields) {
|
|
221
221
|
newData.forms[form].fields = JSON.parse(JSON.stringify(type.forms[form].fields));
|
|
222
222
|
}
|
|
223
|
+
newData.forms[form].allowDeletion =
|
|
224
|
+
(typeof type.forms[form].allowDeletion === "boolean" && type.forms[form].allowDeletion) ||
|
|
225
|
+
(typeof type.forms[form].allowDeletion === "function" && (yield type.forms[form].allowDeletion(req)))
|
|
226
|
+
? true
|
|
227
|
+
: false;
|
|
223
228
|
}
|
|
224
229
|
}
|
|
225
230
|
else {
|
package/dist/Database/Model.d.ts
CHANGED
|
@@ -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
|
package/dist/Database/Model.js
CHANGED
|
@@ -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,11 @@ 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", "=", {
|
|
258
|
+
$in: this[localArrayKey] && Array.isArray(this[localArrayKey]) ? this[localArrayKey] : [],
|
|
259
|
+
});
|
|
260
|
+
}
|
|
224
261
|
/**
|
|
225
262
|
* Returns the model-data without system- and unmapped-fields (new js-object)
|
|
226
263
|
* @returns object
|
package/package.json
CHANGED
|
@@ -259,7 +259,6 @@ export class DataBuilder {
|
|
|
259
259
|
}
|
|
260
260
|
|
|
261
261
|
newData.forms[form] = {};
|
|
262
|
-
|
|
263
262
|
if (typeof type.forms[form].pageActions === "function") {
|
|
264
263
|
newData.forms[form].pageActions = await type.forms[form].pageActions(req);
|
|
265
264
|
} else if (type.forms[form].pageActions) {
|
|
@@ -275,6 +274,12 @@ export class DataBuilder {
|
|
|
275
274
|
} else if (type.forms[form].fields) {
|
|
276
275
|
newData.forms[form].fields = JSON.parse(JSON.stringify(type.forms[form].fields));
|
|
277
276
|
}
|
|
277
|
+
|
|
278
|
+
newData.forms[form].allowDeletion =
|
|
279
|
+
(typeof type.forms[form].allowDeletion === "boolean" && type.forms[form].allowDeletion) ||
|
|
280
|
+
(typeof type.forms[form].allowDeletion === "function" && (await type.forms[form].allowDeletion(req)))
|
|
281
|
+
? true
|
|
282
|
+
: false;
|
|
278
283
|
}
|
|
279
284
|
} else {
|
|
280
285
|
newData[key] = type[key];
|
package/src/Database/Model.ts
CHANGED
|
@@ -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,12 @@ 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", "=", {
|
|
257
|
+
$in: this[localArrayKey] && Array.isArray(this[localArrayKey]) ? this[localArrayKey] : [],
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
221
261
|
/**
|
|
222
262
|
* Returns the model-data without system- and unmapped-fields (new js-object)
|
|
223
263
|
* @returns object
|