anote-server-libs 0.7.3 → 0.8.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.
@@ -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('SELECT * FROM ' + this.table + ' WHERE id=$1' + ((client && lock) ? ' FOR UPDATE' : ''), [id]).then(q => this.buildObject(q.rows[0]));
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then((q) => this.buildObject(q.recordsets[0][0]));
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('SELECT * FROM ' + this.table + ' WHERE id=ANY($1)' + ((client && lock) ? ' FOR UPDATE' : ''), [ids])
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
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('SELECT * FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
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('SELECT *, COUNT(*) OVER() AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
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('SELECT * FROM ' + this.table + ' WHERE id=$1' + ((client && lock) ? ' FOR UPDATE' : ''), [id]).then(q => this.buildObject(q.rows[0]));
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id=@1').then((q: any) => this.buildObject(q.recordsets[0][0]));
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('SELECT * FROM ' + this.table + ' WHERE id=ANY($1)' + ((client && lock) ? ' FOR UPDATE' : ''), [ids])
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + ' WHERE id IN ('
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('SELECT * FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
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('SELECT *, COUNT(*) OVER() AS cnt FROM ' + this.table + (where ? (' WHERE ' + where) : '') + (order ? (' ORDER BY ' + order) : '')
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('SELECT * FROM ' + this.table + ((client && lock) ? ' WITH (UPDLOCK, ROWLOCK)' : '') + (where ? (' WHERE ' + where) : '')
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anote-server-libs",
3
- "version": "0.7.3",
3
+ "version": "0.8.0",
4
4
  "description": "Helpers for express-TS servers",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -14,6 +14,10 @@ function withTransaction(repo, logger, previousMethod, lock, commitIfLost = true
14
14
  }
15
15
  const endTerminator = res.end.bind(res);
16
16
  const jsonTerminator = (obj) => {
17
+ try {
18
+ res.set('Content-Type', 'application/json');
19
+ }
20
+ catch (_) { }
17
21
  res.write((0, utils_1.jsonStringify)(obj) || '{}');
18
22
  endTerminator();
19
23
  };
@@ -21,6 +21,7 @@ export function withTransaction(repo: BaseModelRepository, logger: Logger, previ
21
21
  }
22
22
  const endTerminator = res.end.bind(res);
23
23
  const jsonTerminator = (obj: any) => {
24
+ try { res.set('Content-Type', 'application/json'); } catch(_) {}
24
25
  res.write(jsonStringify(obj) || '{}');
25
26
  endTerminator();
26
27
  };
@@ -1,3 +0,0 @@
1
- {
2
- "typescript.tsdk": "node_modules\\typescript\\lib"
3
- }