-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9da206
commit a780f85
Showing
1 changed file
with
94 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |