mysql2-core 0.2.0 → 0.2.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/lib/index.js +49 -2
- package/package.json +1 -1
- package/src/index.ts +14 -11
package/lib/index.js
CHANGED
|
@@ -63,10 +63,52 @@ function createPool(conf) {
|
|
|
63
63
|
if (conf.max && conf.max > 0 && !conf.connectionLimit) {
|
|
64
64
|
conf.connectionLimit = conf.max;
|
|
65
65
|
}
|
|
66
|
-
var pool = mysql2_1.createPool(__assign(__assign({}, conf), { rowsAsArray: true
|
|
66
|
+
var pool = mysql2_1.createPool(__assign(__assign({}, conf), { rowsAsArray: true }));
|
|
67
67
|
return pool;
|
|
68
68
|
}
|
|
69
69
|
exports.createPool = createPool;
|
|
70
|
+
var PoolManager = (function () {
|
|
71
|
+
function PoolManager(pool) {
|
|
72
|
+
this.pool = pool;
|
|
73
|
+
this.driver = 'mysql';
|
|
74
|
+
this.param = this.param.bind(this);
|
|
75
|
+
this.exec = this.exec.bind(this);
|
|
76
|
+
this.execBatch = this.execBatch.bind(this);
|
|
77
|
+
this.query = this.query.bind(this);
|
|
78
|
+
this.queryOne = this.queryOne.bind(this);
|
|
79
|
+
this.execScalar = this.execScalar.bind(this);
|
|
80
|
+
this.count = this.count.bind(this);
|
|
81
|
+
}
|
|
82
|
+
PoolManager.prototype.param = function (i) {
|
|
83
|
+
return '?';
|
|
84
|
+
};
|
|
85
|
+
PoolManager.prototype.exec = function (sql, args, ctx) {
|
|
86
|
+
var p = (ctx ? ctx : this.pool);
|
|
87
|
+
return exec(p, sql, args);
|
|
88
|
+
};
|
|
89
|
+
PoolManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
|
|
90
|
+
var p = (ctx ? ctx : this.pool);
|
|
91
|
+
return execBatch(p, statements, firstSuccess);
|
|
92
|
+
};
|
|
93
|
+
PoolManager.prototype.query = function (sql, args, m, bools, ctx) {
|
|
94
|
+
var p = (ctx ? ctx : this.pool);
|
|
95
|
+
return query(p, sql, args, m, bools);
|
|
96
|
+
};
|
|
97
|
+
PoolManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
|
|
98
|
+
var p = (ctx ? ctx : this.pool);
|
|
99
|
+
return queryOne(p, sql, args, m, bools);
|
|
100
|
+
};
|
|
101
|
+
PoolManager.prototype.execScalar = function (sql, args, ctx) {
|
|
102
|
+
var p = (ctx ? ctx : this.pool);
|
|
103
|
+
return execScalar(p, sql, args);
|
|
104
|
+
};
|
|
105
|
+
PoolManager.prototype.count = function (sql, args, ctx) {
|
|
106
|
+
var p = (ctx ? ctx : this.pool);
|
|
107
|
+
return count(p, sql, args);
|
|
108
|
+
};
|
|
109
|
+
return PoolManager;
|
|
110
|
+
}());
|
|
111
|
+
exports.PoolManager = PoolManager;
|
|
70
112
|
function execBatch(pool, statements, firstSuccess) {
|
|
71
113
|
if (!statements || statements.length === 0) {
|
|
72
114
|
return Promise.resolve(0);
|
|
@@ -228,7 +270,12 @@ function query(pool, sql, args, m, bools) {
|
|
|
228
270
|
else {
|
|
229
271
|
if (results.length > 0) {
|
|
230
272
|
var arrayResult = results.map(function (item) {
|
|
231
|
-
|
|
273
|
+
if (Array.isArray(item)) {
|
|
274
|
+
return formatData(fields, item);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
return item;
|
|
278
|
+
}
|
|
232
279
|
});
|
|
233
280
|
return resolve(handleResults(arrayResult, m, bools));
|
|
234
281
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Connection, createPool as createPool2, FieldPacket, format, OkPacket, Pool, PoolConnection, ResultSetHeader
|
|
2
|
-
import { buildToSave, buildToSaveBatch
|
|
1
|
+
import { Connection, createPool as createPool2, FieldPacket, format, OkPacket, Pool, PoolConnection, ResultSetHeader } from 'mysql2';
|
|
2
|
+
import { buildToSave, buildToSaveBatch } from './build';
|
|
3
3
|
import { Attribute, Attributes, Manager, Statement, StringMap } from './metadata';
|
|
4
4
|
|
|
5
5
|
export * from './metadata';
|
|
@@ -25,11 +25,11 @@ export interface Config {
|
|
|
25
25
|
export function createPool(conf: Config): Pool {
|
|
26
26
|
if (conf.max && conf.max > 0 && !conf.connectionLimit) {
|
|
27
27
|
conf.connectionLimit = conf.max;
|
|
28
|
-
}
|
|
29
|
-
const pool = createPool2({...conf, rowsAsArray: true
|
|
28
|
+
}
|
|
29
|
+
const pool = createPool2({...conf, rowsAsArray: true});
|
|
30
30
|
return pool;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
// tslint:disable-next-line:max-classes-per-file
|
|
34
34
|
export class PoolManager implements Manager {
|
|
35
35
|
constructor(public pool: Pool) {
|
|
@@ -70,7 +70,6 @@ export class PoolManager implements Manager {
|
|
|
70
70
|
return count(p, sql, args);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
*/
|
|
74
73
|
export function execBatch(pool: Pool, statements: Statement[], firstSuccess?: boolean): Promise<number> {
|
|
75
74
|
if (!statements || statements.length === 0) {
|
|
76
75
|
return Promise.resolve(0);
|
|
@@ -127,7 +126,7 @@ export function execBatch(pool: Pool, statements: Statement[], firstSuccess?: bo
|
|
|
127
126
|
}
|
|
128
127
|
});
|
|
129
128
|
let c = 0;
|
|
130
|
-
c += results0.affectedRows + results.reduce((prev,item) => prev + item.affectedRows, 0);
|
|
129
|
+
c += results0.affectedRows + results.reduce((prev, item) => prev + item.affectedRows, 0);
|
|
131
130
|
return resolve(c);
|
|
132
131
|
}
|
|
133
132
|
});
|
|
@@ -172,7 +171,7 @@ export function execBatch(pool: Pool, statements: Statement[], firstSuccess?: bo
|
|
|
172
171
|
});
|
|
173
172
|
}
|
|
174
173
|
});
|
|
175
|
-
return resolve(results.reduce((prev,item) => prev + item.affectedRows, 0));
|
|
174
|
+
return resolve(results.reduce((prev, item) => prev + item.affectedRows, 0));
|
|
176
175
|
}
|
|
177
176
|
});
|
|
178
177
|
}
|
|
@@ -208,13 +207,17 @@ export async function query<T>(pool: Pool | PoolConnection, sql: string, args?:
|
|
|
208
207
|
if (err) {
|
|
209
208
|
return reject(err);
|
|
210
209
|
} else {
|
|
211
|
-
if(results.length > 0) {
|
|
210
|
+
if (results.length > 0) {
|
|
212
211
|
const arrayResult = results.map(item => {
|
|
213
|
-
|
|
212
|
+
if(Array.isArray(item)) {
|
|
213
|
+
return formatData<T>(fields, item);
|
|
214
|
+
} else {
|
|
215
|
+
return item
|
|
216
|
+
}
|
|
214
217
|
});
|
|
215
218
|
return resolve(handleResults(arrayResult, m, bools));
|
|
216
219
|
} else {
|
|
217
|
-
resolve([])
|
|
220
|
+
resolve([]);
|
|
218
221
|
}
|
|
219
222
|
}
|
|
220
223
|
});
|