@naturalcycles/mysql-lib 4.7.0 → 4.8.1
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.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, RunQueryResult } from '@naturalcycles/db-lib';
|
|
2
2
|
import { BaseCommonDB, DBQuery } from '@naturalcycles/db-lib';
|
|
3
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
4
3
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
5
4
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
5
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
6
6
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
7
7
|
import type { Connection, Pool, PoolConfig, PoolConnection, QueryOptions } from 'mysql';
|
|
8
8
|
export interface MysqlDBOptions extends CommonDBOptions {
|
|
@@ -68,8 +68,8 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
68
68
|
/**
|
|
69
69
|
* dropIfExists=true needed as a safety check
|
|
70
70
|
*/
|
|
71
|
-
createTable<ROW extends ObjectWithId>(table: string, schema:
|
|
71
|
+
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchema<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
|
|
72
72
|
getTables(): Promise<string[]>;
|
|
73
|
-
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<
|
|
73
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>>;
|
|
74
74
|
patchByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: Partial<ROW>): Promise<number>;
|
|
75
75
|
}
|
package/dist/query.util.js
CHANGED
|
@@ -41,7 +41,7 @@ export function dbQueryToSQLDelete(q) {
|
|
|
41
41
|
export function insertSQL(table, rows, verb = 'INSERT', logger = console) {
|
|
42
42
|
// INSERT INTO table_name (column1, column2, column3, ...)
|
|
43
43
|
// VALUES (value1, value2, value3, ...);
|
|
44
|
-
//
|
|
44
|
+
// oxlint-disable-next-line unicorn/no-array-reduce
|
|
45
45
|
const fieldSet = rows.reduce((set, row) => {
|
|
46
46
|
for (const field of Object.keys(row)) {
|
|
47
47
|
set.add(field);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
2
1
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
3
2
|
import type { AnyObject } from '@naturalcycles/js-lib/types';
|
|
3
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
4
4
|
export interface MySQLTableStats {
|
|
5
5
|
Field: string;
|
|
6
6
|
Type: string;
|
|
@@ -15,8 +15,8 @@ export interface MySQLSchemaOptions {
|
|
|
15
15
|
/**
|
|
16
16
|
* It currently skips nullability and declares everything as "DEFAULT NULL".
|
|
17
17
|
*/
|
|
18
|
-
export declare function jsonSchemaToMySQLDDL(table: string, schema:
|
|
19
|
-
export declare function mysqlTableStatsToJsonSchemaField<T extends AnyObject = any>(table: string, stats: MySQLTableStats[], logger: CommonLogger):
|
|
18
|
+
export declare function jsonSchemaToMySQLDDL(table: string, schema: JsonSchema<any>, opt?: MySQLSchemaOptions): string;
|
|
19
|
+
export declare function mysqlTableStatsToJsonSchemaField<T extends AnyObject = any>(table: string, stats: MySQLTableStats[], logger: CommonLogger): JsonSchema<T>;
|
|
20
20
|
/**
|
|
21
21
|
* Because MySQL doesn't support `.` in field names and escapes them as tableName + fieldName.
|
|
22
22
|
*
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@types/node": "^24",
|
|
14
14
|
"dotenv": "^17",
|
|
15
|
-
"@naturalcycles/dev-lib": "
|
|
15
|
+
"@naturalcycles/dev-lib": "18.4.2"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": "./dist/index.js"
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"directory": "packages/mysql-lib"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
|
-
"node": ">=
|
|
39
|
+
"node": ">=24.10.0"
|
|
40
40
|
},
|
|
41
|
-
"version": "4.
|
|
41
|
+
"version": "4.8.1",
|
|
42
42
|
"description": "MySQL client implementing CommonDB interface",
|
|
43
43
|
"keywords": [
|
|
44
44
|
"mysql",
|
package/src/mysql.db.ts
CHANGED
|
@@ -10,11 +10,11 @@ import type {
|
|
|
10
10
|
import { BaseCommonDB, commonDBFullSupport, CommonDBType, DBQuery } from '@naturalcycles/db-lib'
|
|
11
11
|
import { _Memo } from '@naturalcycles/js-lib/decorators/memo.decorator.js'
|
|
12
12
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
13
|
-
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema'
|
|
14
13
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
15
14
|
import { commonLoggerPrefix } from '@naturalcycles/js-lib/log'
|
|
16
15
|
import { _filterUndefinedValues, _mapKeys, _mapValues, _omit } from '@naturalcycles/js-lib/object'
|
|
17
16
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types'
|
|
17
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
18
18
|
import { white } from '@naturalcycles/nodejs-lib/colors'
|
|
19
19
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
20
20
|
import type {
|
|
@@ -386,7 +386,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
386
386
|
*/
|
|
387
387
|
override async createTable<ROW extends ObjectWithId>(
|
|
388
388
|
table: string,
|
|
389
|
-
schema:
|
|
389
|
+
schema: JsonSchema<ROW>,
|
|
390
390
|
opt: CommonDBCreateOptions = {},
|
|
391
391
|
): Promise<void> {
|
|
392
392
|
if (opt.dropIfExists) await this.dropTable(table)
|
|
@@ -401,9 +401,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
401
401
|
.filter(Boolean)
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
-
override async getTableSchema<ROW extends ObjectWithId>(
|
|
405
|
-
table: string,
|
|
406
|
-
): Promise<JsonSchemaRootObject<ROW>> {
|
|
404
|
+
override async getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchema<ROW>> {
|
|
407
405
|
const stats = await this.runSQL<MySQLTableStats[]>({
|
|
408
406
|
sql: `describe ${mysql.escapeId(table)}`,
|
|
409
407
|
})
|
package/src/query.util.ts
CHANGED
|
@@ -60,7 +60,7 @@ export function insertSQL(
|
|
|
60
60
|
// INSERT INTO table_name (column1, column2, column3, ...)
|
|
61
61
|
// VALUES (value1, value2, value3, ...);
|
|
62
62
|
|
|
63
|
-
//
|
|
63
|
+
// oxlint-disable-next-line unicorn/no-array-reduce
|
|
64
64
|
const fieldSet = rows.reduce((set: Set<string>, row) => {
|
|
65
65
|
for (const field of Object.keys(row)) {
|
|
66
66
|
set.add(field)
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
JsonSchemaBoolean,
|
|
3
|
-
JsonSchemaNumber,
|
|
4
|
-
JsonSchemaObject,
|
|
5
|
-
JsonSchemaRootObject,
|
|
6
|
-
JsonSchemaString,
|
|
7
|
-
} from '@naturalcycles/js-lib/json-schema'
|
|
8
1
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
9
2
|
import type { AnyObject } from '@naturalcycles/js-lib/types'
|
|
3
|
+
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
10
4
|
import * as mysql from 'mysql'
|
|
11
5
|
|
|
12
6
|
export interface MySQLTableStats {
|
|
@@ -27,14 +21,14 @@ export interface MySQLSchemaOptions {
|
|
|
27
21
|
*/
|
|
28
22
|
export function jsonSchemaToMySQLDDL(
|
|
29
23
|
table: string,
|
|
30
|
-
schema:
|
|
24
|
+
schema: JsonSchema<any>,
|
|
31
25
|
opt: MySQLSchemaOptions = {},
|
|
32
26
|
): string {
|
|
33
27
|
const { engine = 'InnoDB' } = opt
|
|
34
28
|
|
|
35
29
|
const lines: string[] = [`CREATE TABLE ${mysql.escapeId(table)} (`]
|
|
36
30
|
|
|
37
|
-
const innerLines = Object.entries(schema.properties).map(([k, s]) => {
|
|
31
|
+
const innerLines = Object.entries(schema.properties!).map(([k, s]) => {
|
|
38
32
|
if (k === 'id') {
|
|
39
33
|
return `id VARCHAR(255) NOT NULL`
|
|
40
34
|
}
|
|
@@ -47,7 +41,7 @@ export function jsonSchemaToMySQLDDL(
|
|
|
47
41
|
} else if (s.type === 'integer') {
|
|
48
42
|
type = 'INT(11)'
|
|
49
43
|
} else if (s.type === 'number') {
|
|
50
|
-
if (['unixTimestamp', 'int32'].includes((s as
|
|
44
|
+
if (['unixTimestamp', 'int32'].includes((s as JsonSchema<number>).format!)) {
|
|
51
45
|
type = 'INT(11)'
|
|
52
46
|
} else {
|
|
53
47
|
type = 'FLOAT(11)'
|
|
@@ -84,8 +78,8 @@ export function mysqlTableStatsToJsonSchemaField<T extends AnyObject = any>(
|
|
|
84
78
|
table: string,
|
|
85
79
|
stats: MySQLTableStats[],
|
|
86
80
|
logger: CommonLogger,
|
|
87
|
-
):
|
|
88
|
-
const s:
|
|
81
|
+
): JsonSchema<T> {
|
|
82
|
+
const s: JsonSchema<T> = {
|
|
89
83
|
$id: `${table}.schema.json`,
|
|
90
84
|
type: 'object',
|
|
91
85
|
properties: {} as any,
|
|
@@ -98,19 +92,19 @@ export function mysqlTableStatsToJsonSchemaField<T extends AnyObject = any>(
|
|
|
98
92
|
const t = stat.Type.toLowerCase()
|
|
99
93
|
const notNull = stat.Null?.toUpperCase() !== 'YES'
|
|
100
94
|
if (notNull) {
|
|
101
|
-
s.required
|
|
95
|
+
s.required!.push(name as any)
|
|
102
96
|
}
|
|
103
97
|
|
|
104
98
|
if (t.includes('text') || t.includes('char')) {
|
|
105
|
-
s.properties[name] = { type: 'string' } as
|
|
99
|
+
s.properties![name] = { type: 'string' } as JsonSchema<T[keyof T]>
|
|
106
100
|
} else if (t.includes('lob')) {
|
|
107
|
-
s.properties[name] = { instanceof: 'Buffer' }
|
|
101
|
+
s.properties![name] = { instanceof: 'Buffer' }
|
|
108
102
|
} else if (t.startsWith('tinyint') || t.includes('(1)')) {
|
|
109
|
-
s.properties[name] = { type: 'boolean' } as
|
|
103
|
+
s.properties![name] = { type: 'boolean' } as JsonSchema<T[keyof T]>
|
|
110
104
|
} else if (t === 'int' || t.startsWith('int(')) {
|
|
111
|
-
s.properties[name] = { type: 'integer' } as
|
|
105
|
+
s.properties![name] = { type: 'integer' } as JsonSchema<T[keyof T]>
|
|
112
106
|
} else if (t.startsWith('float')) {
|
|
113
|
-
s.properties[name] = { type: 'number' } as
|
|
107
|
+
s.properties![name] = { type: 'number' } as JsonSchema<T[keyof T]>
|
|
114
108
|
} else {
|
|
115
109
|
logger.log(s)
|
|
116
110
|
throw new Error(`Unknown mysql field type ${name as string} ${stat.Type}`)
|