@naturalcycles/mysql-lib 3.14.7 → 3.15.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.
package/dist/mysql.db.js CHANGED
@@ -173,15 +173,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
173
173
  if (this.cfg.logSQL)
174
174
  this.cfg.logger.log(`stream: ${sql}`);
175
175
  // return this.streamSQL(sql, opt)
176
- return this.pool()
177
- .query(sql)
178
- .stream()
179
- .pipe(new node_stream_1.Transform({
180
- objectMode: true,
181
- transform(row, _encoding, cb) {
182
- cb(null, (0, js_lib_1._filterUndefinedValues)(row, true));
183
- },
184
- }));
176
+ return this.pool().query(sql).stream().map(row => (0, js_lib_1._filterUndefinedValues)(row, true));
185
177
  }
186
178
  // SAVE
187
179
  async saveBatch(table, rowsInput, opt = {}) {
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib';
3
2
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
4
3
  import { MysqlDB, MysqlDBCfg } from './mysql.db';
@@ -22,4 +21,5 @@ export declare class MySQLKeyValueDB implements CommonKeyValueDB {
22
21
  beginTransaction(): Promise<void>;
23
22
  endTransaction(): Promise<void>;
24
23
  count(table: string): Promise<number>;
24
+ increment(_table: string, _id: string, _by?: number): Promise<number>;
25
25
  }
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MySQLKeyValueDB = void 0;
4
- const node_stream_1 = require("node:stream");
5
4
  const js_lib_1 = require("@naturalcycles/js-lib");
6
5
  const mysql_db_1 = require("./mysql.db");
7
6
  class MySQLKeyValueDB {
@@ -63,16 +62,7 @@ class MySQLKeyValueDB {
63
62
  sql += ` LIMIT ${limit}`;
64
63
  if (this.cfg.logSQL)
65
64
  this.db.cfg.logger.log(`stream: ${sql}`);
66
- return this.db
67
- .pool()
68
- .query(sql)
69
- .stream()
70
- .pipe(new node_stream_1.Transform({
71
- objectMode: true,
72
- transform(row, _, cb) {
73
- cb(null, row.id);
74
- },
75
- }));
65
+ return this.db.pool().query(sql).stream().map(row => row.id);
76
66
  }
77
67
  streamValues(table, limit) {
78
68
  let sql = `SELECT v FROM ${table}`;
@@ -80,16 +70,7 @@ class MySQLKeyValueDB {
80
70
  sql += ` LIMIT ${limit}`;
81
71
  if (this.cfg.logSQL)
82
72
  this.db.cfg.logger.log(`stream: ${sql}`);
83
- return this.db
84
- .pool()
85
- .query(sql)
86
- .stream()
87
- .pipe(new node_stream_1.Transform({
88
- objectMode: true,
89
- transform(row, _, cb) {
90
- cb(null, row.v);
91
- },
92
- }));
73
+ return this.db.pool().query(sql).stream().map(row => row.v);
93
74
  }
94
75
  streamEntries(table, limit) {
95
76
  let sql = `SELECT id,v FROM ${table}`;
@@ -97,16 +78,10 @@ class MySQLKeyValueDB {
97
78
  sql += ` LIMIT ${limit}`;
98
79
  if (this.cfg.logSQL)
99
80
  this.db.cfg.logger.log(`stream: ${sql}`);
100
- return this.db
101
- .pool()
102
- .query(sql)
103
- .stream()
104
- .pipe(new node_stream_1.Transform({
105
- objectMode: true,
106
- transform(row, _, cb) {
107
- cb(null, [row.id, row.v]);
108
- },
109
- }));
81
+ return this.db.pool().query(sql).stream().map(row => [
82
+ row.id,
83
+ row.v,
84
+ ]);
110
85
  }
111
86
  async beginTransaction() {
112
87
  await this.db.runSQL({ sql: `BEGIN TRANSACTION` });
@@ -121,5 +96,8 @@ class MySQLKeyValueDB {
121
96
  const rows = await this.db.runSQL({ sql });
122
97
  return rows[0].cnt;
123
98
  }
99
+ async increment(_table, _id, _by) {
100
+ throw new js_lib_1.AppError('MySQLKeyValueDB.increment() is not implemented');
101
+ }
124
102
  }
125
103
  exports.MySQLKeyValueDB = MySQLKeyValueDB;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dbQueryToSQLUpdate = exports.insertSQLSetSingle = exports.insertSQLSingle = exports.insertSQL = exports.dbQueryToSQLDelete = exports.dbQueryToSQLSelect = void 0;
3
+ exports.dbQueryToSQLSelect = dbQueryToSQLSelect;
4
+ exports.dbQueryToSQLDelete = dbQueryToSQLDelete;
5
+ exports.insertSQL = insertSQL;
6
+ exports.insertSQLSingle = insertSQLSingle;
7
+ exports.insertSQLSetSingle = insertSQLSetSingle;
8
+ exports.dbQueryToSQLUpdate = dbQueryToSQLUpdate;
4
9
  const tslib_1 = require("tslib");
5
10
  const js_lib_1 = require("@naturalcycles/js-lib");
6
11
  const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
@@ -25,7 +30,6 @@ function dbQueryToSQLSelect(q) {
25
30
  tokens.push(...offsetLimitTokens(q));
26
31
  return tokens.join(' ');
27
32
  }
28
- exports.dbQueryToSQLSelect = dbQueryToSQLSelect;
29
33
  /**
30
34
  * Returns null in "0 rows" case.
31
35
  */
@@ -40,7 +44,6 @@ function dbQueryToSQLDelete(q) {
40
44
  tokens.push(...offsetLimitTokens(q));
41
45
  return tokens.join(' ');
42
46
  }
43
- exports.dbQueryToSQLDelete = dbQueryToSQLDelete;
44
47
  /**
45
48
  * Returns array of sql statements to respect the max sql size.
46
49
  */
@@ -89,7 +92,6 @@ function insertSQL(table, rows, verb = 'INSERT', logger = console) {
89
92
  return sqls;
90
93
  // todo: handle "upsert" later
91
94
  }
92
- exports.insertSQL = insertSQL;
93
95
  function insertSQLSingle(table, record) {
94
96
  // INSERT INTO table_name (column1, column2, column3, ...)
95
97
  // VALUES (value1, value2, value3, ...);
@@ -110,14 +112,12 @@ function insertSQLSingle(table, record) {
110
112
  ];
111
113
  return tokens.join(' ');
112
114
  }
113
- exports.insertSQLSingle = insertSQLSingle;
114
115
  function insertSQLSetSingle(table, record) {
115
116
  return {
116
117
  sql: `INSERT INTO ${mysql.escapeId(table)} SET ?`,
117
118
  values: [record],
118
119
  };
119
120
  }
120
- exports.insertSQLSetSingle = insertSQLSetSingle;
121
121
  function dbQueryToSQLUpdate(q, record) {
122
122
  // var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
123
123
  const whereTokens = getWhereTokens(q);
@@ -134,7 +134,6 @@ function dbQueryToSQLUpdate(q, record) {
134
134
  ];
135
135
  return mysql.format(tokens.join(' '), Object.values(record));
136
136
  }
137
- exports.dbQueryToSQLUpdate = dbQueryToSQLUpdate;
138
137
  function selectTokens(q) {
139
138
  let fields = ['*'];
140
139
  if (q._selectedFieldNames) {
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapNameFromMySQL = exports.mapNameToMySQL = exports.mysqlTableStatsToJsonSchemaField = exports.jsonSchemaToMySQLDDL = void 0;
3
+ exports.jsonSchemaToMySQLDDL = jsonSchemaToMySQLDDL;
4
+ exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
5
+ exports.mapNameToMySQL = mapNameToMySQL;
6
+ exports.mapNameFromMySQL = mapNameFromMySQL;
4
7
  const tslib_1 = require("tslib");
5
8
  const mysql = tslib_1.__importStar(require("mysql"));
6
9
  /**
@@ -56,7 +59,6 @@ function jsonSchemaToMySQLDDL(table, schema, opt = {}) {
56
59
  lines.push(`) ENGINE=${engine}`);
57
60
  return lines.join('\n');
58
61
  }
59
- exports.jsonSchemaToMySQLDDL = jsonSchemaToMySQLDDL;
60
62
  function mysqlTableStatsToJsonSchemaField(table, stats, logger) {
61
63
  const s = {
62
64
  $id: `${table}.schema.json`,
@@ -94,7 +96,6 @@ function mysqlTableStatsToJsonSchemaField(table, stats, logger) {
94
96
  });
95
97
  return s;
96
98
  }
97
- exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
98
99
  /**
99
100
  * Because MySQL doesn't support `.` in field names and escapes them as tableName + fieldName.
100
101
  *
@@ -103,8 +104,6 @@ exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
103
104
  function mapNameToMySQL(name) {
104
105
  return name.replaceAll('.', '_dot_');
105
106
  }
106
- exports.mapNameToMySQL = mapNameToMySQL;
107
107
  function mapNameFromMySQL(name) {
108
108
  return name.replaceAll('_dot_', '.');
109
109
  }
110
- exports.mapNameFromMySQL = mapNameFromMySQL;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/mysql-lib",
3
3
  "scripts": {
4
- "prepare": "husky install"
4
+ "prepare": "husky"
5
5
  },
6
6
  "dependencies": {
7
7
  "@naturalcycles/db-lib": "^9.1.0",
@@ -11,7 +11,7 @@
11
11
  "mysql": "^2.17.1"
12
12
  },
13
13
  "devDependencies": {
14
- "@naturalcycles/dev-lib": "^13.2.1",
14
+ "@naturalcycles/dev-lib": "^15.18.0",
15
15
  "@types/node": "^20.2.3",
16
16
  "dotenv": "^16.0.0",
17
17
  "jest": "^29.0.0"
@@ -34,9 +34,9 @@
34
34
  "url": "https://github.com/NaturalCycles/mysql-lib"
35
35
  },
36
36
  "engines": {
37
- "node": ">=16.15.0"
37
+ "node": ">=18.12.0"
38
38
  },
39
- "version": "3.14.7",
39
+ "version": "3.15.0",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Readable, Transform } from 'node:stream'
1
+ import { Readable } from 'node:stream'
2
2
  import { promisify } from 'node:util'
3
3
  import {
4
4
  DBPatch,
@@ -278,17 +278,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
278
278
  if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)
279
279
 
280
280
  // return this.streamSQL(sql, opt)
281
- return this.pool()
282
- .query(sql)
283
- .stream()
284
- .pipe(
285
- new Transform({
286
- objectMode: true,
287
- transform(row, _encoding, cb) {
288
- cb(null, _filterUndefinedValues(row, true))
289
- },
290
- }),
291
- )
281
+ return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
282
+ _filterUndefinedValues(row, true),
283
+ )
292
284
  }
293
285
 
294
286
  // SAVE
@@ -1,6 +1,5 @@
1
- import { Transform } from 'node:stream'
2
1
  import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
3
- import { pMap } from '@naturalcycles/js-lib'
2
+ import { AppError, ObjectWithId, pMap } from '@naturalcycles/js-lib'
4
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
5
4
  import { QueryOptions } from 'mysql'
6
5
  import { MysqlDB, MysqlDBCfg } from './mysql.db'
@@ -78,18 +77,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
78
77
  if (limit) sql += ` LIMIT ${limit}`
79
78
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
80
79
 
81
- return this.db
82
- .pool()
83
- .query(sql)
84
- .stream()
85
- .pipe(
86
- new Transform({
87
- objectMode: true,
88
- transform(row, _, cb) {
89
- cb(null, row.id)
90
- },
91
- }),
92
- )
80
+ return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
93
81
  }
94
82
 
95
83
  streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
@@ -97,18 +85,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
97
85
  if (limit) sql += ` LIMIT ${limit}`
98
86
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
99
87
 
100
- return this.db
101
- .pool()
102
- .query(sql)
103
- .stream()
104
- .pipe(
105
- new Transform({
106
- objectMode: true,
107
- transform(row, _, cb) {
108
- cb(null, row.v)
109
- },
110
- }),
111
- )
88
+ return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: Buffer }>).map(row => row.v)
112
89
  }
113
90
 
114
91
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
@@ -116,18 +93,10 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
116
93
  if (limit) sql += ` LIMIT ${limit}`
117
94
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
118
95
 
119
- return this.db
120
- .pool()
121
- .query(sql)
122
- .stream()
123
- .pipe(
124
- new Transform({
125
- objectMode: true,
126
- transform(row, _, cb) {
127
- cb(null, [row.id, row.v])
128
- },
129
- }),
130
- )
96
+ return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject>).map(row => [
97
+ row.id,
98
+ row.v,
99
+ ])
131
100
  }
132
101
 
133
102
  async beginTransaction(): Promise<void> {
@@ -145,4 +114,8 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
145
114
  const rows = await this.db.runSQL<{ cnt: number }[]>({ sql })
146
115
  return rows[0]!.cnt
147
116
  }
117
+
118
+ async increment(_table: string, _id: string, _by?: number): Promise<number> {
119
+ throw new AppError('MySQLKeyValueDB.increment() is not implemented')
120
+ }
148
121
  }