Skip to content

Commit

Permalink
doc(~): updated doc (#156)
Browse files Browse the repository at this point in the history
* doc(~): updated doc

* doc(~): formulations

---------

Co-authored-by: Nicolas Hallaert <[email protected]>
  • Loading branch information
Rossb0b and Nicolas Hallaert authored Dec 17, 2024
1 parent 063be5a commit 60c045c
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 126 deletions.
103 changes: 14 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,99 +32,24 @@ $ npm i @myunisoft/redis
$ yarn add @myunisoft/redis
```

## 📜 API

<h1 align="center">
Connection
</h1>
---

<p align="center">
This class is used to instantiate and close connection to Redis. You need to re-use this instance in every other classes.
There are multiple adapters to exploit the different abstractions available.
</p>

```ts
type ConnectionOptions = Partial<RedisOptions> & {
port?: number;
host?: string;
attempt?: number;
disconnectionTimeout?: number;
};
```

### 📚 Usage

```js
import assert from "assert";
import {
Connection
} from "@myunisoft/redis";

const connection = new Connection();

try {
(await connection.initialize()).unwrap();
}
finally {
await connection.close();
}
```

### initialize(): Promise< Result< null, AssertConnectionErr > >

```ts
type AssertConnectionErr = "Failed at initializing the Redis connection";
```

This function either return void, or an error;

---

### getConnectionPerf(): Promise< GetConnectionPerfResponse >

```ts
type GetConnectionPerfResponse = {
isAlive: boolean;
perf: number;
};
```

This function is used to check Redis connection state.

```ts
const instancePerf = await getConnectionPerf();

if (!instancePerf.isAlive) {
console.log(instancePerf.isAlive);
console.log(instancePerf.perf);
}

console.log(instancePerf.isAlive);
console.log(instancePerf.perf);
```

---

### closeRedis(forceExit: boolean = false): Promise< Result< null, CloseErr > >

```ts
type AssertDisconnectionErr = AssertDisconnectionErrorMessage;
type CloseErr = AssertDisconnectionErrorMessage | "Redis connection already closed";
```

This function is used to close the Redis connection related to the instance.

---

The package also exports many classes listed below.

- [KVPeer](./docs/KVPeer.md)
- [TimedKVPeer](./docs/TimedKVPeer.md)
- [RestrictedKV](./docs/RestrictedKV.md)
- [StoreContext](./docs/StoreContext.md)
- [PubSub](./docs/pubsub/Channel.md)
- [Stream](./docs/stream/Stream.md)
- [Intrapersonal](./docs/stream/Intrapersonal.md)
- [Interpersonal](./docs/stream/Interpersonal.md)
- Adapter
- [MemoryAdapter](./docs/adapter/memory.adapter.md)
- [RedisAdapter](./docs/adapter/redis.adapter.md)
- Abstraction
- [KVPeer](./docs/KVPeer.md)
- [TimedKVPeer](./docs/TimedKVPeer.md)
- [RestrictedKV](./docs/RestrictedKV.md)
- [StoreContext](./docs/StoreContext.md)
- [PubSub](./docs/pubsub/Channel.md)
- [Stream](./docs/stream/Stream.md)
- [Intrapersonal](./docs/stream/Intrapersonal.md)
- [Interpersonal](./docs/stream/Interpersonal.md)


## Contributors ✨
Expand Down
25 changes: 11 additions & 14 deletions docs/KVPeer.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ IsMetadataDefined<T, K> : T;
// 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>;

export class KVPeer<T extends StringOrObject = StringOrObject, K extends Record<string, any> | null = null> extends EventEmitter {
connection: Connection;
export interface KVOptions<T extends StringOrObject = Record<string, any>, K extends Record<string, any> | null = null> {
adapter: DatabaseConnection;
prefix?: string;
type?: KVType;
mapValue?: KVMapper<T, K>;
}

export interface SetValueOptions<T> {
key: KeyType;
value: T;
expiresIn?: number;
}
export type KVPeerSetValueOptions<T extends StringOrObject = StringOrObject> = Omit<
RedisSetValueOptions<T>,
"prefix" | "type"
>;
```

## Constants
Expand All @@ -42,7 +41,7 @@ export interface SetValueOptions<T> {
## 📚 Usage

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

interface MyCustomObject {
foo: string;
Expand All @@ -52,12 +51,10 @@ interface Metadata {
bar: string;
}

const connection = new Connection();

await connection.initialize();
const memoryAdapter = new MemoryAdapter();

const options: KVOptions<MyCustomObject, Metadata> = {
connection,
adapter: memoryAdapter,
prefix: "local",
type: "object",
mapValue: (value: MyCustomObject) => {
Expand All @@ -74,7 +71,7 @@ const customKvWrapper = new RedisKV<MyCustomObject, Metadata>(options);

## 📜 API

### setValue(options: SetValueOptions< T >): Promise< KeyType >
### setValue(options: KVPeerSetValueOptions< T >): Promise< KeyType >

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

Expand All @@ -94,7 +91,7 @@ this method is used to get a value from Redis
```ts
const returnValue = await customKvWrapper.getValue(key);

console.Log(returnValue);
console.log(returnValue);
/*
{
foo: "bar",
Expand Down
5 changes: 4 additions & 1 deletion docs/RestrictedKV.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ type KeyType = string | Buffer;
## 📚 Usage

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

const allowedAttempt = 2;
const banTime = 60;

const memoryAdapter = new MemoryAdapter();

const restrictedKV = new RestrictedKV({
adapter: memoryAdapter,
prefix: "foo-",
allowedAttempt,
banTimeInSecond: banTime
Expand Down
8 changes: 3 additions & 5 deletions docs/StoreContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ interface StoreContextOptions<T extends Store> extends TimedKVPeerOptions<T> {
## 📚 Usage

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

const connection = new Connection();

await connection.initialize();
const memoryAdapter = new MemoryAdapter();

const options = {
connection,
adapter: memoryAdapter,
authenticationField: keyof T | null;
cookiesOptions: SetOption;
}
Expand Down
5 changes: 4 additions & 1 deletion docs/TimedKVPeer.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface TimedKVPeerOptions<T extends object, K extends Record<string, a
## 📚 Usage

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

interface MyCustomObject {
foo: string;
Expand All @@ -32,7 +32,10 @@ function randomKeyCallback() {
return randomBytes(128).toString("hex");
}

const memoryAdapter = new MemoryAdapter();

const store = new TimedKVPeer<MyCustomObject>({
adapter: memoryAdapter,
sessionDuration: 3600,
randomKeyCallback
});
Expand Down
87 changes: 87 additions & 0 deletions docs/adapter/memory.adapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<h1 align="center">
MemoryAdapter
</h1>

<p align="center">
This class is a basic in-memory database adapter.
</p>

## Interface

```ts
export interface InMemSetValueOptions {
key: string;
value: unknown;
expiresIn?: number;
}

export interface InMemIsKeyExpiredOptions {
value: Record<string, unknown>;
banTimeInSecond: number;
}
```

## 📚 Usage

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

const memoryAdapter = new MemoryAdapter();
```

## 📜 API

### setValue(options: InMemSetValueOptions): Result<KeyType, SetValueError>

this method is used to set a key-value pair in memory

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

await memoryAdapter.setValue({ key, value });
```

### deleteValue(key: string): number

this method is used to delete a key-value pair in memory

```ts
const key = "foo";

const result = await memoryAdapter.deleteValue(key);

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

### clearExpired(options: { banTimeInSecond: number; }): (string | Buffer)[]

this method is used to clear expired key-value pairs in memory

```ts
const result = await memoryAdapter.clearExpired({ banTimeInSecond: 10 });

console.log(result); // []
```

### getValue(key: string): null | unknown

this method is used to get a value from memory

```ts
const key = "foo";

const result = await memoryAdapter.getValue(key);

console.log(result); // { foo: "bar" }
```

### flushall()

this method is used to clear all key-value pairs in memory

```ts
await memoryAdapter.flushall();
```
Loading

0 comments on commit 60c045c

Please sign in to comment.