Skip to content

Commit

Permalink
fix: deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jan 17, 2024
1 parent fb17a65 commit cce5ee8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
74 changes: 56 additions & 18 deletions src/datastore.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import {
BaseCommonDB,
CommonDB,
commonDBFullSupport,
CommonDBOptions,
CommonDBSaveMethod,
CommonDBSaveOptions,
CommonDBSupport,
DBQuery,
DBTransaction,
DBTransactionFn,
RunQueryResult,
} from '@naturalcycles/db-lib'
import {
Expand Down Expand Up @@ -132,7 +135,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
override async getByIds<ROW extends ObjectWithId>(
table: string,
ids: string[],
_opt?: DatastoreDBOptions,
opt: DatastoreDBOptions = {},
): Promise<ROW[]> {
if (!ids.length) return []
const keys = ids.map(id => this.key(table, id))
Expand All @@ -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')
Expand All @@ -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 {
Expand Down Expand Up @@ -352,8 +361,16 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
return ids.length
}

override async createTransaction(): Promise<DatastoreDBTransaction> {
return await DatastoreDBTransaction.create(this)
override async runInTransaction(fn: DBTransactionFn): Promise<void> {
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<DatastoreStats[]> {
Expand Down Expand Up @@ -543,10 +560,31 @@ export class DatastoreDBTransaction implements DBTransaction {
return new DatastoreDBTransaction(db, tx)
}

async commit(): Promise<void> {
await this.tx.commit()
}
async rollback(): Promise<void> {
await this.tx.rollback()
}

async getByIds<ROW extends ObjectWithId>(
table: string,
ids: string[],
opt?: CommonDBOptions | undefined,
): Promise<ROW[]> {
return await this.db.getByIds(table, ids, { ...opt, tx: this })
}

async saveBatch<ROW extends Partial<ObjectWithId>>(
table: string,
rows: ROW[],
opt?: CommonDBSaveOptions<ROW> | undefined,
): Promise<void> {
await this.db.saveBatch(table, rows, { ...opt, tx: this })
}

async deleteByIds(
table: string,
ids: string[],
opt?: CommonDBOptions | undefined,
): Promise<number> {
return await this.db.deleteByIds(table, ids, { ...opt, tx: this })
}
}
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit cce5ee8

Please sign in to comment.