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 18, 2024
1 parent 114571d commit 426ef20
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/datastore.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {

await pMap(
_chunk(keys, MAX_ITEMS),
// eslint-disable-next-line @typescript-eslint/return-await

async batch => await ((opt.tx as DatastoreDBTransaction)?.tx || this.ds()).delete(batch),
{
concurrency: DATASTORE_RECOMMENDED_CONCURRENCY,
Expand Down
31 changes: 18 additions & 13 deletions src/datastoreKeyValueDB.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { CommonKeyValueDB, commonKeyValueDBFullSupport, DBQuery } from '@naturalcycles/db-lib'
import {
CommonKeyValueDB,
commonKeyValueDBFullSupport,
DBQuery,
KeyValueDBTuple,
} from '@naturalcycles/db-lib'
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB'
import { AppError, KeyValueTuple, ObjectWithId } from '@naturalcycles/js-lib'
import { AppError, ObjectWithId } from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { DatastoreDB } from './datastore.db'
import { DatastoreDBCfg } from './datastore.model'

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

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

export interface DatastoreKeyValueDBCfg extends DatastoreDBCfg {}

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

async createTable(): Promise<void> {}

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 getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
return (await this.db.getByIds<KVObject>(table, ids)).map(r => [r.id, r.v])
}

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

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

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

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

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

return (
this.db
Expand Down
Loading

0 comments on commit 426ef20

Please sign in to comment.