Skip to content

Commit

Permalink
fix: adapt to db-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Oct 14, 2024
1 parent 8204d13 commit 114571d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 54 deletions.
47 changes: 18 additions & 29 deletions src/datastoreKeyValueDB.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import {
CommonKeyValueDB,
commonKeyValueDBFullSupport,
DBQuery,
KeyValueDBTuple,
} from '@naturalcycles/db-lib'
import { AppError, StringMap } from '@naturalcycles/js-lib'
import { CommonKeyValueDB, commonKeyValueDBFullSupport, DBQuery } from '@naturalcycles/db-lib'
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB'
import { AppError, KeyValueTuple, ObjectWithId } from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { DatastoreDB } from './datastore.db'
import { DatastoreDBCfg } from './datastore.model'

interface KVObject {
interface KVObject<V> {
id: string
v: Buffer
v: V
}

const excludeFromIndexes: (keyof KVObject)[] = ['v']
const excludeFromIndexes: (keyof KVObject<any>)[] = ['v']

export interface DatastoreKeyValueDBCfg extends DatastoreDBCfg {}

Expand All @@ -36,16 +32,16 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {

async createTable(): Promise<void> {}

async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
return (await this.db.getByIds<KVObject>(table, ids)).map(r => [r.id, r.v])
async getByIds<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]> {
return (await this.db.getByIds<KVObject<V>>(table, ids)).map(r => [r.id, r.v])
}

async deleteByIds(table: string, ids: string[]): Promise<void> {
await this.db.deleteByIds(table, ids)
}

async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
await this.db.saveBatch<KVObject>(
async saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void> {
await this.db.saveBatch<KVObject<V>>(
table,
entries.map(([id, v]) => ({ id, v })),
{
Expand All @@ -55,7 +51,7 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
}

streamIds(table: string, limit?: number): ReadableTyped<string> {
const q = DBQuery.create<KVObject>(table)
const q = DBQuery.create<ObjectWithId>(table)
.select(['id'])
.limit(limit || 0)

Expand All @@ -67,9 +63,9 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
)
}

streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
streamValues<V>(table: string, limit?: number): ReadableTyped<V> {
// `select v` doesn't work for some reason
const q = DBQuery.create<KVObject>(table).limit(limit || 0)
const q = DBQuery.create<KVObject<V>>(table).limit(limit || 0)

return (
this.db
Expand All @@ -79,30 +75,23 @@ export class DatastoreKeyValueDB implements CommonKeyValueDB {
)
}

streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
const q = DBQuery.create<KVObject>(table).limit(limit || 0)
streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>> {
const q = DBQuery.create<KVObject<V>>(table).limit(limit || 0)

return (
this.db
.streamQuery(q)
// .on('error', err => stream.emit('error', err))
.map(r => [r.id, r.v] as KeyValueDBTuple)
.map(r => [r.id, r.v])
)
}

async count(table: string): Promise<number> {
const q = DBQuery.create<KVObject>(table)
const q = DBQuery.create<ObjectWithId>(table)
return await this.db.runQueryCount(q)
}

async increment(_table: string, _id: string, _by?: number): Promise<number> {
throw new AppError('DatastoreKeyValueDB.increment() is not implemented')
}

async incrementBatch(
_table: string,
_incrementMap: StringMap<number>,
): Promise<StringMap<number>> {
async incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]> {
throw new AppError('DatastoreKeyValueDB.incrementBatch() is not implemented')
}
}
10 changes: 2 additions & 8 deletions src/test/datastoreKeyValueDB.manual.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CommonKeyValueDao } from '@naturalcycles/db-lib'
import { runCommonKeyValueDBTest, TEST_TABLE } from '@naturalcycles/db-lib/dist/testing'
import { runCommonKeyValueDBTest } from '@naturalcycles/db-lib/dist/testing'
import { runCommonKeyValueDaoTest } from '@naturalcycles/db-lib/dist/testing/keyValueDaoTest'
import { requireEnvKeys } from '@naturalcycles/nodejs-lib'
import { DatastoreKeyValueDB } from '../datastoreKeyValueDB'
Expand All @@ -16,11 +15,6 @@ const db = new DatastoreKeyValueDB({
credentials,
})

const dao = new CommonKeyValueDao<Buffer>({
db,
table: TEST_TABLE,
})

describe('runCommonKeyValueDBTest', () => runCommonKeyValueDBTest(db))

describe('runCommonKeyValueDaoTest', () => runCommonKeyValueDaoTest(dao))
describe('runCommonKeyValueDaoTest', () => runCommonKeyValueDaoTest(db))
34 changes: 17 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,10 @@
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@es-joy/jsdoccomment@~0.48.0":
version "0.48.0"
resolved "https://registry.yarnpkg.com/@es-joy/jsdoccomment/-/jsdoccomment-0.48.0.tgz#5d9dc1a295cf5d1ed224dffafb4800d5c7206c27"
integrity sha512-G6QUWIcC+KvSwXNsJyDTHvqUdNoAVJPPgkc3+Uk4WBKqZvoXhlvazOgm9aL0HwihJLQf0l+tOE2UFzXBqCqgDw==
"@es-joy/jsdoccomment@~0.49.0":
version "0.49.0"
resolved "https://registry.yarnpkg.com/@es-joy/jsdoccomment/-/jsdoccomment-0.49.0.tgz#e5ec1eda837c802eca67d3b29e577197f14ba1db"
integrity sha512-xjZTSFgECpb9Ohuk5yMX5RhUEbfeQcuOp8IF60e+wyzWEF0M5xeSgqsfLtvPEX8BIyOX9saZqzuGPmZ8oWc+5Q==
dependencies:
comment-parser "1.4.1"
esquery "^1.6.0"
Expand Down Expand Up @@ -1045,17 +1045,17 @@
typescript "^5.0.2"

"@naturalcycles/db-lib@^9.0.0":
version "9.22.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.22.0.tgz#d01153b033e22155ade705aa049fe089978c5798"
integrity sha512-989fWQqlfMrtoaKxzqWN2Eh7Y7MrJcqoq5wl7Uldm84eUe3OUY87H0BYgGr/1kO309l2EuzhEkkEzcuO9QyKJw==
version "9.22.1"
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.22.1.tgz#debfd7d5dda951070441cb27e15fdf1aecd825ff"
integrity sha512-Y5AEdc/0pQKiJM7XHmaVrAB6AW+NZu70QbhGWINS+r/TPLuvnLFfi9KViiNmfByIS7WRNu1D2XNiZr3FbLgceg==
dependencies:
"@naturalcycles/js-lib" "^14.116.0"
"@naturalcycles/nodejs-lib" "^13.1.1"

"@naturalcycles/dev-lib@^15.2.0":
version "15.22.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-15.22.0.tgz#f1a31b5e0483e2d5beaadf8e5bce9345789b9fef"
integrity sha512-QZQEvP2yM27hDATOOHoaNDXXphUwt/dDMbVd7RATQMGoSmjV8QYANJSisWLPAVAee1eF+yQ+t/SUDNrtFoLSXg==
version "15.23.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-15.23.0.tgz#44de477d9f842bc6397a84649809e053b6b9628f"
integrity sha512-m1qbFzgmqDGerZYnDqtqlg6j1wc+MM/f6jKXBNHs4h90bAJtxXVG2pQaGoAaDdrsXHyT3eq6aE9iCZh7DjyIvw==
dependencies:
"@biomejs/biome" "^1.8.3"
"@commitlint/cli" "^19.0.0"
Expand Down Expand Up @@ -2326,11 +2326,11 @@ eslint-plugin-jest@^28.0.0:
"@typescript-eslint/utils" "^6.0.0 || ^7.0.0 || ^8.0.0"

eslint-plugin-jsdoc@^50.0.0:
version "50.3.2"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.3.2.tgz#f9b8b3d70abe7a84e22a3bbadf22a935c6194b4a"
integrity sha512-TjgZocG53N3a84PdCFGqVMWLWwDitOUuKjlJftwTu/iTiD7N/Q2Q3eEy/Q4GfJqpM4rTJCkzUYWQfol6RZNDcA==
version "50.4.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.4.1.tgz#845ddf4c395891babe135023d97a45c750d94c4c"
integrity sha512-OXIq+JJQPCLAKL473/esioFOwbXyRE5MAQ4HbZjcp3e+K3zdxt2uDpGs3FR+WezUXNStzEtTfgx15T+JFrVwBA==
dependencies:
"@es-joy/jsdoccomment" "~0.48.0"
"@es-joy/jsdoccomment" "~0.49.0"
are-docs-informative "^0.0.2"
comment-parser "1.4.1"
debug "^4.3.6"
Expand Down Expand Up @@ -2731,9 +2731,9 @@ get-caller-file@^2.0.5:
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==

get-east-asian-width@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/get-east-asian-width/-/get-east-asian-width-1.2.0.tgz#5e6ebd9baee6fb8b7b6bd505221065f0cd91f64e"
integrity sha512-2nk+7SIVb14QrgXFHcm84tD4bKQz0RxPuMT8Ag5KPOq7J5fEmAg0UbXdTOSHqNuHSU28k55qnceesxXRZGzKWA==
version "1.3.0"
resolved "https://registry.yarnpkg.com/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz#21b4071ee58ed04ee0db653371b55b4299875389"
integrity sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ==

get-package-type@^0.1.0:
version "0.1.0"
Expand Down

0 comments on commit 114571d

Please sign in to comment.