Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 62ba326

Browse files
feat: adapt to db-lib
1 parent ab094e9 commit 62ba326

File tree

4 files changed

+292
-286
lines changed

4 files changed

+292
-286
lines changed

src/mysql.db.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
CommonDBSaveOptions,
1010
CommonDBSupport,
1111
CommonDBType,
12-
DBPatch,
1312
DBQuery,
1413
RunQueryResult,
1514
} from '@naturalcycles/db-lib'
@@ -96,8 +95,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
9695

9796
override support: CommonDBSupport = {
9897
...commonDBFullSupport,
99-
updateSaveMethod: false,
100-
transactions: false,
98+
updateSaveMethod: false, // todo: can be implemented
99+
transactions: false, // todo: can be implemented
100+
increment: false, // todo: can be implemented
101101
}
102102

103103
constructor(cfg: MysqlDBCfg = {}) {
@@ -315,7 +315,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
315315
if (row.id) {
316316
// Update already existing
317317
const query = new DBQuery(table).filterEq('id', row.id)
318-
await this.updateByQuery(query, _omit(row, ['id']))
318+
await this.patchByQuery(query, _omit(row, ['id']))
319319
} else {
320320
// Create new
321321
const sql = insertSQL(table, [row], 'INSERT', this.cfg.logger)[0]!
@@ -335,7 +335,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
335335
// Update already existing
336336
_assert(row.id, 'id is required for updating')
337337
const query = new DBQuery(table).filterEq('id', row.id)
338-
await this.updateByQuery(query, _omit(row, ['id']))
338+
await this.patchByQuery(query, _omit(row, ['id']))
339339
}
340340
return
341341
}
@@ -409,9 +409,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
409409
return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
410410
}
411411

412-
override async updateByQuery<ROW extends ObjectWithId>(
412+
override async patchByQuery<ROW extends ObjectWithId>(
413413
q: DBQuery<ROW>,
414-
patch: DBPatch<ROW>,
414+
patch: Partial<ROW>,
415415
): Promise<number> {
416416
const sql = dbQueryToSQLUpdate(q, patch)
417417
if (!sql) return 0

src/mysqlKeyValueDB.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
2-
import { AppError, ObjectWithId, pMap } from '@naturalcycles/js-lib'
1+
import {
2+
CommonDBCreateOptions,
3+
CommonKeyValueDB,
4+
commonKeyValueDBFullSupport,
5+
KeyValueDBTuple,
6+
} from '@naturalcycles/db-lib'
7+
import { AppError, ObjectWithId, pMap, StringMap } from '@naturalcycles/js-lib'
38
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
49
import { QueryOptions } from 'mysql'
510
import { MysqlDB, MysqlDBCfg } from './mysql.db'
@@ -16,6 +21,11 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
1621

1722
db: MysqlDB
1823

24+
support = {
25+
...commonKeyValueDBFullSupport,
26+
increment: false,
27+
}
28+
1929
async ping(): Promise<void> {
2030
await this.db.ping()
2131
}
@@ -118,4 +128,11 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
118128
async increment(_table: string, _id: string, _by?: number): Promise<number> {
119129
throw new AppError('MySQLKeyValueDB.increment() is not implemented')
120130
}
131+
132+
async incrementBatch(
133+
_table: string,
134+
_incrementMap: StringMap<number>,
135+
): Promise<StringMap<number>> {
136+
throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented')
137+
}
121138
}

src/query.util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function insertSQLSetSingle(table: string, record: Record<any, any>): Que
142142
}
143143
}
144144

145-
export function dbQueryToSQLUpdate(q: DBQuery<any>, record: Record<any, any>): string | null {
145+
export function dbQueryToSQLUpdate(q: DBQuery<any>, patch: Record<any, any>): string | null {
146146
// var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
147147
const whereTokens = getWhereTokens(q)
148148
if (!whereTokens) return null
@@ -151,13 +151,13 @@ export function dbQueryToSQLUpdate(q: DBQuery<any>, record: Record<any, any>): s
151151
`UPDATE`,
152152
mysql.escapeId(q.table),
153153
`SET`,
154-
Object.keys(record)
154+
Object.keys(patch)
155155
.map(f => mysql.escapeId(mapNameToMySQL(f)) + ' = ?')
156156
.join(', '),
157157
...whereTokens,
158158
]
159159

160-
return mysql.format(tokens.join(' '), Object.values(record))
160+
return mysql.format(tokens.join(' '), Object.values(patch))
161161
}
162162

163163
function selectTokens(q: DBQuery<any>): string[] {

0 commit comments

Comments
 (0)