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

feat: initial support for redis/client #645

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ flag_management:
- packages/cache-interop
- packages/cache-ioredis
- packages/cache-redis
- packages/cache-redis-client
statuses:
- type: project
target: 60%
Expand All @@ -38,6 +39,13 @@ flag_management:
- type: project
target: 40%
threshold: 1%
- name: cacheNodeRedisClientUnit
paths:
- packages/cache-redis-client
statuses:
- type: project
target: 40%
threshold: 1%
- name: dsnParserUnit
paths:
- packages/dsn-parser
Expand Down
2 changes: 1 addition & 1 deletion packages/cache-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"@soluble/cache-interop": "workspace:^",
"@soluble/cache-ioredis": "workspace:^",
"@soluble/cache-redis": "workspace:^",
"@soluble/cache-redis-client": "workspace:^",
"@soluble/dsn-parser": "workspace:^",
"@soluble/eslint-config-bases": "workspace:^",
"@types/ioredis": "4.28.10",
"@types/jest": "29.2.3",
"@types/redis": "2.8.32",
"eslint": "8.28.0",
Expand Down
19 changes: 19 additions & 0 deletions packages/cache-e2e-tests/test/setup/getTestAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CacheInterface } from '@soluble/cache-interop';
import { MapCacheAdapter } from '@soluble/cache-interop';
import { IoRedisCacheAdapter } from '@soluble/cache-ioredis';
import { RedisCacheAdapter } from '@soluble/cache-redis';
import { RedisCacheAdapter as RedisClientCacheAdapter } from '@soluble/cache-redis-client';
import { E2eDockerContainers } from './E2eDockerContainers';

/**
Expand Down Expand Up @@ -46,6 +47,24 @@ export const getTestAdapters = () => {
});
},
],
[
'RedisClientCacheAdapter/Redis7',
async () => {
const { dsn } = await E2eDockerContainers.getContainer('redis7');
return new RedisClientCacheAdapter({
connection: dsn,
});
},
],
[
'RedisClientCacheAdapter/Redis6',
async () => {
const { dsn } = await E2eDockerContainers.getContainer('redis6');
return new RedisClientCacheAdapter({
connection: dsn,
});
},
],
] as [
name: string,
factory: () => CacheInterface | Promise<CacheInterface>
Expand Down
5 changes: 5 additions & 0 deletions packages/cache-redis-client/.escheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ecmaVersion": "es2020",
"module": true,
"files": "./dist/**/*.js"
}
6 changes: 6 additions & 0 deletions packages/cache-redis-client/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist
out
build
coverage
_release

42 changes: 42 additions & 0 deletions packages/cache-redis-client/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Specific eslint rules for this workspace, learn how to compose
* @link https://github.com/belgattitude/perso/tree/main/packages/eslint-config-bases
*/

// Workaround for https://github.com/eslint/eslint/issues/3458
require('@soluble/eslint-config-bases/patch/modern-module-resolution');

const {
getDefaultIgnorePatterns,
} = require('@soluble/eslint-config-bases/helpers');

module.exports = {
root: true,
parserOptions: {
// tsconfigRootDir: __dirname,
project: 'tsconfig.json',
},
ignorePatterns: [
...getDefaultIgnorePatterns(),
'**/build',
'**/.cache',
'**/dist',
'**/_release',
'.cache',
'**/docs',
],
extends: [
'@soluble/eslint-config-bases/typescript',
'@soluble/eslint-config-bases/sonar',
'@soluble/eslint-config-bases/regexp',
'@soluble/eslint-config-bases/jest',
// Apply prettier and disable incompatible rules
'@soluble/eslint-config-bases/prettier',
],
rules: {
// optional overrides per project
},
overrides: [
// optional overrides per project file match
],
};
37 changes: 37 additions & 0 deletions packages/cache-redis-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# dependencies
node_modules
/.pnp
.pnp.js

# production output
/dist
/build
/out

# tests
/coverage
/.nyc_output

# editor
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# os
.DS_Store

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

7 changes: 7 additions & 0 deletions packages/cache-redis-client/.size-limit.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = [
{
name: 'JS',
path: ['dist/index.js'],
limit: '2.5KB',
},
];
1 change: 1 addition & 0 deletions packages/cache-redis-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @soluble/cache-redis-client
21 changes: 21 additions & 0 deletions packages/cache-redis-client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021-current Sébastien Vanvelthem

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 85 additions & 0 deletions packages/cache-redis-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<div align="center">
<h1 align="center"><a aria-label="soluble/cache-redis-client" href="https://github.com/soluble-io/cache-interop/tree/main/packages/cache-redis-client">@soluble/cache-redis-client</a></h1>
<h4 align="center"><a aria-label="soluble/cache-interop" href="">@soluble/cache-interop</a> cache adapter for <a aria-label="node-redis" href="https://github.com/NodeRedis/node-redis">node-redis</a></h4>
</div>

