database-connector 2.2.14 → 2.3.0
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/models/index.js +22 -0
- package/package.json +1 -1
package/models/index.js
CHANGED
|
@@ -15,6 +15,27 @@ const connectToDatabase = async (databaseUrl) => {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Transaction helper
|
|
20
|
+
*/
|
|
21
|
+
const withTransaction = async (fn) => {
|
|
22
|
+
const session = await mongoose.startSession();
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
session.startTransaction();
|
|
26
|
+
|
|
27
|
+
const result = await fn(session);
|
|
28
|
+
|
|
29
|
+
await session.commitTransaction();
|
|
30
|
+
return result;
|
|
31
|
+
} catch (err) {
|
|
32
|
+
await session.abortTransaction();
|
|
33
|
+
throw err;
|
|
34
|
+
} finally {
|
|
35
|
+
session.endSession();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
18
39
|
const Cart = require('./Cart');
|
|
19
40
|
const Product = require('./Product');
|
|
20
41
|
const View = require('./View');
|
|
@@ -45,6 +66,7 @@ const Advertisement = require('./Advertisement');
|
|
|
45
66
|
|
|
46
67
|
module.exports = {
|
|
47
68
|
connectToDatabase,
|
|
69
|
+
withTransaction,
|
|
48
70
|
configure: config.configure,
|
|
49
71
|
getConfig: config.getConfig,
|
|
50
72
|
PaymentType,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "database-connector",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "MongoDB models package with Mongoose schemas for e-commerce applications. Includes User, Product, Store, Order and more with built-in validation and virtual properties.",
|
|
5
5
|
"main": "models/index.js",
|
|
6
6
|
"scripts": {
|