anote-server-libs 0.7.4 → 0.8.1
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.
|
@@ -44,13 +44,14 @@ function promiseAllStepN(n, list) {
|
|
|
44
44
|
}
|
|
45
45
|
Promise.allConcurrent = (n) => (list) => promiseAllStepN(n, list);
|
|
46
46
|
class Dao {
|
|
47
|
-
constructor(pool, poolMssql, logger, table, nFields, updateDefinition) {
|
|
47
|
+
constructor(pool, poolMssql, logger, table, nFields, updateDefinition, selectDefinition = 'SELECT *') {
|
|
48
48
|
this.pool = pool;
|
|
49
49
|
this.poolMssql = poolMssql;
|
|
50
50
|
this.logger = logger;
|
|
51
51
|
this.table = table;
|
|
52
52
|
this.nFields = nFields;
|
|
53
53
|
this.updateDefinition = updateDefinition;
|
|
54
|
+
this.selectDefinition = selectDefinition;
|
|
54
55
|
}
|
|
55
56
|
groupResultSet(q, key) {
|
|
56
57
|
const storage = {};
|
|
@@ -124,12 +125,12 @@ exports.Dao = Dao;
|
|
|
124
125
|
class ModelDao extends Dao {
|
|
125
126
|
get(id, client, lock = true) {
|
|
126
127
|
if (this.pool) {
|
|
127
|
-
return (client || this.pool).query('
|
|
128
|
+
return (client || this.pool).query(this.selectDefinition + ' FROM ' + this.table + ' WHERE id=$1' + ((client && lock) ? ' FOR UPDATE' : ''), [id]).then(q => this.buildObject(q.rows[0]));
|
|
128
129
|
}
|
|
129
130
|
else {
|
|
130
131
|
const request = (client || this.poolMssql).request();
|
|
131
132
|
request.input('1', id);
|
|
132
|
-
return request.query('
|
|
133
|
+
return request.query(this.selectDefinition + ' FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then((q) => this.buildObject(q.recordsets[0][0]));
|
|
133
134
|
}
|
|
134
135
|
}
|
|
135
136
|
count(where, inputs = [], client) {
|
|
@@ -145,19 +146,19 @@ class ModelDao extends Dao {
|
|
|
145
146
|
}
|
|
146
147
|
getList(ids, client, lock = true) {
|
|
147
148
|
if (this.pool) {
|
|
148
|
-
return (client || this.pool).query('
|
|
149
|
+
return (client || this.pool).query(this.selectDefinition + ' FROM ' + this.table + ' WHERE id=ANY($1)' + ((client && lock) ? ' FOR UPDATE' : ''), [ids])
|
|
149
150
|
.then(q => q.rows.map(r => this.buildObject(r)));
|
|
150
151
|
}
|
|
151
152
|
else {
|
|
152
153
|
const request = (client || this.poolMssql).request();
|
|
153
|
-
return request.query('
|
|
154
|
+
return request.query(this.selectDefinition + ' FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
|
|
154
155
|
+ (ids.length > 0 ? (typeof ids[0] === 'string' ? '\'' + ids.join('\',\'') + '\'' : ids.join(',')) : '') + ')')
|
|
155
156
|
.then((q) => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
159
|
getAllBy(order, offset, limit, where, inputs = [], client, lock = true) {
|
|
159
160
|
if (this.pool) {
|
|
160
|
-
return (client || this.pool).query('
|
|
161
|
+
return (client || this.pool).query(this.selectDefinition + ' FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
161
162
|
+ (offset ? (' OFFSET ' + offset) : '') + (limit !== undefined ? (' LIMIT ' + limit) : '') + ((client && lock) ? ' FOR UPDATE' : ''), inputs)
|
|
162
163
|
.then(q => q.rows.map(r => this.buildObject(r)));
|
|
163
164
|
}
|
|
@@ -165,14 +166,14 @@ class ModelDao extends Dao {
|
|
|
165
166
|
const request = (client || this.poolMssql).request();
|
|
166
167
|
if (where)
|
|
167
168
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
168
|
-
return request.query('
|
|
169
|
+
return request.query(this.selectDefinition + ' FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
169
170
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : ''))
|
|
170
171
|
.then((q) => q.recordsets[0].map((r) => this.buildObject(r)));
|
|
171
172
|
}
|
|
172
173
|
}
|
|
173
174
|
getViewCountBy(order, offset, limit, where, inputs = [], client, lock = true) {
|
|
174
175
|
if (this.pool) {
|
|
175
|
-
return (client || this.pool).query('
|
|
176
|
+
return (client || this.pool).query(this.selectDefinition + ', COUNT(*) OVER() AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
176
177
|
+ (offset ? (' OFFSET ' + offset) : '') + (limit !== undefined ? (' LIMIT ' + limit) : '') + ((client && lock) ? ' FOR UPDATE' : ''), inputs)
|
|
177
178
|
.then(q => ({
|
|
178
179
|
views: q.rows.map(r => this.buildObject(r)),
|
|
@@ -184,7 +185,7 @@ class ModelDao extends Dao {
|
|
|
184
185
|
if (where)
|
|
185
186
|
where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
186
187
|
return Promise.allConcurrent(1)([
|
|
187
|
-
() => request.query('
|
|
188
|
+
() => request.query(this.selectDefinition + ' FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
188
189
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : '')),
|
|
189
190
|
() => request.query('SELECT COUNT(DISTINCT id) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : ''))
|
|
190
191
|
]).then(([q1, q2]) => ({
|
|
@@ -69,7 +69,7 @@ export interface ViewCount<T> {
|
|
|
69
69
|
|
|
70
70
|
export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
|
|
71
71
|
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger, public table: string, protected nFields: number,
|
|
72
|
-
protected updateDefinition: string) {
|
|
72
|
+
protected updateDefinition: string, protected selectDefinition = 'SELECT *') {
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
groupResultSet(q: any[], key: string): any[][] {
|
|
@@ -149,11 +149,11 @@ export abstract class Dao<R, T extends Model<R>> implements ModelRepr {
|
|
|
149
149
|
export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
150
150
|
get(id: R, client?: ClientBase | Transaction, lock = true): Promise<T> {
|
|
151
151
|
if(this.pool) {
|
|
152
|
-
return (<ClientBase | Pool>(client || this.pool)).query('
|
|
152
|
+
return (<ClientBase | Pool>(client || this.pool)).query(this.selectDefinition + ' FROM ' + this.table + ' WHERE id=$1' + ((client && lock) ? ' FOR UPDATE' : ''), [id]).then(q => this.buildObject(q.rows[0]));
|
|
153
153
|
} else {
|
|
154
154
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
155
155
|
request.input('1', id);
|
|
156
|
-
return request.query('
|
|
156
|
+
return request.query(this.selectDefinition + ' 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
|
|
|
@@ -169,11 +169,11 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
169
169
|
|
|
170
170
|
getList(ids: R[], client?: ClientBase | Transaction, lock = true): Promise<T[]> {
|
|
171
171
|
if(this.pool) {
|
|
172
|
-
return (<ClientBase | Pool>(client || this.pool)).query('
|
|
172
|
+
return (<ClientBase | Pool>(client || this.pool)).query(this.selectDefinition + ' FROM ' + this.table + ' WHERE id=ANY($1)' + ((client && lock) ? ' FOR UPDATE' : ''), [ids])
|
|
173
173
|
.then(q => q.rows.map(r => this.buildObject(r)));
|
|
174
174
|
} else {
|
|
175
175
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
176
|
-
return request.query('
|
|
176
|
+
return request.query(this.selectDefinition + ' 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
178
|
.then((q: any) => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
179
179
|
}
|
|
@@ -181,13 +181,13 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
181
181
|
|
|
182
182
|
getAllBy(order?: string, offset?: number, limit?: number, where?: string, inputs: any[] = [], client?: ClientBase | Transaction, lock = true): Promise<T[]> {
|
|
183
183
|
if(this.pool) {
|
|
184
|
-
return (<ClientBase | Pool>(client || this.pool)).query('
|
|
184
|
+
return (<ClientBase | Pool>(client || this.pool)).query(this.selectDefinition + ' FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
185
185
|
+ (offset ? (' OFFSET ' + offset) : '') + (limit !== undefined ? (' LIMIT ' + limit) : '') + ((client && lock) ? ' FOR UPDATE' : ''), inputs)
|
|
186
186
|
.then(q => q.rows.map(r => this.buildObject(r)));
|
|
187
187
|
} else {
|
|
188
188
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
189
189
|
if(where) where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
190
|
-
return request.query('
|
|
190
|
+
return request.query(this.selectDefinition + ' 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
192
|
.then((q: any) => q.recordsets[0].map((r: any) => this.buildObject(r)));
|
|
193
193
|
}
|
|
@@ -195,7 +195,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
195
195
|
|
|
196
196
|
getViewCountBy(order?: string, offset?: number, limit?: number, where?: string, inputs: any[] = [], client?: ClientBase | Transaction, lock = true): Promise<ViewCount<T>> {
|
|
197
197
|
if(this.pool) {
|
|
198
|
-
return (<ClientBase | Pool>(client || this.pool)).query('
|
|
198
|
+
return (<ClientBase | Pool>(client || this.pool)).query(this.selectDefinition + ', COUNT(*) OVER() AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
|
|
199
199
|
+ (offset ? (' OFFSET ' + offset) : '') + (limit !== undefined ? (' LIMIT ' + limit) : '') + ((client && lock) ? ' FOR UPDATE' : ''), inputs)
|
|
200
200
|
.then(q => ({
|
|
201
201
|
views: q.rows.map(r => this.buildObject(r)),
|
|
@@ -205,7 +205,7 @@ export abstract class ModelDao<R, T extends Model<R>> extends Dao<R, T> {
|
|
|
205
205
|
const request = (<Transaction | ConnectionPool>(client || this.poolMssql)).request();
|
|
206
206
|
if(where) where.match(/(@\d+)/g).forEach((match, i) => request.input(match.substr(1), inputs[i]));
|
|
207
207
|
return Promise.allConcurrent(1)([
|
|
208
|
-
() => request.query('
|
|
208
|
+
() => request.query(this.selectDefinition + ' FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
|
|
209
209
|
+ (order ? (' ORDER BY ' + order) : '') + (offset !== undefined ? (' OFFSET ' + offset + ' ROWS') : '') + (limit !== undefined ? (' FETCH NEXT ' + limit + ' ROWS ONLY') : '')),
|
|
210
210
|
() => request.query('SELECT COUNT(DISTINCT id) AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : ''))
|
|
211
211
|
]).then(([q1, q2]: [any, any]) => ({
|
package/package.json
CHANGED
|
@@ -61,13 +61,13 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
|
|
|
61
61
|
if (res.statusCode > 303 && res.statusCode !== 412) {
|
|
62
62
|
if (logger) {
|
|
63
63
|
if (res.statusCode > 499) {
|
|
64
|
-
logger.error('Uncaught 500: %j', obj
|
|
64
|
+
logger.error('Uncaught 500: %j', obj?.error?.additionalInfo);
|
|
65
65
|
}
|
|
66
66
|
else {
|
|
67
|
-
logger.warn('Client error 4XX: %j', obj
|
|
67
|
+
logger.warn('Client error 4XX: %j', obj?.error);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj.error.additionalInfo2 = { message: err.message }).then(() => {
|
|
70
|
+
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err) => obj && obj.error && (obj.error.additionalInfo2 = { message: err.message })).then(() => {
|
|
71
71
|
if (repo.db)
|
|
72
72
|
dbClient.release();
|
|
73
73
|
jsonTerminator(obj);
|
|
@@ -66,12 +66,12 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
|
|
|
66
66
|
if(res.statusCode > 303 && res.statusCode !== 412) {
|
|
67
67
|
if(logger) {
|
|
68
68
|
if(res.statusCode > 499) {
|
|
69
|
-
logger.error('Uncaught 500: %j', obj
|
|
69
|
+
logger.error('Uncaught 500: %j', obj?.error?.additionalInfo);
|
|
70
70
|
} else {
|
|
71
|
-
logger.warn('Client error 4XX: %j', obj
|
|
71
|
+
logger.warn('Client error 4XX: %j', obj?.error);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj.error.additionalInfo2 = {message: err.message}).then(() => {
|
|
74
|
+
(repo.db ? dbClient.query('ROLLBACK') : dbClient.rollback()).catch((err: any) => obj && obj.error && (obj.error.additionalInfo2 = {message: err.message})).then(() => {
|
|
75
75
|
if(repo.db) dbClient.release();
|
|
76
76
|
jsonTerminator(obj);
|
|
77
77
|
});
|
package/.vscode/settings.json
DELETED