<p align="center">
<a aria-label="Version" href="https://npm.im/@soluble/cache-redis-client">
<img alt="Codecov" src="https://img.shields.io/npm/v/@soluble/cache-redis-client.svg?style=for-the-badge&labelColor=000000" />
</a>
<a aria-label="Downloads" href="https://npm.im/@soluble/cache-redis">
<img alt="Downloads" src="https://img.shields.io/npm/dy/@soluble/cache-redis-client?style=for-the-badge&labelColor=000000" />
</a>
<a aria-label="Coverage" href="https://codecov.io/gh/soluble-io/cache-interop">
<img alt="Codecov" src="https://img.shields.io/codecov/c/github/soluble-io/cache-interop?label=Coverage&logo=codecov&style=for-the-badge&labelColor=000000" />
</a>
<a aria-label="Codefactor" href="https://www.codefactor.io/repository/github/soluble-io/cache-interop">
<img alt="Codefactor" src="https://img.shields.io/codefactor/grade/github/soluble-io/cache-interop?label=CF&logo=codefactor&style=for-the-badge&labelColor=000000" />
</a>
<a aria-label="CodeClimate" href="https://codeclimate.com/github/soluble-io/cache-interop/maintainability">
<img alt="CodeClimate" src="https://img.shields.io/codeclimate/maintainability/soluble-io/cache-interop?logo=code-climate&style=for-the-badge&labelColor=000000" />
</a>
<a aria-label="TechDebt" href="https://codeclimate.com/github/soluble-io/cache-interop/maintainability">
<img alt="TechDebt" src="https://img.shields.io/codeclimate/tech-debt/soluble-io/cache-interop?label=TechDebt&logo=code-climate&style=for-the-badge&labelColor=000000" />
</a>
<a aria-label="Typings">
<img alt="Typings" src="https://img.shields.io/static/v1?label=typings&message=3.5%2B&logo=typescript&style=for-the-badge&labelColor=000000&color=9cf" />
</a>
<a aria-label="Licence" href="https://github.com/soluble-io/cache-interop/blob/main/LICENSE">
<img alt="Licence" src="https://img.shields.io/npm/l/@soluble/cache-ioredis?style=for-the-badge&labelColor=000000" />
</a>
</p>

# About | [Documentation](https://github.com/soluble-io/cache-interop/)

Cache adapter for [@redis/client](https://github.com/NodeRedis/node-redis) client.

## Install

```bash
$ yarn add @soluble/cache-redis-client
```

## Usage

```typescript
import { RedisCacheAdapter } from "@soluble/cache-redis-client";

const cache = new RedisCacheAdapter({
connection: "redis://:pass@localhost:6379/8",
});

const { data, error } = await cache.getOrSet("key", asyncPromise, {
ttl: 30,
});

if (await cache.has("key")) {
await cache.delete("key");
}
```

## Constructor

### Connection

RedisAdapter `connection` param can be a DSN, a RedisConnection,
the native [ClientOpts](https://github.com/NodeRedis/node-redis#options-object-properties) or an existing [RedisClient](https://github.com/NodeRedis/node-redis) connection.

> You can use the `getRedisOptionsFromDsn` function to initiate a connection
> with native parameters.
>
> ```typescript
> import {
> RedisCacheAdapter,
> getRedisOptionsFromDsn,
> } from "@soluble/cache-redis-client";
>
> const dsn = "redis://localhost:6379/db2";
>
> const cache = new RedisCacheAdapter({
> connection: getRedisOptionsFromDsn(dsn, {
> // here all node-redis client options
> enable_offline_queue: false,
> }),
> });
> ```
54 changes: 54 additions & 0 deletions packages/cache-redis-client/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @ts-check
import { pathsToModuleNameMapper } from 'ts-jest';
import { getTsconfig } from 'get-tsconfig';
import { getJestCachePath } from '../../cache.config.js';

const tsConfigFile = new URL('./tsconfig.jest.json', import.meta.url).pathname;

/**
* Transform the tsconfig paths into jest compatible one (support extends)
* @param {string} tsConfigFile
*/
const getTsConfigBasePaths = (tsConfigFile) => {
const parsedTsConfig = getTsconfig(tsConfigFile);
if (parsedTsConfig === null) {
throw new Error(`Cannot find tsconfig file: ${tsConfigFile}`);
}
const tsPaths = parsedTsConfig.config.compilerOptions?.paths;
return tsPaths
? pathsToModuleNameMapper(tsPaths, {
prefix: '<rootDir>/',
})
: {};
};

/** @type {import('ts-jest/dist').InitialOptionsTsJest} */
const config = {
displayName: `cache-redis-client:unit`,
preset: 'ts-jest/presets/default-esm',
cacheDirectory: getJestCachePath('@soluble/cache-redis-client'),
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts'],
verbose: true,
rootDir: './src',
testMatch: ['<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'],
moduleNameMapper: {
...getTsConfigBasePaths(tsConfigFile),
},
// false by default, overrides in cli, ie: yarn test:unit --collect-coverage=true
collectCoverage: false,
coverageDirectory: '<rootDir>/../coverage',
collectCoverageFrom: [
'<rootDir>/**/*.{ts,tsx,js,jsx}',
'!**/*.test.{js,ts}',
'!**/__mock__/*',
],
globals: {
'ts-jest': {
useESM: true,
tsconfig: tsConfigFile,
},
},
};

export default config;
Loading