From cce5ee84c7bff66ffe88f0b19852ba6f5034a3d0 Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Wed, 17 Jan 2024 21:25:42 +0100 Subject: [PATCH] fix: deps --- src/datastore.db.ts | 74 ++++++++++++++++++++++++++++++++++----------- yarn.lock | 18 +++++------ 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/datastore.db.ts b/src/datastore.db.ts index 24007af..22fffad 100644 --- a/src/datastore.db.ts +++ b/src/datastore.db.ts @@ -5,10 +5,13 @@ import { BaseCommonDB, CommonDB, commonDBFullSupport, + CommonDBOptions, CommonDBSaveMethod, + CommonDBSaveOptions, CommonDBSupport, DBQuery, DBTransaction, + DBTransactionFn, RunQueryResult, } from '@naturalcycles/db-lib' import { @@ -132,7 +135,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB { override async getByIds( table: string, ids: string[], - _opt?: DatastoreDBOptions, + opt: DatastoreDBOptions = {}, ): Promise { if (!ids.length) return [] const keys = ids.map(id => this.key(table, id)) @@ -141,10 +144,13 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB { if (this.cfg.timeout) { // First try try { - const r = await pTimeout(() => this.ds().get(keys), { - timeout: this.cfg.timeout, - name: `datastore.getByIds(${table})`, - }) + const r = await pTimeout( + () => ((opt.tx as DatastoreDBTransaction)?.tx || this.ds()).get(keys), + { + timeout: this.cfg.timeout, + name: `datastore.getByIds(${table})`, + }, + ) rows = r[0] } catch { this.cfg.logger.log('datastore recreated on error') @@ -155,15 +161,18 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB { this.cachedDatastore = new DS(this.cfg) // Second try (will throw) - const r = await pRetry(() => this.ds().get(keys), { - ...this.getPRetryOptions(`datastore.getByIds(${table}) second try`), - maxAttempts: 3, - timeout: this.cfg.timeout, - errorData: { - // This error will be grouped ACROSS all endpoints and usages - fingerprint: [DATASTORE_TIMEOUT], + const r = await pRetry( + () => ((opt.tx as DatastoreDBTransaction)?.tx || this.ds()).get(keys), + { + ...this.getPRetryOptions(`datastore.getByIds(${table}) second try`), + maxAttempts: 3, + timeout: this.cfg.timeout, + errorData: { + // This error will be grouped ACROSS all endpoints and usages + fingerprint: [DATASTORE_TIMEOUT], + }, }, - }) + ) rows = r[0] } } else { @@ -352,8 +361,16 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB { return ids.length } - override async createTransaction(): Promise { - return await DatastoreDBTransaction.create(this) + override async runInTransaction(fn: DBTransactionFn): Promise { + const tx = await DatastoreDBTransaction.create(this) + + try { + await fn(tx) + await tx.tx.commit() + } catch (err) { + await tx.tx.rollback() + throw err + } } async getAllStats(): Promise { @@ -543,10 +560,31 @@ export class DatastoreDBTransaction implements DBTransaction { return new DatastoreDBTransaction(db, tx) } - async commit(): Promise { - await this.tx.commit() - } async rollback(): Promise { await this.tx.rollback() } + + async getByIds( + table: string, + ids: string[], + opt?: CommonDBOptions | undefined, + ): Promise { + return await this.db.getByIds(table, ids, { ...opt, tx: this }) + } + + async saveBatch>( + table: string, + rows: ROW[], + opt?: CommonDBSaveOptions | undefined, + ): Promise { + await this.db.saveBatch(table, rows, { ...opt, tx: this }) + } + + async deleteByIds( + table: string, + ids: string[], + opt?: CommonDBOptions | undefined, + ): Promise { + return await this.db.deleteByIds(table, ids, { ...opt, tx: this }) + } } diff --git a/yarn.lock b/yarn.lock index 3f93a84..d6eb776 100644 --- a/yarn.lock +++ b/yarn.lock @@ -836,9 +836,9 @@ typescript "^5.0.2" "@naturalcycles/db-lib@^9.0.0": - version "9.0.0" - resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.0.0.tgz#5f3a3e80d1de02d24e1f17213265700ab0a4dbfe" - integrity sha512-+d8AIXPOt9FgxgagNyweilde1CJp2TwK2yf0vw98x4JMgYmycoF0MgZjXvyDICh0hzOn3vj3Arxve0jLFrbduQ== + version "9.1.0" + resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.1.0.tgz#4c2d206301caeb67381713566d1945df91435a1f" + integrity sha512-qAJai7bRWY37+Wo/p8KyxaJ+d1UE2XIgIZoc8dcdYtB3XyEUjz3ZNNA4GeurgtxOBqeD5U0UxcOHbijJaHyzCg== dependencies: "@naturalcycles/js-lib" "^14.116.0" "@naturalcycles/nodejs-lib" "^13.1.1" @@ -2173,9 +2173,9 @@ ecdsa-sig-formatter@1.0.11, ecdsa-sig-formatter@^1.0.11: safe-buffer "^5.0.1" electron-to-chromium@^1.4.601: - version "1.4.635" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.635.tgz#e4e064b8711a98827652ce17cc11b0e0184c40d1" - integrity sha512-iu/2D0zolKU3iDGXXxdOzNf72Jnokn+K1IN6Kk4iV6l1Tr2g/qy+mvmtfAiBwZe5S3aB5r92vp+zSZ69scYRrg== + version "1.4.636" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.636.tgz#302cf4c3016d9d714ba246243a7c97b528e22fe7" + integrity sha512-NLE0GIy1OL9wRiKL20h9TkctBEYZuc99tquSS9MVdTahnuHputoETHeqDzgqGqyOY9NUH0g9wjfEuw5OD+wRcQ== emittery@^0.13.1: version "0.13.1" @@ -4953,9 +4953,9 @@ stream-events@^1.0.5: stubs "^3.0.0" stream-shift@^1.0.0: - version "1.0.2" - resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.2.tgz#548bff71c92322e1ade886979f7f67c0723eb9e4" - integrity sha512-rV4Bovi9xx0BFzOb/X0B2GqoIjvqPCttZdu0Wgtx2Dxkj7ETyWl9gmqJ4EutWRLvtZWm8dxE+InQZX1IryZn/w== + version "1.0.3" + resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.3.tgz#85b8fab4d71010fc3ba8772e8046cc49b8a3864b" + integrity sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ== string-argv@0.3.2: version "0.3.2"