chyz 1.0.12-rc.7 → 1.0.13-rc.10

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.
Files changed (116) hide show
  1. package/BaseChyz.ts +118 -18
  2. package/Chyz.ts +0 -3
  3. package/Examples/Controllers/ApiController.ts +96 -29
  4. package/Examples/Controllers/KeyCloakController.ts +100 -0
  5. package/Examples/Controllers/PublicController.ts +5 -7
  6. package/Examples/Controllers/SiteController.ts +82 -32
  7. package/Examples/Models/Categories.ts +47 -0
  8. package/Examples/Models/Customer.ts +115 -0
  9. package/Examples/Models/KeycloakUser.ts +66 -0
  10. package/Examples/Models/Order.ts +64 -0
  11. package/Examples/Models/OrderItem.ts +27 -0
  12. package/Examples/Models/ProductModels.ts +49 -0
  13. package/Examples/Models/ProductToCategories.ts +46 -0
  14. package/Examples/Models/Products.ts +50 -0
  15. package/Examples/Models/Stocks.ts +60 -0
  16. package/Examples/Models/User.ts +66 -35
  17. package/Examples/Models/UserPermission.ts +37 -0
  18. package/Examples/Models/index.ts +19 -0
  19. package/Examples/index-keycloack.ts +78 -0
  20. package/Examples/index.ts +16 -14
  21. package/Examples/keycloak.json +7 -0
  22. package/Examples/log/app.log +6126 -1045
  23. package/Examples/log/errors.log +1503 -234
  24. package/Examples/package.json +46 -44
  25. package/Examples/tsconfig.json +3 -2
  26. package/README.md +116 -16
  27. package/base/ActionFilter.ts +2 -2
  28. package/base/BaseError.ts +2 -2
  29. package/base/DataErrorDbException.ts +1 -1
  30. package/base/DbConnection.ts +8 -2
  31. package/base/ForbiddenHttpException.ts +1 -1
  32. package/base/InvalidArgumentException.ts +16 -0
  33. package/base/InvalidConfigException.ts +1 -1
  34. package/base/Model.ts +206 -17
  35. package/base/ModelManager.ts +14 -0
  36. package/base/NotFoundHttpException.ts +1 -1
  37. package/base/RestClient.ts +28 -0
  38. package/base/UnauthorizedHttpException.ts +2 -1
  39. package/base/ValidationHttpException.ts +14 -0
  40. package/base/db/Exception.ts +14 -0
  41. package/base/index.ts +4 -0
  42. package/dist/BaseChyz.js +108 -23
  43. package/dist/BaseChyz.js.map +1 -1
  44. package/dist/Chyz.js.map +1 -1
  45. package/dist/base/ActionFilter.js +2 -2
  46. package/dist/base/ActionFilter.js.map +1 -1
  47. package/dist/base/BaseError.js +2 -2
  48. package/dist/base/BaseError.js.map +1 -1
  49. package/dist/base/DataErrorDbException.js +1 -1
  50. package/dist/base/DataErrorDbException.js.map +1 -1
  51. package/dist/base/DbConnection.js +9 -2
  52. package/dist/base/DbConnection.js.map +1 -1
  53. package/dist/base/ForbiddenHttpException.js +1 -1
  54. package/dist/base/ForbiddenHttpException.js.map +1 -1
  55. package/dist/base/InvalidArgumentException.js +18 -0
  56. package/dist/base/InvalidArgumentException.js.map +1 -0
  57. package/dist/base/InvalidConfigException.js +1 -1
  58. package/dist/base/InvalidConfigException.js.map +1 -1
  59. package/dist/base/Model.js +187 -15
  60. package/dist/base/Model.js.map +1 -1
  61. package/dist/base/ModelManager.js +17 -0
  62. package/dist/base/ModelManager.js.map +1 -0
  63. package/dist/base/NotFoundHttpException.js +1 -1
  64. package/dist/base/NotFoundHttpException.js.map +1 -1
  65. package/dist/base/RestClient.js +27 -0
  66. package/dist/base/RestClient.js.map +1 -0
  67. package/dist/base/UnauthorizedHttpException.js +1 -1
  68. package/dist/base/UnauthorizedHttpException.js.map +1 -1
  69. package/dist/base/ValidationHttpException.js +18 -0
  70. package/dist/base/ValidationHttpException.js.map +1 -0
  71. package/dist/base/db/Exception.js +16 -0
  72. package/dist/base/db/Exception.js.map +1 -0
  73. package/dist/base/index.js +4 -0
  74. package/dist/base/index.js.map +1 -1
  75. package/dist/filters/AccessControl.js +2 -2
  76. package/dist/filters/AccessControl.js.map +1 -1
  77. package/dist/filters/AccessRule.js.map +1 -1
  78. package/dist/filters/auth/AuthMethod.js +2 -2
  79. package/dist/filters/auth/AuthMethod.js.map +1 -1
  80. package/dist/filters/auth/HttpHeaderAuth.js.map +1 -1
  81. package/dist/filters/auth/JwtHttpBearerAuth.js +1 -0
  82. package/dist/filters/auth/JwtHttpBearerAuth.js.map +1 -1
  83. package/dist/filters/auth/KeyCloakHttpBearerAuth.js +117 -0
  84. package/dist/filters/auth/KeyCloakHttpBearerAuth.js.map +1 -0
  85. package/dist/index.js +28 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/package.json +57 -0
  88. package/dist/rbac/AuthAssignment.js +45 -0
  89. package/dist/rbac/AuthAssignment.js.map +1 -0
  90. package/dist/rbac/AuthItem.js +52 -0
  91. package/dist/rbac/AuthItem.js.map +1 -0
  92. package/dist/rbac/AuthItemChild.js +44 -0
  93. package/dist/rbac/AuthItemChild.js.map +1 -0
  94. package/dist/rbac/AuthManager.js +351 -0
  95. package/dist/rbac/AuthManager.js.map +1 -0
  96. package/dist/web/{User.js → WebUser.js} +12 -7
  97. package/dist/web/WebUser.js.map +1 -0
  98. package/filters/AccessControl.ts +3 -3
  99. package/filters/AccessRule.ts +2 -2
  100. package/filters/auth/AuthMethod.ts +4 -4
  101. package/filters/auth/HttpHeaderAuth.ts +2 -2
  102. package/filters/auth/JwtHttpBearerAuth.ts +4 -5
  103. package/filters/auth/KeyCloakHttpBearerAuth.ts +115 -0
  104. package/index.ts +14 -0
  105. package/package-lock.json +5259 -0
  106. package/package.json +57 -52
  107. package/rbac/AuthAssignment.ts +50 -0
  108. package/rbac/AuthItem.ts +57 -0
  109. package/rbac/AuthItemChild.ts +50 -0
  110. package/rbac/AuthManager.ts +388 -0
  111. package/web/{User.ts → WebUser.ts} +10 -6
  112. package/.idea/Chy-Nodejs-Framework.iml +0 -12
  113. package/.idea/jsLibraryMappings.xml +0 -6
  114. package/.idea/modules.xml +0 -8
  115. package/.idea/vcs.xml +0 -6
  116. package/dist/web/User.js.map +0 -1
