Skip to content

Commit

Permalink
fix: adapt to db-lib without CommonDB.getByIds
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Dec 3, 2022
1 parent 2e5da1e commit 3122fb6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/datastore.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
await this.getAllStats()
}

override async getByIds<ROW extends ObjectWithId>(
async getByIds<ROW extends ObjectWithId>(
table: string,
ids: ROW['id'][],
_opt?: DatastoreDBOptions,
Expand Down Expand Up @@ -176,9 +176,12 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
dbQuery: DBQuery<ROW>,
_opt?: DatastoreDBOptions,
): Promise<RunQueryResult<ROW>> {
if (dbQuery._ids?.length) {
const idFilter = dbQuery._filters.find(f => f.name === 'id')
if (idFilter) {
const ids: string[] = idFilter.op === '==' ? [idFilter.val] : idFilter.val

return {
rows: await this.getByIds(dbQuery.table, dbQuery._ids),
rows: await this.getByIds(dbQuery.table, ids),
}
}

Expand Down Expand Up @@ -295,6 +298,12 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
q: DBQuery<ROW>,
opt?: DatastoreDBOptions,
): Promise<number> {
const idFilter = q._filters.find(f => f.name === 'id')
if (idFilter) {
const ids: string[] = idFilter.op === '==' ? [idFilter.val] : idFilter.val
return await this.deleteByIds(q.table, ids, opt)
}

const datastoreQuery = dbQueryToDatastoreQuery(q.select([]), this.ds().createQuery(q.table))
const { rows } = await this.runDatastoreQuery<ROW>(datastoreQuery)
return await this.deleteByIds(
Expand All @@ -308,7 +317,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
* Limitation: Datastore's delete returns void, so we always return all ids here as "deleted"
* regardless if they were actually deleted or not.
*/
override async deleteByIds<ROW extends ObjectWithId>(
async deleteByIds<ROW extends ObjectWithId>(
table: string,
ids: ROW['id'][],
opt: DatastoreDBOptions = {},
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,9 @@
typescript "^4.0.2"

"@naturalcycles/db-lib@^8.46.1":
version "8.47.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-8.47.0.tgz#dc6d7f3c3aa6d915803074adca72153fe80a9050"
integrity sha512-2ByptyD59MtjcuczofIkb2NpkjgCjUMXEZDEYWDko10Li1UjdFcpoPPOlulF2chhkwoV0VZiaHeDPw3Oyqod9A==
version "8.48.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-8.48.0.tgz#5034c214204ae8e8e261127c6ccdcaacf9eb6ebd"
integrity sha512-APKvpzz5NZMPmE6hgt+wvqgIzB0aAo+AHI9T0/v75wIq8La0piVYIrju2BDktnaFn+6QR19xa0HVYDRoXlnT+w==
dependencies:
"@naturalcycles/js-lib" "^14.116.0"
"@naturalcycles/nodejs-lib" "^12.0.0"
Expand Down Expand Up @@ -2572,9 +2572,9 @@ eslint-visitor-keys@^3.3.0:
integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA==

eslint@^8.0.0:
version "8.28.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.28.0.tgz#81a680732634677cc890134bcdd9fdfea8e63d6e"
integrity sha512-S27Di+EVyMxcHiwDrFzk8dJYAaD+/5SoWKxL1ri/71CRHsnJnRDPNt2Kzj24+MT9FDupf4aqqyqPrvI8MvQ4VQ==
version "8.29.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.29.0.tgz#d74a88a20fb44d59c51851625bc4ee8d0ec43f87"
integrity sha512-isQ4EEiyUjZFbEKvEGJKKGBwXtvXX+zJbkVKCgTuB9t/+jUBcy8avhkEwWJecI15BkRkOYmvIM5ynbhRjEkoeg==
dependencies:
"@eslint/eslintrc" "^1.3.3"
"@humanwhocodes/config-array" "^0.11.6"
Expand Down Expand Up @@ -2769,9 +2769,9 @@ fastest-levenshtein@^1.0.16:
integrity sha512-eRnCtTTtGZFpQCwhJiUOuxPQWRXVKYDn0b2PeHfXL6/Zi53SLAzAHfVhVWK2AryC/WH05kGfxhFIPvTF0SXQzg==

fastq@^1.6.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"
integrity sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==
version "1.14.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.14.0.tgz#107f69d7295b11e0fccc264e1fc6389f623731ce"
integrity sha512-eR2D+V9/ExcbF9ls441yIuN6TI2ED1Y2ZcA5BmMtJsOkWOFRJQ0Jt0g1UwqXJJVAb+V+umH5Dfr8oh4EVP7VVg==
dependencies:
reusify "^1.0.4"

Expand Down

0 comments on commit 3122fb6

Please sign in to comment.