Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(KVPeer): update doc #166

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 94 additions & 71 deletions docs/KVPeer.md
Original file line number Diff line number Diff line change
@@ -1,113 +1,136 @@
<h1 align="center">
RedisKV
KVPeer
</h1>

<p align="center">
This class is used to store and retrieve key-value pairs in Redis.
This class is used to store, retrieve and delete key-value pairs in Redis.
</p>

## Interface
## 📚 Usage

```ts
export type KVType = "raw" | "object";

export type StringOrObject = string | Record<string, any>;
import { KVPeer, RedisAdapter } from "@myunisoft/redis";

type IsMetadataDefined<T extends Record<string, any>, K extends Record<string, any> | null = null> = K extends Record<string, any> ? T & { customData: K } : T;
const redisAdapter = new RedisAdapter();
await redisAdapter.initialize();

type MappedValue<T extends StringOrObject, K extends Record<string, any> | null = null> = T extends Record<string, any> ?
IsMetadataDefined<T, K> : T;
const kvPeer = new KVPeer({
type: "raw",
adapter: redisAdapter,
});

// How to restraint usage of the mapValue fn while T extends string?
export type KVMapper<T extends StringOrObject, K extends Record<string, any> | null = null> = (value: T) => MappedValue<T, K>;
await kvPeer.setValue({ key: "myKey", value: "boo" });
const value = await kvPeer.getValue("myKey"); // "boo"
```

export interface KVOptions<T extends StringOrObject = Record<string, any>, K extends Record<string, any> | null = null> {
adapter: DatabaseConnection;
type?: KVType;
mapValue?: KVMapper<T, K>;
prefix?: string;
prefixSeparator?: string;
}
## 📜 API

export type KVPeerSetValueOptions<T extends StringOrObject = StringOrObject> = Omit<
RedisSetValueOptions<T>,
"type"
>;
```
### constructor(options: KVOptions<T>)

## Constants
You can instantiate a new `KVPeer` object by passing the following options:
- `adapter: DatabaseConnection` - the database connection object.
- `type?: KVType` - the type of the value that will be stored in Redis. It can be either `"raw"` or `"object"`. Default is `"raw"`.
- `mapValue?: KVMapper<T, K>` - a function that will be used to map the value before storing it in Redis (only for `"object"` type). There is no map by default.
- `prefix?: string` - a prefix that will be added to the key before storing it in Redis.
- `prefixSeparator?: string` - a separator that will be used to separate the prefix from the key. Default is `"-"`.

- kDefaultKVType = "raw"
### setValue(options: KVPeerSetValueOptions<T>): Promise<Result<KeyType, Error>>

## 📚 Usage
This method is used to set a key-value pair in Redis.

```ts
import { RedisKV, MemoryAdapter } from "@myunisoft/redis";

interface MyCustomObject {
foo: string;
}
const key = "foo";
const value = {
bar: "baz",
};

interface Metadata {
bar: string;
const result = await kvPeer.setValue({ key, value });
if (result.err) {
console.error(result.val);
}
```

const memoryAdapter = new MemoryAdapter();
Options are defined as follows:

const options: KVOptions<MyCustomObject, Metadata> = {
adapter: memoryAdapter,
type: "object",
mapValue: (value: MyCustomObject) => {
value.metadata = {
bar: "foo"
};
- `key` - the key that will be used to store the value in Redis.
- `value` - the value that will be stored in Redis.
- `expiresIn` - the time in **seconds** after which the key will expire.

return value;
}
```ts
export interface RedisSetValueOptions<T extends StringOrObject = Record<string, any>> {
key: KeyType;
value: Partial<T>;
type: KVType;
expiresIn?: number;
}

const customKvWrapper = new RedisKV<MyCustomObject, Metadata>(options);
export type KVPeerSetValueOptions<T extends StringOrObject = StringOrObject> = Omit<
RedisSetValueOptions<T>,
"type"
>;
```

## 📜 API

### setValue(options: KVPeerSetValueOptions< T >): Promise< KeyType >
### getValue(key: KeyType): Promise<MappedValue<T, K> | null >

this method is used to set a key-value pair in Redis
This method is used to get a value in Redis.

```ts
const key = "foo";
const value: MyCustomObject = {
foo: "bar",
};

await customKvWrapper.setValue(key, value); // "local-foo"
const value = await kvPeer.getValue("foo");
// { bar: "baz" }
```

### getValue(key: KeyType): Promise< MappedValue< T, K > | null >
### deleteValue(key: KeyType): Promise<number>

this method is used to get a value from Redis
This method is used to delete a key-value pair in Redis.

```ts
const returnValue = await customKvWrapper.getValue(key);

console.log(returnValue);
/*
{
foo: "bar",
customData: {
bar: "foo"
}
}
*/
const result = await kvPeer.deleteValue("key");

console.log(result); // 0 for Failure, 1 for Success
```

### deleteValue(key: KeyType): Promise< number >
## Usage with `mapValue`

```ts
const kvPeer = new KVPeer({
type: "object",
adapter: redisAdapter,
mapValue: (value) => {
return {
...value,
metadata: {
bar: "baz",
},
};
},
});

await kvPeer.setValue({ key: "myKey", value: { foo: "foz" } });
const value = await kvPeer.getValue("myKey");
// { foo: "foz", metadata: { bar: "baz" } }
```

this method is used to delete a key-value pair
## Interface

```ts
const result = await customKvWrapper.deleteValue("key");
export type KeyType = string | Buffer;
export type KVType = "raw" | "object";

console.log(result); // 0 for Failure, 1 for Success
export type StringOrObject = string | Record<string, any>;

type IsMetadataDefined<T extends Record<string, any>, K extends Record<string, any> | null = null> =
K extends Record<string, any> ? T & { customData: K; } : T;

type MappedValue<T extends StringOrObject, K extends Record<string, any> | null = null> = T extends Record<string, any> ?
IsMetadataDefined<T, K> : T;

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;
type?: KVType;
mapValue?: KVMapper<T, K>;
prefix?: string;
prefixSeparator?: string;
}
```
Loading