myorm_pg 2.5.3 → 2.6.1

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 (61) hide show
  1. package/README.md +20 -0
  2. package/lib/Index.d.ts +32 -32
  3. package/lib/Index.js +53 -53
  4. package/lib/core/decorators/SchemasDecorators.d.ts +44 -44
  5. package/lib/core/decorators/SchemasDecorators.js +144 -144
  6. package/lib/core/decorators/SchemasDecorators.js.map +1 -1
  7. package/lib/core/design/Type.d.ts +38 -37
  8. package/lib/core/design/Type.d.ts.map +1 -1
  9. package/lib/core/design/Type.js +142 -137
  10. package/lib/core/design/Type.js.map +1 -1
  11. package/lib/core/enums/DBTypes.d.ts +18 -18
  12. package/lib/core/enums/DBTypes.js +22 -22
  13. package/lib/core/enums/DBTypes.js.map +1 -1
  14. package/lib/core/enums/RelationType.d.ts +6 -6
  15. package/lib/core/enums/RelationType.js +10 -10
  16. package/lib/core/enums/RelationType.js.map +1 -1
  17. package/lib/core/exceptions/ConnectionFailException.d.ts +4 -4
  18. package/lib/core/exceptions/ConnectionFailException.js +12 -12
  19. package/lib/core/exceptions/ConstraintFailException.d.ts +4 -4
  20. package/lib/core/exceptions/ConstraintFailException.js +12 -12
  21. package/lib/core/exceptions/Exception.d.ts +4 -4
  22. package/lib/core/exceptions/Exception.js +9 -9
  23. package/lib/core/exceptions/InvalidOperationException.d.ts +5 -5
  24. package/lib/core/exceptions/InvalidOperationException.js +13 -13
  25. package/lib/core/exceptions/NotImplementedException.d.ts +4 -4
  26. package/lib/core/exceptions/NotImplementedException.js +12 -12
  27. package/lib/core/exceptions/QueryFailException.d.ts +5 -5
  28. package/lib/core/exceptions/QueryFailException.js +13 -13
  29. package/lib/core/exceptions/TypeNotSuportedException.d.ts +4 -4
  30. package/lib/core/exceptions/TypeNotSuportedException.js +12 -12
  31. package/lib/core/objects/DBOperationLogHandler.d.ts +15 -15
  32. package/lib/core/objects/DBOperationLogHandler.js +16 -16
  33. package/lib/core/objects/DBOperationLogHandler.js.map +1 -1
  34. package/lib/core/objects/interfaces/IDBConnection.d.ts +13 -13
  35. package/lib/core/objects/interfaces/IDBConnection.js +2 -2
  36. package/lib/core/objects/interfaces/IDBContext.d.ts +41 -41
  37. package/lib/core/objects/interfaces/IDBContext.js +2 -2
  38. package/lib/core/objects/interfaces/IDBManager.d.ts +12 -12
  39. package/lib/core/objects/interfaces/IDBManager.js +2 -2
  40. package/lib/core/objects/interfaces/IDBSet.d.ts +41 -37
  41. package/lib/core/objects/interfaces/IDBSet.d.ts.map +1 -1
  42. package/lib/core/objects/interfaces/IDBSet.js +2 -2
  43. package/lib/core/objects/interfaces/IStatement.d.ts +16 -16
  44. package/lib/core/objects/interfaces/IStatement.js +15 -15
  45. package/lib/core/objects/interfaces/IStatement.js.map +1 -1
  46. package/lib/implementations/PGDBConnection.d.ts +18 -18
  47. package/lib/implementations/PGDBConnection.js +102 -102
  48. package/lib/implementations/PGDBContext.d.ts +55 -55
  49. package/lib/implementations/PGDBContext.js +298 -298
  50. package/lib/implementations/PGDBManager.d.ts +31 -31
  51. package/lib/implementations/PGDBManager.js +242 -242
  52. package/lib/implementations/PGDBSet.d.ts +56 -51
  53. package/lib/implementations/PGDBSet.d.ts.map +1 -1
  54. package/lib/implementations/PGDBSet.js +1072 -941
  55. package/lib/implementations/PGDBSet.js.map +1 -1
  56. package/lib/implementations/PGFluentField.d.ts +17 -17
  57. package/lib/implementations/PGFluentField.js +153 -153
  58. package/lib/implementations/PGSetHelper.d.ts +18 -18
  59. package/lib/implementations/PGSetHelper.js +47 -47
  60. package/lib/implementations/PGSetHelper.js.map +1 -1
  61. package/package.json +1 -1
