anote-server-libs 0.8.2 → 0.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anote-server-libs",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "Helpers for express-TS servers",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -17,19 +17,19 @@
17
17
  "license": "MIT",
18
18
  "dependencies": {
19
19
  "@types/memcached": "2.2.10",
20
- "@types/mssql": "9.1.6",
21
- "@types/pg": "8.11.10",
20
+ "@types/mssql": "9.1.8",
21
+ "@types/pg": "8.15.6",
22
22
  "ajv": "8.17.1",
23
23
  "memcached": "2.2.2",
24
- "mssql": "11.0.1",
25
- "pg": "8.13.1"
24
+ "mssql": "12.1.1",
25
+ "pg": "8.16.3"
26
26
  },
27
27
  "peerDependencies": {
28
- "express": "4.21.2",
29
- "winston": "3.17.0"
28
+ "express": "5.1.0",
29
+ "winston": "3.18.3"
30
30
  },
31
31
  "devDependencies": {
32
- "@types/express": "5.0.0",
33
- "@types/node": "22.10.7",
32
+ "@types/express": "5.0.5",
33
+ "@types/node": "22.12.0",
34
34
  "typescript": "5.7.3" }
35
35
  }
@@ -57,6 +57,9 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
57
57
  res.locals.dbClientOnCommit = [];
58
58
  return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
59
59
  const finish = () => {
60
+ res.send = () => {
61
+ throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
62
+ };
60
63
  res.json = (obj) => {
61
64
  if (res.statusCode > 303 && res.statusCode !== 412) {
62
65
  if (logger) {
@@ -134,8 +137,12 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
134
137
  else {
135
138
  finish();
136
139
  }
140
+ }).catch((err) => {
141
+ dbClient.release();
142
+ throw err;
137
143
  });
138
144
  }).catch(err => {
145
+ clearTimeout(connectTimeoutHandler);
139
146
  res.status(500).json({
140
147
  error: {
141
148
  errorKey: 'internal.db',
@@ -62,6 +62,9 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
62
62
  res.locals.dbClientOnCommit = [];
63
63
  return (repo.db ? dbClient.query('BEGIN') : dbClient.begin()).then(() => {
64
64
  const finish = () => {
65
+ res.send = () => {
66
+ throw new Error('res.send() should not be used within transactions. Use res.json() instead.');
67
+ };
65
68
  res.json = (obj: any) => {
66
69
  if(res.statusCode > 303 && res.statusCode !== 412) {
67
70
  if(logger) {
@@ -125,14 +128,19 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
125
128
  additionalInfo: {message: err.message}
126
129
  }
127
130
  });
128
- dbClient.release();
131
+ dbClient.release(); // Lock acquisition failed, release will also rollback
129
132
  });
130
133
  } else {
131
134
  finish();
132
135
  }
136
+ }).catch((err: any) => {
137
+ // Error beginning transaction
138
+ dbClient.release();
139
+ throw err;
133
140
  });
134
141
  }).catch(err => {
135
- // Error connecting to database or acquiring transaction, restarting worker after the timeout as well...
142
+ // Error connecting to database for other reason than timeout or beginning transaction
143
+ clearTimeout(connectTimeoutHandler);
136
144
  res.status(500).json({
137
145
  error: {
138
146
  errorKey: 'internal.db',