@@ -1,44 +1,46 @@
1
- {
2
- "name": "edis-manager-node",
3
- "version": "1.0.0",
4
- "description": "Entranet ve Erken Deprem Bildirim Sistemi (EDİS) entegrasyonu",
5
- "main": "index.js",
6
- "scripts": {
7
- "dev": "nodemon index.ts",
8
- "debug": "ts-node index.ts",
9
- "test": "echo \"Error: no test specified\" && exit 1"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/cihan53/edis-manager-node.git"
14
- },
15
- "author": "",
16
- "license": "MIT",
17
- "bugs": {
18
- "url": "https://github.com/cihan53/edis-manager-node/issues"
19
- },
20
- "homepage": "https://github.com/cihan53/edis-manager-node#readme",
21
- "dependencies": {
22
- "babel-plugin-transform-decorators-legacy": "^1.3.5",
23
- "body-parser": "^1.19.0",
24
- "dotenv-flow": "^3.2.0",
25
- "express": "^4.17.1",
26
- "glob": "^7.1.7",
27
- "jsonwebtoken": "^8.5.1",
28
- "lodash": "^4.17.21",
29
- "log4js": "^6.3.0",
30
- "method-override": "^3.0.0",
31
- "passport": "^0.4.1",
32
- "pg": "^8.7.1",
33
- "pg-hstore": "^2.3.4",
34
- "reflect-metadata": "^0.1.13",
35
- "sequelize": "^6.6.5"
36
- },
37
- "devDependencies": {
38
- "@types/express": "^4.17.13",
39
- "@types/node": "^16.4.13",
40
- "nodemon": "^2.0.12",
41
- "ts-node": "^10.2.0",
42
- "typescript": "^4.3.5"
43
- }
44
- }
1
+ {
2
+ "name": "edis-manager-node",
3
+ "version": "1.0.0",
4
+ "description": "Entranet ve Erken Deprem Bildirim Sistemi (EDİS) entegrasyonu",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "dev": "nodemon index.ts",
8
+ "debug": "ts-node index.ts",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/cihan53/edis-manager-node.git"
14
+ },
15
+ "author": "",
16
+ "license": "MIT",
17
+ "bugs": {
18
+ "url": "https://github.com/cihan53/edis-manager-node/issues"
19
+ },
20
+ "homepage": "https://github.com/cihan53/edis-manager-node#readme",
21
+ "dependencies": {
22
+ "babel-plugin-transform-decorators-legacy": "^1.3.5",
23
+ "bcrypt": "^5.0.1",
24
+ "body-parser": "^1.19.0",
25
+ "dotenv-flow": "^3.2.0",
26
+ "express": "^4.17.1",
27
+ "glob": "^7.1.7",
28
+ "jsonwebtoken": "^8.5.1",
29
+ "keycloak-connect": "^15.0.2",
30
+ "lodash": "^4.17.21",
31
+ "log4js": "^6.3.0",
32
+ "method-override": "^3.0.0",
33
+ "passport": "^0.4.1",
34
+ "pg": "^8.7.1",
35
+ "pg-hstore": "^2.3.4",
36
+ "reflect-metadata": "^0.1.13",
37
+ "sequelize": "^6.6.5"
38
+ },
39
+ "devDependencies": {
40
+ "@types/express": "^4.17.13",
41
+ "@types/node": "^16.4.13",
42
+ "nodemon": "^2.0.12",
43
+ "ts-node": "^10.2.0",
44
+ "typescript": "^4.3.5"
45
+ }
46
+ }
@@ -3,10 +3,11 @@
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
- // "moduleResolution": "node"
10
+ "moduleResolution": "node"
10
11
  },
