@stackpress/inquire-mysql2 0.1.3 → 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.
@@ -1,15 +1,18 @@
1
1
  import type { ResultSetHeader, Connection as Database } from 'mysql2/promise';
2
- import type { Dialect, Connection, QueryObject } from '@stackpress/inquire/dist/types';
2
+ import type { Dialect, Connection, QueryObject, Transaction } from '@stackpress/inquire/dist/types';
3
3
  import type { Results } from './types';
4
4
  export default class Mysql2Connection implements Connection {
5
5
  readonly dialect: Dialect;
6
6
  readonly resource: Database;
7
+ protected _lastId?: number | string;
8
+ get lastId(): string | number | undefined;
7
9
  constructor(resource: Database);
8
- query<R = unknown>(queries: QueryObject[]): Promise<R[]>;
9
- raw<R = unknown>(queries: QueryObject[]): Promise<[ResultSetHeader, undefined] | Results<R>>;
10
- protected _format(request: QueryObject): {
10
+ format(request: QueryObject): {
11
11
  query: string;
12
12
  values: import("@stackpress/inquire/dist/types").Value[];
13
13
  };
14
+ query<R = unknown>(request: QueryObject): Promise<R[]>;
15
+ raw<R = unknown>(request: QueryObject): Promise<[ResultSetHeader, undefined] | Results<R>>;
16
+ transaction<R = unknown>(callback: Transaction<R>): Promise<R[]>;
14
17
  protected _query<R = unknown>(request: QueryObject): Promise<Results<R> | [ResultSetHeader, undefined]>;
15
18
  }
@@ -13,47 +13,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const Mysql_1 = __importDefault(require("@stackpress/inquire/dist/dialect/Mysql"));
16
- const Exception_1 = __importDefault(require("@stackpress/inquire/dist/Exception"));
17
16
  class Mysql2Connection {
17
+ get lastId() {
18
+ return this._lastId;
19
+ }
18
20
  constructor(resource) {
19
21
  this.dialect = Mysql_1.default;
20
22
  this.resource = resource;
21
23
  }
22
- query(queries) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- const results = yield this.raw(queries);
25
- return Array.isArray(results[0]) ? results[0] : [];
26
- });
27
- }
28
- raw(queries) {
29
- return __awaiter(this, void 0, void 0, function* () {
30
- if (queries.length === 0) {
31
- throw Exception_1.default.for('No queries to execute.');
32
- }
33
- const queue = queries.slice();
34
- const last = queue.pop();
35
- if (queue.length === 0) {
36
- const formatted = this._format(last);
37
- return yield this._query(formatted);
38
- }
39
- try {
40
- yield this.resource.query('START TRANSACTION');
41
- for (const request of queries) {
42
- const formatted = this._format(request);
43
- yield this._query(formatted);
44
- }
45
- const formatted = this._format(last);
46
- const results = yield this._query(formatted);
47
- yield this.resource.query('COMMIT');
48
- return results;
49
- }
50
- catch (e) {
51
- yield this.resource.query('ROLLBACK');
52
- throw e;
53
- }
54
- });
55
- }
56
- _format(request) {
24
+ format(request) {
57
25
  let { query, values = [] } = request;
58
26
  for (let i = 0; i < values.length; i++) {
59
27
  const value = values[i];
@@ -69,6 +37,35 @@ class Mysql2Connection {
69
37
  }
70
38
  return { query, values };
71
39
  }
40
+ query(request) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const results = yield this.raw(request);
43
+ if (!Array.isArray(results[0]) && results[0].insertId) {
44
+ this._lastId = results[0].insertId;
45
+ }
46
+ return Array.isArray(results[0]) ? results[0] : [];
47
+ });
48
+ }
49
+ raw(request) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const formatted = this.format(request);
52
+ return yield this._query(formatted);
53
+ });
54
+ }
55
+ transaction(callback) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ try {
58
+ yield this.resource.beginTransaction();
59
+ const results = yield callback(this);
60
+ yield this.resource.commit();
61
+ return results;
62
+ }
63
+ catch (e) {
64
+ yield this.resource.rollback();
65
+ throw e;
66
+ }
67
+ });
68
+ }
72
69
  _query(request) {
73
70
  return __awaiter(this, void 0, void 0, function* () {
74
71
  const { query, values = [] } = request;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackpress/inquire-mysql2",
3
- "version": "0.1.3",
3
+ "version": "0.2.2",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Generic typed mysql2",
6
6
  "author": "Chris <chris@incept.asia>",
@@ -19,7 +19,7 @@
19
19
  "build": "tsc"
20
20
  },
21
21
  "dependencies": {
22
- "@stackpress/inquire": "0.1.3"
22
+ "@stackpress/inquire": "0.2.2"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "mysql2": "^3.11.5"