@@ -1,32 +1,32 @@
1
- import 'reflect-metadata';
2
- import IDBManager from '../core/objects/interfaces/IDBManager';
3
- import PGDBConnection from './PGDBConnection';
4
- import DBOperationLogHandler from '../core/objects/DBOperationLogHandler';
5
- export default class PGDBManager implements IDBManager {
6
- private _connection;
7
- private _logger?;
8
- constructor(connection: PGDBConnection);
9
- CheckConnection(): Promise<boolean>;
10
- CheckDatabase(dababase: string): Promise<boolean>;
11
- CreateDataBase(dababase: string): Promise<void>;
12
- CheckTable(cTor: Function): Promise<boolean>;
13
- CreateTable(cTor: Function): Promise<void>;
14
- CheckColumn(cTor: Function, key: string): Promise<boolean>;
15
- CreateColumn(cTor: Function, key: string): Promise<void>;
16
- UpdateDatabaseForEntity(cTor: Function): Promise<void>;
17
- ExecuteNonQuery(query: string): Promise<void>;
18
- Execute(query: string): Promise<any>;
19
- static Build(host: string, port: number, dababase: string, user: string, pass: string): PGDBManager;
20
- static BuildFromEnviroment(): PGDBManager;
21
- private CreatePromisse;
22
- /**
23
- * @private
24
- * @method
25
- * @param {string} type the desing type of class property
26
- * @returns {string} the postgres type correspondent
27
- */
28
- private CastToPostgreSQLType;
29
- SetLogger(logger: DBOperationLogHandler): void;
30
- private Log;
31
- }
1
+ import 'reflect-metadata';
2
+ import IDBManager from '../core/objects/interfaces/IDBManager';
3
+ import PGDBConnection from './PGDBConnection';
4
+ import DBOperationLogHandler from '../core/objects/DBOperationLogHandler';
5
+ export default class PGDBManager implements IDBManager {
6
+ private _connection;
7
+ private _logger?;
8
+ constructor(connection: PGDBConnection);
9
+ CheckConnection(): Promise<boolean>;
10
+ CheckDatabase(dababase: string): Promise<boolean>;
11
+ CreateDataBase(dababase: string): Promise<void>;
12
+ CheckTable(cTor: Function): Promise<boolean>;
13
+ CreateTable(cTor: Function): Promise<void>;
14
+ CheckColumn(cTor: Function, key: string): Promise<boolean>;
15
+ CreateColumn(cTor: Function, key: string): Promise<void>;
16
+ UpdateDatabaseForEntity(cTor: Function): Promise<void>;
17
+ ExecuteNonQuery(query: string): Promise<void>;
18
+ Execute(query: string): Promise<any>;
19
+ static Build(host: string, port: number, dababase: string, user: string, pass: string): PGDBManager;
20
+ static BuildFromEnviroment(): PGDBManager;
21
+ private CreatePromisse;
22
+ /**
23
+ * @private
24
+ * @method
25
+ * @param {string} type the desing type of class property
26
+ * @returns {string} the postgres type correspondent
27
+ */
28
+ private CastToPostgreSQLType;
29
+ SetLogger(logger: DBOperationLogHandler): void;
30
+ private Log;
31
+ }
32
32
  //# sourceMappingURL=PGDBManager.d.ts.map
