emilsoftware-utilities 1.3.2 → 1.3.5
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/node-firebird.d.ts +1 -0
- package/node-firebird.js +17 -0
- package/orm.d.ts +5 -0
- package/orm.js +147 -29
- package/package.json +2 -2
- package/src/node-firebird.ts +1 -0
- package/src/orm.ts +69 -19
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "es-node-firebird";
|
package/node-firebird.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("es-node-firebird"), exports);
|
package/orm.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Database, Options, Transaction } from "es-node-firebird";
|
|
2
|
+
interface QueryWithParams {
|
|
3
|
+
query: string;
|
|
4
|
+
params: any[];
|
|
5
|
+
}
|
|
2
6
|
interface Orm {
|
|
3
7
|
quote: (value: string) => string;
|
|
4
8
|
testConnection: (options: Options) => Promise<any>;
|
|
@@ -7,6 +11,7 @@ interface Orm {
|
|
|
7
11
|
trimParam: (param: any) => string;
|
|
8
12
|
connect: (options: Options) => Promise<any>;
|
|
9
13
|
startTransaction: (db: Database) => Promise<any>;
|
|
14
|
+
executeMultiple: (options: Options, qwps: QueryWithParams[]) => any;
|
|
10
15
|
executeQueries: (transaction: Transaction, queries: string[], params: any[]) => any;
|
|
11
16
|
commitTransaction: (transaction: Transaction) => Promise<any>;
|
|
12
17
|
rollbackTransaction: (transaction: Transaction) => Promise<any>;
|
package/orm.js
CHANGED
|
@@ -22,6 +22,42 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
25
61
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
62
|
exports.Orm = void 0;
|
|
27
63
|
// @ts-ignore
|
|
@@ -146,35 +182,116 @@ var rollbackTransaction = function (transaction) {
|
|
|
146
182
|
});
|
|
147
183
|
});
|
|
148
184
|
};
|
|
149
|
-
var
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
185
|
+
var executeMultiple = function (options, queriesWithParams) { return __awaiter(void 0, void 0, void 0, function () {
|
|
186
|
+
var db, transaction, _loop_1, _i, queriesWithParams_1, qwp, error_1;
|
|
187
|
+
return __generator(this, function (_a) {
|
|
188
|
+
switch (_a.label) {
|
|
189
|
+
case 0:
|
|
190
|
+
_a.trys.push([0, 8, , 11]);
|
|
191
|
+
return [4 /*yield*/, connect(options)];
|
|
192
|
+
case 1:
|
|
193
|
+
// Connetti al database
|
|
194
|
+
db = _a.sent();
|
|
195
|
+
return [4 /*yield*/, startTransaction(db)];
|
|
196
|
+
case 2:
|
|
197
|
+
// Inizia la transazione
|
|
198
|
+
transaction = _a.sent();
|
|
199
|
+
_loop_1 = function (qwp) {
|
|
200
|
+
return __generator(this, function (_b) {
|
|
201
|
+
switch (_b.label) {
|
|
202
|
+
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
203
|
+
transaction.query(qwp.query, qwp.params, function (err, result) {
|
|
204
|
+
if (err)
|
|
205
|
+
return reject(err);
|
|
206
|
+
else
|
|
207
|
+
return resolve(result);
|
|
208
|
+
});
|
|
209
|
+
})];
|
|
210
|
+
case 1:
|
|
211
|
+
_b.sent();
|
|
212
|
+
return [2 /*return*/];
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
_i = 0, queriesWithParams_1 = queriesWithParams;
|
|
217
|
+
_a.label = 3;
|
|
218
|
+
case 3:
|
|
219
|
+
if (!(_i < queriesWithParams_1.length)) return [3 /*break*/, 6];
|
|
220
|
+
qwp = queriesWithParams_1[_i];
|
|
221
|
+
return [5 /*yield**/, _loop_1(qwp)];
|
|
222
|
+
case 4:
|
|
223
|
+
_a.sent();
|
|
224
|
+
_a.label = 5;
|
|
225
|
+
case 5:
|
|
226
|
+
_i++;
|
|
227
|
+
return [3 /*break*/, 3];
|
|
228
|
+
case 6:
|
|
229
|
+
// Commit della transazione
|
|
230
|
+
return [4 /*yield*/, commitTransaction(transaction)];
|
|
231
|
+
case 7:
|
|
232
|
+
// Commit della transazione
|
|
233
|
+
_a.sent();
|
|
234
|
+
// Stacca il database
|
|
235
|
+
db.detach();
|
|
236
|
+
// Ritorna il messaggio di successo
|
|
237
|
+
return [2 /*return*/, 'OK'];
|
|
238
|
+
case 8:
|
|
239
|
+
error_1 = _a.sent();
|
|
240
|
+
if (!transaction) return [3 /*break*/, 10];
|
|
241
|
+
return [4 /*yield*/, rollbackTransaction(transaction)];
|
|
242
|
+
case 9:
|
|
243
|
+
_a.sent();
|
|
244
|
+
_a.label = 10;
|
|
245
|
+
case 10:
|
|
246
|
+
if (db) {
|
|
247
|
+
db.detach();
|
|
248
|
+
}
|
|
249
|
+
throw error_1;
|
|
250
|
+
case 11: return [2 /*return*/];
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}); };
|
|
254
|
+
var executeQueries = function (transaction, queries, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
255
|
+
var error_2, error_3;
|
|
256
|
+
return __generator(this, function (_a) {
|
|
257
|
+
switch (_a.label) {
|
|
258
|
+
case 0:
|
|
259
|
+
_a.trys.push([0, 6, , 7]);
|
|
260
|
+
_a.label = 1;
|
|
261
|
+
case 1:
|
|
262
|
+
_a.trys.push([1, 3, , 5]);
|
|
263
|
+
return [4 /*yield*/, queries.reduce(function (promiseChain, currentQuery, index) {
|
|
264
|
+
return promiseChain.then(function () { return new Promise(function (resolve, reject) {
|
|
265
|
+
transaction.query(currentQuery, params[index], function (err, result) {
|
|
266
|
+
if (err)
|
|
267
|
+
return reject(err);
|
|
268
|
+
else
|
|
269
|
+
return resolve(result);
|
|
270
|
+
});
|
|
271
|
+
}); });
|
|
272
|
+
}, Promise.resolve())];
|
|
273
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
274
|
+
case 3:
|
|
275
|
+
error_2 = _a.sent();
|
|
276
|
+
return [4 /*yield*/, new Promise(function (resolve_1, reject_1) {
|
|
277
|
+
transaction.rollback(function (rollbackErr) {
|
|
278
|
+
if (rollbackErr) {
|
|
279
|
+
return reject_1(rollbackErr);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
return reject_1(error_2);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
})];
|
|
286
|
+
case 4: return [2 /*return*/, _a.sent()];
|
|
287
|
+
case 5: return [3 /*break*/, 7];
|
|
288
|
+
case 6:
|
|
289
|
+
error_3 = _a.sent();
|
|
290
|
+
throw error_3;
|
|
291
|
+
case 7: return [2 /*return*/];
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}); };
|
|
178
295
|
exports.Orm = {
|
|
179
296
|
quote: quote,
|
|
180
297
|
testConnection: testConnection,
|
|
@@ -184,6 +301,7 @@ exports.Orm = {
|
|
|
184
301
|
connect: connect,
|
|
185
302
|
startTransaction: startTransaction,
|
|
186
303
|
executeQueries: executeQueries,
|
|
304
|
+
executeMultiple: executeMultiple,
|
|
187
305
|
commitTransaction: commitTransaction,
|
|
188
306
|
rollbackTransaction: rollbackTransaction
|
|
189
307
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emilsoftware-utilities",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"description": "Utilities for EmilSoftware",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://github.com/mttdev382/emilsoftware-utilities#readme",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"es-node-firebird": "^1.2.
|
|
22
|
+
"es-node-firebird": "^1.2.5",
|
|
23
23
|
"winston": "^3.11.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "es-node-firebird";
|
package/src/orm.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
2
|
import * as Firebird from "es-node-firebird";
|
|
3
|
-
import {Logger} from "./logger";
|
|
4
|
-
import {Database, Options, Transaction} from "es-node-firebird";
|
|
5
|
-
import {Utilities} from "./utilities";
|
|
3
|
+
import { Logger } from "./logger";
|
|
4
|
+
import { Database, Options, Transaction } from "es-node-firebird";
|
|
5
|
+
import { Utilities } from "./utilities";
|
|
6
6
|
|
|
7
7
|
const logger: Logger = new Logger(__filename);
|
|
8
8
|
|
|
@@ -110,33 +110,81 @@ const commitTransaction = (transaction: Transaction): Promise<any> => {
|
|
|
110
110
|
const rollbackTransaction = (transaction: Transaction): Promise<any> => {
|
|
111
111
|
return new Promise((resolve, reject) => {
|
|
112
112
|
transaction.rollback(err => {
|
|
113
|
-
if (err) return reject(err);
|
|
113
|
+
if (err) return reject(err);
|
|
114
|
+
else return resolve('Transaction rolled back successfully.');
|
|
114
115
|
});
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
118
|
|
|
119
|
+
interface QueryWithParams {
|
|
120
|
+
query: string,
|
|
121
|
+
params: any[]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const executeMultiple = async (options: Options, queriesWithParams: QueryWithParams[]): Promise<any> => {
|
|
125
|
+
let db: Database;
|
|
126
|
+
let transaction: Transaction;
|
|
118
127
|
|
|
119
|
-
const executeQueries = (transaction: Transaction, queries: string[], params: any[]): Promise<any> => {
|
|
120
128
|
try {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
// Connetti al database
|
|
130
|
+
db = await connect(options);
|
|
131
|
+
|
|
132
|
+
// Inizia la transazione
|
|
133
|
+
transaction = await startTransaction(db);
|
|
134
|
+
|
|
135
|
+
// Esegui le query in serie
|
|
136
|
+
for (const qwp of queriesWithParams) {
|
|
137
|
+
await new Promise((resolve, reject) => {
|
|
138
|
+
transaction.query(qwp.query, qwp.params, (err: any, result: any): void => {
|
|
124
139
|
if (err) return reject(err);
|
|
125
140
|
else return resolve(result);
|
|
126
141
|
});
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Commit della transazione
|
|
146
|
+
await commitTransaction(transaction);
|
|
147
|
+
|
|
148
|
+
// Stacca il database
|
|
149
|
+
db.detach();
|
|
150
|
+
|
|
151
|
+
// Ritorna il messaggio di successo
|
|
152
|
+
return 'OK';
|
|
153
|
+
|
|
154
|
+
} catch (error) {
|
|
155
|
+
// In caso di errore, fai rollback della transazione
|
|
156
|
+
if (transaction) {
|
|
157
|
+
await rollbackTransaction(transaction);
|
|
158
|
+
}
|
|
159
|
+
if (db) {
|
|
160
|
+
db.detach();
|
|
161
|
+
}
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const executeQueries = async (transaction: Transaction, queries: string[], params: any[]): Promise<any> => {
|
|
167
|
+
try {
|
|
168
|
+
try {
|
|
169
|
+
return await queries.reduce((promiseChain: Promise<any>, currentQuery: string, index: number) => {
|
|
170
|
+
return promiseChain.then(() => new Promise((resolve, reject) => {
|
|
171
|
+
transaction.query(currentQuery, params[index], (err: any, result: any): void => {
|
|
172
|
+
if (err) return reject(err);
|
|
173
|
+
else return resolve(result);
|
|
137
174
|
});
|
|
175
|
+
}));
|
|
176
|
+
}, Promise.resolve());
|
|
177
|
+
} catch (error) {
|
|
178
|
+
return await new Promise((resolve_1, reject_1) => {
|
|
179
|
+
transaction.rollback((rollbackErr: any) => {
|
|
180
|
+
if (rollbackErr) {
|
|
181
|
+
return reject_1(rollbackErr);
|
|
182
|
+
} else {
|
|
183
|
+
return reject_1(error);
|
|
184
|
+
}
|
|
138
185
|
});
|
|
139
186
|
});
|
|
187
|
+
}
|
|
140
188
|
} catch (error) {
|
|
141
189
|
throw error;
|
|
142
190
|
}
|
|
@@ -150,9 +198,10 @@ interface Orm {
|
|
|
150
198
|
trimParam: (param: any) => string,
|
|
151
199
|
connect: (options: Options) => Promise<any>,
|
|
152
200
|
startTransaction: (db: Database) => Promise<any>,
|
|
201
|
+
executeMultiple: (options: Options, qwps: QueryWithParams[]) => any,
|
|
153
202
|
executeQueries: (transaction: Transaction, queries: string[], params: any[]) => any,
|
|
154
203
|
commitTransaction: (transaction: Transaction) => Promise<any>,
|
|
155
|
-
rollbackTransaction: (transaction: Transaction) => Promise<any
|
|
204
|
+
rollbackTransaction: (transaction: Transaction) => Promise<any>,
|
|
156
205
|
}
|
|
157
206
|
|
|
158
207
|
export const Orm: Orm = {
|
|
@@ -164,6 +213,7 @@ export const Orm: Orm = {
|
|
|
164
213
|
connect,
|
|
165
214
|
startTransaction,
|
|
166
215
|
executeQueries,
|
|
216
|
+
executeMultiple,
|
|
167
217
|
commitTransaction,
|
|
168
218
|
rollbackTransaction
|
|
169
219
|
}
|