@payloadcms/kv-redis 0.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018-2024 Payload CMS, Inc. <info@payloadcms.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Redis KV Adapter for Payload (beta)
2
+
3
+ This package provides a way to use [Redis](https://redis.io) as a KV adapter with Payload.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ pnpm add @payloadcms/kv-redis
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { redisKVAdapter } from '@payloadcms/kv-redis'
15
+
16
+ export default buildConfig({
17
+ collections: [Media],
18
+ kv: redisKVAdapter({
19
+ // Redis connection URL. Defaults to process.env.REDIS_URL
20
+ redisURL: 'redis://localhost:6379',
21
+ // Optional prefix for Redis keys to isolate the store. Defaults to 'payload-kv'
22
+ keyPrefix: 'kv-storage',
23
+ }),
24
+ })
25
+ ```
26
+
27
+ Then you can access the KV storage using `payload.kv`:
28
+
29
+ ```ts
30
+ await payload.kv.set('key', { value: 1 })
31
+ const data = await payload.kv.get('key')
32
+ payload.logger.info(data)
33
+ ```
@@ -0,0 +1,25 @@
1
+ import type { KVAdapter, KVAdapterResult, KVStoreValue } from 'payload';
2
+ import { Redis } from 'ioredis';
3
+ export declare class RedisKVAdapter implements KVAdapter {
4
+ readonly keyPrefix: string;
5
+ redisClient: Redis;
6
+ constructor(keyPrefix: string, redisURL: string);
7
+ clear(): Promise<void>;
8
+ delete(key: string): Promise<void>;
9
+ get(key: string): Promise<KVStoreValue | null>;
10
+ has(key: string): Promise<boolean>;
11
+ keys(): Promise<string[]>;
12
+ set(key: string, data: KVStoreValue): Promise<void>;
13
+ }
14
+ export type RedisKVAdapterOptions = {
15
+ /**
16
+ * Optional prefix for Redis keys to isolate the store
17
+ *
18
+ * @default 'payload-kv:'
19
+ */
20
+ keyPrefix?: string;
21
+ /** Redis connection URL (e.g., 'redis://localhost:6379'). Defaults to process.env.REDIS_URL */
22
+ redisURL?: string;
23
+ };
24
+ export declare const redisKVAdapter: (options?: RedisKVAdapterOptions) => KVAdapterResult;
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAEvE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,qBAAa,cAAe,YAAW,SAAS;IAI5C,QAAQ,CAAC,SAAS,EAAE,MAAM;IAH5B,WAAW,EAAE,KAAK,CAAA;gBAGP,SAAS,EAAE,MAAM,EAC1B,QAAQ,EAAE,MAAM;IAKZ,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAQtB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC;IAU9C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKlC,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAUzB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;CAG1D;AAED,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,+FAA+F;IAC/F,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,cAAc,aAAa,qBAAqB,KAAQ,eAWpE,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,51 @@
1
+ import { Redis } from 'ioredis';
2
+ export class RedisKVAdapter {
3
+ keyPrefix;
4
+ redisClient;
5
+ constructor(keyPrefix, redisURL){
6
+ this.keyPrefix = keyPrefix;
7
+ this.redisClient = new Redis(redisURL);
8
+ }
9
+ async clear() {
10
+ const keys = await this.redisClient.keys(`${this.keyPrefix}*`);
11
+ if (keys.length > 0) {
12
+ await this.redisClient.del(keys);
13
+ }
14
+ }
15
+ async delete(key) {
16
+ await this.redisClient.del(`${this.keyPrefix}${key}`);
17
+ }
18
+ async get(key) {
19
+ const data = await this.redisClient.get(`${this.keyPrefix}${key}`);
20
+ if (data === null) {
21
+ return data;
22
+ }
23
+ return JSON.parse(data);
24
+ }
25
+ async has(key) {
26
+ const exists = await this.redisClient.exists(`${this.keyPrefix}${key}`);
27
+ return exists === 1;
28
+ }
29
+ async keys() {
30
+ const prefixedKeys = await this.redisClient.keys(`${this.keyPrefix}*`);
31
+ if (this.keyPrefix) {
32
+ return prefixedKeys.map((key)=>key.replace(this.keyPrefix, ''));
33
+ }
34
+ return prefixedKeys;
35
+ }
36
+ async set(key, data) {
37
+ await this.redisClient.set(`${this.keyPrefix}${key}`, JSON.stringify(data));
38
+ }
39
+ }
40
+ export const redisKVAdapter = (options = {})=>{
41
+ const keyPrefix = options.keyPrefix ?? 'payload-kv:';
42
+ const redisURL = options.redisURL ?? process.env.REDIS_URL;
43
+ if (!redisURL) {
44
+ throw new Error('redisURL or REDIS_URL env variable is required');
45
+ }
46
+ return {
47
+ init: ()=>new RedisKVAdapter(keyPrefix, redisURL)
48
+ };
49
+ };
50
+
51
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { KVAdapter, KVAdapterResult, KVStoreValue } from 'payload'\n\nimport { Redis } from 'ioredis'\n\nexport class RedisKVAdapter implements KVAdapter {\n redisClient: Redis\n\n constructor(\n readonly keyPrefix: string,\n redisURL: string,\n ) {\n this.redisClient = new Redis(redisURL)\n }\n\n async clear(): Promise<void> {\n const keys = await this.redisClient.keys(`${this.keyPrefix}*`)\n\n if (keys.length > 0) {\n await this.redisClient.del(keys)\n }\n }\n\n async delete(key: string): Promise<void> {\n await this.redisClient.del(`${this.keyPrefix}${key}`)\n }\n\n async get(key: string): Promise<KVStoreValue | null> {\n const data = await this.redisClient.get(`${this.keyPrefix}${key}`)\n\n if (data === null) {\n return data\n }\n\n return JSON.parse(data)\n }\n\n async has(key: string): Promise<boolean> {\n const exists = await this.redisClient.exists(`${this.keyPrefix}${key}`)\n return exists === 1\n }\n\n async keys(): Promise<string[]> {\n const prefixedKeys = await this.redisClient.keys(`${this.keyPrefix}*`)\n\n if (this.keyPrefix) {\n return prefixedKeys.map((key) => key.replace(this.keyPrefix, ''))\n }\n\n return prefixedKeys\n }\n\n async set(key: string, data: KVStoreValue): Promise<void> {\n await this.redisClient.set(`${this.keyPrefix}${key}`, JSON.stringify(data))\n }\n}\n\nexport type RedisKVAdapterOptions = {\n /**\n * Optional prefix for Redis keys to isolate the store\n *\n * @default 'payload-kv:'\n */\n keyPrefix?: string\n /** Redis connection URL (e.g., 'redis://localhost:6379'). Defaults to process.env.REDIS_URL */\n redisURL?: string\n}\n\nexport const redisKVAdapter = (options: RedisKVAdapterOptions = {}): KVAdapterResult => {\n const keyPrefix = options.keyPrefix ?? 'payload-kv:'\n const redisURL = options.redisURL ?? process.env.REDIS_URL\n\n if (!redisURL) {\n throw new Error('redisURL or REDIS_URL env variable is required')\n }\n\n return {\n init: () => new RedisKVAdapter(keyPrefix, redisURL),\n }\n}\n"],"names":["Redis","RedisKVAdapter","redisClient","constructor","keyPrefix","redisURL","clear","keys","length","del","delete","key","get","data","JSON","parse","has","exists","prefixedKeys","map","replace","set","stringify","redisKVAdapter","options","process","env","REDIS_URL","Error","init"],"mappings":"AAEA,SAASA,KAAK,QAAQ,UAAS;AAE/B,OAAO,MAAMC;;IACXC,YAAkB;IAElBC,YACE,AAASC,SAAiB,EAC1BC,QAAgB,CAChB;aAFSD,YAAAA;QAGT,IAAI,CAACF,WAAW,GAAG,IAAIF,MAAMK;IAC/B;IAEA,MAAMC,QAAuB;QAC3B,MAAMC,OAAO,MAAM,IAAI,CAACL,WAAW,CAACK,IAAI,CAAC,GAAG,IAAI,CAACH,SAAS,CAAC,CAAC,CAAC;QAE7D,IAAIG,KAAKC,MAAM,GAAG,GAAG;YACnB,MAAM,IAAI,CAACN,WAAW,CAACO,GAAG,CAACF;QAC7B;IACF;IAEA,MAAMG,OAAOC,GAAW,EAAiB;QACvC,MAAM,IAAI,CAACT,WAAW,CAACO,GAAG,CAAC,GAAG,IAAI,CAACL,SAAS,GAAGO,KAAK;IACtD;IAEA,MAAMC,IAAID,GAAW,EAAgC;QACnD,MAAME,OAAO,MAAM,IAAI,CAACX,WAAW,CAACU,GAAG,CAAC,GAAG,IAAI,CAACR,SAAS,GAAGO,KAAK;QAEjE,IAAIE,SAAS,MAAM;YACjB,OAAOA;QACT;QAEA,OAAOC,KAAKC,KAAK,CAACF;IACpB;IAEA,MAAMG,IAAIL,GAAW,EAAoB;QACvC,MAAMM,SAAS,MAAM,IAAI,CAACf,WAAW,CAACe,MAAM,CAAC,GAAG,IAAI,CAACb,SAAS,GAAGO,KAAK;QACtE,OAAOM,WAAW;IACpB;IAEA,MAAMV,OAA0B;QAC9B,MAAMW,eAAe,MAAM,IAAI,CAAChB,WAAW,CAACK,IAAI,CAAC,GAAG,IAAI,CAACH,SAAS,CAAC,CAAC,CAAC;QAErE,IAAI,IAAI,CAACA,SAAS,EAAE;YAClB,OAAOc,aAAaC,GAAG,CAAC,CAACR,MAAQA,IAAIS,OAAO,CAAC,IAAI,CAAChB,SAAS,EAAE;QAC/D;QAEA,OAAOc;IACT;IAEA,MAAMG,IAAIV,GAAW,EAAEE,IAAkB,EAAiB;QACxD,MAAM,IAAI,CAACX,WAAW,CAACmB,GAAG,CAAC,GAAG,IAAI,CAACjB,SAAS,GAAGO,KAAK,EAAEG,KAAKQ,SAAS,CAACT;IACvE;AACF;AAaA,OAAO,MAAMU,iBAAiB,CAACC,UAAiC,CAAC,CAAC;IAChE,MAAMpB,YAAYoB,QAAQpB,SAAS,IAAI;IACvC,MAAMC,WAAWmB,QAAQnB,QAAQ,IAAIoB,QAAQC,GAAG,CAACC,SAAS;IAE1D,IAAI,CAACtB,UAAU;QACb,MAAM,IAAIuB,MAAM;IAClB;IAEA,OAAO;QACLC,MAAM,IAAM,IAAI5B,eAAeG,WAAWC;IAC5C;AACF,EAAC"}
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@payloadcms/kv-redis",
3
+ "version": "0.0.1-beta.0",
4
+ "description": "Redis KV adapter for Payload",
5
+ "homepage": "https://payloadcms.com",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/payloadcms/payload.git",
9
+ "directory": "packages/kv-redis"
10
+ },
11
+ "license": "MIT",
12
+ "author": "Payload <dev@payloadcms.com> (https://payloadcms.com)",
13
+ "maintainers": [
14
+ {
15
+ "name": "Payload",
16
+ "email": "info@payloadcms.com",
17
+ "url": "https://payloadcms.com"
18
+ }
19
+ ],
20
+ "type": "module",
21
+ "exports": {
22
+ ".": {
23
+ "import": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ }
27
+ },
28
+ "main": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "dependencies": {
34
+ "ioredis": "^5.4.1"
35
+ },
36
+ "devDependencies": {
37
+ "payload": "3.61.1"
38
+ },
39
+ "peerDependencies": {
40
+ "payload": "3.61.1"
41
+ },
42
+ "engines": {
43
+ "node": "^18.20.2 || >=20.9.0"
44
+ },
45
+ "scripts": {
46
+ "build": "pnpm build:types && pnpm build:swc",
47
+ "build:clean": "find . \\( -type d \\( -name build -o -name dist -o -name .cache \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} + && pnpm build",
48
+ "build:swc": "swc ./src -d ./dist --config-file .swcrc --strip-leading-paths",
49
+ "build:types": "tsc --emitDeclarationOnly --outDir dist",
50
+ "clean": "rimraf {dist,*.tsbuildinfo}",
51
+ "lint": "eslint .",
52
+ "lint:fix": "eslint . --fix"
53
+ }
54
+ }