Skip to content

Commit

Permalink
feat: adapt to db-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Oct 12, 2024
1 parent ab094e9 commit 62ba326
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 286 deletions.
14 changes: 7 additions & 7 deletions src/mysql.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
CommonDBSaveOptions,
CommonDBSupport,
CommonDBType,
DBPatch,
DBQuery,
RunQueryResult,
} from '@naturalcycles/db-lib'
Expand Down Expand Up @@ -96,8 +95,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {

override support: CommonDBSupport = {
...commonDBFullSupport,
updateSaveMethod: false,
transactions: false,
updateSaveMethod: false, // todo: can be implemented
transactions: false, // todo: can be implemented
increment: false, // todo: can be implemented
}

constructor(cfg: MysqlDBCfg = {}) {
Expand Down Expand Up @@ -315,7 +315,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
if (row.id) {
// Update already existing
const query = new DBQuery(table).filterEq('id', row.id)
await this.updateByQuery(query, _omit(row, ['id']))
await this.patchByQuery(query, _omit(row, ['id']))
} else {
// Create new
const sql = insertSQL(table, [row], 'INSERT', this.cfg.logger)[0]!
Expand All @@ -335,7 +335,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
// Update already existing
_assert(row.id, 'id is required for updating')
const query = new DBQuery(table).filterEq('id', row.id)
await this.updateByQuery(query, _omit(row, ['id']))
await this.patchByQuery(query, _omit(row, ['id']))
}
return
}
Expand Down Expand Up @@ -409,9 +409,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
}

override async updateByQuery<ROW extends ObjectWithId>(
override async patchByQuery<ROW extends ObjectWithId>(
q: DBQuery<ROW>,
patch: DBPatch<ROW>,
patch: Partial<ROW>,
): Promise<number> {
const sql = dbQueryToSQLUpdate(q, patch)
if (!sql) return 0
Expand Down
21 changes: 19 additions & 2 deletions src/mysqlKeyValueDB.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
import { AppError, ObjectWithId, pMap } from '@naturalcycles/js-lib'
import {
CommonDBCreateOptions,
CommonKeyValueDB,
commonKeyValueDBFullSupport,
KeyValueDBTuple,
} from '@naturalcycles/db-lib'
import { AppError, ObjectWithId, pMap, StringMap } from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { QueryOptions } from 'mysql'
import { MysqlDB, MysqlDBCfg } from './mysql.db'
Expand All @@ -16,6 +21,11 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {

db: MysqlDB

support = {
...commonKeyValueDBFullSupport,
increment: false,
}

async ping(): Promise<void> {
await this.db.ping()
}
Expand Down Expand Up @@ -118,4 +128,11 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
async increment(_table: string, _id: string, _by?: number): Promise<number> {
throw new AppError('MySQLKeyValueDB.increment() is not implemented')
}

async incrementBatch(
_table: string,
_incrementMap: StringMap<number>,
): Promise<StringMap<number>> {
throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented')
}
}
6 changes: 3 additions & 3 deletions src/query.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function insertSQLSetSingle(table: string, record: Record<any, any>): Que
}
}

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

return mysql.format(tokens.join(' '), Object.values(record))
return mysql.format(tokens.join(' '), Object.values(patch))
}

function selectTokens(q: DBQuery<any>): string[] {
Expand Down
Loading

0 comments on commit 62ba326

Please sign in to comment.