11
12
  "exclude": [
12
13
  "node_modules"
package/README.md CHANGED
@@ -12,18 +12,26 @@ Klasör Yapısı<br>
12
12
  index.ts<br>
13
13
 
14
14
  `##Başlangıç<br>
15
- yarn start
15
+ yarn start
16
+
17
+ ## index.ts alanlar düzenlenmeli.
18
+
19
+ ```typescript
20
+ require('dotenv-flow').config();
21
+
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";
16
26
 
17
- ##index.ts alanlar düzenlenmeli.
18
- ```js
19
27
  let config = {
20
28
  components: {
21
- db:{
22
- class:DbConnection,
29
+ db: {
30
+ class: DbConnection,
23
31
  database: process.env.DBDATABASE,
24
- username:process.env.DBUSER,
25
- password:process.env.DBPASS,
26
- options:{
32
+ username: process.env.DBUSER,
33
+ password: process.env.DBPASS,
34
+ options: {
27
35
  host: process.env.DBHOST,
28
36
  dialect: 'postgres', /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
29
37
  // disable logging; default: console.log
@@ -31,21 +39,22 @@ let config = {
31
39
  }
32
40
  },
33
41
  user: {
34
- 'class': User,
35
- 'identityClass':Identity
42
+ 'class': WebUser,
43
+ 'identityClass': User
36
44
  }
37
45
  }
38
46
  }
39
47
  Chyz.app(config).Start();
40
48
  ```
41
49
 
42
- ##Create Model
50
+ ## Create Model
51
+
43
52
  Veritabanı işlemleri için model oluşturma, sequelize desteklidir.
44
53
 
45
- ```js
46
- import {Model,DataTypes} from "chyz/base/Model";
54
+ ```typescript
55
+ import {Model, DataTypes} from "chyz/base/Model";
47
56
 
48
- export class Customer extends Model {
57
+ export class CustomerCLass extends Model {
49
58
  public tableName() {
50
59
  return 'customer';
51
60
  }
@@ -77,11 +86,102 @@ export class Customer extends Model {
77
86
  }
78
87
 
79
88
  }
89
+
90
+
91
+ ```
92
+ ````typescript
93
+ export class ProductsClass extends Model {
94
+ [x: string]: any;
95
+
96
+ tableName() {
97
+ return 'products';
98
+ }
99
+
100
+ attributes() {
101
+ return {
102
+ // Model attributes are defined here
103
+ title: {
104
+ type: DataTypes.STRING,
105
+ allowNull: false
106
+ },
107
+ model_id: {
108
+ type: DataTypes.INTEGER,
109
+ allowNull: false
110
+ },
111
+ properties: {
112
+ type: DataTypes.STRING,
113
+ allowNull: false
114
+ }
115
+
116
+ }
117
+ }
118
+
119
+ relations(): Relation[] {
120
+ return [
121
+ {
122
+ type: "hasOne",
123
+ foreignKey: "id",
124
+ sourceKey: "customer_id",
125
+ model: Customer.model()
126
+ }
127
+ ]
128
+ }
129
+ }
130
+
131
+
132
+ ````
133
+
134
+ ## Http POST ve GET verilerini model'e yükleme
135
+
136
+ ````typescript
137
+
138
+ /**
139
+ * post data
140
+ * {
141
+ * "Customer":{
142
+ * "firstname":"cihan",
143
+ * "lastname":"ozturk"
144
+ * ....
145
+ * }
146
+ * }
147
+ * @type {Customer}
148
+ */
149
+ import {Customer} from "./Customer";
150
+ //Customer Model Create
151
+ let customer: Customer = Customer;
152
+ customer.load(req.body, "Customer");//load customer data
153
+ let cus: any = await customer.save();
154
+
155
+
156
+ ````
157
+ ## Transaction
158
+ Transaction oluşturma
159
+
160
+
161
+ ```typescript
162
+ let transaction
163
+ try {
164
+ // get transaction
165
+ transaction = await BaseChyz.getComponent("db").transaction();
166
+ //Customer Model Create
167
+ let customer: Customer = new Customer();
168
+ customer.load(data, "Customer");//load customer data
169
+ let cus: any = await customer.save({}, {transaction});
170
+ if (!cus) {
171
+ throw new ValidationHttpException(customer.errors);
172
+ }
173
+ } catch (e) {
174
+ if (transaction) {
175
+ await transaction.rollback();
176
+ BaseChyz.warn("Rollback transaction")
177
+ }
178
+ ...
179
+ }
80
180
  ```
81
181
 
82
- ##Yetkilendirme için kullanıcı modeli
182
+ ## Yetkilendirme için kullanıcı modeli
83
183
 
84
- ```js
184
+ ```typescript
85
185
  /*
86
186
  * Copyright (c) 2021. Chy Bilgisayar Bilisim
87
187
  * Author: Cihan Ozturk
@@ -25,7 +25,7 @@ export class ActionFilter extends Behavior {
25
25
  }
26
26
 
27
27
  public async beforeFilter(route: any, req: Request, res: Response) {
28
- if (!this.isActive(route.id)) {
28
+ if (!this.isActive(route)) {
29
29
  return;
30
30
  }
31
31
 
@@ -51,7 +51,7 @@ export class ActionFilter extends Behavior {
51
51
  for (const exceptKey in this.except) {
52
52
  let pattern = this.except[exceptKey];
53
53
  let match = id.match(pattern)
54
- if (match.length > 0) {
54
+ if (match && match.length > 0) {
55
55
  exceptMatch = true;
56
56
  }
57
57
  }
package/base/BaseError.ts CHANGED
@@ -8,11 +8,11 @@
8
8
  export class BaseError extends Error {
9
9
  private statusCode: number;
10
10
 
11
- constructor(message: string) {
11
+ constructor(message: string,statusCode=500) {
12
12
  super(message);
13
13
  this.message=message;
14
14
  this.name = this.constructor.name // good practice
15
- this.statusCode = 500 // error code for responding to client
15
+ this.statusCode = statusCode // error code for responding to client
16
16
  Error.captureStackTrace(this)
17
17
  }
18
18
 
@@ -11,6 +11,6 @@ import {BaseError} from "./BaseError";
11
11
 
12
12
  export class DataErrorDbException extends BaseError {
13
13
  constructor ( public message: string) {
14
- super(message);
14
+ super(message,502);
15
15
  }
16
16
  }
@@ -15,6 +15,7 @@ export class DbConnection extends Component {
15
15
  public username!: string
16
16
  public password!: string
17
17
  public options?: object
18
+ public _transaction:any;
18
19
 
19
20
  private _db: any
20
21
 
@@ -24,10 +25,10 @@ export class DbConnection extends Component {
24
25
  sequelize
25
26
  .authenticate()
26
27
  .then(() => {
27
- console.log('Connection has been established successfully.');
28
+ BaseChyz.info('Connection has been established successfully.');
28
29
  })
29
30
  .catch((err: any) => {
30
- console.error('Unable to connect to the database:', err);
31
+ BaseChyz.error('Unable to connect to the database:', err);
31
32
 
32
33
  });
33
34
 
@@ -42,4 +43,9 @@ export class DbConnection extends Component {
42
43
  set db(value: any) {
43
44
  this._db = value;
44
45
  }
46
+
47
+ public transaction(){
48
+ return this.db.transaction();
49
+ }
50
+
45
51
  }
@@ -8,7 +8,7 @@
8
8
  import {BaseError} from "./BaseError";
9
9
  export class ForbiddenHttpException extends BaseError {
10
10
  constructor ( message: string) {
11
- super(message);
11
+ super(message,403);
12
12
  }
13
13
  }
14
14
 
@@ -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
+ }
@@ -11,6 +11,6 @@ import {BaseError} from "./BaseError";
11
11
 
12
12
  export class InvalidConfigException extends BaseError {
13
13
  constructor ( public message: string) {
14
- super(message);
14
+ super(message,500);
15
15
  }
16
16
  }
package/base/Model.ts CHANGED
@@ -9,9 +9,21 @@ 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 {DataTypes} from "sequelize";
12
+ import {DatabaseError, DataTypes, ExclusionConstraintError, ForeignKeyConstraintError, Model as SModel, TimeoutError, UniqueConstraintError, ValidationError,} from "sequelize";
13
+ import {Exception} from "./db/Exception";
13
14
 
14
- export {DataTypes} from "sequelize";
15
+ export {DataTypes, NOW} from "sequelize";
16
+
17
+ export interface Relation{
18
+ type: "hasOne" | "hasMany" | "belongsToMany" | "belongsTo",
19
+ allowNull?:boolean,
20
+ sourceKey?:string,
21
+ model:SModel,
22
+ foreignKey:string,
23
+ name?:string,
24
+ through?:any,
25
+ as?:string
26
+ }
15
27
 
16
28
  /**
17
29
  * ValidateMe.init({
@@ -66,36 +78,109 @@ export {DataTypes} from "sequelize";
66
78
  }
67
79
  }, { sequelize });
68
80
  */
81
+
69
82
  export class Model extends Component {
70
- private sequelize: any
83
+ _sequelize: any ;
71
84
  private _tableName: string;
72
85
  private _model: any;
73
86
  private _attributes: any = {};
87
+ private _errors: any = {}
88
+
74
89
 
75
- constructor() {
90
+ constructor(sequelize?: IDBDatabase) {
76
91
  super();
77
92
  this._tableName = this.constructor.name;
78
- this.sequelize = BaseChyz.getComponent("db").db;
93
+ // this._sequelize = BaseChyz.getComponent("db").db;
94
+ if (sequelize != null)
95
+ this._sequelize = sequelize;
96
+ else
97
+ this._sequelize = BaseChyz.getComponent("db").db;
79
98
 
80
99
  if (!Utils.isEmpty(this.attributes())) {
81
-
82
- this._model = this.sequelize.define(this.constructor.name, this.attributes(), {
100
+ this._model = this._sequelize.define(this.constructor.name, this.attributes(), {
83
101
  tableName: this.tableName(),
84
- timestamps: false
102
+ timestamps: false,
103
+ createdAt: false,
104
+ updateAt:false
85
105
  });
86
106
 
87
107
  } else {
88
108
  throw new InvalidConfigException(BaseChyz.t("Invalid model configuration, is not emty attributes"))
89
109
  }
90
110
 
91
- this.init();
111
+ // this.init();
112
+
113
+ }
114
+
115
+
116
+ /**
117
+ * Returns the database connection used by this AR class.
118
+ * By default, the "db" application component is used as the database connection.
119
+ * You may override this method if you want to use a different database connection.
120
+ * @return Connection the database connection used by this AR class.
121
+ */
122
+ public static getDb()
123
+ {
124
+ return BaseChyz.getComponent("db").db
125
+ }
92
126
 
127
+ get sequelize(): any {
128
+ return this._sequelize;
129
+ }
130
+
131
+ set sequelize(value: any) {
132
+ this._sequelize = value;
133
+ }
134
+
135
+
136
+ /**
137
+ *
138
+ */
139
+ get errors(): any {
140
+ return this._errors;
141
+ }
142
+
143
+ set errors(value: any) {
144
+ this._errors = value;
93
145
  }
94
146
 
95
147
  public init() {
96
- BaseChyz.debug("Model init....",this.constructor.name)
148
+ BaseChyz.debug("Model init....", this.constructor.name)
149
+ /**
150
+ * init buraya
151
+ */
152
+ BaseChyz.debug("Relation init....", this.constructor.name)
153
+ for (const relation of this.relations()) {
154
+ let m = relation.model;
155
+
156
+ if(relation.type=="hasOne" ){
157
+ // @ts-ignore
158
+ delete relation.model
159
+ this.model().hasOne(m, relation );
160
+ }
161
+ //
162
+ if(relation.type=="hasMany" ){
163
+ // @ts-ignore
164
+ delete relation.model;
165
+ this.model().hasMany(m, relation );
166
+ }
167
+
168
+ if(relation.type=="belongsTo" ){
169
+ // @ts-ignore
170
+ delete relation.model;
171
+ this.model().belongsTo(m, relation );
172
+ }
173
+
174
+ if(relation.type=="belongsToMany" ){
175
+ // @ts-ignore
176
+ delete relation.model;
177
+ this.model().belongsToMany(m, relation );
178
+ }
179
+ }
180
+
97
181
  }
98
182
 
183
+
99
184
  public tableName() {
100
185
  return this._tableName;
101
186
  }
@@ -112,21 +197,98 @@ export class Model extends Component {
112
197
  return this._model;
113
198
  }
114
199
 
115
- public save() {
200
+ public async save(params = {}, options = {}) {
116
201
  // now instantiate an object
117
- return this._model.build(this._attributes).save()
202
+ let p = Object.assign(params, this._attributes)
203
+ let result: any;
204
+ try {
205
+ result = await this.model().create(p, options)
206
+ } catch (e) {
207
+ BaseChyz.debug(`Model[${this.constructor.name}].create`, e)
208
+ if (e instanceof ValidationError) {
209
+ let validationErrorItems = e.errors;
210
+ validationErrorItems.forEach(({message, path}) => {
211
+ // @ts-ignore
212
+ this._errors[path] = message
213
+ })
214
+
215
+ return false;
216
+ } else if (e instanceof DatabaseError) {
217
+
218
+ } else if (e instanceof TimeoutError) {
219
+
220
+ } else if (e instanceof UniqueConstraintError) {
221
+
222
+ } else if (e instanceof ForeignKeyConstraintError) {
223
+
224
+ } else if (e instanceof ExclusionConstraintError) {
225
+
226
+ }
227
+ throw new Exception(e.message, this.errors, e.code);
228
+ }
229
+
230
+ return result;
231
+
232
+ }
233
+
234
+ public async bulkCreate(params = {}, options = {}) {
235
+ // now instantiate an object
236
+ let p = Object.assign(params, this._attributes)
237
+ let result: any;
238
+ try {
239
+ result = await this.model().bulkCreate(p, options)
240
+ } catch (e) {
241
+ BaseChyz.debug(`Model[${this.constructor.name}].bulkCreate`, e)
242
+ if (e instanceof ValidationError) {
243
+ let validationErrorItems = e.errors;
244
+ validationErrorItems.forEach(({message, path}) => {
245
+ // @ts-ignore
246
+ this._errors[path] = message
247
+ })
248
+
249
+ return false;
250
+ } else if (e instanceof DatabaseError) {
251
+
252
+ } else if (e instanceof TimeoutError) {
253
+
254
+ } else if (e instanceof UniqueConstraintError) {
255
+
256
+ } else if (e instanceof ForeignKeyConstraintError) {
257
+
258
+ } else if (e instanceof ExclusionConstraintError) {
259
+
260
+ }
261
+ throw new Exception(e.message, this.errors, e.code);
262
+ }
263
+
264
+ return result;
265
+
118
266
  }
119
267
 
120
- public update() {
121
- return this._model.build(this._attributes).update()
268
+ public update(params = {}, options = {}) {
269
+ let p = Object.assign(params, this._attributes)
270
+ return this.model().update(p, options)
122
271
  }
123
272
 
124
- public findOne(...args: any[]){
273
+ public delete(params = {}, options = {}) {
274
+ let p = Object.assign(params, this._attributes)
275
+ return this.model().delete(p, options)
276
+ }
277
+
278
+ /**
279
+ *
280
+ * @param args
281
+ */
282
+ public findOne(...args: any[]) {
125
283
  return this._model.findOne(...arguments)
126
284
  }
127
285
 
128
- public delete(...args: any[]) {
129
- return this._model.delete(...arguments)
286
+ /**
287
+ *
288
+ * @param args
289
+ */
290
+ public findAll(...args: any[]) {
291
+ return this._model.findAll(...arguments)
130
292
  }
131
293
 
132
294
 
@@ -134,6 +296,11 @@ export class Model extends Component {
134
296
 
135
297
  }
136
298
 
299
+ /**
300
+ *
301
+ * @param data
302
+ * @param formName
303
+ */
137
304
  public load(data: any, formName: any = null) {
138
305
  let scope = formName === null ? this.formName() : formName;
139
306
  if (scope === '' && !Utils.isEmpty(data)) {
@@ -146,6 +313,18 @@ export class Model extends Component {
146
313
  return false;
147
314
  }
148
315
 
316
+ public bulkLoad(data: any, formName: any = null) {
317
+ let scope = formName === null ? this.formName() : formName;
318
+ if (scope === '' && !Utils.isEmpty(data)) {
319
+ this.setAttribute(data);
320
+ return true;
321
+ } else if (data[scope]) {
322
+ this.setAttribute(data[scope]);
323
+ return true;
324
+ }
325
+ return false;
326
+ }
327
+
149
328
  public setAttribute(values: any, safeOnly = true) {
150
329
  if (values instanceof Object) {
151
330
  let attributes = this.attributes();
@@ -162,4 +341,14 @@ export class Model extends Component {
162
341
  public attributes() {
163
342
  return {}
164
343
  }
344
+
345
+ /**
346
+ * relation return array
347
+ * [
348
+ *
349
+ * ]
350
+ */
351
+ public relations():Relation[]{
352
+ return []
353
+ }
165
354
  }
@@ -0,0 +1,14 @@
1
+ /*
2
+ *
3
+ * Copyright (c) 2021.. Chy Bilgisayar Bilisim
4
+ * Author: Cihan Ozturk
5
+ * E-mail: cihan@chy.com.tr
6
+ * Github:https://github.com/cihan53/
7
+ *
8
+ */
9
+
10
+ export const ModelManager: any = {
11
+ _register(map: any) {
12
+ Object.assign(this, map)
13
+ }
14
+ }