@nymphjs/driver-mysql 1.0.0-beta.4 → 1.0.0-beta.41

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/CHANGELOG.md CHANGED
@@ -3,6 +3,170 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.0.0-beta.41](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.40...v1.0.0-beta.41) (2023-07-12)
7
+
8
+ ### Features
9
+
10
+ - remove synchronous database queries ([b579fb2](https://github.com/sciactive/nymphjs/commit/b579fb2eacd96cdd1b386a62c5c00cdbb2438f6e))
11
+
12
+ # [1.0.0-beta.40](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.39...v1.0.0-beta.40) (2023-07-10)
13
+
14
+ **Note:** Version bump only for package @nymphjs/driver-mysql
15
+
16
+ # [1.0.0-beta.39](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.38...v1.0.0-beta.39) (2023-07-09)
17
+
18
+ **Note:** Version bump only for package @nymphjs/driver-mysql
19
+
20
+ # [1.0.0-beta.38](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.37...v1.0.0-beta.38) (2023-07-09)
21
+
22
+ **Note:** Version bump only for package @nymphjs/driver-mysql
23
+
24
+ # [1.0.0-beta.37](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.36...v1.0.0-beta.37) (2023-07-09)
25
+
26
+ **Note:** Version bump only for package @nymphjs/driver-mysql
27
+
28
+ # [1.0.0-beta.36](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.35...v1.0.0-beta.36) (2023-07-09)
29
+
30
+ **Note:** Version bump only for package @nymphjs/driver-mysql
31
+
32
+ # [1.0.0-beta.35](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.34...v1.0.0-beta.35) (2023-06-14)
33
+
34
+ **Note:** Version bump only for package @nymphjs/driver-mysql
35
+
36
+ # [1.0.0-beta.34](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.33...v1.0.0-beta.34) (2023-05-13)
37
+
38
+ **Note:** Version bump only for package @nymphjs/driver-mysql
39
+
40
+ # [1.0.0-beta.33](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.32...v1.0.0-beta.33) (2023-05-13)
41
+
42
+ **Note:** Version bump only for package @nymphjs/driver-mysql
43
+
44
+ # [1.0.0-beta.32](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.31...v1.0.0-beta.32) (2023-05-13)
45
+
46
+ **Note:** Version bump only for package @nymphjs/driver-mysql
47
+
48
+ # [1.0.0-beta.31](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.30...v1.0.0-beta.31) (2023-05-12)
49
+
50
+ **Note:** Version bump only for package @nymphjs/driver-mysql
51
+
52
+ # [1.0.0-beta.30](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.29...v1.0.0-beta.30) (2023-05-12)
53
+
54
+ **Note:** Version bump only for package @nymphjs/driver-mysql
55
+
56
+ # [1.0.0-beta.29](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.28...v1.0.0-beta.29) (2023-05-08)
57
+
58
+ ### Bug Fixes
59
+
60
+ - use 4 byte utf8 encoding in mysql ([c335b6e](https://github.com/sciactive/nymphjs/commit/c335b6e8416f935c9116e7f54de753d4b2255a73))
61
+
62
+ # [1.0.0-beta.28](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.27...v1.0.0-beta.28) (2023-05-05)
63
+
64
+ **Note:** Version bump only for package @nymphjs/driver-mysql
65
+
66
+ # [1.0.0-beta.27](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.26...v1.0.0-beta.27) (2023-05-04)
67
+
68
+ ### Bug Fixes
69
+
70
+ - properly escape mysql ids with dots ([64bfaa5](https://github.com/sciactive/nymphjs/commit/64bfaa5d606f97a753ca7e6c5d2d86f7c0f7729a))
71
+
72
+ # [1.0.0-beta.26](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.25...v1.0.0-beta.26) (2023-05-04)
73
+
74
+ ### Features
75
+
76
+ - update packages and migrate to mysql2 ([72ad611](https://github.com/sciactive/nymphjs/commit/72ad611bd2bf7bf85c3ba8a3486503d9b50c49d6))
77
+
78
+ # [1.0.0-beta.25](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.24...v1.0.0-beta.25) (2023-05-04)
79
+
80
+ ### Bug Fixes
81
+
82
+ - don't create empty entities ([1d4d2e9](https://github.com/sciactive/nymphjs/commit/1d4d2e99af2e9cdc647bcf58ac34572836f41176))
83
+
84
+ # [1.0.0-beta.24](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.23...v1.0.0-beta.24) (2023-05-02)
85
+
86
+ **Note:** Version bump only for package @nymphjs/driver-mysql
87
+
88
+ # [1.0.0-beta.23](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.22...v1.0.0-beta.23) (2023-05-02)
89
+
90
+ **Note:** Version bump only for package @nymphjs/driver-mysql
91
+
92
+ # [1.0.0-beta.22](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.21...v1.0.0-beta.22) (2023-05-01)
93
+
94
+ **Note:** Version bump only for package @nymphjs/driver-mysql
95
+
96
+ # [1.0.0-beta.21](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.20...v1.0.0-beta.21) (2023-05-01)
97
+
98
+ **Note:** Version bump only for package @nymphjs/driver-mysql
99
+
100
+ # [1.0.0-beta.20](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.19...v1.0.0-beta.20) (2023-04-30)
101
+
102
+ **Note:** Version bump only for package @nymphjs/driver-mysql
103
+
104
+ # [1.0.0-beta.19](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.18...v1.0.0-beta.19) (2023-04-29)
105
+
106
+ **Note:** Version bump only for package @nymphjs/driver-mysql
107
+
108
+ # [1.0.0-beta.18](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.17...v1.0.0-beta.18) (2023-04-27)
109
+
110
+ **Note:** Version bump only for package @nymphjs/driver-mysql
111
+
112
+ # [1.0.0-beta.17](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.16...v1.0.0-beta.17) (2023-04-24)
113
+
114
+ ### Features
115
+
116
+ - use a long lived worker thread for synchronous mysql and postgres queries ([7e2bf84](https://github.com/sciactive/nymphjs/commit/7e2bf84a2d584d6906c31f44147025b793a05026))
117
+
118
+ # [1.0.0-beta.16](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.15...v1.0.0-beta.16) (2023-03-31)
119
+
120
+ **Note:** Version bump only for package @nymphjs/driver-mysql
121
+
122
+ # [1.0.0-beta.15](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.14...v1.0.0-beta.15) (2023-03-23)
123
+
124
+ ### Features
125
+
126
+ - add option to sort results by a property ([16384e7](https://github.com/sciactive/nymphjs/commit/16384e7bdab88abb55ccccabb06ac09f92fa8a03))
127
+
128
+ # [1.0.0-beta.14](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.13...v1.0.0-beta.14) (2023-03-17)
129
+
130
+ **Note:** Version bump only for package @nymphjs/driver-mysql
131
+
132
+ # [1.0.0-beta.13](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.12...v1.0.0-beta.13) (2023-03-16)
133
+
134
+ **Note:** Version bump only for package @nymphjs/driver-mysql
135
+
136
+ # [1.0.0-beta.12](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.11...v1.0.0-beta.12) (2023-03-04)
137
+
138
+ **Note:** Version bump only for package @nymphjs/driver-mysql
139
+
140
+ # [1.0.0-beta.11](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.10...v1.0.0-beta.11) (2023-02-27)
141
+
142
+ **Note:** Version bump only for package @nymphjs/driver-mysql
143
+
144
+ # [1.0.0-beta.10](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.9...v1.0.0-beta.10) (2023-01-19)
145
+
146
+ **Note:** Version bump only for package @nymphjs/driver-mysql
147
+
148
+ # [1.0.0-beta.9](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.8...v1.0.0-beta.9) (2023-01-09)
149
+
150
+ **Note:** Version bump only for package @nymphjs/driver-mysql
151
+
152
+ # [1.0.0-beta.8](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.7...v1.0.0-beta.8) (2023-01-09)
153
+
154
+ **Note:** Version bump only for package @nymphjs/driver-mysql
155
+
156
+ # [1.0.0-beta.7](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.6...v1.0.0-beta.7) (2023-01-05)
157
+
158
+ ### Bug Fixes
159
+
160
+ - sqlite transaction returns wrong instance of nymph if it has been cloned ([b278c76](https://github.com/sciactive/nymphjs/commit/b278c7633722cb1cca7a941187ae2f1ff8ebdc7b))
161
+
162
+ # [1.0.0-beta.6](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.5...v1.0.0-beta.6) (2023-01-05)
163
+
164
+ **Note:** Version bump only for package @nymphjs/driver-mysql
165
+
166
+ # [1.0.0-beta.5](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.4...v1.0.0-beta.5) (2022-11-24)
167
+
168
+ **Note:** Version bump only for package @nymphjs/driver-mysql
169
+
6
170
  # [1.0.0-beta.4](https://github.com/sciactive/nymphjs/compare/v1.0.0-beta.3...v1.0.0-beta.4) (2022-11-23)
7
171
 
8
172
  **Note:** Version bump only for package @nymphjs/driver-mysql
@@ -1,8 +1,8 @@
1
- import { default as MySQLType } from '@types/mysql';
1
+ import { Pool, PoolConnection } from 'mysql2';
2
2
  import { NymphDriver, EntityConstructor, EntityInterface, FormattedSelector, Options, Selector } from '@nymphjs/nymph';
3
3
  import { MySQLDriverConfig } from './conf';
4
4
  type MySQLDriverTransaction = {
5
- connection: MySQLType.PoolConnection | null;
5
+ connection: PoolConnection | null;
6
6
  count: number;
7
7
  };
8
8
  export default class MySQLDriver extends NymphDriver {
@@ -10,11 +10,12 @@ export default class MySQLDriver extends NymphDriver {
10
10
  private mysqlConfig;
11
11
  protected prefix: string;
12
12
  protected connected: boolean;
13
- protected link: MySQLType.Pool;
13
+ protected link: Pool;
14
14
  protected transaction: MySQLDriverTransaction | null;
15
15
  static escape(input: string): string;
16
16
  static escapeValue(input: string): string;
17
- constructor(config: Partial<MySQLDriverConfig>, link?: MySQLType.Pool, transaction?: MySQLDriverTransaction);
17
+ constructor(config: Partial<MySQLDriverConfig>, link?: Pool, transaction?: MySQLDriverTransaction);
18
+ clone(): MySQLDriver;
18
19
  private getConnection;
19
20
  connect(): Promise<true>;
20
21
  disconnect(): Promise<false>;
@@ -23,12 +24,9 @@ export default class MySQLDriver extends NymphDriver {
23
24
  private createTables;
24
25
  private translateQuery;
25
26
  private query;
26
- private querySync;
27
27
  private queryIter;
28
- private queryIterSync;
29
28
  private queryGet;
30
29
  private queryRun;
31
- private queryRunSync;
32
30
  commit(name: string): Promise<boolean>;
33
31
  deleteEntityByID(guid: string, className?: EntityConstructor | string | null): Promise<boolean>;
34
32
  deleteUID(name: string): Promise<boolean>;
@@ -37,9 +35,6 @@ export default class MySQLDriver extends NymphDriver {
37
35
  protected performQuery(options: Options, formattedSelectors: FormattedSelector[], etype: string): {
38
36
  result: any;
39
37
  };
40
- protected performQuerySync(options: Options, formattedSelectors: FormattedSelector[], etype: string): {
41
- result: any;
42
- };
43
38
  getEntities<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
44
39
  return: 'count';
45
40
  }, ...selectors: Selector[]): Promise<number>;
@@ -47,13 +42,6 @@ export default class MySQLDriver extends NymphDriver {
47
42
  return: 'guid';
48
43
  }, ...selectors: Selector[]): Promise<string[]>;
49
44
  getEntities<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): Promise<ReturnType<T['factorySync']>[]>;
50
- protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
51
- return: 'count';
52
- }, ...selectors: Selector[]): number;
53
- protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(options: Options<T> & {
54
- return: 'guid';
55
- }, ...selectors: Selector[]): string[];
56
- protected getEntitiesSync<T extends EntityConstructor = EntityConstructor>(options?: Options<T>, ...selectors: Selector[]): ReturnType<T['factorySync']>[];
57
45
  getUID(name: string): Promise<number | null>;
58
46
  import(filename: string): Promise<boolean>;
59
47
  newUID(name: string): Promise<number | null>;
@@ -3,18 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const child_process_1 = __importDefault(require("child_process"));
7
- const mysql_1 = __importDefault(require("@vlasky/mysql"));
6
+ const mysql2_1 = __importDefault(require("mysql2"));
7
+ const sqlstring_1 = __importDefault(require("sqlstring"));
8
8
  const nymph_1 = require("@nymphjs/nymph");
9
9
  const guid_1 = require("@nymphjs/guid");
10
10
  const conf_1 = require("./conf");
11
- const mysql = mysql_1.default;
12
11
  class MySQLDriver extends nymph_1.NymphDriver {
13
12
  static escape(input) {
14
- return mysql.escapeId(input);
13
+ return sqlstring_1.default.escapeId(input, true);
15
14
  }
16
15
  static escapeValue(input) {
17
- return mysql.escape(input);
16
+ return mysql2_1.default.escape(input);
18
17
  }
19
18
  constructor(config, link, transaction) {
20
19
  super();
@@ -41,6 +40,9 @@ class MySQLDriver extends nymph_1.NymphDriver {
41
40
  this.connect();
42
41
  }
43
42
  }
43
+ clone() {
44
+ return new MySQLDriver(this.config, this.link, this.transaction ?? undefined);
45
+ }
44
46
  getConnection() {
45
47
  if (this.transaction != null && this.transaction.connection != null) {
46
48
  return Promise.resolve(this.transaction.connection);
@@ -51,9 +53,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
51
53
  try {
52
54
  if (this.connected) {
53
55
  const connection = await new Promise((resolve, reject) => this.link.getConnection((err, con) => err ? reject(err) : resolve(con)));
54
- await new Promise((resolve) => connection.ping(() => {
55
- resolve(0);
56
- }));
57
56
  connection.release();
58
57
  }
59
58
  }
@@ -62,7 +61,10 @@ class MySQLDriver extends nymph_1.NymphDriver {
62
61
  }
63
62
  if (!this.connected) {
64
63
  try {
65
- this.link = mysql.createPool(this.mysqlConfig);
64
+ this.link = mysql2_1.default.createPool({
65
+ ...this.mysqlConfig,
66
+ charset: 'utf8mb4_bin',
67
+ });
66
68
  this.connected = true;
67
69
  }
68
70
  catch (e) {
@@ -92,8 +94,8 @@ class MySQLDriver extends nymph_1.NymphDriver {
92
94
  isConnected() {
93
95
  return this.connected;
94
96
  }
95
- createTables(etype = null) {
96
- this.queryRunSync('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";');
97
+ async createTables(etype = null) {
98
+ await this.queryRun('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";');
97
99
  let foreignKeyDataTableGuid = '';
98
100
  let foreignKeyDataComparisonsTableGuid = '';
99
101
  let foreignKeyReferencesTableGuid = '';
@@ -103,7 +105,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
103
105
  foreignKeyReferencesTableGuid = ` REFERENCES ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)}(\`guid\`) ON DELETE CASCADE`;
104
106
  }
105
107
  if (etype != null) {
106
- this.queryRunSync(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} (
108
+ await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} (
107
109
  \`guid\` BINARY(12) NOT NULL,
108
110
  \`tags\` LONGTEXT,
109
111
  \`cdate\` DOUBLE PRECISION NOT NULL,
@@ -113,8 +115,8 @@ class MySQLDriver extends nymph_1.NymphDriver {
113
115
  INDEX \`id_mdate\` USING BTREE (\`mdate\`),
114
116
  FULLTEXT \`id_tags\` (\`tags\`)
115
117
  ) ENGINE ${this.config.engine}
116
- CHARACTER SET utf8 COLLATE utf8_bin;`);
117
- this.queryRunSync(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (
118
+ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;`);
119
+ await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} (
118
120
  \`guid\` BINARY(12) NOT NULL${foreignKeyDataTableGuid},
119
121
  \`name\` TEXT NOT NULL,
120
122
  \`value\` LONGTEXT NOT NULL,
@@ -122,8 +124,8 @@ class MySQLDriver extends nymph_1.NymphDriver {
122
124
  INDEX \`id_name\` USING HASH (\`name\`(255)),
123
125
  INDEX \`id_name_value\` USING BTREE (\`name\`(255), \`value\`(512))
124
126
  ) ENGINE ${this.config.engine}
125
- CHARACTER SET utf8 COLLATE utf8_bin;`);
126
- this.queryRunSync(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} (
127
+ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;`);
128
+ await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} (
127
129
  \`guid\` BINARY(12) NOT NULL${foreignKeyDataComparisonsTableGuid},
128
130
  \`name\` TEXT NOT NULL,
129
131
  \`truthy\` BOOLEAN,
@@ -132,23 +134,23 @@ class MySQLDriver extends nymph_1.NymphDriver {
132
134
  PRIMARY KEY (\`guid\`, \`name\`(255)),
133
135
  INDEX \`id_name\` USING HASH (\`name\`(255))
134
136
  ) ENGINE ${this.config.engine}
135
- CHARACTER SET utf8 COLLATE utf8_bin;`);
136
- this.queryRunSync(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} (
137
+ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;`);
138
+ await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}references_${etype}`)} (
137
139
  \`guid\` BINARY(12) NOT NULL${foreignKeyReferencesTableGuid},
138
140
  \`name\` TEXT NOT NULL,
139
141
  \`reference\` BINARY(12) NOT NULL,
140
142
  PRIMARY KEY (\`guid\`, \`name\`(255), \`reference\`),
141
143
  INDEX \`id_name_reference\` USING BTREE (\`name\`(255), \`reference\`)
142
144
  ) ENGINE ${this.config.engine}
143
- CHARACTER SET utf8 COLLATE utf8_bin;`);
145
+ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;`);
144
146
  }
145
147
  else {
146
- this.queryRunSync(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}uids`)} (
148
+ await this.queryRun(`CREATE TABLE IF NOT EXISTS ${MySQLDriver.escape(`${this.prefix}uids`)} (
147
149
  \`name\` TEXT NOT NULL,
148
150
  \`cur_uid\` BIGINT(20) UNSIGNED NOT NULL,
149
151
  PRIMARY KEY (\`name\`(100))
150
152
  ) ENGINE ${this.config.engine}
151
- CHARACTER SET utf8 COLLATE utf8_bin;`);
153
+ CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;`);
152
154
  }
153
155
  return true;
154
156
  }
@@ -170,9 +172,9 @@ class MySQLDriver extends nymph_1.NymphDriver {
170
172
  }
171
173
  catch (e) {
172
174
  const errorCode = e?.errno;
173
- if (errorCode === 1146 && this.createTables()) {
175
+ if (errorCode === 1146 && (await this.createTables())) {
174
176
  for (let etype of etypes) {
175
- this.createTables(etype);
177
+ await this.createTables(etype);
176
178
  }
177
179
  try {
178
180
  return await runQuery();
@@ -197,39 +199,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
197
199
  }
198
200
  }
199
201
  }
200
- querySync(runQuery, query, etypes = []) {
201
- try {
202
- return runQuery();
203
- }
204
- catch (e) {
205
- const errorCode = e?.errno;
206
- if (errorCode === 1146 && this.createTables()) {
207
- for (let etype of etypes) {
208
- this.createTables(etype);
209
- }
210
- try {
211
- return runQuery();
212
- }
213
- catch (e2) {
214
- throw new nymph_1.QueryFailedError('Query failed: ' + e2?.errno + ' - ' + e2?.message, query);
215
- }
216
- }
217
- else if (errorCode === 2006) {
218
- if (!this.connect()) {
219
- throw new nymph_1.QueryFailedError('Query failed: ' + e?.errno + ' - ' + e?.message, query);
220
- }
221
- try {
222
- return runQuery();
223
- }
224
- catch (e2) {
225
- throw new nymph_1.QueryFailedError('Query failed: ' + e2?.errno + ' - ' + e2?.message, query);
226
- }
227
- }
228
- else {
229
- throw e;
230
- }
231
- }
232
- }
233
202
  queryIter(query, { etypes = [], params = {}, } = {}) {
234
203
  const { query: newQuery, params: newParams } = this.translateQuery(query, params);
235
204
  return this.query(async () => {
@@ -249,36 +218,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
249
218
  return results;
250
219
  }, `${query} -- ${JSON.stringify(params)}`, etypes);
251
220
  }
252
- queryIterSync(query, { etypes = [], params = {}, } = {}) {
253
- const { query: newQuery, params: newParams } = this.translateQuery(query, params);
254
- return this.querySync(() => {
255
- const output = child_process_1.default.spawnSync(process.argv0, [__dirname + '/runMysqlSync.js'], {
256
- input: JSON.stringify({
257
- mysqlConfig: this.mysqlConfig,
258
- query: newQuery,
259
- params: newParams,
260
- }),
261
- timeout: 30000,
262
- maxBuffer: 100 * 1024 * 1024,
263
- encoding: 'utf8',
264
- windowsHide: true,
265
- });
266
- try {
267
- const { results } = JSON.parse(output.stdout);
268
- return results;
269
- }
270
- catch (e) {
271
- }
272
- if (output.status === 0) {
273
- throw new Error('Unknown parse error.');
274
- }
275
- const err = JSON.parse(output.stderr);
276
- const e = new Error(err.name);
277
- for (const name in err) {
278
- e[name] = err[name];
279
- }
280
- }, `${query} -- ${JSON.stringify(params)}`, etypes);
281
- }
282
221
  queryGet(query, { etypes = [], params = {}, } = {}) {
283
222
  const { query: newQuery, params: newParams } = this.translateQuery(query, params);
284
223
  return this.query(async () => {
@@ -317,36 +256,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
317
256
  return { changes: results.changedRows ?? 0 };
318
257
  }, `${query} -- ${JSON.stringify(params)}`, etypes);
319
258
  }
320
- queryRunSync(query, { etypes = [], params = {}, } = {}) {
321
- const { query: newQuery, params: newParams } = this.translateQuery(query, params);
322
- return this.querySync(() => {
323
- const output = child_process_1.default.spawnSync(process.argv0, [__dirname + '/runMysqlSync.js'], {
324
- input: JSON.stringify({
325
- mysqlConfig: this.mysqlConfig,
326
- query: newQuery,
327
- params: newParams,
328
- }),
329
- timeout: 30000,
330
- maxBuffer: 100 * 1024 * 1024,
331
- encoding: 'utf8',
332
- windowsHide: true,
333
- });
334
- try {
335
- const { results } = JSON.parse(output.stdout);
336
- return { changes: results.changedRows ?? 0 };
337
- }
338
- catch (e) {
339
- }
340
- if (output.status === 0) {
341
- throw new Error('Unknown parse error.');
342
- }
343
- const err = JSON.parse(output.stderr);
344
- const e = new Error(err.name);
345
- for (const name in err) {
346
- e[name] = err[name];
347
- }
348
- }, `${query} -- ${JSON.stringify(params)}`, etypes);
349
- }
350
259
  async commit(name) {
351
260
  if (name == null || typeof name !== 'string' || name.length === 0) {
352
261
  throw new nymph_1.InvalidParametersError('Transaction commit attempted without a name.');
@@ -496,6 +405,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
496
405
  const fTable = `f${tableSuffix}`;
497
406
  const ieTable = `ie${tableSuffix}`;
498
407
  const countTable = `count${tableSuffix}`;
408
+ const sTable = `s${tableSuffix}`;
499
409
  const sort = options.sort ?? 'cdate';
500
410
  const queryParts = this.iterateSelectorsForQuery(formattedSelectors, (key, value, typeIsOr, typeIsNot) => {
501
411
  const clauseNot = key.startsWith('!');
@@ -1289,18 +1199,38 @@ class MySQLDriver extends nymph_1.NymphDriver {
1289
1199
  return curQuery;
1290
1200
  });
1291
1201
  let sortBy;
1202
+ let sortByInner;
1203
+ let sortJoin = '';
1204
+ let sortJoinInner = '';
1205
+ const order = options.reverse ? ' DESC' : '';
1292
1206
  switch (sort) {
1293
1207
  case 'mdate':
1294
- sortBy = '`mdate`';
1208
+ sortBy = `${eTable}.\`mdate\`${order}`;
1209
+ sortByInner = `${ieTable}.\`mdate\`${order}`;
1295
1210
  break;
1296
1211
  case 'cdate':
1212
+ sortBy = `${eTable}.\`cdate\`${order}`;
1213
+ sortByInner = `${ieTable}.\`cdate\`${order}`;
1214
+ break;
1297
1215
  default:
1298
- sortBy = '`cdate`';
1216
+ const name = `param${++count.i}`;
1217
+ sortJoin = `LEFT JOIN (
1218
+ SELECT \`guid\`, \`string\`, \`number\`
1219
+ FROM ${MySQLDriver.escape(this.prefix + 'comparisons_' + etype)}
1220
+ WHERE \`name\`=@${name}
1221
+ ORDER BY \`number\`${order}, \`string\`${order}
1222
+ ) ${sTable} ON ${eTable}.\`guid\`=${sTable}.\`guid\``;
1223
+ sortJoinInner = `LEFT JOIN (
1224
+ SELECT \`guid\`, \`string\`, \`number\`
1225
+ FROM ${MySQLDriver.escape(this.prefix + 'comparisons_' + etype)}
1226
+ WHERE \`name\`=@${name}
1227
+ ORDER BY \`number\`${order}, \`string\`${order}
1228
+ ) ${sTable} ON ${ieTable}.\`guid\`=${sTable}.\`guid\``;
1229
+ sortBy = `${sTable}.\`number\`${order}, ${sTable}.\`string\`${order}`;
1230
+ sortByInner = sortBy;
1231
+ params[name] = sort;
1299
1232
  break;
1300
1233
  }
1301
- if (options.reverse) {
1302
- sortBy += ' DESC';
1303
- }
1304
1234
  let query;
1305
1235
  if (queryParts.length) {
1306
1236
  if (subquery) {
@@ -1336,8 +1266,9 @@ class MySQLDriver extends nymph_1.NymphDriver {
1336
1266
  : `${ieTable}.\`guid\``;
1337
1267
  query = `SELECT ${guidColumn} AS \`guid\`
1338
1268
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
1269
+ ${sortJoinInner}
1339
1270
  WHERE (${whereClause})
1340
- ORDER BY ${ieTable}.${sortBy}${limit}${offset}`;
1271
+ ORDER BY ${sortByInner}, ${ieTable}.\`guid\`${limit}${offset}`;
1341
1272
  }
1342
1273
  else {
1343
1274
  query = `SELECT
@@ -1352,13 +1283,15 @@ class MySQLDriver extends nymph_1.NymphDriver {
1352
1283
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
1353
1284
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}.\`guid\`=${dTable}.\`guid\`
1354
1285
  INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}.\`guid\`=${cTable}.\`guid\` AND ${dTable}.\`name\`=${cTable}.\`name\`
1286
+ ${sortJoin}
1355
1287
  INNER JOIN (
1356
1288
  SELECT ${ieTable}.\`guid\`
1357
1289
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
1290
+ ${sortJoinInner}
1358
1291
  WHERE (${whereClause})
1359
- ORDER BY ${ieTable}.${sortBy}${limit}${offset}
1292
+ ORDER BY ${sortByInner}${limit}${offset}
1360
1293
  ) ${fTable} ON ${eTable}.\`guid\`=${fTable}.\`guid\`
1361
- ORDER BY ${eTable}.${sortBy}`;
1294
+ ORDER BY ${sortBy}, ${eTable}.\`guid\``;
1362
1295
  }
1363
1296
  }
1364
1297
  }
@@ -1393,7 +1326,8 @@ class MySQLDriver extends nymph_1.NymphDriver {
1393
1326
  : `${ieTable}.\`guid\``;
1394
1327
  query = `SELECT ${guidColumn} AS \`guid\`
1395
1328
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
1396
- ORDER BY ${ieTable}.${sortBy}${limit}${offset}`;
1329
+ ${sortJoinInner}
1330
+ ORDER BY ${sortByInner}, ${ieTable}.\`guid\`${limit}${offset}`;
1397
1331
  }
1398
1332
  else {
1399
1333
  if (limit || offset) {
@@ -1409,12 +1343,14 @@ class MySQLDriver extends nymph_1.NymphDriver {
1409
1343
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
1410
1344
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}.\`guid\`=${dTable}.\`guid\`
1411
1345
  INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}.\`guid\`=${cTable}.\`guid\` AND ${dTable}.\`name\`=${cTable}.\`name\`
1346
+ ${sortJoin}
1412
1347
  INNER JOIN (
1413
1348
  SELECT ${ieTable}.\`guid\`
1414
1349
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${ieTable}
1415
- ORDER BY ${ieTable}.${sortBy}${limit}${offset}
1350
+ ${sortJoinInner}
1351
+ ORDER BY ${sortByInner}${limit}${offset}
1416
1352
  ) ${fTable} ON ${eTable}.\`guid\`=${fTable}.\`guid\`
1417
- ORDER BY ${eTable}.${sortBy}`;
1353
+ ORDER BY ${sortBy}, ${eTable}.\`guid\``;
1418
1354
  }
1419
1355
  else {
1420
1356
  query = `SELECT
@@ -1429,7 +1365,8 @@ class MySQLDriver extends nymph_1.NymphDriver {
1429
1365
  FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} ${eTable}
1430
1366
  LEFT JOIN ${MySQLDriver.escape(`${this.prefix}data_${etype}`)} ${dTable} ON ${eTable}.\`guid\`=${dTable}.\`guid\`
1431
1367
  INNER JOIN ${MySQLDriver.escape(`${this.prefix}comparisons_${etype}`)} ${cTable} ON ${dTable}.\`guid\`=${cTable}.\`guid\` AND ${dTable}.\`name\`=${cTable}.\`name\`
1432
- ORDER BY ${eTable}.${sortBy}`;
1368
+ ${sortJoin}
1369
+ ORDER BY ${sortBy}, ${eTable}.\`guid\``;
1433
1370
  }
1434
1371
  }
1435
1372
  }
@@ -1450,13 +1387,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
1450
1387
  result,
1451
1388
  };
1452
1389
  }
1453
- performQuerySync(options, formattedSelectors, etype) {
1454
- const { query, params, etypes } = this.makeEntityQuery(options, formattedSelectors, etype);
1455
- const result = (this.queryIterSync(query, { etypes, params }) || [])[Symbol.iterator]();
1456
- return {
1457
- result,
1458
- };
1459
- }
1460
1390
  async getEntities(options = {}, ...selectors) {
1461
1391
  const { result: resultPromise, process } = this.getEntitesRowLike(options, selectors, (options, formattedSelectors, etype) => this.performQuery(options, formattedSelectors, etype), () => {
1462
1392
  const next = result.next();
@@ -1480,28 +1410,6 @@ class MySQLDriver extends nymph_1.NymphDriver {
1480
1410
  }
1481
1411
  return value;
1482
1412
  }
1483
- getEntitiesSync(options = {}, ...selectors) {
1484
- const { result, process } = this.getEntitesRowLike(options, selectors, (options, formattedSelectors, etype) => this.performQuerySync(options, formattedSelectors, etype), () => {
1485
- const next = result.next();
1486
- return next.done ? null : next.value;
1487
- }, () => undefined, (row) => Number(row.count), (row) => row.guid, (row) => ({
1488
- tags: row.tags.length > 2 ? row.tags.slice(1, -1).split(',') : [],
1489
- cdate: isNaN(Number(row.cdate)) ? null : Number(row.cdate),
1490
- mdate: isNaN(Number(row.mdate)) ? null : Number(row.mdate),
1491
- }), (row) => ({
1492
- name: row.name,
1493
- svalue: row.value === 'N'
1494
- ? JSON.stringify(row.number)
1495
- : row.value === 'S'
1496
- ? JSON.stringify(row.string)
1497
- : row.value,
1498
- }));
1499
- const value = process();
1500
- if (value instanceof Error) {
1501
- throw value;
1502
- }
1503
- return value;
1504
- }
1505
1413
  async getUID(name) {
1506
1414
  if (name == null) {
1507
1415
  throw new nymph_1.InvalidParametersError('Name not given for UID.');
@@ -1731,6 +1639,10 @@ class MySQLDriver extends nymph_1.NymphDriver {
1731
1639
  };
1732
1640
  try {
1733
1641
  const result = await this.saveEntityRowLike(entity, async (_entity, guid, tags, data, sdata, cdate, etype) => {
1642
+ if (Object.keys(data).length === 0 &&
1643
+ Object.keys(sdata).length === 0) {
1644
+ return false;
1645
+ }
1734
1646
  await this.queryRun(`INSERT INTO ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} (\`guid\`, \`tags\`, \`cdate\`, \`mdate\`) VALUES (UNHEX(@guid), @tags, @cdate, @cdate);`, {
1735
1647
  etypes: [etype],
1736
1648
  params: {
@@ -1742,6 +1654,10 @@ class MySQLDriver extends nymph_1.NymphDriver {
1742
1654
  await insertData(guid, data, sdata, etype);
1743
1655
  return true;
1744
1656
  }, async (entity, guid, tags, data, sdata, mdate, etype) => {
1657
+ if (Object.keys(data).length === 0 &&
1658
+ Object.keys(sdata).length === 0) {
1659
+ return false;
1660
+ }
1745
1661
  if (this.config.rowLocking) {
1746
1662
  const promises = [];
1747
1663
  promises.push(this.queryRun(`SELECT 1 FROM ${MySQLDriver.escape(`${this.prefix}entities_${etype}`)} WHERE \`guid\`=UNHEX(@guid) GROUP BY 1 FOR UPDATE;`, {
@@ -1867,8 +1783,7 @@ class MySQLDriver extends nymph_1.NymphDriver {
1867
1783
  this.transaction = null;
1868
1784
  }
1869
1785
  const nymph = this.nymph.clone();
1870
- nymph.driver = new MySQLDriver(this.config, this.link, transaction);
1871
- nymph.driver.init(nymph);
1786
+ nymph.driver.transaction = transaction;
1872
1787
  return nymph;
1873
1788
  }
1874
1789
  }