@webtypen/webframez-core 0.1.9 → 0.1.11

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.
@@ -14,39 +14,39 @@ export declare class Model {
14
14
  * @param value
15
15
  * @returns QueryBulder
16
16
  */
17
- static where(column: any, operator: any, value: any): QueryBuilder;
17
+ static where(column: any, operator: any, value: any, collection?: string): QueryBuilder;
18
18
  /**
19
19
  * Creates a new query-builder object and adds a orderBy-clause
20
20
  * @param column
21
21
  * @param sort
22
22
  * @returns QueryBulder
23
23
  */
24
- static orderBy(column: any, sort: any): QueryBuilder;
24
+ static orderBy(column: any, sort: any, collection?: string): QueryBuilder;
25
25
  /**
26
26
  * Creates a new query-builder object and executes first()
27
27
  * @param options?
28
28
  * @returns QueryBulder
29
29
  */
30
- static first(options?: any): Promise<any>;
30
+ static first(options?: any, collection?: string): Promise<any>;
31
31
  /**
32
32
  * Creates a new query-builder object and executes get()
33
33
  * @param options?
34
34
  * @returns QueryBulder
35
35
  */
36
- static get(options?: any): Promise<any>;
36
+ static get(options?: any, collection?: string): Promise<any>;
37
37
  /**
38
38
  * Creates a new query-builder object and executes paginate()
39
39
  * @param count
40
40
  * @param options?
41
41
  * @returns QueryBulder
42
42
  */
43
- static paginate(count: number, options?: any): Promise<any>;
43
+ static paginate(count: number, options?: any, collection?: string): Promise<any>;
44
44
  /**
45
45
  * Executes a database-aggregation (mostly used by no-sql connections)
46
46
  * @param aggregation
47
47
  * @returns any
48
48
  */
49
- static aggregate(aggregation: any): Promise<any>;
49
+ static aggregate(aggregation: any, collection?: string): Promise<any>;
50
50
  /**
51
51
  * Returns the model-data without system- and unmapped-fields (new js-object)
52
52
  * @returns object
@@ -19,15 +19,7 @@ class Model {
19
19
  this.__connection = undefined;
20
20
  this.__hidden = [];
21
21
  this.__unmapped = [];
22
- this.__unmappedSystem = [
23
- "__primaryKey",
24
- "__table",
25
- "__connection",
26
- "__unmapped",
27
- "__unmappedSystem",
28
- "__is_deleted",
29
- "__hidden",
30
- ];
22
+ this.__unmappedSystem = ["__primaryKey", "__table", "__connection", "__unmapped", "__unmappedSystem", "__is_deleted", "__hidden"];
31
23
  }
32
24
  /**
33
25
  * Creates a new query-builder object and adds a where-clause
@@ -36,11 +28,16 @@ class Model {
36
28
  * @param value
37
29
  * @returns QueryBulder
38
30
  */
39
- static where(column, operator, value) {
40
- const model = new this();
31
+ static where(column, operator, value, collection) {
32
+ const model = collection ? null : new this();
41
33
  const queryBuilder = new QueryBuilder_1.QueryBuilder();
42
34
  queryBuilder.setModelMapping(this);
43
- queryBuilder.table(model.__table);
35
+ if (model) {
36
+ queryBuilder.table(model.__table);
37
+ }
38
+ else if (collection) {
39
+ queryBuilder.table(collection);
40
+ }
44
41
  queryBuilder.where(column, operator, value);
45
42
  return queryBuilder;
46
43
  }
@@ -50,11 +47,16 @@ class Model {
50
47
  * @param sort
51
48
  * @returns QueryBulder
52
49
  */
53
- static orderBy(column, sort) {
54
- const model = new this();
50
+ static orderBy(column, sort, collection) {
51
+ const model = collection ? null : new this();
55
52
  const queryBuilder = new QueryBuilder_1.QueryBuilder();
56
53
  queryBuilder.setModelMapping(this);
57
- queryBuilder.table(model.__table);
54
+ if (model) {
55
+ queryBuilder.table(model.__table);
56
+ }
57
+ else if (collection) {
58
+ queryBuilder.table(collection);
59
+ }
58
60
  queryBuilder.orderBy(column, sort);
59
61
  return queryBuilder;
60
62
  }
@@ -63,12 +65,17 @@ class Model {
63
65
  * @param options?
64
66
  * @returns QueryBulder
65
67
  */
66
- static first(options) {
68
+ static first(options, collection) {
67
69
  return __awaiter(this, void 0, void 0, function* () {
68
- const model = new this();
70
+ const model = collection ? null : new this();
69
71
  const queryBuilder = new QueryBuilder_1.QueryBuilder();
70
72
  queryBuilder.setModelMapping(this);
71
- queryBuilder.table(model.__table);
73
+ if (model) {
74
+ queryBuilder.table(model.__table);
75
+ }
76
+ else if (collection) {
77
+ queryBuilder.table(collection);
78
+ }
72
79
  return yield queryBuilder.first(options);
73
80
  });
74
81
  }
@@ -77,12 +84,17 @@ class Model {
77
84
  * @param options?
78
85
  * @returns QueryBulder
79
86
  */
80
- static get(options) {
87
+ static get(options, collection) {
81
88
  return __awaiter(this, void 0, void 0, function* () {
82
- const model = new this();
89
+ const model = collection ? null : new this();
83
90
  const queryBuilder = new QueryBuilder_1.QueryBuilder();
84
91
  queryBuilder.setModelMapping(this);
85
- queryBuilder.table(model.__table);
92
+ if (model) {
93
+ queryBuilder.table(model.__table);
94
+ }
95
+ else if (collection) {
96
+ queryBuilder.table(collection);
97
+ }
86
98
  return yield queryBuilder.get(options);
87
99
  });
88
100
  }
@@ -92,12 +104,17 @@ class Model {
92
104
  * @param options?
93
105
  * @returns QueryBulder
94
106
  */
95
- static paginate(count, options) {
107
+ static paginate(count, options, collection) {
96
108
  return __awaiter(this, void 0, void 0, function* () {
97
- const model = new this();
109
+ const model = collection ? null : new this();
98
110
  const queryBuilder = new QueryBuilder_1.QueryBuilder();
99
111
  queryBuilder.setModelMapping(this);
100
- queryBuilder.table(model.__table);
112
+ if (model) {
113
+ queryBuilder.table(model.__table);
114
+ }
115
+ else if (collection) {
116
+ queryBuilder.table(collection);
117
+ }
101
118
  return yield queryBuilder.paginate(count, options);
102
119
  });
103
120
  }
@@ -106,12 +123,12 @@ class Model {
106
123
  * @param aggregation
107
124
  * @returns any
108
125
  */
109
- static aggregate(aggregation) {
126
+ static aggregate(aggregation, collection) {
110
127
  return __awaiter(this, void 0, void 0, function* () {
111
128
  const model = new this();
112
129
  return yield DBConnection_1.DBConnection.execute({
113
130
  type: "aggregation",
114
- table: model.__table,
131
+ table: collection ? collection : model ? model.__table : undefined,
115
132
  aggregation: aggregation,
116
133
  }, model.__connection);
117
134
  });
@@ -136,9 +153,7 @@ class Model {
136
153
  toArray() {
137
154
  const out = {};
138
155
  for (let i in this) {
139
- if (!this.__unmapped.includes(i) &&
140
- !this.__unmappedSystem.includes(i) &&
141
- !this.__hidden.includes(i)) {
156
+ if (!this.__unmapped.includes(i) && !this.__unmappedSystem.includes(i) && !this.__hidden.includes(i)) {
142
157
  out[i] = this[i];
143
158
  }
144
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
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",
@@ -2,193 +2,201 @@ import { DBConnection } from "./DBConnection";
2
2
  import { QueryBuilder } from "./QueryBuilder";
3
3
 
4
4
  export class Model {
5
- [key: string]: any | undefined; // allow-custom-properties
6
- __primaryKey = "_id";
7
- __table: string = "";
8
- __connection: string | undefined = undefined;
9
- __hidden: string[] = [];
10
- __unmapped: string[] = [];
11
- __unmappedSystem: string[] = [
12
- "__primaryKey",
13
- "__table",
14
- "__connection",
15
- "__unmapped",
16
- "__unmappedSystem",
17
- "__is_deleted",
18
- "__hidden",
19
- ];
20
-
21
- /**
22
- * Creates a new query-builder object and adds a where-clause
23
- * @param column
24
- * @param operator
25
- * @param value
26
- * @returns QueryBulder
27
- */
28
- static where(column: any, operator: any, value: any): QueryBuilder {
29
- const model = new this();
30
- const queryBuilder = new QueryBuilder();
31
- queryBuilder.setModelMapping(this);
32
- queryBuilder.table(model.__table);
33
- queryBuilder.where(column, operator, value);
34
-
35
- return queryBuilder;
36
- }
37
-
38
- /**
39
- * Creates a new query-builder object and adds a orderBy-clause
40
- * @param column
41
- * @param sort
42
- * @returns QueryBulder
43
- */
44
- static orderBy(column: any, sort: any): QueryBuilder {
45
- const model = new this();
46
- const queryBuilder = new QueryBuilder();
47
- queryBuilder.setModelMapping(this);
48
- queryBuilder.table(model.__table);
49
- queryBuilder.orderBy(column, sort);
50
-
51
- return queryBuilder;
52
- }
53
-
54
- /**
55
- * Creates a new query-builder object and executes first()
56
- * @param options?
57
- * @returns QueryBulder
58
- */
59
- static async first(options?: any) {
60
- const model = new this();
61
- const queryBuilder = new QueryBuilder();
62
- queryBuilder.setModelMapping(this);
63
- queryBuilder.table(model.__table);
64
-
65
- return await queryBuilder.first(options);
66
- }
67
-
68
- /**
69
- * Creates a new query-builder object and executes get()
70
- * @param options?
71
- * @returns QueryBulder
72
- */
73
- static async get(options?: any) {
74
- const model = new this();
75
- const queryBuilder = new QueryBuilder();
76
- queryBuilder.setModelMapping(this);
77
- queryBuilder.table(model.__table);
78
-
79
- return await queryBuilder.get(options);
80
- }
81
-
82
- /**
83
- * Creates a new query-builder object and executes paginate()
84
- * @param count
85
- * @param options?
86
- * @returns QueryBulder
87
- */
88
- static async paginate(count: number, options?: any) {
89
- const model = new this();
90
- const queryBuilder = new QueryBuilder();
91
- queryBuilder.setModelMapping(this);
92
- queryBuilder.table(model.__table);
93
-
94
- return await queryBuilder.paginate(count, options);
95
- }
96
-
97
- /**
98
- * Executes a database-aggregation (mostly used by no-sql connections)
99
- * @param aggregation
100
- * @returns any
101
- */
102
- static async aggregate(aggregation: any) {
103
- const model = new this();
104
- return await DBConnection.execute(
105
- {
106
- type: "aggregation",
107
- table: model.__table,
108
- aggregation: aggregation,
109
- },
110
- model.__connection
111
- );
112
- }
113
-
114
- /**
115
- * Returns the model-data without system- and unmapped-fields (new js-object)
116
- * @returns object
117
- */
118
- getModelData(): object {
119
- const out: { [key: string]: any } = {};
120
- for (let i in this) {
121
- if (!this.__unmapped.includes(i) && !this.__unmappedSystem.includes(i)) {
122
- out[i] = this[i];
123
- }
5
+ [key: string]: any | undefined; // allow-custom-properties
6
+ __primaryKey = "_id";
7
+ __table: string = "";
8
+ __connection: string | undefined = undefined;
9
+ __hidden: string[] = [];
10
+ __unmapped: string[] = [];
11
+ __unmappedSystem: string[] = ["__primaryKey", "__table", "__connection", "__unmapped", "__unmappedSystem", "__is_deleted", "__hidden"];
12
+
13
+ /**
14
+ * Creates a new query-builder object and adds a where-clause
15
+ * @param column
16
+ * @param operator
17
+ * @param value
18
+ * @returns QueryBulder
19
+ */
20
+ static where(column: any, operator: any, value: any, collection?: string): QueryBuilder {
21
+ const model = collection ? null : new this();
22
+ const queryBuilder = new QueryBuilder();
23
+ queryBuilder.setModelMapping(this);
24
+ if (model) {
25
+ queryBuilder.table(model.__table);
26
+ } else if (collection) {
27
+ queryBuilder.table(collection);
28
+ }
29
+ queryBuilder.where(column, operator, value);
30
+
31
+ return queryBuilder;
124
32
  }
125
- return out;
126
- }
127
-
128
- /**
129
- * Returns the model-data without system-, unmapped- and hidden-fields (new js-object)
130
- * @returns object
131
- */
132
- toArray(): object {
133
- const out: { [key: string]: any } = {};
134
- for (let i in this) {
135
- if (
136
- !this.__unmapped.includes(i) &&
137
- !this.__unmappedSystem.includes(i) &&
138
- !this.__hidden.includes(i)
139
- ) {
140
- out[i] = this[i];
141
- }
33
+
34
+ /**
35
+ * Creates a new query-builder object and adds a orderBy-clause
36
+ * @param column
37
+ * @param sort
38
+ * @returns QueryBulder
39
+ */
40
+ static orderBy(column: any, sort: any, collection?: string): QueryBuilder {
41
+ const model = collection ? null : new this();
42
+ const queryBuilder = new QueryBuilder();
43
+ queryBuilder.setModelMapping(this);
44
+ if (model) {
45
+ queryBuilder.table(model.__table);
46
+ } else if (collection) {
47
+ queryBuilder.table(collection);
48
+ }
49
+ queryBuilder.orderBy(column, sort);
50
+
51
+ return queryBuilder;
52
+ }
53
+
54
+ /**
55
+ * Creates a new query-builder object and executes first()
56
+ * @param options?
57
+ * @returns QueryBulder
58
+ */
59
+ static async first(options?: any, collection?: string) {
60
+ const model = collection ? null : new this();
61
+ const queryBuilder = new QueryBuilder();
62
+ queryBuilder.setModelMapping(this);
63
+ if (model) {
64
+ queryBuilder.table(model.__table);
65
+ } else if (collection) {
66
+ queryBuilder.table(collection);
67
+ }
68
+
69
+ return await queryBuilder.first(options);
70
+ }
71
+
72
+ /**
73
+ * Creates a new query-builder object and executes get()
74
+ * @param options?
75
+ * @returns QueryBulder
76
+ */
77
+ static async get(options?: any, collection?: string) {
78
+ const model = collection ? null : new this();
79
+ const queryBuilder = new QueryBuilder();
80
+ queryBuilder.setModelMapping(this);
81
+ if (model) {
82
+ queryBuilder.table(model.__table);
83
+ } else if (collection) {
84
+ queryBuilder.table(collection);
85
+ }
86
+
87
+ return await queryBuilder.get(options);
88
+ }
89
+
90
+ /**
91
+ * Creates a new query-builder object and executes paginate()
92
+ * @param count
93
+ * @param options?
94
+ * @returns QueryBulder
95
+ */
96
+ static async paginate(count: number, options?: any, collection?: string) {
97
+ const model = collection ? null : new this();
98
+ const queryBuilder = new QueryBuilder();
99
+ queryBuilder.setModelMapping(this);
100
+ if (model) {
101
+ queryBuilder.table(model.__table);
102
+ } else if (collection) {
103
+ queryBuilder.table(collection);
104
+ }
105
+
106
+ return await queryBuilder.paginate(count, options);
142
107
  }
143
- return out;
144
- }
145
-
146
- /**
147
- * Saves the object in the database
148
- * @returns Model
149
- */
150
- async save() {
151
- let status = null;
152
- if (this[this.__primaryKey] !== undefined && this[this.__primaryKey]) {
153
- // Update
154
- status = await DBConnection.execute({
155
- type: "updateOne",
156
- table: this.__table,
157
- primaryKey: this.__primaryKey,
158
- filter: { [this.__primaryKey]: this[this.__primaryKey] },
159
- data: this.getModelData(),
160
- });
161
- } else {
162
- // Insert
163
- status = await DBConnection.execute({
164
- type: "insertOne",
165
- table: this.__table,
166
- data: this.getModelData(),
167
- });
108
+
109
+ /**
110
+ * Executes a database-aggregation (mostly used by no-sql connections)
111
+ * @param aggregation
112
+ * @returns any
113
+ */
114
+ static async aggregate(aggregation: any, collection?: string) {
115
+ const model = new this();
116
+ return await DBConnection.execute(
117
+ {
118
+ type: "aggregation",
119
+ table: collection ? collection : model ? model.__table : undefined,
120
+ aggregation: aggregation,
121
+ },
122
+ model.__connection
123
+ );
124
+ }
125
+
126
+ /**
127
+ * Returns the model-data without system- and unmapped-fields (new js-object)
128
+ * @returns object
129
+ */
130
+ getModelData(): object {
131
+ const out: { [key: string]: any } = {};
132
+ for (let i in this) {
133
+ if (!this.__unmapped.includes(i) && !this.__unmappedSystem.includes(i)) {
134
+ out[i] = this[i];
135
+ }
136
+ }
137
+ return out;
168
138
  }
169
139
 
170
- // onModelSave
171
- const driver = await DBConnection.getDriver(this.__connection);
172
- return driver.onModelSave(this, status);
173
- }
174
-
175
- /**
176
- * Deletes the object from the database
177
- * @returns boolean
178
- */
179
- async delete() {
180
- if (!this[this.__primaryKey]) {
181
- return false;
140
+ /**
141
+ * Returns the model-data without system-, unmapped- and hidden-fields (new js-object)
142
+ * @returns object
143
+ */
144
+ toArray(): object {
145
+ const out: { [key: string]: any } = {};
146
+ for (let i in this) {
147
+ if (!this.__unmapped.includes(i) && !this.__unmappedSystem.includes(i) && !this.__hidden.includes(i)) {
148
+ out[i] = this[i];
149
+ }
150
+ }
151
+ return out;
182
152
  }
183
153
 
184
- const queryBuilder = new QueryBuilder();
185
- queryBuilder.setModelMapping(this);
186
- queryBuilder.table(this.__table);
187
- queryBuilder.where(this.__primaryKey, "=", this[this.__primaryKey]);
188
- await queryBuilder.delete({ mode: "deleteOne" });
154
+ /**
155
+ * Saves the object in the database
156
+ * @returns Model
157
+ */
158
+ async save() {
159
+ let status = null;
160
+ if (this[this.__primaryKey] !== undefined && this[this.__primaryKey]) {
161
+ // Update
162
+ status = await DBConnection.execute({
163
+ type: "updateOne",
164
+ table: this.__table,
165
+ primaryKey: this.__primaryKey,
166
+ filter: { [this.__primaryKey]: this[this.__primaryKey] },
167
+ data: this.getModelData(),
168
+ });
169
+ } else {
170
+ // Insert
171
+ status = await DBConnection.execute({
172
+ type: "insertOne",
173
+ table: this.__table,
174
+ data: this.getModelData(),
175
+ });
176
+ }
177
+
178
+ // onModelSave
179
+ const driver = await DBConnection.getDriver(this.__connection);
180
+ return driver.onModelSave(this, status);
181
+ }
189
182
 
190
- this.__is_deleted = true;
183
+ /**
184
+ * Deletes the object from the database
185
+ * @returns boolean
186
+ */
187
+ async delete() {
188
+ if (!this[this.__primaryKey]) {
189
+ return false;
190
+ }
191
191
 
192
- return true;
193
- }
192
+ const queryBuilder = new QueryBuilder();
193
+ queryBuilder.setModelMapping(this);
194
+ queryBuilder.table(this.__table);
195
+ queryBuilder.where(this.__primaryKey, "=", this[this.__primaryKey]);
196
+ await queryBuilder.delete({ mode: "deleteOne" });
197
+
198
+ this.__is_deleted = true;
199
+
200
+ return true;
201
+ }
194
202
  }