anote-server-libs 0.6.1 → 0.6.2
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/.vscode/settings.json +3 -0
- package/models/ApiCall.js +3 -0
- package/models/Migration.js +3 -0
- package/models/repository/BaseModelRepository.js +7 -0
- package/models/repository/MemoryCache.js +3 -1
- package/models/repository/MemoryCache.ts +1 -1
- package/models/repository/ModelDao.js +13 -7
- package/models/repository/ModelDao.ts +7 -7
- package/package.json +6 -6
- package/services/WithBody.js +2 -2
- package/services/WithTransaction.js +2 -2
- package/services/utils.js +11 -11
- package/tsconfig.json +1 -1
package/models/ApiCall.js
CHANGED
|
@@ -4,6 +4,9 @@ exports.ApiCallRepository = void 0;
|
|
|
4
4
|
const mssql_1 = require("mssql");
|
|
5
5
|
const ModelDao_1 = require("./repository/ModelDao");
|
|
6
6
|
class ApiCallRepository extends ModelDao_1.ModelDao {
|
|
7
|
+
pool;
|
|
8
|
+
poolMssql;
|
|
9
|
+
logger;
|
|
7
10
|
constructor(pool, poolMssql, logger) {
|
|
8
11
|
super(pool, poolMssql, logger, 'api_call', 5, pool ? 'id=$1,"updatedOn"=$2,"responseCode"=$3,"responseJson"=$4,"expiresAt"=$5' :
|
|
9
12
|
'id=@1,"updatedOn"=@2,"responseCode"=@3,"responseJson"=@4,"expiresAt"=@5');
|
package/models/Migration.js
CHANGED
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MigrationRepository = void 0;
|
|
4
4
|
const ModelDao_1 = require("./repository/ModelDao");
|
|
5
5
|
class MigrationRepository extends ModelDao_1.ModelDao {
|
|
6
|
+
pool;
|
|
7
|
+
poolMssql;
|
|
8
|
+
logger;
|
|
6
9
|
constructor(pool, poolMssql, logger) {
|
|
7
10
|
super(pool, poolMssql, logger, 'migration', 5, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5' :
|
|
8
11
|
'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5');
|
|
@@ -6,6 +6,13 @@ const fs = require("fs");
|
|
|
6
6
|
const ApiCall_1 = require("../ApiCall");
|
|
7
7
|
const Migration_1 = require("../Migration");
|
|
8
8
|
class BaseModelRepository {
|
|
9
|
+
db;
|
|
10
|
+
dbSpare;
|
|
11
|
+
dbMssql;
|
|
12
|
+
logger;
|
|
13
|
+
cache;
|
|
14
|
+
Migration;
|
|
15
|
+
ApiCall;
|
|
9
16
|
constructor(db, dbSpare, dbMssql, logger, cache) {
|
|
10
17
|
this.db = db;
|
|
11
18
|
this.dbSpare = dbSpare;
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MemoryCache = void 0;
|
|
4
4
|
class MemoryCache {
|
|
5
|
+
localKey;
|
|
6
|
+
cache;
|
|
7
|
+
localCache = {};
|
|
5
8
|
constructor(localKey, cache) {
|
|
6
9
|
this.localKey = localKey;
|
|
7
10
|
this.cache = cache;
|
|
8
|
-
this.localCache = {};
|
|
9
11
|
}
|
|
10
12
|
list() {
|
|
11
13
|
if (this.cache)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ModelDao = exports.Dao =
|
|
3
|
+
exports.ModelDao = exports.Dao = void 0;
|
|
4
|
+
exports.promiseAllStepN = promiseAllStepN;
|
|
4
5
|
function promiseAllStepN(n, list) {
|
|
5
6
|
if (!list || !list.length)
|
|
6
7
|
return Promise.resolve([]);
|
|
@@ -41,9 +42,14 @@ function promiseAllStepN(n, list) {
|
|
|
41
42
|
}
|
|
42
43
|
});
|
|
43
44
|
}
|
|
44
|
-
exports.promiseAllStepN = promiseAllStepN;
|
|
45
45
|
Promise.allConcurrent = (n) => (list) => promiseAllStepN(n, list);
|
|
46
46
|
class Dao {
|
|
47
|
+
pool;
|
|
48
|
+
poolMssql;
|
|
49
|
+
logger;
|
|
50
|
+
table;
|
|
51
|
+
nFields;
|
|
52
|
+
updateDefinition;
|
|
47
53
|
constructor(pool, poolMssql, logger, table, nFields, updateDefinition) {
|
|
48
54
|
this.pool = pool;
|
|
49
55
|
this.poolMssql = poolMssql;
|
|
@@ -100,7 +106,7 @@ class Dao {
|
|
|
100
106
|
const request = (client || this.poolMssql).request();
|
|
101
107
|
this.serialize(instance, request);
|
|
102
108
|
return request.query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=@\d+/g, '').replace(/=[^)]+\)/g, '') + ')'
|
|
103
|
-
+ ' VALUES(' + new Array(this.nFields).fill(undefined).map((_, i) => '@' + (i + 1)).join(',') + '); SELECT SCOPE_IDENTITY() AS id').then(q => {
|
|
109
|
+
+ ' VALUES(' + new Array(this.nFields).fill(undefined).map((_, i) => '@' + (i + 1)).join(',') + '); SELECT SCOPE_IDENTITY() AS id').then((q) => {
|
|
104
110
|
return q.recordsets[0][0].id || instance.id;
|
|
105
111
|
});
|
|
106
112
|
}
|
|
@@ -129,7 +135,7 @@ class ModelDao extends Dao {
|
|
|
129
135
|
else {
|
|
130
136
|
const request = (client || this.poolMssql).request();
|
|
131
137
|
request.input('1', id);
|
|
132
|
-
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then(q => this.buildObject(q.recordsets[0][0]));
|
|
138
|
+
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then((q) => this.buildObject(q.recordsets[0][0]));
|
|
133
139
|
}
|
|
134
140
|
}
|
|
135
141
|
count(where, inputs = [], client) {
|
|
@@ -140,7 +146,7 @@ class ModelDao extends Dao {
|
|
|
140
146
|
const request = (client || this.poolMssql).request();
|
|
141
147
|
if (where)
|
|
142
148
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
143
|
-
return request.query('SELECT count(*) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '')).then(q => q.recordsets[0][0].cnt);
|
|
149
|
+
return request.query('SELECT count(*) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '')).then((q) => q.recordsets[0][0].cnt);
|
|
144
150
|
}
|
|
145
151
|
}
|
|
146
152
|
getList(ids, client, lock = true) {
|
|
@@ -152,7 +158,7 @@ class ModelDao extends Dao {
|
|
|
152
158
|
const request = (client || this.poolMssql).request();
|
|
153
159
|
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
|
|
154
160
|
+ (ids.length > 0 ? (typeof ids[0] === 'string' ? '\'' + ids.join('\',\'') + '\'' : ids.join(',')) : '') + ')')
|
|
155
|
-
.then(q => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
161
|
+
.then((q) => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
156
162
|
}
|
|
157
163
|
}
|
|
158
164
|
getAllBy(order, offset, limit, where, inputs = [], client, lock = true) {
|
|
@@ -167,7 +173,7 @@ class ModelDao extends Dao {
|
|
|
167
173
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
168
174
|
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
169
175
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : ''))
|
|
170
|
-
.then(q => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
176
|
+
.then((q) => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
171
177
|
}
|
|
172
178
|
}
|
|
173
179
|
getViewCountBy(order, offset, limit, where, inputs = [], client, lock = true) {
|
|
@@ -73,7 +73,7 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
groupResultSet(q: any[], key: string): any[][] {
|
|
76
|
-
const storage = {};
|
|
76
|
+
const storage: any = {};
|
|
77
77
|
for(let i = 0; i < q.length; i++) {
|
|
78
78
|
storage[q[i][key]] = storage[q[i][key]] || [];
|
|
79
79
|
storage[q[i][key]].push(q[i]);
|
|
@@ -82,7 +82,7 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
groupResultSetBy(q: any[], key: (qs: any) => string): any[][] {
|
|
85
|
-
const storage = {};
|
|
85
|
+
const storage: any = {};
|
|
86
86
|
for(let i = 0; i < q.length; i++) {
|
|
87
87
|
storage[key(q[i])] = storage[key(q[i])] || [];
|
|
88
88
|
storage[key(q[i])].push(q[i]);
|
|
@@ -120,7 +120,7 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
|
|
|
120
120
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
121
121
|
this.serialize(instance, request);
|
|
122
122
|
return request.query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=@\d+/g, '').replace(/=[^)]+\)/g, '') + ')'
|
|
123
|
-
+ ' VALUES(' + new Array(this.nFields).fill(undefined).map((_, i: number) => '@' + (i + 1)).join(',') + '); SELECT SCOPE_IDENTITY() AS id').then(q => {
|
|
123
|
+
+ ' VALUES(' + new Array(this.nFields).fill(undefined).map((_, i: number) => '@' + (i + 1)).join(',') + '); SELECT SCOPE_IDENTITY() AS id').then((q: any) => {
|
|
124
124
|
return q.recordsets[0][0].id || instance.id;
|
|
125
125
|
});
|
|
126
126
|
}
|
|
@@ -153,7 +153,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
153
153
|
} else {
|
|
154
154
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
155
155
|
request.input('1', id);
|
|
156
|
-
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then(q => this.buildObject(q.recordsets[0][0]));
|
|
156
|
+
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then((q: any) => this.buildObject(q.recordsets[0][0]));
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
|
|
@@ -163,7 +163,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
163
163
|
} else {
|
|
164
164
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
165
165
|
if(where) where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
166
|
-
return request.query('SELECT count(*) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '')).then(q => q.recordsets[0][0].cnt);
|
|
166
|
+
return request.query('SELECT count(*) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '')).then((q: any) => q.recordsets[0][0].cnt);
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
|
|
@@ -175,7 +175,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
175
175
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
176
176
|
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
|
|
177
177
|
+ (ids.length > 0 ? (typeof ids[0] === 'string' ? '\'' + ids.join('\',\'') + '\'' : ids.join(',')) : '') + ')')
|
|
178
|
-
.then(q => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
178
|
+
.then((q: any) => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
181
|
|
|
@@ -189,7 +189,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
189
189
|
if(where) where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
190
190
|
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
191
191
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : ''))
|
|
192
|
-
.then(q => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
192
|
+
.then((q: any) => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
195
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anote-server-libs",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Helpers for express-TS servers",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"@types/memcached": "2.2.10",
|
|
20
20
|
"@types/mssql": "9.1.5",
|
|
21
21
|
"@types/pg": "8.11.6",
|
|
22
|
-
"ajv": "8.
|
|
22
|
+
"ajv": "8.16.0",
|
|
23
23
|
"memcached": "2.2.2",
|
|
24
|
-
"mssql": "
|
|
25
|
-
"pg": "8.
|
|
24
|
+
"mssql": "11.0.0",
|
|
25
|
+
"pg": "8.12.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"express": "4.19.2",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/express": "4.17.21",
|
|
33
|
-
"@types/node": "
|
|
34
|
-
"typescript": "5.
|
|
33
|
+
"@types/node": "20.12.2",
|
|
34
|
+
"typescript": "5.5.2"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/services/WithBody.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.ajv = void 0;
|
|
4
|
+
exports.WithBody = WithBody;
|
|
4
5
|
const ajv_1 = require("ajv");
|
|
5
6
|
exports.ajv = new ajv_1.default();
|
|
6
7
|
exports.ajv.addKeyword({
|
|
@@ -60,4 +61,3 @@ function WithBody(schema) {
|
|
|
60
61
|
return undefined;
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
|
-
exports.WithBody = WithBody;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.withTransactionConfig = void 0;
|
|
4
|
+
exports.withTransaction = withTransaction;
|
|
4
5
|
const utils_1 = require("./utils");
|
|
5
6
|
exports.withTransactionConfig = {
|
|
6
7
|
timeoutMillis: 15000
|
|
@@ -136,4 +137,3 @@ function withTransaction(repo, logger, previousMethod, lock) {
|
|
|
136
137
|
});
|
|
137
138
|
};
|
|
138
139
|
}
|
|
139
|
-
exports.withTransaction = withTransaction;
|
package/services/utils.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.utils = void 0;
|
|
4
|
+
exports.atob = atob;
|
|
5
|
+
exports.btoa = btoa;
|
|
6
|
+
exports.clientErrorHandle = clientErrorHandle;
|
|
7
|
+
exports.gcd = gcd;
|
|
8
|
+
exports.lcm = lcm;
|
|
9
|
+
exports.jsonStringify = jsonStringify;
|
|
10
|
+
exports.idempotent = idempotent;
|
|
11
|
+
exports.sendSelfPostableMessage = sendSelfPostableMessage;
|
|
12
|
+
exports.fpEuros = fpEuros;
|
|
13
|
+
exports.digitize = digitize;
|
|
4
14
|
function atob(str) {
|
|
5
15
|
return Buffer.from(str, 'base64').toString('binary');
|
|
6
16
|
}
|
|
7
|
-
exports.atob = atob;
|
|
8
17
|
function btoa(str) {
|
|
9
18
|
return Buffer.from(str).toString('base64');
|
|
10
19
|
}
|
|
11
|
-
exports.btoa = btoa;
|
|
12
20
|
function clientErrorHandle(err) {
|
|
13
21
|
this.error('Error on DB client: %j', err);
|
|
14
22
|
}
|
|
15
|
-
exports.clientErrorHandle = clientErrorHandle;
|
|
16
23
|
exports.utils = {
|
|
17
24
|
clientErrorHandler: undefined
|
|
18
25
|
};
|
|
@@ -27,7 +34,6 @@ function gcd(values) {
|
|
|
27
34
|
result = gcdTwo(values[i], result);
|
|
28
35
|
return result;
|
|
29
36
|
}
|
|
30
|
-
exports.gcd = gcd;
|
|
31
37
|
function lcm(values) {
|
|
32
38
|
let l = 1, divisor = 2;
|
|
33
39
|
while (true) {
|
|
@@ -59,7 +65,6 @@ function lcm(values) {
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
|
-
exports.lcm = lcm;
|
|
63
68
|
function jsonStringify(obj) {
|
|
64
69
|
const cache = {};
|
|
65
70
|
return JSON.stringify(obj, function (_, value) {
|
|
@@ -77,7 +82,6 @@ function jsonStringify(obj) {
|
|
|
77
82
|
return value;
|
|
78
83
|
});
|
|
79
84
|
}
|
|
80
|
-
exports.jsonStringify = jsonStringify;
|
|
81
85
|
function idempotent(repo, debug, logger) {
|
|
82
86
|
return function (req, res, next) {
|
|
83
87
|
let idempotenceKey = req.header('x-idempotent-key');
|
|
@@ -138,7 +142,6 @@ function idempotent(repo, debug, logger) {
|
|
|
138
142
|
next();
|
|
139
143
|
};
|
|
140
144
|
}
|
|
141
|
-
exports.idempotent = idempotent;
|
|
142
145
|
function sendSelfPostableMessage(res, code, messageType, err) {
|
|
143
146
|
res.type('text/html').status(code).write(`
|
|
144
147
|
<!DOCTYPE HTML>
|
|
@@ -159,11 +162,9 @@ function sendSelfPostableMessage(res, code, messageType, err) {
|
|
|
159
162
|
`);
|
|
160
163
|
res.end();
|
|
161
164
|
}
|
|
162
|
-
exports.sendSelfPostableMessage = sendSelfPostableMessage;
|
|
163
165
|
function fpEuros(n) {
|
|
164
166
|
return Math.round(n * 100) / 100;
|
|
165
167
|
}
|
|
166
|
-
exports.fpEuros = fpEuros;
|
|
167
168
|
function digitize(value, opts) {
|
|
168
169
|
if (value === undefined || value === null)
|
|
169
170
|
return 'undefined';
|
|
@@ -194,4 +195,3 @@ function digitize(value, opts) {
|
|
|
194
195
|
}
|
|
195
196
|
return (opts && opts.currency) ? (parts[0] + '.00') : parts[0];
|
|
196
197
|
}
|
|
197
|
-
exports.digitize = digitize;
|