chyz 1.0.13-rc.2 → 1.0.13-rc.23
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/BaseChyz.ts +60 -18
- package/Doc/Moel kullanma.md +13 -0
- package/Examples/Controllers/ApiController.ts +35 -26
- package/Examples/Controllers/BasicApiController.ts +121 -0
- package/Examples/Controllers/KeyCloakController.ts +4 -4
- package/Examples/Controllers/SiteController.ts +18 -8
- package/Examples/Models/AuthAssignment.ts +50 -0
- package/Examples/Models/AuthItem.ts +59 -0
- package/Examples/Models/AuthItemChild.ts +49 -0
- package/Examples/Models/Categories.ts +14 -3
- package/Examples/Models/Customer.ts +2 -2
- package/Examples/Models/KeycloakUser.ts +4 -0
- package/Examples/Models/Order.ts +5 -5
- package/Examples/Models/OrderItem.ts +2 -2
- package/Examples/Models/ProductModels.ts +4 -5
- package/Examples/Models/ProductToCategories.ts +15 -4
- package/Examples/Models/Products.ts +9 -8
- package/Examples/Models/Stocks.ts +2 -2
- package/Examples/Models/User.ts +8 -1
- package/Examples/Models/UserPermission.ts +2 -2
- package/Examples/Models/index.ts +19 -0
- package/Examples/index.ts +20 -1
- package/Examples/log/app.log +9456 -0
- package/Examples/log/errors.log +1904 -0
- package/Examples/tsconfig.json +2 -1
- package/README.md +267 -16
- package/base/ActionFilter.ts +1 -1
- package/base/BaseError.ts +4 -2
- package/base/InvalidArgumentException.ts +16 -0
- package/base/Model.ts +81 -55
- package/base/ModelManager.ts +19 -0
- package/base/RestClient.ts +4 -4
- package/base/ValidationHttpException.ts +1 -1
- package/base/index.ts +2 -0
- package/dist/BaseChyz.js +48 -9
- package/dist/BaseChyz.js.map +1 -1
- package/dist/base/ActionFilter.js +1 -1
- package/dist/base/ActionFilter.js.map +1 -1
- package/dist/base/BaseError.js +6 -2
- package/dist/base/BaseError.js.map +1 -1
- package/dist/base/InvalidArgumentException.js +18 -0
- package/dist/base/InvalidArgumentException.js.map +1 -0
- package/dist/base/Model.js +47 -32
- package/dist/base/Model.js.map +1 -1
- package/dist/base/ModelManager.js +9 -0
- package/dist/base/ModelManager.js.map +1 -0
- package/dist/base/RestClient.js +4 -4
- package/dist/base/RestClient.js.map +1 -1
- package/dist/base/ValidationHttpException.js +1 -1
- package/dist/base/index.js +2 -0
- package/dist/base/index.js.map +1 -1
- package/dist/filters/AccessControl.js +15 -3
- package/dist/filters/AccessControl.js.map +1 -1
- package/dist/filters/AccessRule.js +99 -38
- package/dist/filters/AccessRule.js.map +1 -1
- package/dist/filters/auth/HttpBasicAuth.js +65 -0
- package/dist/filters/auth/HttpBasicAuth.js.map +1 -1
- package/dist/filters/auth/JwtHttpBearerAuth.js +1 -1
- package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
- package/dist/filters/auth/index.js +1 -0
- package/dist/filters/auth/index.js.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/package.json +55 -0
- package/dist/rbac/AuthAssignment.js +45 -0
- package/dist/rbac/AuthAssignment.js.map +1 -0
- package/dist/rbac/AuthItem.js +52 -0
- package/dist/rbac/AuthItem.js.map +1 -0
- package/dist/rbac/AuthItemChild.js +44 -0
- package/dist/rbac/AuthItemChild.js.map +1 -0
- package/dist/rbac/AuthManager.js +359 -0
- package/dist/rbac/AuthManager.js.map +1 -0
- package/dist/web/WebUser.js +78 -0
- package/dist/web/WebUser.js.map +1 -1
- package/filters/AccessControl.ts +19 -6
- package/filters/AccessRule.ts +61 -16
- package/filters/auth/HttpBasicAuth.ts +68 -0
- package/filters/auth/JwtHttpBearerAuth.ts +1 -1
- package/filters/auth/index.ts +1 -0
- package/index.ts +2 -2
- package/package-lock.json +5259 -0
- package/package.json +6 -6
- package/rbac/AuthAssignment.ts +50 -0
- package/rbac/AuthItem.ts +57 -0
- package/rbac/AuthItemChild.ts +50 -0
- package/rbac/AuthManager.ts +398 -0
- package/web/IdentityInterface.ts +6 -0
- package/web/WebUser.ts +88 -1
- package/Examples/yarn.lock +0 -2549
package/Examples/tsconfig.json
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
"target": "ES6",
|
|
4
4
|
"experimentalDecorators": true,
|
|
5
5
|
"emitDecoratorMetadata": true,
|
|
6
|
+
// "isolatedModules": true,
|
|
6
7
|
"removeComments": true,
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
+
// "esModuleInterop": true,
|
|
8
9
|
"allowSyntheticDefaultImports": true,
|
|
9
10
|
"moduleResolution": "node"
|
|
10
11
|
},
|
package/README.md
CHANGED
|
@@ -8,7 +8,6 @@ Klasör Yapısı<br>
|
|
|
8
8
|
*---Controllers <br>
|
|
9
9
|
*---Models<br>
|
|
10
10
|
*---Log<br>
|
|
11
|
-
*---Framework<br>
|
|
12
11
|
index.ts<br>
|
|
13
12
|
|
|
14
13
|
`##Başlangıç<br>
|
|
@@ -17,15 +16,23 @@ yarn start
|
|
|
17
16
|
## index.ts alanlar düzenlenmeli.
|
|
18
17
|
|
|
19
18
|
```typescript
|
|
19
|
+
|
|
20
|
+
import {BaseChyz} from "../index";
|
|
21
|
+
|
|
20
22
|
require('dotenv-flow').config();
|
|
23
|
+
import Chyz ,{ DbConnection, BaseChyz} from "chyz/dist/";
|
|
24
|
+
import {AuthManager} from "chyz/dist/rbac/AuthManager"
|
|
25
|
+
import {WebUser} from "chyz/dist/web/WebUser";
|
|
26
|
+
import {User as Identity} from "./Models/User";
|
|
21
27
|
|
|
22
|
-
import BaseChyz from "chyz/dist/BaseChyz";
|
|
23
|
-
import Chyz, {DbConnection} from "chyz/dist";
|
|
24
|
-
import {WebUser} from "../web/WebUser";
|
|
25
|
-
import {User} from "./Models/User";
|
|
26
28
|
|
|
27
29
|
let config = {
|
|
30
|
+
port: process.env.PORT || 8870,
|
|
31
|
+
controllerpath: "Examples\\Controllers",
|
|
28
32
|
components: {
|
|
33
|
+
authManager: {
|
|
34
|
+
class: AuthManager,
|
|
35
|
+
},
|
|
29
36
|
db: {
|
|
30
37
|
class: DbConnection,
|
|
31
38
|
database: process.env.DBDATABASE,
|
|
@@ -35,7 +42,7 @@ let config = {
|
|
|
35
42
|
host: process.env.DBHOST,
|
|
36
43
|
dialect: 'postgres', /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
|
|
37
44
|
// disable logging; default: console.log
|
|
38
|
-
logging:
|
|
45
|
+
logging: (msg: any) => BaseChyz.debug(msg)
|
|
39
46
|
}
|
|
40
47
|
},
|
|
41
48
|
user: {
|
|
@@ -43,14 +50,168 @@ let config = {
|
|
|
43
50
|
'identityClass': User
|
|
44
51
|
}
|
|
45
52
|
}
|
|
53
|
+
|
|
46
54
|
}
|
|
47
55
|
Chyz.app(config).Start();
|
|
48
56
|
```
|
|
49
57
|
|
|
58
|
+
##Controller
|
|
59
|
+
Basit şekilde kontroller oluşturulabilir.
|
|
60
|
+
```typescript
|
|
61
|
+
/*
|
|
62
|
+
*
|
|
63
|
+
* Copyright (c) 2021-2021.. Chy Bilgisayar Bilisim
|
|
64
|
+
* Author: Cihan Ozturk
|
|
65
|
+
* E-mail: cihan@chy.com.tr
|
|
66
|
+
* Github:https://github.com/cihan53/
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
import {AccessControl, BaseChyz, JwtHttpBearerAuth, ModelManager, Request, Response,} from "chyz/dist";
|
|
71
|
+
import {ForbiddenHttpException, Model, NotFoundHttpException, ValidationHttpException} from "chyz/dist/base";
|
|
72
|
+
|
|
73
|
+
import Utils from 'chyz/dist/requiments/Utils';
|
|
74
|
+
import {controller, get, post} from "chyz/dist/decorator";
|
|
75
|
+
import {Controller} from "chyz/dist/base/Controller";
|
|
76
|
+
import * as Util from "util";
|
|
77
|
+
import {uid} from "uid";
|
|
78
|
+
|
|
79
|
+
import {User} from "../Models/User";
|
|
80
|
+
import {AuthManager} from "../Lib/AuthManager";
|
|
81
|
+
import {CategoriesClass} from "../Models/Categories";
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
const keygen = require('ssh-keygen');
|
|
85
|
+
const os = require('os');
|
|
86
|
+
const bcrypt = require('bcrypt');
|
|
87
|
+
const JsonWebToken = require("jsonwebtoken");
|
|
88
|
+
const {Op} = require("sequelize");
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@controller("/api")
|
|
92
|
+
class ApiController extends Controller {
|
|
93
|
+
|
|
94
|
+
public myCheck(token) {
|
|
95
|
+
console.log("myyyyyyyyyyyyyyyyyyyyy")
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public behaviors(): any[] {
|
|
99
|
+
|
|
100
|
+
return [{
|
|
101
|
+
'authenticator': {
|
|
102
|
+
"class": JwtHttpBearerAuth,
|
|
103
|
+
// "auth": this.myCheck
|
|
104
|
+
},
|
|
105
|
+
'access': {
|
|
106
|
+
'class': AccessControl,
|
|
107
|
+
'only': ['order/list' ],
|
|
108
|
+
'rules': [
|
|
109
|
+
|
|
110
|
+
{
|
|
111
|
+
'allow': true,
|
|
112
|
+
'actions': ['order/list' ],
|
|
113
|
+
'roles': ['editor'],
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@get("/")
|
|
121
|
+
Index(req: Request, res: Response) {
|
|
122
|
+
|
|
123
|
+
BaseChyz.logs().info("Site Controller Burası", this.id)
|
|
124
|
+
return res.json({message: "index sayfası"})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@post("orderCreate")
|
|
128
|
+
async Login(req: Request, res: Response) {
|
|
129
|
+
let data = req.body;
|
|
130
|
+
data.Customer.status = "true";
|
|
131
|
+
|
|
132
|
+
//Customer Model Create
|
|
133
|
+
let customer = ModelManager.Customer.save();
|
|
134
|
+
//Order Model Create
|
|
135
|
+
let order = ModelManager.Order;
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
let transaction
|
|
139
|
+
try {
|
|
140
|
+
// get transaction
|
|
141
|
+
transaction = await BaseChyz.getComponent("db").transaction();
|
|
142
|
+
customer.load(data, "Customer");//load customer data
|
|
143
|
+
let cus: any = await customer.save({}, {transaction});
|
|
144
|
+
|
|
145
|
+
if (!cus) {
|
|
146
|
+
throw new ValidationHttpException(customer.errors);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
data.Order.customer_id = cus.id;
|
|
150
|
+
|
|
151
|
+
order.load(data, "Order");
|
|
152
|
+
let res1 = await order.save({}, {transaction});
|
|
153
|
+
if (!res1) {
|
|
154
|
+
throw new ValidationHttpException(order.errors);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// commit
|
|
158
|
+
await transaction.commit();
|
|
159
|
+
|
|
160
|
+
} catch (e) {
|
|
161
|
+
if (transaction) {
|
|
162
|
+
await transaction.rollback();
|
|
163
|
+
BaseChyz.warn("Rollback transaction")
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (e instanceof ValidationHttpException)
|
|
167
|
+
throw new ValidationHttpException(e.message)
|
|
168
|
+
else
|
|
169
|
+
throw new ForbiddenHttpException(e.message)
|
|
170
|
+
}
|
|
171
|
+
return res.send("Post Controller")
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
@get("order/list")
|
|
176
|
+
async listOrder(req: Request, res: Response) {
|
|
177
|
+
const {Products}: { Products: ProductsClass } = ModelManager;
|
|
178
|
+
let product = await Products.findAll( );
|
|
179
|
+
return res.json(product)
|
|
180
|
+
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
@get("categories")
|
|
184
|
+
async Categories(req: Request, res: Response) {
|
|
185
|
+
let product = await ModelManager.Categories.findAll({
|
|
186
|
+
include: [
|
|
187
|
+
{
|
|
188
|
+
model: ModelManager.Products.model(),
|
|
189
|
+
}
|
|
190
|
+
]
|
|
191
|
+
});
|
|
192
|
+
return res.json(product)
|
|
193
|
+
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
error(req: Request, res: Response) {
|
|
197
|
+
BaseChyz.logs().info("Error Sayfası")
|
|
198
|
+
return res.send("Post Controller")
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
module.exports = ApiController
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
|
|
50
207
|
## Create Model
|
|
51
208
|
|
|
52
209
|
Veritabanı işlemleri için model oluşturma, sequelize desteklidir.
|
|
53
210
|
|
|
211
|
+
Model adı "**Class**" şeklinde bitmeli.
|
|
212
|
+
|
|
213
|
+
Örnek = "ModelName**Class**"
|
|
214
|
+
|
|
54
215
|
```typescript
|
|
55
216
|
import {Model, DataTypes} from "chyz/base/Model";
|
|
56
217
|
|
|
@@ -86,11 +247,11 @@ export class CustomerCLass extends Model {
|
|
|
86
247
|
}
|
|
87
248
|
|
|
88
249
|
}
|
|
89
|
-
|
|
90
|
-
export { Customer };
|
|
250
|
+
|
|
91
251
|
|
|
92
252
|
```
|
|
93
253
|
````typescript
|
|
254
|
+
|
|
94
255
|
export class ProductsClass extends Model {
|
|
95
256
|
[x: string]: any;
|
|
96
257
|
|
|
@@ -129,8 +290,49 @@ export class ProductsClass extends Model {
|
|
|
129
290
|
}
|
|
130
291
|
}
|
|
131
292
|
|
|
132
|
-
|
|
133
|
-
|
|
293
|
+
/**
|
|
294
|
+
*
|
|
295
|
+
*/
|
|
296
|
+
export class CategoriesClass extends Model {
|
|
297
|
+
[x: string]: any;
|
|
298
|
+
|
|
299
|
+
alias() {
|
|
300
|
+
return "c"
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
tableName() {
|
|
304
|
+
return 'categories';
|
|
305
|
+
}
|
|
306
|
+
attributes() {
|
|
307
|
+
return {
|
|
308
|
+
// Model attributes are defined here
|
|
309
|
+
title: {
|
|
310
|
+
type: DataTypes.STRING,
|
|
311
|
+
allowNull: false
|
|
312
|
+
},
|
|
313
|
+
properties: {
|
|
314
|
+
type: DataTypes.STRING,
|
|
315
|
+
allowNull: false
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
relations(): Relation[] {
|
|
321
|
+
return [
|
|
322
|
+
{
|
|
323
|
+
type: "belongsToMany",
|
|
324
|
+
foreignKey: "category_id",
|
|
325
|
+
sourceKey: "id",
|
|
326
|
+
as: 'product',
|
|
327
|
+
model: ModelManager.Products.model(),
|
|
328
|
+
through: ModelManager.ProductToCategories.model()
|
|
329
|
+
}
|
|
330
|
+
]
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
134
336
|
````
|
|
135
337
|
|
|
136
338
|
## Http POST ve GET verilerini model'e yükleme
|
|
@@ -148,9 +350,11 @@ export {Products}
|
|
|
148
350
|
* }
|
|
149
351
|
* @type {Customer}
|
|
150
352
|
*/
|
|
353
|
+
import { ModelManager} from "chyz/dist";
|
|
151
354
|
import {Customer} from "./Customer";
|
|
355
|
+
|
|
152
356
|
//Customer Model Create
|
|
153
|
-
let customer: Customer = Customer;
|
|
357
|
+
let customer: Customer = ModelManager.Customer;
|
|
154
358
|
customer.load(req.body, "Customer");//load customer data
|
|
155
359
|
let cus: any = await customer.save();
|
|
156
360
|
|
|
@@ -166,7 +370,7 @@ Transaction oluşturma
|
|
|
166
370
|
// get transaction
|
|
167
371
|
transaction = await BaseChyz.getComponent("db").transaction();
|
|
168
372
|
//Customer Model Create
|
|
169
|
-
let customer: Customer =
|
|
373
|
+
let customer: Customer = ModelManager.Customer;
|
|
170
374
|
customer.load(data, "Customer");//load customer data
|
|
171
375
|
let cus: any = await customer.save({}, {transaction});
|
|
172
376
|
if (!cus) {
|
|
@@ -175,9 +379,9 @@ Transaction oluşturma
|
|
|
175
379
|
} catch (e) {
|
|
176
380
|
if (transaction) {
|
|
177
381
|
await transaction.rollback();
|
|
178
|
-
BaseChyz.warn("Rollback transaction")
|
|
382
|
+
BaseChyz.warn("Rollback transaction");
|
|
179
383
|
}
|
|
180
|
-
|
|
384
|
+
|
|
181
385
|
}
|
|
182
386
|
```
|
|
183
387
|
|
|
@@ -219,6 +423,44 @@ export class User extends Model implements IdentityInterface {
|
|
|
219
423
|
throw new Error("Method not implemented.");
|
|
220
424
|
}
|
|
221
425
|
|
|
426
|
+
/**
|
|
427
|
+
* Returns auth manager associated with the user component.
|
|
428
|
+
*
|
|
429
|
+
* By default this is the `authManager` application component.
|
|
430
|
+
* You may override this method to return a different auth manager instance if needed.
|
|
431
|
+
*/
|
|
432
|
+
protected getAuthManager() {
|
|
433
|
+
return BaseChyz.getComponent("authManager");
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Returns the access checker used for checking access.
|
|
438
|
+
* @return CheckAccessInterface
|
|
439
|
+
*/
|
|
440
|
+
protected getAccessChecker() {
|
|
441
|
+
return this.accessChecker !== null ? this.accessChecker : this.getAuthManager();
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
*
|
|
446
|
+
* @param permissionName
|
|
447
|
+
* @param params
|
|
448
|
+
* @param allowCaching
|
|
449
|
+
*/
|
|
450
|
+
public async can(permissionName, params: any[] = [], allowCaching: boolean = true) {
|
|
451
|
+
let accessChecker;
|
|
452
|
+
if ((accessChecker = this.getAccessChecker()) === null) {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
let access = await accessChecker.checkAccess(this.getId(), permissionName, params);
|
|
457
|
+
this._access[permissionName] = access;
|
|
458
|
+
if (allowCaching && Utils.isEmpty(params)) {
|
|
459
|
+
|
|
460
|
+
}
|
|
461
|
+
return access;
|
|
462
|
+
}
|
|
463
|
+
|
|
222
464
|
public attributes() {
|
|
223
465
|
return {
|
|
224
466
|
// Model attributes are defined here
|
|
@@ -242,16 +484,25 @@ export class User extends Model implements IdentityInterface {
|
|
|
242
484
|
}
|
|
243
485
|
|
|
244
486
|
async findIdentityByAccessToken(token, type) {
|
|
487
|
+
|
|
488
|
+
|
|
245
489
|
let decoded = JsonWebToken.decode(token, {complete: true})
|
|
490
|
+
if (!decoded.payload.user) {
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
246
493
|
let identity = await this.findOne({where: {id: parseInt(decoded.payload.user)}});
|
|
247
494
|
if(identity){
|
|
248
495
|
BaseChyz.debug("Find Identity By AccessToken: User Found", decoded.payload)
|
|
249
496
|
try {
|
|
250
497
|
JsonWebToken.verify(token, identity.salt_text);
|
|
498
|
+
this.setIdentity(identity);
|
|
251
499
|
BaseChyz.debug("Find Identity By AccessToken: User Verify Success")
|
|
252
|
-
return
|
|
500
|
+
return this;
|
|
253
501
|
} catch(err) {
|
|
254
|
-
|
|
502
|
+
if (err.name == "TokenExpiredError")
|
|
503
|
+
BaseChyz.debug("Find Identity By AccessToken: Token Expired")
|
|
504
|
+
else
|
|
505
|
+
BaseChyz.debug("Find Identity By AccessToken: User Verify Failed")
|
|
255
506
|
return null;
|
|
256
507
|
}
|
|
257
508
|
}
|
package/base/ActionFilter.ts
CHANGED
package/base/BaseError.ts
CHANGED
|
@@ -5,15 +5,17 @@
|
|
|
5
5
|
* Github:https://github.com/cihan53/
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import Utils from "../requiments/Utils";
|
|
9
|
+
|
|
8
10
|
export class BaseError extends Error {
|
|
9
11
|
private statusCode: number;
|
|
10
12
|
|
|
11
13
|
constructor(message: string,statusCode=500) {
|
|
12
14
|
super(message);
|
|
13
|
-
this.message=message;
|
|
15
|
+
this.message= Utils.isString(message)?message: JSON.stringify(message);
|
|
14
16
|
this.name = this.constructor.name // good practice
|
|
15
17
|
this.statusCode = statusCode // error code for responding to client
|
|
16
|
-
Error.captureStackTrace(this)
|
|
18
|
+
//Error.captureStackTrace(this)
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
toString(){
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2021. Chy Bilgisayar Bilisim
|
|
3
|
+
* Author: Cihan Ozturk
|
|
4
|
+
* E-mail: cihan@chy.com.tr
|
|
5
|
+
* Github:https://github.com/cihan53/
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import {BaseError} from "./BaseError";
|
|
11
|
+
|
|
12
|
+
export class InvalidArgumentException extends BaseError {
|
|
13
|
+
constructor ( public message: string) {
|
|
14
|
+
super(message,500);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/base/Model.ts
CHANGED
|
@@ -9,20 +9,29 @@ import BaseChyz from "../BaseChyz";
|
|
|
9
9
|
import Utils from "../requiments/Utils";
|
|
10
10
|
import {Component} from "./Component";
|
|
11
11
|
import {InvalidConfigException} from "./InvalidConfigException";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
DatabaseError,
|
|
14
|
+
DataTypes,
|
|
15
|
+
ExclusionConstraintError,
|
|
16
|
+
ForeignKeyConstraintError,
|
|
17
|
+
Model as SModel,
|
|
18
|
+
TimeoutError,
|
|
19
|
+
UniqueConstraintError,
|
|
20
|
+
ValidationError,
|
|
21
|
+
} from "sequelize";
|
|
13
22
|
import {Exception} from "./db/Exception";
|
|
14
23
|
|
|
15
24
|
export {DataTypes, NOW} from "sequelize";
|
|
16
25
|
|
|
17
|
-
export interface Relation{
|
|
18
|
-
type:
|
|
19
|
-
allowNull?:boolean,
|
|
20
|
-
sourceKey
|
|
21
|
-
model:SModel,
|
|
22
|
-
foreignKey:string,
|
|
23
|
-
name?:string,
|
|
24
|
-
through?:
|
|
25
|
-
as?:string
|
|
26
|
+
export interface Relation {
|
|
27
|
+
type: "hasOne" | "hasMany" | "belongsToMany" | "belongsTo",
|
|
28
|
+
allowNull?: boolean,
|
|
29
|
+
sourceKey?: string,
|
|
30
|
+
model: SModel,
|
|
31
|
+
foreignKey: string,
|
|
32
|
+
name?: string,
|
|
33
|
+
through?: any,
|
|
34
|
+
as?: string
|
|
26
35
|
}
|
|
27
36
|
|
|
28
37
|
/**
|
|
@@ -80,7 +89,8 @@ export interface Relation{
|
|
|
80
89
|
*/
|
|
81
90
|
|
|
82
91
|
export class Model extends Component {
|
|
83
|
-
_sequelize: any
|
|
92
|
+
_sequelize: any;
|
|
93
|
+
_register: any;
|
|
84
94
|
private _tableName: string;
|
|
85
95
|
private _model: any;
|
|
86
96
|
private _attributes: any = {};
|
|
@@ -89,58 +99,29 @@ export class Model extends Component {
|
|
|
89
99
|
|
|
90
100
|
constructor(sequelize?: IDBDatabase) {
|
|
91
101
|
super();
|
|
92
|
-
this._tableName = this.
|
|
102
|
+
this._tableName = this.alias();
|
|
103
|
+
|
|
104
|
+
BaseChyz.debug("Model constructor", this._tableName)
|
|
93
105
|
// this._sequelize = BaseChyz.getComponent("db").db;
|
|
94
106
|
if (sequelize != null)
|
|
95
107
|
this._sequelize = sequelize;
|
|
96
108
|
else
|
|
97
|
-
this._sequelize =
|
|
109
|
+
this._sequelize = this.getDb();
|
|
98
110
|
|
|
99
111
|
if (!Utils.isEmpty(this.attributes())) {
|
|
100
|
-
|
|
112
|
+
|
|
113
|
+
this._model = this._sequelize.define(this._tableName, this.attributes(), {
|
|
101
114
|
tableName: this.tableName(),
|
|
102
|
-
timestamps: false
|
|
115
|
+
timestamps: false,
|
|
116
|
+
createdAt: false,
|
|
117
|
+
updateAt: false
|
|
103
118
|
});
|
|
104
119
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* init buraya
|
|
109
|
-
*/
|
|
110
|
-
|
|
111
|
-
for (const relation of this.relations()) {
|
|
112
|
-
let m = relation.model;
|
|
113
|
-
|
|
114
|
-
if(relation.type=="hasOne" ){
|
|
115
|
-
// @ts-ignore
|
|
116
|
-
delete relation.model
|
|
117
|
-
this.model().hasOne(m, relation );
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if(relation.type=="hasMany" ){
|
|
121
|
-
// @ts-ignore
|
|
122
|
-
delete relation.model;
|
|
123
|
-
this.model().hasMany(m, relation );
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if(relation.type=="belongsTo" ){
|
|
127
|
-
// @ts-ignore
|
|
128
|
-
delete relation.model;
|
|
129
|
-
this.model().belongsTo(m, relation );
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if(relation.type=="belongsToMany" ){
|
|
133
|
-
// @ts-ignore
|
|
134
|
-
delete relation.model;
|
|
135
|
-
this.model().belongsToMany(m, relation );
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
120
|
} else {
|
|
140
121
|
throw new InvalidConfigException(BaseChyz.t("Invalid model configuration, is not emty attributes"))
|
|
141
122
|
}
|
|
142
123
|
|
|
143
|
-
this.init();
|
|
124
|
+
// this.init();
|
|
144
125
|
|
|
145
126
|
}
|
|
146
127
|
|
|
@@ -151,8 +132,7 @@ export class Model extends Component {
|
|
|
151
132
|
* You may override this method if you want to use a different database connection.
|
|
152
133
|
* @return Connection the database connection used by this AR class.
|
|
153
134
|
*/
|
|
154
|
-
public
|
|
155
|
-
{
|
|
135
|
+
public getDb() {
|
|
156
136
|
return BaseChyz.getComponent("db").db
|
|
157
137
|
}
|
|
158
138
|
|
|
@@ -178,9 +158,45 @@ export class Model extends Component {
|
|
|
178
158
|
|
|
179
159
|
public init() {
|
|
180
160
|
BaseChyz.debug("Model init....", this.constructor.name)
|
|
161
|
+
/**
|
|
162
|
+
* init buraya
|
|
163
|
+
*/
|
|
164
|
+
BaseChyz.debug("Relation init....", this.constructor.name)
|
|
165
|
+
for (const relation of this.relations()) {
|
|
166
|
+
let m = relation.model;
|
|
167
|
+
|
|
168
|
+
if (relation.type == "hasOne") {
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
delete relation.model
|
|
171
|
+
this.model().hasOne(m, relation);
|
|
172
|
+
}
|
|
173
|
+
//
|
|
174
|
+
if (relation.type == "hasMany") {
|
|
175
|
+
// @ts-ignore
|
|
176
|
+
delete relation.model;
|
|
177
|
+
this.model().hasMany(m, relation);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (relation.type == "belongsTo") {
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
delete relation.model;
|
|
183
|
+
this.model().belongsTo(m, relation);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (relation.type == "belongsToMany") {
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
delete relation.model;
|
|
189
|
+
this.model().belongsToMany(m, relation);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
181
193
|
}
|
|
182
194
|
|
|
183
195
|
|
|
196
|
+
public alias() {
|
|
197
|
+
return this.constructor.name;
|
|
198
|
+
}
|
|
199
|
+
|
|
184
200
|
public tableName() {
|
|
185
201
|
return this._tableName;
|
|
186
202
|
}
|
|
@@ -203,7 +219,7 @@ export class Model extends Component {
|
|
|
203
219
|
let result: any;
|
|
204
220
|
try {
|
|
205
221
|
result = await this.model().create(p, options)
|
|
206
|
-
} catch (e) {
|
|
222
|
+
} catch (e: any) {
|
|
207
223
|
BaseChyz.debug(`Model[${this.constructor.name}].create`, e)
|
|
208
224
|
if (e instanceof ValidationError) {
|
|
209
225
|
let validationErrorItems = e.errors;
|
|
@@ -237,7 +253,7 @@ export class Model extends Component {
|
|
|
237
253
|
let result: any;
|
|
238
254
|
try {
|
|
239
255
|
result = await this.model().bulkCreate(p, options)
|
|
240
|
-
} catch (e) {
|
|
256
|
+
} catch (e: any) {
|
|
241
257
|
BaseChyz.debug(`Model[${this.constructor.name}].bulkCreate`, e)
|
|
242
258
|
if (e instanceof ValidationError) {
|
|
243
259
|
let validationErrorItems = e.errors;
|
|
@@ -291,6 +307,14 @@ export class Model extends Component {
|
|
|
291
307
|
return this._model.findAll(...arguments)
|
|
292
308
|
}
|
|
293
309
|
|
|
310
|
+
/**
|
|
311
|
+
* return {count : number , rows: any}
|
|
312
|
+
* @param args
|
|
313
|
+
*/
|
|
314
|
+
public findAndCountAll(...args: any[]) {
|
|
315
|
+
return this._model.findAndCountAll(...arguments)
|
|
316
|
+
}
|
|
317
|
+
|
|
294
318
|
|
|
295
319
|
public validate() {
|
|
296
320
|
|
|
@@ -348,7 +372,9 @@ export class Model extends Component {
|
|
|
348
372
|
*
|
|
349
373
|
* ]
|
|
350
374
|
*/
|
|
351
|
-
public relations():Relation[]{
|
|
375
|
+
public relations(): Relation[] {
|
|
352
376
|
return []
|
|
353
377
|
}
|
|
378
|
+
|
|
379
|
+
|
|
354
380
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {Model} from "./Model"
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2021.. Chy Bilgisayar Bilisim
|
|
6
|
+
* Author: Cihan Ozturk
|
|
7
|
+
* E-mail: cihan@chy.com.tr
|
|
8
|
+
* Github:https://github.com/cihan53/
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
interface ObjectConstructor {
|
|
12
|
+
_register(o: Model): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const ModelManager: any = {
|
|
16
|
+
_register(map: Model) {
|
|
17
|
+
Object.assign(this, map)
|
|
18
|
+
}
|
|
19
|
+
}
|
package/base/RestClient.ts
CHANGED
|
@@ -13,12 +13,12 @@ import {Component} from "./Component";
|
|
|
13
13
|
const axios = require('axios')
|
|
14
14
|
|
|
15
15
|
export class RestClient extends Component {
|
|
16
|
-
public post(url: string, args: any[]) {
|
|
17
|
-
return axios.post(url, args)
|
|
16
|
+
public post(url: string, args: any[],headers:any={}) {
|
|
17
|
+
return axios.post(url, args,headers)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
public get(url: string, args: any[]) {
|
|
21
|
-
return axios.
|
|
20
|
+
public get(url: string, args: any[],headers:any={}) {
|
|
21
|
+
return axios.get(url, args ,headers )
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
public Rest(params: any) {
|