@@ -1,243 +1,243 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- require("reflect-metadata");
16
- const TypeNotSuportedException_1 = __importDefault(require("../core/exceptions/TypeNotSuportedException"));
17
- const Type_1 = __importDefault(require("../core/design/Type"));
18
- const PGDBConnection_1 = __importDefault(require("./PGDBConnection"));
19
- const SchemasDecorators_1 = __importDefault(require("../core/decorators/SchemasDecorators"));
20
- const InvalidOperationException_1 = __importDefault(require("../core/exceptions/InvalidOperationException"));
21
- const Index_1 = require("../Index");
22
- const RelationType_1 = require("../core/enums/RelationType");
23
- const DBOperationLogHandler_1 = require("../core/objects/DBOperationLogHandler");
24
- class PGDBManager {
25
- constructor(connection) {
26
- this._connection = connection;
27
- }
28
- CheckConnection() {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- this.Log("Checking connection", DBOperationLogHandler_1.LogType.CHECKCONNECTION);
31
- try {
32
- yield this._connection.Open();
33
- return true;
34
- }
35
- catch (_a) {
36
- return false;
37
- }
38
- finally {
39
- yield this._connection.Close();
40
- }
41
- });
42
- }
43
- CheckDatabase(dababase) {
44
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
45
- this.Log(`Checking database ${dababase}`, DBOperationLogHandler_1.LogType.CHECKDATABASE);
46
- yield this._connection.AsPostgres().Open();
47
- let result = yield this._connection.Execute(`select * from pg_database where datname = '${dababase}'`);
48
- return result.rows.length > 0;
49
- }));
50
- }
51
- CreateDataBase(dababase) {
52
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
53
- this.Log(`Creating database ${dababase}`, DBOperationLogHandler_1.LogType.CREATEDATABASE);
54
- yield this._connection.AsPostgres().Open();
55
- yield this._connection.Execute(`create database ${dababase} with owner ${this._connection.UserName};`);
56
- }));
57
- }
58
- CheckTable(cTor) {
59
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
60
- let table = Type_1.default.GetTableName(cTor);
61
- this.Log(`Checking table ${table}`, DBOperationLogHandler_1.LogType.CHECKTABLE);
62
- yield this._connection.Open();
63
- let result = yield this._connection.Execute(`select * from information_schema.tables where table_catalog = '${this._connection.DataBaseName}' and table_name = '${table}';`);
64
- return result.rows.length > 0;
65
- }));
66
- }
67
- CreateTable(cTor) {
68
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
69
- let table = Type_1.default.GetTableName(cTor);
70
- this.Log(`Creating table ${table}`, DBOperationLogHandler_1.LogType.CREATETABLE);
71
- yield this._connection.Open();
72
- yield this._connection.Execute(`create table if not exists "${table}"();`);
73
- }));
74
- }
75
- CheckColumn(cTor, key) {
76
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
77
- let table = Type_1.default.GetTableName(cTor);
78
- let column = Type_1.default.GetColumnName(cTor, key);
79
- this.Log(`Checking column ${table}.${column}`, DBOperationLogHandler_1.LogType.CHECKCOLUMN);
80
- yield this._connection.Open();
81
- let result = yield this._connection.Execute(`select * from information_schema.columns where table_name = '${table}' and column_name = '${column}';`);
82
- return result.rows.length > 0;
83
- }));
84
- }
85
- CreateColumn(cTor, key) {
86
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
87
- let table = Type_1.default.GetTableName(cTor);
88
- let column = Type_1.default.GetColumnName(cTor, key);
89
- this.Log(`Creating column ${table}.${column}`, DBOperationLogHandler_1.LogType.CHECKCOLUMN);
90
- let type = "";
91
- try {
92
- type = this.CastToPostgreSQLType(Type_1.default.GetDesingTimeTypeName(cTor, key));
93
- }
94
- catch (ex) {
95
- let subType = Type_1.default.GetDesingType(cTor, key);
96
- let relation = SchemasDecorators_1.default.GetRelationAttribute(cTor, key);
97
- if (subType == undefined || subType == Array) {
98
- if (relation)
99
- subType = relation.TypeBuilder();
100
- if (relation == undefined) {
101
- throw new InvalidOperationException_1.default(`Can not determine the relation of porperty ${cTor.name}${key}`);
102
- }
103
- }
104
- let relatedKey = SchemasDecorators_1.default.ExtractPrimaryKey(subType);
105
- if (!relatedKey)
106
- throw new InvalidOperationException_1.default(`Can not determine the primary key of ${subType.name}`);
107
- if ((relation === null || relation === void 0 ? void 0 : relation.Relation) == RelationType_1.RelationType.ONE_TO_MANY || (relation === null || relation === void 0 ? void 0 : relation.Relation) == RelationType_1.RelationType.MANY_TO_MANY) {
108
- type = this.CastToPostgreSQLType(Type_1.default.AsArray(Type_1.default.GetDesingTimeTypeName(subType, relatedKey)));
109
- }
110
- else {
111
- type = this.CastToPostgreSQLType(Type_1.default.GetDesingTimeTypeName(subType, relatedKey));
112
- }
113
- if (type == Index_1.DBTypes.SERIAL)
114
- type = this.CastToPostgreSQLType(Index_1.DBTypes.LONG);
115
- }
116
- yield this._connection.Open();
117
- yield this._connection.Execute(`alter table "${table}" add column "${column}" ${type};`);
118
- if (SchemasDecorators_1.default.IsPrimaryKey(cTor, key)) {
119
- yield this._connection.Execute(`alter table "${table}" add constraint ${table}_${column}_pk primary key (${column});`);
120
- }
121
- }));
122
- }
123
- UpdateDatabaseForEntity(cTor) {
124
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
125
- this.Log(`Checking entity ${cTor.name}`, DBOperationLogHandler_1.LogType.CHECKENTITY);
126
- let table_name = Type_1.default.GetTableName(cTor);
127
- if (table_name == undefined)
128
- throw new TypeNotSuportedException_1.default(`The type ${cTor.name} is not supported. Can not determine the table name of type`);
129
- yield this._connection.Open();
130
- if (!(yield this.CheckTable(cTor)))
131
- yield this.CreateTable(cTor);
132
- for (let column of Type_1.default.GetProperties(cTor)) {
133
- if (!(yield this.CheckColumn(cTor, column))) {
134
- yield this.CreateColumn(cTor, column);
135
- }
136
- }
137
- }));
138
- }
139
- ExecuteNonQuery(query) {
140
- return __awaiter(this, void 0, void 0, function* () {
141
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
142
- yield this._connection.Open();
143
- this.Log(query, DBOperationLogHandler_1.LogType.QUERY);
144
- yield this._connection.Execute(query);
145
- }));
146
- });
147
- }
148
- Execute(query) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
151
- yield this._connection.Open();
152
- this.Log(query, DBOperationLogHandler_1.LogType.QUERY);
153
- return yield this._connection.Execute(query);
154
- }));
155
- });
156
- }
157
- static Build(host, port, dababase, user, pass) {
158
- return new PGDBManager(new PGDBConnection_1.default(host, port, dababase, user, pass));
159
- }
160
- static BuildFromEnviroment() {
161
- let host = process.env.DB_HOST || "";
162
- let port = process.env.DB_PORT || "0";
163
- let username = process.env.DB_USER || "";
164
- let password = process.env.DB_PASS || "";
165
- let database = process.env.DB_NAME || "";
166
- let intPort = 0;
167
- try {
168
- intPort = Number.parseInt(port);
169
- }
170
- catch (_a) { }
171
- if (!host)
172
- throw new InvalidOperationException_1.default(`DB_HOST enviroment variable was no value`);
173
- if (!port || Number.isNaN(intPort))
174
- throw new InvalidOperationException_1.default(`DB_PORT enviroment variable was no value`);
175
- if (!username)
176
- throw new InvalidOperationException_1.default(`DB_USER enviroment variable was no value`);
177
- if (!password)
178
- throw new InvalidOperationException_1.default(`DB_PASS enviroment variable was no value`);
179
- if (!database)
180
- throw new InvalidOperationException_1.default(`DB_NAME enviroment variable was no value`);
181
- return PGDBManager.Build(host, intPort, database, username, password);
182
- }
183
- CreatePromisse(func) {
184
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
185
- let success = true;
186
- let result;
187
- try {
188
- result = yield func();
189
- }
190
- catch (err) {
191
- success = false;
192
- result = err;
193
- }
194
- finally {
195
- yield this._connection.Close();
196
- if (success)
197
- resolve(result);
198
- else
199
- reject(result);
200
- }
201
- }));
202
- }
203
- /**
204
- * @private
205
- * @method
206
- * @param {string} type the desing type of class property
207
- * @returns {string} the postgres type correspondent
208
- */
209
- CastToPostgreSQLType(type) {
210
- switch (type.toLowerCase()) {
211
- case "integer": return "integer";
212
- case "number": return "bigint";
213
- case "long": return "bigint";
214
- case "double": return "float";
215
- case "text": return "text";
216
- case "string": return "text";
217
- case "date": return "date";
218
- case "datetime": return "timestamp";
219
- case "boolean": return "boolean";
220
- case "serial": return "serial";
221
- case "integer[]": return "integer[]";
222
- case "number[]": return "bigint[]";
223
- case "long[]": return "bigint[]";
224
- case "text[]": return "text[]";
225
- case "string[]": return "text[]";
226
- case "date[]": return "date[]";
227
- case "datetime[]": return "timestamp[]";
228
- case "boolean[]": return "boolean[]";
229
- case "double[]": return "float[]";
230
- default: throw new TypeNotSuportedException_1.default(`The type ${type} is not suported`);
231
- }
232
- }
233
- SetLogger(logger) { this._logger = logger; }
234
- Log(message, type) {
235
- if (this._logger)
236
- try {
237
- this._logger(message, type);
238
- }
239
- catch (_a) { }
240
- }
241
- }
242
- exports.default = PGDBManager;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ require("reflect-metadata");
16
+ const TypeNotSuportedException_1 = __importDefault(require("../core/exceptions/TypeNotSuportedException"));
17
+ const Type_1 = __importDefault(require("../core/design/Type"));
18
+ const PGDBConnection_1 = __importDefault(require("./PGDBConnection"));
19
+ const SchemasDecorators_1 = __importDefault(require("../core/decorators/SchemasDecorators"));
20
+ const InvalidOperationException_1 = __importDefault(require("../core/exceptions/InvalidOperationException"));
21
+ const Index_1 = require("../Index");
22
+ const RelationType_1 = require("../core/enums/RelationType");
23
+ const DBOperationLogHandler_1 = require("../core/objects/DBOperationLogHandler");
24
+ class PGDBManager {
25
+ constructor(connection) {
26
+ this._connection = connection;
27
+ }
28
+ CheckConnection() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ this.Log("Checking connection", DBOperationLogHandler_1.LogType.CHECKCONNECTION);
31
+ try {
32
+ yield this._connection.Open();
33
+ return true;
34
+ }
35
+ catch (_a) {
36
+ return false;
37
+ }
38
+ finally {
39
+ yield this._connection.Close();
40
+ }
41
+ });
42
+ }
43
+ CheckDatabase(dababase) {
44
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
45
+ this.Log(`Checking database ${dababase}`, DBOperationLogHandler_1.LogType.CHECKDATABASE);
46
+ yield this._connection.AsPostgres().Open();
47
+ let result = yield this._connection.Execute(`select * from pg_database where datname = '${dababase}'`);
48
+ return result.rows.length > 0;
49
+ }));
50
+ }
51
+ CreateDataBase(dababase) {
52
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
53
+ this.Log(`Creating database ${dababase}`, DBOperationLogHandler_1.LogType.CREATEDATABASE);
54
+ yield this._connection.AsPostgres().Open();
55
+ yield this._connection.Execute(`create database ${dababase} with owner ${this._connection.UserName};`);
56
+ }));
57
+ }
58
+ CheckTable(cTor) {
59
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
60
+ let table = Type_1.default.GetTableName(cTor);
61
+ this.Log(`Checking table ${table}`, DBOperationLogHandler_1.LogType.CHECKTABLE);
62
+ yield this._connection.Open();
63
+ let result = yield this._connection.Execute(`select * from information_schema.tables where table_catalog = '${this._connection.DataBaseName}' and table_name = '${table}';`);
64
+ return result.rows.length > 0;
65
+ }));
66
+ }
67
+ CreateTable(cTor) {
68
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
69
+ let table = Type_1.default.GetTableName(cTor);
70
+ this.Log(`Creating table ${table}`, DBOperationLogHandler_1.LogType.CREATETABLE);
71
+ yield this._connection.Open();
72
+ yield this._connection.Execute(`create table if not exists "${table}"();`);
73
+ }));
74
+ }
75
+ CheckColumn(cTor, key) {
76
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
77
+ let table = Type_1.default.GetTableName(cTor);
78
+ let column = Type_1.default.GetColumnName(cTor, key);
79
+ this.Log(`Checking column ${table}.${column}`, DBOperationLogHandler_1.LogType.CHECKCOLUMN);
80
+ yield this._connection.Open();
81
+ let result = yield this._connection.Execute(`select * from information_schema.columns where table_name = '${table}' and column_name = '${column}';`);
82
+ return result.rows.length > 0;
83
+ }));
84
+ }
85
+ CreateColumn(cTor, key) {
86
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
87
+ let table = Type_1.default.GetTableName(cTor);
88
+ let column = Type_1.default.GetColumnName(cTor, key);
89
+ this.Log(`Creating column ${table}.${column}`, DBOperationLogHandler_1.LogType.CHECKCOLUMN);
90
+ let type = "";
91
+ try {
92
+ type = this.CastToPostgreSQLType(Type_1.default.GetDesingTimeTypeName(cTor, key));
93
+ }
94
+ catch (ex) {
95
+ let subType = Type_1.default.GetDesingType(cTor, key);
96
+ let relation = SchemasDecorators_1.default.GetRelationAttribute(cTor, key);
97
+ if (subType == undefined || subType == Array) {
98
+ if (relation)
99
+ subType = relation.TypeBuilder();
100
+ if (relation == undefined) {
101
+ throw new InvalidOperationException_1.default(`Can not determine the relation of porperty ${cTor.name}${key}`);
102
+ }
103
+ }
104
+ let relatedKey = SchemasDecorators_1.default.ExtractPrimaryKey(subType);
105
+ if (!relatedKey)
106
+ throw new InvalidOperationException_1.default(`Can not determine the primary key of ${subType.name}`);
107
+ if ((relation === null || relation === void 0 ? void 0 : relation.Relation) == RelationType_1.RelationType.ONE_TO_MANY || (relation === null || relation === void 0 ? void 0 : relation.Relation) == RelationType_1.RelationType.MANY_TO_MANY) {
108
+ type = this.CastToPostgreSQLType(Type_1.default.AsArray(Type_1.default.GetDesingTimeTypeName(subType, relatedKey)));
109
+ }
110
+ else {
111
+ type = this.CastToPostgreSQLType(Type_1.default.GetDesingTimeTypeName(subType, relatedKey));
112
+ }
113
+ if (type == Index_1.DBTypes.SERIAL)
114
+ type = this.CastToPostgreSQLType(Index_1.DBTypes.LONG);
115
+ }
116
+ yield this._connection.Open();
117
+ yield this._connection.Execute(`alter table "${table}" add column "${column}" ${type};`);
118
+ if (SchemasDecorators_1.default.IsPrimaryKey(cTor, key)) {
119
+ yield this._connection.Execute(`alter table "${table}" add constraint ${table}_${column}_pk primary key (${column});`);
120
+ }
121
+ }));
122
+ }
123
+ UpdateDatabaseForEntity(cTor) {
124
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
125
+ this.Log(`Checking entity ${cTor.name}`, DBOperationLogHandler_1.LogType.CHECKENTITY);
126
+ let table_name = Type_1.default.GetTableName(cTor);
127
+ if (table_name == undefined)
128
+ throw new TypeNotSuportedException_1.default(`The type ${cTor.name} is not supported. Can not determine the table name of type`);
129
+ yield this._connection.Open();
130
+ if (!(yield this.CheckTable(cTor)))
131
+ yield this.CreateTable(cTor);
132
+ for (let column of Type_1.default.GetProperties(cTor)) {
133
+ if (!(yield this.CheckColumn(cTor, column))) {
134
+ yield this.CreateColumn(cTor, column);
135
+ }
136
+ }
137
+ }));
138
+ }
139
+ ExecuteNonQuery(query) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
142
+ yield this._connection.Open();
143
+ this.Log(query, DBOperationLogHandler_1.LogType.QUERY);
144
+ yield this._connection.Execute(query);
145
+ }));
146
+ });
147
+ }
148
+ Execute(query) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ return this.CreatePromisse(() => __awaiter(this, void 0, void 0, function* () {
151
+ yield this._connection.Open();
152
+ this.Log(query, DBOperationLogHandler_1.LogType.QUERY);
153
+ return yield this._connection.Execute(query);
154
+ }));
155
+ });
156
+ }
157
+ static Build(host, port, dababase, user, pass) {
158
+ return new PGDBManager(new PGDBConnection_1.default(host, port, dababase, user, pass));
159
+ }
160
+ static BuildFromEnviroment() {
161
+ let host = process.env.DB_HOST || "";
162
+ let port = process.env.DB_PORT || "0";
163
+ let username = process.env.DB_USER || "";
164
+ let password = process.env.DB_PASS || "";
165
+ let database = process.env.DB_NAME || "";
166
+ let intPort = 0;
167
+ try {
168
+ intPort = Number.parseInt(port);
169
+ }
170
+ catch (_a) { }
171
+ if (!host)
172
+ throw new InvalidOperationException_1.default(`DB_HOST enviroment variable was no value`);
173
+ if (!port || Number.isNaN(intPort))
174
+ throw new InvalidOperationException_1.default(`DB_PORT enviroment variable was no value`);
175
+ if (!username)
176
+ throw new InvalidOperationException_1.default(`DB_USER enviroment variable was no value`);
177
+ if (!password)
178
+ throw new InvalidOperationException_1.default(`DB_PASS enviroment variable was no value`);
179
+ if (!database)
180
+ throw new InvalidOperationException_1.default(`DB_NAME enviroment variable was no value`);
181
+ return PGDBManager.Build(host, intPort, database, username, password);
182
+ }
183
+ CreatePromisse(func) {
184
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
185
+ let success = true;
186
+ let result;
187
+ try {
188
+ result = yield func();
189
+ }
190
+ catch (err) {
191
+ success = false;
192
+ result = err;
193
+ }
194
+ finally {
195
+ yield this._connection.Close();
196
+ if (success)
197
+ resolve(result);
198
+ else
199
+ reject(result);
200
+ }
201
+ }));
202
+ }
203
+ /**
204
+ * @private
205
+ * @method
206
+ * @param {string} type the desing type of class property
207
+ * @returns {string} the postgres type correspondent
208
+ */
209
+ CastToPostgreSQLType(type) {
210
+ switch (type.toLowerCase()) {
211
+ case "integer": return "integer";
212
+ case "number": return "bigint";
213
+ case "long": return "bigint";
214
+ case "double": return "float";
215
+ case "text": return "text";
216
+ case "string": return "text";
217
+ case "date": return "date";
218
+ case "datetime": return "timestamp";
219
+ case "boolean": return "boolean";
220
+ case "serial": return "serial";
221
+ case "integer[]": return "integer[]";
222
+ case "number[]": return "bigint[]";
223
+ case "long[]": return "bigint[]";
224
+ case "text[]": return "text[]";
225
+ case "string[]": return "text[]";
226
+ case "date[]": return "date[]";
227
+ case "datetime[]": return "timestamp[]";
228
+ case "boolean[]": return "boolean[]";
229
+ case "double[]": return "float[]";
230
+ default: throw new TypeNotSuportedException_1.default(`The type ${type} is not suported`);
231
+ }
232
+ }
233
+ SetLogger(logger) { this._logger = logger; }
234
+ Log(message, type) {
235
+ if (this._logger)
236
+ try {
237
+ this._logger(message, type);
238
+ }
239
+ catch (_a) { }
240
+ }
241
+ }
242
+ exports.default = PGDBManager;
243
243
  //# sourceMappingURL=PGDBManager.js.map