mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
types: add types for database abstration layer (#10762)
* types: add types for database abstration layer Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * types: fix more type dependent return value cases Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * types: make INodeBBDatabaseBackend implement the five major interface set Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com> * update types * update type names * add reverse for options in processSortedSet * add getSortedSetMembersWithScores and getSortedSetsMembersWithScores --------- Signed-off-by: steve <29133953+stevefan1999-personal@users.noreply.github.com>
This commit is contained in:
56
types/database/hash.d.ts
vendored
Normal file
56
types/database/hash.d.ts
vendored
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
export interface Hash {
|
||||||
|
decrObjectField(
|
||||||
|
key: string | string[],
|
||||||
|
field: string,
|
||||||
|
): Promise<number | number[]>
|
||||||
|
|
||||||
|
deleteObjectField(key: string, field: string): Promise<void>
|
||||||
|
|
||||||
|
deleteObjectFields(key: string, fields: string[]): Promise<void>
|
||||||
|
|
||||||
|
getObject(key: string, fields: string[]): Promise<object>
|
||||||
|
|
||||||
|
getObjectField(key: string, field: string): Promise<any>
|
||||||
|
|
||||||
|
getObjectFields(key: string, fields: string[]): Promise<Record<string, any>>
|
||||||
|
|
||||||
|
getObjectKeys(key: string): Promise<string[]>
|
||||||
|
|
||||||
|
getObjectValues(key: string): Promise<any[]>
|
||||||
|
|
||||||
|
getObjects(keys: string[], fields: string[]): Promise<any[]>
|
||||||
|
|
||||||
|
getObjectsFields(
|
||||||
|
keys: string[],
|
||||||
|
fields: string[],
|
||||||
|
): Promise<Record<string, any>[]>
|
||||||
|
|
||||||
|
incrObjectField(
|
||||||
|
key: string | string[],
|
||||||
|
field: string,
|
||||||
|
): Promise<number | number[]>
|
||||||
|
|
||||||
|
incrObjectFieldBy(
|
||||||
|
key: string | string[],
|
||||||
|
field: string,
|
||||||
|
value: number,
|
||||||
|
): Promise<number | number[]>
|
||||||
|
|
||||||
|
incrObjectFieldByBulk(
|
||||||
|
data: [key: string, batch: Record<string, number>][],
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
isObjectField(key: string, field: string): Promise<boolean>
|
||||||
|
|
||||||
|
isObjectFields(key: string, fields: string[]): Promise<boolean[]>
|
||||||
|
|
||||||
|
setObject(key: string | string[], data: Record<string, any>): Promise<void>
|
||||||
|
|
||||||
|
setObjectBulk(args: [key: string, data: Record<string, any>][]): Promise<void>
|
||||||
|
|
||||||
|
setObjectField(
|
||||||
|
key: string | string[],
|
||||||
|
field: string,
|
||||||
|
value: any,
|
||||||
|
): Promise<void>
|
||||||
|
}
|
||||||
54
types/database/index.d.ts
vendored
Normal file
54
types/database/index.d.ts
vendored
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import { Store } from 'express-session'
|
||||||
|
|
||||||
|
export { Hash } from './hash'
|
||||||
|
export { List } from './list'
|
||||||
|
export { Set } from './set'
|
||||||
|
export { Item } from './string'
|
||||||
|
export {
|
||||||
|
SortedSet,
|
||||||
|
SortedSetTheoryOperation,
|
||||||
|
SortedSetScanBaseParameters,
|
||||||
|
} from './zset'
|
||||||
|
|
||||||
|
export interface Database {
|
||||||
|
checkCompatibility(callback: () => void): Promise<void>
|
||||||
|
|
||||||
|
checkCompatibilityVersion(
|
||||||
|
version: string,
|
||||||
|
callback: () => void,
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
close(): Promise<void>
|
||||||
|
|
||||||
|
createIndices(callback: () => void): Promise<void>
|
||||||
|
|
||||||
|
createSessionStore(options: any): Promise<Store>
|
||||||
|
|
||||||
|
emptydb(): Promise<void>
|
||||||
|
|
||||||
|
flushdb(): Promise<void>
|
||||||
|
|
||||||
|
info(db: any): Promise<any>
|
||||||
|
|
||||||
|
init(): Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RedisStyleMatchString =
|
||||||
|
| string
|
||||||
|
| `*${string}`
|
||||||
|
| `${string}*`
|
||||||
|
| `*${string}*`
|
||||||
|
export type RedisStyleRangeString = `${'(' | '['}${string}` | `${string}`
|
||||||
|
|
||||||
|
export enum ObjectType {
|
||||||
|
HASH = 'hash',
|
||||||
|
LIST = 'list',
|
||||||
|
SET = 'set',
|
||||||
|
STRING = 'string',
|
||||||
|
SORTED_SET = 'zset',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ValueAndScore = { value: string; score: number }
|
||||||
|
export type RedisStyleAggregate = 'SUM' | 'MIN' | 'MAX'
|
||||||
|
export type NumberTowardsMinima = number | '-inf'
|
||||||
|
export type NumberTowardsMaxima = number | '+inf'
|
||||||
15
types/database/list.d.ts
vendored
Normal file
15
types/database/list.d.ts
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
export interface List {
|
||||||
|
listPrepend(key: string, value: string): Promise<void>
|
||||||
|
|
||||||
|
listAppend(key: string, value: string): Promise<void>
|
||||||
|
|
||||||
|
listRemoveLast(key: string): Promise<string | null>
|
||||||
|
|
||||||
|
listRemoveAll(key: string, value: string | string[]): Promise<void>
|
||||||
|
|
||||||
|
listTrim(key: string, start: number, stop: number): Promise<void>
|
||||||
|
|
||||||
|
getListRange(key: string, start: number, stop: number): Promise<string[]>
|
||||||
|
|
||||||
|
listLength(key: string): Promise<number>
|
||||||
|
}
|
||||||
25
types/database/set.d.ts
vendored
Normal file
25
types/database/set.d.ts
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export interface Set {
|
||||||
|
getSetMembers(key: string): Promise<string[]>
|
||||||
|
|
||||||
|
getSetsMembers(keys: string[]): Promise<string[][]>
|
||||||
|
|
||||||
|
isMemberOfSets(sets: string[], value: string): Promise<boolean[]>
|
||||||
|
|
||||||
|
isSetMember(key: string, value: string): Promise<boolean>
|
||||||
|
|
||||||
|
isSetMembers(key: string, values: string[]): Promise<boolean[]>
|
||||||
|
|
||||||
|
setAdd(key: string, value: string | string[]): Promise<void>
|
||||||
|
|
||||||
|
setCount(key: string): Promise<number>
|
||||||
|
|
||||||
|
setRemove(key: string | string[], value: string | string[]): Promise<void>
|
||||||
|
|
||||||
|
setRemoveRandom(key: string): Promise<string>
|
||||||
|
|
||||||
|
setsAdd(keys: string[], value: string | string[]): Promise<void>
|
||||||
|
|
||||||
|
setsCount(keys: string[]): Promise<number[]>
|
||||||
|
|
||||||
|
setsRemove(keys: string[], value: string): Promise<void>
|
||||||
|
}
|
||||||
35
types/database/string.d.ts
vendored
Normal file
35
types/database/string.d.ts
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import { ObjectType, RedisStyleMatchString } from './index'
|
||||||
|
|
||||||
|
export interface Item {
|
||||||
|
delete(key: string): Promise<void>
|
||||||
|
|
||||||
|
deleteAll(keys: string[]): Promise<void>
|
||||||
|
|
||||||
|
exists(key: string): Promise<boolean>
|
||||||
|
|
||||||
|
exists(key: string[]): Promise<boolean[]>
|
||||||
|
|
||||||
|
expire(key: string, seconds: number): Promise<void>
|
||||||
|
|
||||||
|
expireAt(key: string, timestampInSeconds: number): Promise<void>
|
||||||
|
|
||||||
|
get(key: string): Promise<string | null>
|
||||||
|
|
||||||
|
increment(key: string): Promise<number>
|
||||||
|
|
||||||
|
pexpire(key: string, ms: number): Promise<void>
|
||||||
|
|
||||||
|
pexpireAt(key: string, timestampInMs: number): Promise<void>
|
||||||
|
|
||||||
|
pttl(key: string): Promise<number>
|
||||||
|
|
||||||
|
rename(oldkey: string, newkey: string): Promise<void>
|
||||||
|
|
||||||
|
scan(params: { match: RedisStyleMatchString }): Promise<string[]>
|
||||||
|
|
||||||
|
set(key: string, value: string): Promise<void>
|
||||||
|
|
||||||
|
ttl(key: string): Promise<number>
|
||||||
|
|
||||||
|
type(key: string): Promise<ObjectType | null>
|
||||||
|
}
|
||||||
245
types/database/zset.d.ts
vendored
Normal file
245
types/database/zset.d.ts
vendored
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
import {
|
||||||
|
NumberTowardsMaxima,
|
||||||
|
NumberTowardsMinima,
|
||||||
|
RedisStyleAggregate,
|
||||||
|
RedisStyleMatchString,
|
||||||
|
RedisStyleRangeString,
|
||||||
|
ValueAndScore,
|
||||||
|
} from './index'
|
||||||
|
|
||||||
|
export type SortedSetTheoryOperation = {
|
||||||
|
sets: string[]
|
||||||
|
sort?: 'ASC' | 'DESC'
|
||||||
|
start?: number
|
||||||
|
stop?: number
|
||||||
|
weights?: number[]
|
||||||
|
aggregate?: RedisStyleAggregate
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SortedSetScanBaseParameters = {
|
||||||
|
key: string
|
||||||
|
match: RedisStyleMatchString
|
||||||
|
limit?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SortedSet {
|
||||||
|
getSortedSetIntersect(
|
||||||
|
params: SortedSetTheoryOperation & { withScores: true },
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetIntersect(
|
||||||
|
params: SortedSetTheoryOperation & { withScores?: false },
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetMembers(key: string): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetMembersWithScores(key: string): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetRange(
|
||||||
|
key: string | string[],
|
||||||
|
start: number,
|
||||||
|
stop: number,
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRangeByLex(
|
||||||
|
key: string | string[],
|
||||||
|
min: RedisStyleRangeString | '-',
|
||||||
|
max: RedisStyleRangeString | '+',
|
||||||
|
start?: number,
|
||||||
|
count?: number,
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRangeByScore(
|
||||||
|
key: string | string[],
|
||||||
|
start: number,
|
||||||
|
count: number,
|
||||||
|
min: NumberTowardsMinima,
|
||||||
|
max: NumberTowardsMaxima,
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRangeByScoreWithScores(
|
||||||
|
key: string | string[],
|
||||||
|
start: number,
|
||||||
|
count: number,
|
||||||
|
min: NumberTowardsMinima,
|
||||||
|
max: NumberTowardsMaxima,
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetRangeWithScores(
|
||||||
|
key: string | string[],
|
||||||
|
start: number,
|
||||||
|
stop: number,
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetRevIntersect(
|
||||||
|
params: SortedSetTheoryOperation & { withScores: true },
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetRevIntersect(
|
||||||
|
params: SortedSetTheoryOperation & { withScores?: false },
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRevRange(
|
||||||
|
key: string | string[],
|
||||||
|
start: number,
|
||||||
|
stop: number,
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRevRangeByLex(
|
||||||
|
key: string,
|
||||||
|
max: RedisStyleRangeString | '+',
|
||||||
|
min: RedisStyleRangeString | '-',
|
||||||
|
start?: number,
|
||||||
|
count?: number,
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRevRangeByScore(
|
||||||
|
key: string,
|
||||||
|
start: number,
|
||||||
|
count: number,
|
||||||
|
max: NumberTowardsMaxima | '+',
|
||||||
|
min: NumberTowardsMinima | '-',
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRevRangeByScoreWithScores(
|
||||||
|
key: string,
|
||||||
|
start: number,
|
||||||
|
count: number,
|
||||||
|
max: NumberTowardsMaxima,
|
||||||
|
min: NumberTowardsMinima,
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetRevRangeWithScores(
|
||||||
|
key: string,
|
||||||
|
start: number,
|
||||||
|
stop: number,
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetRevUnion(
|
||||||
|
params: SortedSetTheoryOperation & { withScores?: false },
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetRevUnion(
|
||||||
|
params: SortedSetTheoryOperation & { withScores: true },
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetScan(
|
||||||
|
params: SortedSetScanBaseParameters & { withScores: true },
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetScan(
|
||||||
|
params: SortedSetScanBaseParameters & { withScores?: false },
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetUnion(
|
||||||
|
params: SortedSetTheoryOperation & { withScores: true },
|
||||||
|
): Promise<ValueAndScore[]>
|
||||||
|
|
||||||
|
getSortedSetUnion(
|
||||||
|
params: SortedSetTheoryOperation & { withScores?: false },
|
||||||
|
): Promise<string[]>
|
||||||
|
|
||||||
|
getSortedSetsMembers(keys: string[]): Promise<string[][]>
|
||||||
|
|
||||||
|
getSortedSetsMembersWithScores(keys: string[]): Promise<ValueAndScore[][]>
|
||||||
|
|
||||||
|
isMemberOfSortedSets(keys: string[], value: string): Promise<boolean[]>
|
||||||
|
|
||||||
|
isSortedSetMember(key: string, value: string): Promise<boolean>
|
||||||
|
|
||||||
|
isSortedSetMembers(key: string, values: string[]): Promise<boolean[]>
|
||||||
|
|
||||||
|
processSortedSet(
|
||||||
|
setKey: string,
|
||||||
|
processFn: (ids: number[]) => Promise<void> | void,
|
||||||
|
options: { withScores?: boolean; batch?: number; interval?: number, reverse?: boolean; },
|
||||||
|
): Promise<any>
|
||||||
|
|
||||||
|
sortedSetAdd(key: string, score: number, value: string): Promise<void>
|
||||||
|
|
||||||
|
sortedSetAdd(key: string, score: number[], value: string[]): Promise<void>
|
||||||
|
|
||||||
|
sortedSetAddBulk(
|
||||||
|
args: [key: string, score: number[], value: string[]][],
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
sortedSetCard(key: string): Promise<number>
|
||||||
|
|
||||||
|
sortedSetCount(
|
||||||
|
key: string,
|
||||||
|
min: NumberTowardsMinima,
|
||||||
|
max: NumberTowardsMaxima,
|
||||||
|
): Promise<number>
|
||||||
|
|
||||||
|
sortedSetIncrBy(
|
||||||
|
key: string,
|
||||||
|
increment: number,
|
||||||
|
value: string,
|
||||||
|
): Promise<number>
|
||||||
|
|
||||||
|
sortedSetIncrByBulk(
|
||||||
|
data: [key: string, increment: number, value: string][],
|
||||||
|
): Promise<number[]>
|
||||||
|
|
||||||
|
sortedSetIntersectCard(keys: string[]): Promise<number>
|
||||||
|
|
||||||
|
sortedSetLexCount(
|
||||||
|
key: string,
|
||||||
|
min: RedisStyleRangeString,
|
||||||
|
max: RedisStyleRangeString,
|
||||||
|
): Promise<number>
|
||||||
|
|
||||||
|
sortedSetRank(key: string, value: string): Promise<number | null>
|
||||||
|
|
||||||
|
sortedSetRanks(key: string, values: string[]): Promise<(number | null)[]>
|
||||||
|
|
||||||
|
sortedSetRemove(
|
||||||
|
key: string | string[],
|
||||||
|
value: string | string[],
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
sortedSetRemoveBulk(data: [key: string, member: string][]): Promise<void>
|
||||||
|
|
||||||
|
sortedSetRemoveRangeByLex(
|
||||||
|
key: string,
|
||||||
|
min: RedisStyleRangeString | '-',
|
||||||
|
max: RedisStyleRangeString | '+',
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
sortedSetRevRank(key: string, value: string): Promise<number>
|
||||||
|
|
||||||
|
sortedSetRevRanks(key: string, values: string[]): Promise<number[]>
|
||||||
|
|
||||||
|
sortedSetScore(key: string, value: string): Promise<number | null>
|
||||||
|
|
||||||
|
sortedSetScores(key: string, values: string[]): Promise<number[]>
|
||||||
|
|
||||||
|
sortedSetUnionCard(keys: string[]): Promise<number>
|
||||||
|
|
||||||
|
sortedSetsAdd(
|
||||||
|
keys: string[],
|
||||||
|
scores: number | number[],
|
||||||
|
value: string,
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
sortedSetsCard(keys: string[]): Promise<number[]>
|
||||||
|
|
||||||
|
sortedSetsCardSum(keys: string[]): Promise<number>
|
||||||
|
|
||||||
|
sortedSetsRanks<T extends readonly [] | readonly string[]>(
|
||||||
|
keys: T,
|
||||||
|
values: { [K in keyof T]: string },
|
||||||
|
): Promise<number[]>
|
||||||
|
|
||||||
|
sortedSetsRemove(keys: string[], value: string): Promise<void>
|
||||||
|
|
||||||
|
sortedSetsRemoveRangeByScore(
|
||||||
|
keys: string[],
|
||||||
|
min: NumberTowardsMinima,
|
||||||
|
max: NumberTowardsMaxima,
|
||||||
|
): Promise<void>
|
||||||
|
|
||||||
|
sortedSetsRevRanks(keys: string[], values: string[]): Promise<number[]>
|
||||||
|
|
||||||
|
sortedSetsScore(keys: string[], value: string): Promise<number[]>
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user