Skip to content

Commit

Permalink
refactor(./types/index): DatabaseConnection taking generic in order t…
Browse files Browse the repository at this point in the history
…o extends custom implem (#167)
  • Loading branch information
Rossb0b authored Jan 15, 2025
1 parent fc29d98 commit ccc01f2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/class/KVPeer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ type MappedValue<T extends StringOrObject, K extends Record<string, any> | null

export type KVMapper<T extends StringOrObject, K extends Record<string, any> | null = null> = (value: T) => MappedValue<T, K>;

export interface KVOptions<T extends StringOrObject = Record<string, any>, K extends Record<string, any> | null = null> {
adapter: DatabaseConnection;
export interface KVOptions<
T extends StringOrObject = Record<string, any>,
K extends Record<string, any> | null = null,
L extends unknown = unknown
> {
adapter: DatabaseConnection<L>;
type?: KVType;
mapValue?: KVMapper<T, K>;
prefix?: string;
Expand All @@ -45,14 +49,18 @@ export type KVPeerSetValueOptions<T extends StringOrObject = StringOrObject> = O
* new KVPeer();
* ```
*/
export class KVPeer<T extends StringOrObject = StringOrObject, K extends Record<string, any> | null = null> extends EventEmitter {
export class KVPeer<
T extends StringOrObject = StringOrObject,
K extends Record<string, any> | null = null,
L extends unknown = unknown
> extends EventEmitter {
protected type: KVType;
protected mapValue: KVMapper<T, K>;
protected adapter: DatabaseConnection;
protected adapter: DatabaseConnection<L>;
protected prefix: string;
protected prefixSeparator: string;

constructor(options: KVOptions<T, K>) {
constructor(options: KVOptions<T, K, L>) {
super();

const { type, mapValue, adapter, prefix = "", prefixSeparator = "-" } = options;
Expand Down
16 changes: 12 additions & 4 deletions src/class/TimedKVPeer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ const kDefaultTtl = 1_000 * 60 * 10;
// eslint-disable-next-line func-style
const kDefaultRandomKeyGenerator = () => randomBytes(6).toString("hex");

export interface TimedKVPeerOptions<T extends object, K extends Record<string, any> | null = null>
extends Omit<KVOptions<T, K>, "type"> {
export interface TimedKVPeerOptions<
T extends object,
K extends Record<string, any> | null = null,
L extends unknown = unknown
>
extends Omit<KVOptions<T, K, L>, "type"> {
/** How long the keys are kept, by default set to 10 minutes **/
ttl?: number;
/** A random key callback generator for setValue() method **/
Expand All @@ -33,11 +37,15 @@ interface TimedSetValueOptions<T extends object> extends Omit<
* @class TimedKVPeer
* @description TimedKVPeer represents an abstraction design to store time-lifed key-value peer. You probably don't need to use this class directly.
*/
export class TimedKVPeer<T extends object, K extends Record<string, any> | null = null> extends KVPeer<T, K> {
export class TimedKVPeer<
T extends object,
K extends Record<string, any> | null = null,
L extends unknown = unknown
> extends KVPeer<T, K, L> {
protected randomKeyGenerator: () => string;
private ttl: number;

constructor(options: TimedKVPeerOptions<T, K>) {
constructor(options: TimedKVPeerOptions<T, K, L>) {
super({ ...options, type: "object" });

this.ttl = options.ttl ?? kDefaultTtl;
Expand Down
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export type GetConnectionPerfResponse = {
perf?: number;
};

export interface DatabaseConnection {
export type DatabaseConnection<T extends unknown = unknown> = BasicDatabaseConnection & T;

export interface BasicDatabaseConnection {
initialize?(...unknown): Promise<unknown>;
close?(forceExit?: boolean): Promise<void>;
isAlive?(): Promise<boolean>;
Expand Down

0 comments on commit ccc01f2

Please sign in to comment.