anote-server-libs 0.3.2 → 0.3.3
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.
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseModelRepository = void 0;
|
|
4
|
+
const crypto = require("crypto");
|
|
4
5
|
const fs = require("fs");
|
|
5
|
-
const node_forge_1 = require("node-forge");
|
|
6
6
|
const ApiCall_1 = require("../ApiCall");
|
|
7
7
|
const Migration_1 = require("../Migration");
|
|
8
8
|
class BaseModelRepository {
|
|
@@ -23,7 +23,7 @@ class BaseModelRepository {
|
|
|
23
23
|
this.Migration = new Migration_1.MigrationRepository(db, dbMssql, logger);
|
|
24
24
|
this.ApiCall = new ApiCall_1.ApiCallRepository(db, dbMssql, logger);
|
|
25
25
|
}
|
|
26
|
-
migrate(migrationsPath, callback) {
|
|
26
|
+
migrate(migrationsPath, callback, onlyAboveOrEquals = 0) {
|
|
27
27
|
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
28
28
|
'id integer PRIMARY KEY,' +
|
|
29
29
|
'hash text NOT NULL,' +
|
|
@@ -51,19 +51,19 @@ class BaseModelRepository {
|
|
|
51
51
|
return {
|
|
52
52
|
id: file,
|
|
53
53
|
content: content,
|
|
54
|
-
hash:
|
|
54
|
+
hash: crypto.createHash('sha256').update(content).digest('hex')
|
|
55
55
|
};
|
|
56
56
|
});
|
|
57
57
|
if (migrationsAvailable.length === 0
|
|
58
58
|
|| migrationsAvailable.length
|
|
59
59
|
!== migrationsAvailable[migrationsAvailable.length - 1].id)
|
|
60
60
|
process.exit(5);
|
|
61
|
-
let highestCommon =
|
|
61
|
+
let highestCommon = onlyAboveOrEquals;
|
|
62
62
|
while (highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
63
63
|
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
64
64
|
highestCommon++;
|
|
65
65
|
this.applyDownUntil(migrations, migrations.length, highestCommon).then(() => {
|
|
66
|
-
this.applyUpUntil(migrationsAvailable, highestCommon, migrationsAvailable.length).then(callback, process.exit);
|
|
66
|
+
this.applyUpUntil(migrationsAvailable, Math.min(highestCommon, migrations.length), migrationsAvailable.length).then(callback, process.exit);
|
|
67
67
|
}, process.exit);
|
|
68
68
|
});
|
|
69
69
|
}, () => process.exit(3));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ModelDao = exports.promiseAllStepN = void 0;
|
|
3
|
+
exports.ModelDao = exports.Dao = exports.promiseAllStepN = void 0;
|
|
4
4
|
function promiseAllStepN(n, list) {
|
|
5
5
|
if (!list || !list.length)
|
|
6
6
|
return Promise.resolve([]);
|
|
@@ -74,18 +74,11 @@ class Dao {
|
|
|
74
74
|
return request.query('UPDATE ' + this.table + ' SET ' + this.updateDefinition + ' WHERE id=@' + (this.nFields + 1)).then(() => instance.id);
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
replace(instance, client) {
|
|
78
|
-
if (instance.archivedOn)
|
|
79
|
-
return Promise.reject('Record archived!');
|
|
80
|
-
return this.create({ ...instance, replaced_id: instance.id }, client).then(id => {
|
|
81
|
-
return this.archive(instance.id, id, client).then(() => id);
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
77
|
create(instance, client) {
|
|
85
78
|
instance.createdOn = instance.updatedOn = new Date();
|
|
86
79
|
if (this.pool) {
|
|
87
80
|
const props = this.serialize(instance);
|
|
88
|
-
return (client || this.pool).query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=\$\d+/g, '') + ')'
|
|
81
|
+
return (client || this.pool).query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=\$\d+/g, '').replace(/=[^)]+\)/g, '') + ')'
|
|
89
82
|
+ ' VALUES(' + new Array(this.nFields).fill(undefined).map((_, i) => '$' + (i + 1)).join(',') + ') RETURNING id', props).then(q => {
|
|
90
83
|
const idNum = parseInt(q.rows[0].id, 10);
|
|
91
84
|
if (String(idNum) !== q.rows[0].id)
|
|
@@ -108,7 +101,7 @@ class Dao {
|
|
|
108
101
|
const now = new Date();
|
|
109
102
|
instances.forEach(instance => instance.updatedOn = now);
|
|
110
103
|
const props = [].concat.apply([], instances.map(instance => this.serialize(instance)));
|
|
111
|
-
return (client || this.pool).query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=\$\d+/g, '') + ')'
|
|
104
|
+
return (client || this.pool).query('INSERT INTO ' + this.table + '(' + this.updateDefinition.replace(/=\$\d+/g, '').replace(/=[^)]+\)/g, '') + ')'
|
|
112
105
|
+ ' VALUES' + instances.map((_, j) => ('(' + new Array(this.nFields).fill(undefined).map((__, i) => '$' + (j * this.nFields + i + 1)).join(', ') + ')')).join(',') + ' RETURNING id', props).then(q => q.rows.map(r => {
|
|
113
106
|
const idNum = parseInt(r.id, 10);
|
|
114
107
|
if (String(idNum) !== q.rows[0].id)
|
|
@@ -117,6 +110,7 @@ class Dao {
|
|
|
117
110
|
}));
|
|
118
111
|
}
|
|
119
112
|
}
|
|
113
|
+
exports.Dao = Dao;
|
|
120
114
|
class ModelDao extends Dao {
|
|
121
115
|
get(id, client, lock = true) {
|
|
122
116
|
if (this.pool) {
|
|
@@ -125,7 +119,7 @@ class ModelDao extends Dao {
|
|
|
125
119
|
else {
|
|
126
120
|
const request = (client || this.poolMssql).request();
|
|
127
121
|
request.input('1', id);
|
|
128
|
-
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (ROWLOCK)' : '') + ' WHERE id=@1').then(q => this.buildObject(q.recordsets[0][0]));
|
|
122
|
+
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then(q => this.buildObject(q.recordsets[0][0]));
|
|
129
123
|
}
|
|
130
124
|
}
|
|
131
125
|
count(where, inputs = [], client) {
|
|
@@ -146,9 +140,9 @@ class ModelDao extends Dao {
|
|
|
146
140
|
}
|
|
147
141
|
else {
|
|
148
142
|
const request = (client || this.poolMssql).request();
|
|
149
|
-
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (ROWLOCK)' : '') + ' WHERE id IN ('
|
|
143
|
+
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
|
|
150
144
|
+ (ids.length > 0 ? (typeof ids[0] === 'string' ? '\'' + ids.join('\',\'') + '\'' : ids.join(',')) : '') + ')')
|
|
151
|
-
.then(q => q.recordsets[0].map(r => this.buildObject(r)));
|
|
145
|
+
.then(q => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
152
146
|
}
|
|
153
147
|
}
|
|
154
148
|
getAllBy(order, offset, limit, where, inputs = [], client, lock = true) {
|
|
@@ -161,9 +155,9 @@ class ModelDao extends Dao {
|
|
|
161
155
|
const request = (client || this.poolMssql).request();
|
|
162
156
|
if (where)
|
|
163
157
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
164
|
-
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
158
|
+
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
165
159
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : ''))
|
|
166
|
-
.then(q => q.recordsets[0].map(r => this.buildObject(r)));
|
|
160
|
+
.then(q => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
167
161
|
}
|
|
168
162
|
}
|
|
169
163
|
getViewCountBy(order, offset, limit, where, inputs = [], client, lock = true) {
|
|
@@ -180,7 +174,7 @@ class ModelDao extends Dao {
|
|
|
180
174
|
if (where)
|
|
181
175
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
182
176
|
return Promise.allConcurrent(1)([
|
|
183
|
-
() => request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
177
|
+
() => request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
184
178
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : '')),
|
|
185
179
|
() => request.query('SELECT COUNT(DISTINCT id) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : ''))
|
|
186
180
|
]).then(([q1, q2]) => ({
|
|
@@ -204,7 +198,7 @@ class ModelDao extends Dao {
|
|
|
204
198
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
205
199
|
return Promise.allConcurrent(1)([
|
|
206
200
|
() => request.query('SELECT ' + rows.map(r => '"' + r + '"').join(',') + ' FROM ' + this.table
|
|
207
|
-
+ ((client && lock) ? ' WITH (ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
201
|
+
+ ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
208
202
|
+ (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : '')),
|
|
209
203
|
() => request.query('SELECT COUNT(DISTINCT id) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : ''))
|
|
210
204
|
]).then(([q1, q2]) => ({
|
|
@@ -224,6 +218,13 @@ class ModelDao extends Dao {
|
|
|
224
218
|
return request.query('UPDATE ' + this.table + ' SET "archivedOn"=now(),replaced_by_id=@1 WHERE id=@2');
|
|
225
219
|
}
|
|
226
220
|
}
|
|
221
|
+
replace(instance, client) {
|
|
222
|
+
if (instance.archivedOn)
|
|
223
|
+
return Promise.reject('Record archived!');
|
|
224
|
+
return this.create({ ...instance, replaced_id: instance.id }, client).then(id => {
|
|
225
|
+
return this.archive(instance.id, id, client).then(() => id);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
227
228
|
delete(id, client) {
|
|
228
229
|
if (this.pool) {
|
|
229
230
|
return client.query('DELETE FROM ' + this.table + ' WHERE id=$1', [id]);
|
|
@@ -166,7 +166,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
166
166
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
167
167
|
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
|
|
168
168
|
+ (ids.length > 0 ? (typeof ids[0] === 'string' ? '\'' + ids.join('\',\'') + '\'' : ids.join(',')) : '') + ')')
|
|
169
|
-
.then(q => q.recordsets[0].map(r => this.buildObject(r)));
|
|
169
|
+
.then(q => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
@@ -180,7 +180,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
180
180
|
if(where) where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
181
181
|
return request.query('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
182
182
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : ''))
|
|
183
|
-
.then(q => q.recordsets[0].map(r => this.buildObject(r)));
|
|
183
|
+
.then(q => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@ function withTransaction(repo, logger, previousMethod, lock) {
|
|
|
12
12
|
const connectTimeoutHandler = setTimeout(() => {
|
|
13
13
|
logger.error('Error timed out getting a client, exiting...');
|
|
14
14
|
process.exit(22);
|
|
15
|
-
},
|
|
15
|
+
}, 15000);
|
|
16
16
|
Promise.all([
|
|
17
17
|
repo.db ? repo.db.connect() : Promise.resolve(undefined),
|
|
18
18
|
repo.dbMssql ? Promise.resolve(repo.dbMssql.transaction()) : Promise.resolve(undefined)
|
|
@@ -117,12 +117,6 @@ function withTransaction(repo, logger, previousMethod, lock) {
|
|
|
117
117
|
else {
|
|
118
118
|
finish();
|
|
119
119
|
}
|
|
120
|
-
}).catch((err) => {
|
|
121
|
-
if (repo.db)
|
|
122
|
-
dbClient.release();
|
|
123
|
-
else
|
|
124
|
-
dbClient.rollback();
|
|
125
|
-
throw err;
|
|
126
120
|
});
|
|
127
121
|
}).catch(err => {
|
|
128
122
|
res.status(500).json({
|