anote-server-libs 0.8.1 → 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.
|
@@ -194,9 +194,9 @@ class ModelDao extends Dao {
|
|
|
194
194
|
}));
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
|
-
getRowsViewCountBy(
|
|
197
|
+
getRowsViewCountBy(cols, order, offset, limit, where, inputs = [], client, lock = true) {
|
|
198
198
|
if (this.pool) {
|
|
199
|
-
return (client || this.pool).query('SELECT ' +
|
|
199
|
+
return (client || this.pool).query('SELECT ' + cols.map(r => r.indexOf(' ') > -1 ? r : ('"' + r + '"')).join(',') + ', COUNT(*) OVER() AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
200
200
|
+ (offset ? (' OFFSET ' + offset) : '') + (limit !== undefined ? (' LIMIT ' + limit) : '') + ((client && lock) ? ' FOR UPDATE' : ''), inputs)
|
|
201
201
|
.then(q => ({
|
|
202
202
|
views: q.rows.map(r => this.buildObject(r)),
|
|
@@ -208,7 +208,7 @@ class ModelDao extends Dao {
|
|
|
208
208
|
if (where)
|
|
209
209
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
210
210
|
return Promise.allConcurrent(1)([
|
|
211
|
-
() => request.query('SELECT ' +
|
|
211
|
+
() => request.query('SELECT ' + cols.map(r => r.indexOf(' ') > -1 ? r : ('"' + r + '"')).join(',') + ' FROM ' + this.table
|
|
212
212
|
+ ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
213
213
|
+ (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : '')),
|
|
214
214
|
() => request.query('SELECT COUNT(DISTINCT id) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : ''))
|
|
@@ -215,9 +215,9 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
-
getRowsViewCountBy(
|
|
218
|
+
getRowsViewCountBy(cols: string[], order?: string, offset?: number, limit?: number, where?: string, inputs: any[] = [], client?: ClientBase | Transaction, lock = true): Promise<ViewCount<any>> {
|
|
219
219
|
if(this.pool) {
|
|
220
|
-
return (<ClientBase | Pool>(client || this.pool)).query('SELECT ' +
|
|
220
|
+
return (<ClientBase | Pool>(client || this.pool)).query('SELECT ' + cols.map(r => r.indexOf(' ') > -1 ? r : ('"' + r + '"')).join(',') + ', COUNT(*) OVER() AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
221
221
|
+ (offset ? (' OFFSET ' + offset) : '') + (limit !== undefined ? (' LIMIT ' + limit) : '') + ((client && lock) ? ' FOR UPDATE' : ''), inputs)
|
|
222
222
|
.then(q => ({
|
|
223
223
|
views: q.rows.map(r => this.buildObject(r)),
|
|
@@ -227,7 +227,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
227
227
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
228
228
|
if(where) where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
229
229
|
return Promise.allConcurrent(1)([
|
|
230
|
-
() => request.query('SELECT ' +
|
|
230
|
+
() => request.query('SELECT ' + cols.map(r => r.indexOf(' ') > -1 ? r : ('"' + r + '"')).join(',') + ' FROM ' + this.table
|
|
231
231
|
+ ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
232
232
|
+ (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : '')),
|
|
233
233
|
() => request.query('SELECT COUNT(DISTINCT id) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : ''))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anote-server-libs",
|
|
3
|
-
"version": "0.
|
|
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.
|
|
21
|
-
"@types/pg": "8.
|
|
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": "
|
|
25
|
-
"pg": "8.
|
|
24
|
+
"mssql": "12.1.1",
|
|
25
|
+
"pg": "8.16.3"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"express": "
|
|
29
|
-
"winston": "3.
|
|
28
|
+
"express": "5.1.0",
|
|
29
|
+
"winston": "3.18.3"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@types/express": "5.0.
|
|
33
|
-
"@types/node": "22.
|
|
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
|
|
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',
|