Skip to content

Commit

Permalink
chore: apply lint
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnagydavid committed Sep 30, 2024
1 parent fa99339 commit 132b6b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
8 changes: 2 additions & 6 deletions src/redisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ export class RedisClient implements CommonClient {
await this.redis().set(key, value, 'EXAT', expireAt)
}

async hsetWithTTL(
key: string,
value: AnyObject,
expireAt: UnixTimestampNumber
): Promise<void> {
async hsetWithTTL(key: string, value: AnyObject, expireAt: UnixTimestampNumber): Promise<void> {
const valueKeys = Object.keys(value)
const numberOfKeys = valueKeys.length
const keyList = valueKeys.join(' ')
Expand All @@ -178,7 +174,7 @@ export class RedisClient implements CommonClient {
await this.redis().hset(key, value)
await this.redis().call(command!, args)
}

async mset(obj: Record<string, string | number>): Promise<void> {
await this.redis().mset(obj)
}
Expand Down
55 changes: 18 additions & 37 deletions src/redisHashKeyValueDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import {
CommonKeyValueDB,
KeyValueDBTuple,
} from '@naturalcycles/db-lib'
import {
_chunk,
_isTruthy,
_mapObject,
_stringMapEntries,
_zip,
StringMap,
} from '@naturalcycles/js-lib'
import { _chunk, _mapObject, StringMap } from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { RedisClient } from './redisClient'
import { RedisKeyValueDBCfg } from './redisKeyValueDB'
Expand Down Expand Up @@ -40,13 +33,8 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
if (!ids.length) return []
// we assume that the order of returned values is the same as order of input ids
const bufs = await this.client.hmgetBuffer(
this.keyOfHashField,
this.idsToKeys(table, ids)
)
return bufs
.map((buf, i) => [ids[i], buf] as KeyValueDBTuple)
.filter(([_k, v]) => v !== null)
const bufs = await this.client.hmgetBuffer(this.keyOfHashField, this.idsToKeys(table, ids))
return bufs.map((buf, i) => [ids[i], buf] as KeyValueDBTuple).filter(([_k, v]) => v !== null)
}

async deleteByIds(table: string, ids: string[]): Promise<void> {
Expand All @@ -57,14 +45,14 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
async saveBatch(
table: string,
entries: KeyValueDBTuple[],
opt?: CommonKeyValueDBSaveBatchOptions
opt?: CommonKeyValueDBSaveBatchOptions,
): Promise<void> {
if (!entries.length) return

const map: StringMap<any> = _mapObject(
Object.fromEntries(entries),
(k, v) => [this.idToKey(table, String(k)), v]
)
const map: StringMap<any> = _mapObject(Object.fromEntries(entries), (k, v) => [
this.idToKey(table, String(k)),
v,
])

if (opt?.expireAt) {
await this.client.hsetWithTTL(this.keyOfHashField, map, opt.expireAt)
Expand All @@ -78,7 +66,7 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
.hscanStream(this.keyOfHashField, {
match: `${table}:*`,
})
.flatMap((keyValueList) => {
.flatMap(keyValueList => {
const keys: string[] = []
keyValueList.forEach((keyOrValue, index) => {
if (index % 2 !== 0) return
Expand All @@ -100,31 +88,28 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
match: `${table}:*`,
})
.flatMap(
async (keyValueList) => {
async keyValueList => {
const values: string[] = []
keyValueList.forEach((keyOrValue, index) => {
if (index % 2 !== 1) return
values.push(keyOrValue)
})
return values.map((v) => Buffer.from(v))
return values.map(v => Buffer.from(v))
},
{
concurrency: 16,
}
},
)
.take(limit || Infinity)
}

streamEntries(
table: string,
limit?: number | undefined
): ReadableTyped<KeyValueDBTuple> {
streamEntries(table: string, limit?: number | undefined): ReadableTyped<KeyValueDBTuple> {
return this.client
.hscanStream(this.keyOfHashField, {
match: `${table}:*`,
})
.flatMap(
async (keyValueList) => {
async keyValueList => {
const entries = _chunk(keyValueList, 2)
return entries.map(([k, v]) => {
return [
Expand All @@ -135,7 +120,7 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
},
{
concurrency: 16,
}
},
)
.take(limit || Infinity)
}
Expand All @@ -147,11 +132,7 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
}

async increment(table: string, id: string, by: number = 1): Promise<number> {
return await this.client.hincr(
this.keyOfHashField,
this.idToKey(table, id),
by
)
return await this.client.hincr(this.keyOfHashField, this.idToKey(table, id), by)
}

async createTable(table: string, opt?: CommonDBCreateOptions): Promise<void> {
Expand All @@ -161,15 +142,15 @@ export class RedisHashKeyValueDB implements CommonKeyValueDB, AsyncDisposable {
}

private idsToKeys(table: string, ids: string[]): string[] {
return ids.map((id) => this.idToKey(table, id))
return ids.map(id => this.idToKey(table, id))
}

private idToKey(table: string, id: string): string {
return `${table}:${id}`
}

private keysToIds(table: string, keys: string[]): string[] {
return keys.map((key) => this.keyToId(table, key))
return keys.map(key => this.keyToId(table, key))
}

private keyToId(table: string, key: string): string {
Expand Down

0 comments on commit 132b6b0

Please sign in to comment.