From 6b00889fcf7a3701851d3af8afc0443d880a428a Mon Sep 17 00:00:00 2001 From: Naily Date: Wed, 6 Nov 2024 10:32:12 +0800 Subject: [PATCH] docs(changeset): feat: add library template, add ioredis module --- .changeset/shy-mugs-nail.md | 6 ++ .vscode/settings.json | 3 +- naily.config.ts | 2 - package.json | 1 + packages/cli/src/index.ts | 1 + packages/ioredis/package.json | 33 +++++++ .../ioredis/src/configure-ioredis-protocol.ts | 4 + packages/ioredis/src/index.ts | 3 + packages/ioredis/src/ioredis.service.ts | 40 +++++++++ packages/ioredis/src/plugin.ts | 14 +++ packages/ioredis/src/types.ts | 17 ++++ packages/ioredis/tsconfig.json | 10 +++ packages/ioredis/tsup.config.ts | 11 +++ pnpm-lock.yaml | 87 ++++++++++++++++++- 14 files changed, 226 insertions(+), 6 deletions(-) create mode 100644 .changeset/shy-mugs-nail.md create mode 100644 packages/ioredis/package.json create mode 100644 packages/ioredis/src/configure-ioredis-protocol.ts create mode 100644 packages/ioredis/src/index.ts create mode 100644 packages/ioredis/src/ioredis.service.ts create mode 100644 packages/ioredis/src/plugin.ts create mode 100644 packages/ioredis/src/types.ts create mode 100644 packages/ioredis/tsconfig.json create mode 100644 packages/ioredis/tsup.config.ts diff --git a/.changeset/shy-mugs-nail.md b/.changeset/shy-mugs-nail.md new file mode 100644 index 0000000..a40372d --- /dev/null +++ b/.changeset/shy-mugs-nail.md @@ -0,0 +1,6 @@ +--- +"@nailyjs/ioredis": patch +"@nailyjs/cli": patch +--- + +feat: add library template, add ioredis module diff --git a/.vscode/settings.json b/.vscode/settings.json index 613b515..0340d99 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -83,7 +83,8 @@ "typeorm": "database", "rpc": "api", "unplugin-rpc": "plugin", - "eslint": "config" + "eslint": "config", + "ioredis": "database" }, "material-icon-theme.activeIconPack": "nest", "material-icon-theme.files.associations": { diff --git a/naily.config.ts b/naily.config.ts index 4f3972f..16a3909 100644 --- a/naily.config.ts +++ b/naily.config.ts @@ -16,8 +16,6 @@ export default defineConfig({ }, }, - cacheManager: {}, - typeorm: { type: 'sqlite', database: path.join(cwd(), './node_modules/.cache/naily/typeorm.db'), diff --git a/package.json b/package.json index 3161d8a..359a19c 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "build:eslint": "pnpm -F @nailyjs/eslint build", "build:unplugin-rpc": "pnpm -F unplugin-rpc build", "build:cache": "pnpm -F @nailyjs/cache build", + "build:ioredis": "pnpm -F @nailyjs/ioredis build", "play:rpc": "pnpm -F rpc dev", "lint": "eslint .", diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 65d5172..fc53da9 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -34,6 +34,7 @@ export class CliBootstrap extends AbstractBootstrap { choices: [ { title: 'backend-app', value: 'template-backend-app', description: 'Basic naily backend application' }, { title: 'vitesse-naily', value: 'vitesse-naily', description: 'Antfu\'s Vitesse with Naily' }, + { title: 'library', value: 'template-library', description: 'Naily library template' }, ], }).catch(() => exit(0)) if (!template.template) return exit(0) diff --git a/packages/ioredis/package.json b/packages/ioredis/package.json new file mode 100644 index 0000000..7084adb --- /dev/null +++ b/packages/ioredis/package.json @@ -0,0 +1,33 @@ +{ + "name": "@nailyjs/ioredis", + "type": "module", + "version": "2.0.1", + "description": "ioredis module for naily.js.", + "author": "Naily Zero (https://naily.cc)", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "require": "./dist/index.cjs" + } + }, + "main": "./dist/index.cjs", + "module": "./dist/index.js", + "types": "./dist/index.d.ts", + "publishConfig": { + "access": "public" + }, + "scripts": { + "build": "tsup", + "watch": "tsup -w", + "prepublishOnly": "tsup" + }, + "dependencies": { + "@nailyjs/config": "workspace:*", + "@nailyjs/ioc": "workspace:*", + "ioredis": "^5.4.1" + }, + "devDependencies": { + "tsup": "^8.3.0" + } +} diff --git a/packages/ioredis/src/configure-ioredis-protocol.ts b/packages/ioredis/src/configure-ioredis-protocol.ts new file mode 100644 index 0000000..5067347 --- /dev/null +++ b/packages/ioredis/src/configure-ioredis-protocol.ts @@ -0,0 +1,4 @@ +export const CustomIoRedis = '__naily_custom_ioredis__' +export interface CustomIoRedis { + configure(ioRedisOptions: Naily.Configuration.NailyUserConfig['ioRedis']): Naily.Configuration.NailyUserConfig['ioRedis'] | Promise +} diff --git a/packages/ioredis/src/index.ts b/packages/ioredis/src/index.ts new file mode 100644 index 0000000..c178d28 --- /dev/null +++ b/packages/ioredis/src/index.ts @@ -0,0 +1,3 @@ +export * from './configure-ioredis-protocol' +export * from './ioredis.service' +export * from './plugin' diff --git a/packages/ioredis/src/ioredis.service.ts b/packages/ioredis/src/ioredis.service.ts new file mode 100644 index 0000000..d660630 --- /dev/null +++ b/packages/ioredis/src/ioredis.service.ts @@ -0,0 +1,40 @@ +import { Value } from '@nailyjs/config' +import { Autowired, ClassWrapper, Container, Optional, Service } from '@nailyjs/ioc' +import Redis from 'ioredis' +import { CustomIoRedis } from './configure-ioredis-protocol' + +@Service() +export class IORedisFactoryService { + constructor( + @Value('naily.ioRedis') + private readonly ioRedisOptions: Naily.Configuration.NailyUserConfig['ioRedis'], + @Optional() + @Autowired(CustomIoRedis) + private readonly configureService: CustomIoRedis, + ) {} + + async setup(container: Container): Promise { + let ioRedisOptions = this.ioRedisOptions + if (this.configureService && typeof this.configureService.configure === 'function') + ioRedisOptions = await this.configureService.configure(ioRedisOptions) + + if (Array.isArray(ioRedisOptions)) { + for (const ioRedisOption of ioRedisOptions) { + const ioRedisInstance = new Redis(ioRedisOption) + await ioRedisInstance.connect() + container.createConstantWrapper(ioRedisOption.injectionToken, ioRedisInstance).save() + } + } + else { + const ioRedisInstance = new Redis(ioRedisOptions) + await ioRedisInstance.connect() + container.createConstantWrapper(Redis, ioRedisInstance).save() + } + } + + static getInstance(container: Container): IORedisFactoryService { + const wrapper = container.getContainer().get(IORedisFactoryService) as ClassWrapper + if (wrapper && wrapper.wrapperType === 'class') return wrapper.getClassFactory().getOrCreateInstance() + return container.createClassWrapper(IORedisFactoryService).getClassFactory().getOrCreateInstance() + } +} diff --git a/packages/ioredis/src/plugin.ts b/packages/ioredis/src/plugin.ts new file mode 100644 index 0000000..df155dc --- /dev/null +++ b/packages/ioredis/src/plugin.ts @@ -0,0 +1,14 @@ +import { Container, IocPlugin } from '@nailyjs/ioc' +import { IORedisFactoryService } from './ioredis.service' + +class IoRedisPluginImpl implements IocPlugin { + name: string = 'naily:ioredis-plugin' + + async beforeRun(container: Container): Promise { + await IORedisFactoryService.getInstance(container).setup(container) + } +} + +export function IoRedisPlugin(): IocPlugin { + return new IoRedisPluginImpl() +} diff --git a/packages/ioredis/src/types.ts b/packages/ioredis/src/types.ts new file mode 100644 index 0000000..72d0a6f --- /dev/null +++ b/packages/ioredis/src/types.ts @@ -0,0 +1,17 @@ +import { InjectionTokenable } from '@nailyjs/ioc' +import { RedisOptions } from 'ioredis' + +declare global { + namespace Naily { + namespace Configuration { + interface NailyUserConfig { + ioRedis?: IoRedisOptions | InjectionTokenable[] + } + interface NailyUserIntelliSense { + ioRedis?: IoRedisOptions + } + } + } +} + +export interface IoRedisOptions extends RedisOptions {} diff --git a/packages/ioredis/tsconfig.json b/packages/ioredis/tsconfig.json new file mode 100644 index 0000000..ddc0a63 --- /dev/null +++ b/packages/ioredis/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "ES2022", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "module": "ES2022", + "moduleResolution": "Bundler" + }, + "include": ["src"] +} diff --git a/packages/ioredis/tsup.config.ts b/packages/ioredis/tsup.config.ts new file mode 100644 index 0000000..31fd75b --- /dev/null +++ b/packages/ioredis/tsup.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from 'tsup' + +export default defineConfig({ + entry: { + index: './src/index.ts', + }, + dts: true, + sourcemap: true, + clean: true, + format: ['cjs', 'esm'], +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a65972a..1238224 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -103,7 +103,7 @@ importers: version: link:../../packages/typeorm typeorm: specifier: ^0.3.20 - version: 0.3.20(sqlite3@5.1.7) + version: 0.3.20(ioredis@5.4.1)(sqlite3@5.1.7) unplugin-rpc: specifier: workspace:* version: link:../../packages/unplugin-rpc @@ -238,6 +238,22 @@ importers: specifier: ^8.3.0 version: 8.3.5(@swc/core@1.7.42)(jiti@2.4.0)(postcss@8.4.47)(tsx@4.19.2)(typescript@5.6.3)(yaml@2.6.0) + packages/ioredis: + dependencies: + '@nailyjs/config': + specifier: workspace:* + version: link:../config + '@nailyjs/ioc': + specifier: workspace:* + version: link:../ioc + ioredis: + specifier: ^5.4.1 + version: 5.4.1 + devDependencies: + tsup: + specifier: ^8.3.0 + version: 8.3.5(@swc/core@1.7.42)(jiti@2.4.0)(postcss@8.4.47)(tsx@4.19.2)(typescript@5.6.3)(yaml@2.6.0) + packages/rpc: dependencies: '@nailyjs/backend': @@ -276,7 +292,7 @@ importers: version: link:../ioc typeorm: specifier: ^0.3.20 - version: 0.3.20(sqlite3@5.1.7) + version: 0.3.20(ioredis@5.4.1)(sqlite3@5.1.7) devDependencies: tsup: specifier: ^8.3.0 @@ -1019,6 +1035,9 @@ packages: resolution: {integrity: sha512-JBxkERygn7Bv/GbN5Rv8Ul6LVknS+5Bp6RgDC/O8gEBU/yeH5Ui5C/OlWrTb6qct7LjjfT6Re2NxB0ln0yYybA==} engines: {node: '>=18.18'} + '@ioredis/commands@1.2.0': + resolution: {integrity: sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg==} + '@isaacs/cliui@8.0.2': resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} engines: {node: '>=12'} @@ -1771,6 +1790,10 @@ packages: resolution: {integrity: sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==} engines: {node: '>=12'} + cluster-key-slot@1.1.2: + resolution: {integrity: sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==} + engines: {node: '>=0.10.0'} + color-convert@2.0.1: resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==} engines: {node: '>=7.0.0'} @@ -1907,6 +1930,10 @@ packages: delegates@1.0.0: resolution: {integrity: sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==} + denque@2.1.0: + resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} + engines: {node: '>=0.10'} + dequal@2.0.3: resolution: {integrity: sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==} engines: {node: '>=6'} @@ -2521,6 +2548,10 @@ packages: resolution: {integrity: sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==} engines: {node: '>= 0.10'} + ioredis@5.4.1: + resolution: {integrity: sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==} + engines: {node: '>=12.22.0'} + ip-address@9.0.5: resolution: {integrity: sha512-zHtQzGojZXTwZTHQqra+ETKd4Sn3vgi7uBmlPoXVWZqYvuKmtI0l/VZTjqGmJY9x88GGOaZ9+G9ES8hC4T4X8g==} engines: {node: '>= 12'} @@ -2758,6 +2789,12 @@ packages: lodash-es@4.17.21: resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} + lodash.defaults@4.2.0: + resolution: {integrity: sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==} + + lodash.isarguments@3.1.0: + resolution: {integrity: sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==} + lodash.merge@4.6.2: resolution: {integrity: sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==} @@ -3412,6 +3449,14 @@ packages: resolution: {integrity: sha512-HFM8rkZ+i3zrV+4LQjwQ0W+ez98pApMGM3HUrN04j3CqzPOzl9nmP15Y8YXNm8QHGv/eacOVEjqhmWpkRV0NAw==} engines: {node: '>= 0.10'} + redis-errors@1.2.0: + resolution: {integrity: sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==} + engines: {node: '>=4'} + + redis-parser@3.0.0: + resolution: {integrity: sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==} + engines: {node: '>=4'} + refa@0.12.1: resolution: {integrity: sha512-J8rn6v4DBb2nnFqkqwy6/NnTYMcgLA+sLr0iIO41qpv0n+ngb7ksag2tMRl0inb1bbO/esUwzW1vbJi7K0sI0g==} engines: {node: ^12.0.0 || ^14.0.0 || >=16.0.0} @@ -3661,6 +3706,9 @@ packages: stackback@0.0.2: resolution: {integrity: sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==} + standard-as-callback@2.1.0: + resolution: {integrity: sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==} + std-env@3.7.0: resolution: {integrity: sha512-JPbdCEQLj1w5GilpiHAx3qJvFndqybBysA3qUOnznweH4QbNYUsW/ea8QzSrnh0vNsezMMw5bcVool8lM0gwzg==} @@ -4844,6 +4892,8 @@ snapshots: '@humanwhocodes/retry@0.3.1': {} + '@ioredis/commands@1.2.0': {} + '@isaacs/cliui@8.0.2': dependencies: string-width: 5.1.2 @@ -5707,6 +5757,8 @@ snapshots: strip-ansi: 6.0.1 wrap-ansi: 7.0.0 + cluster-key-slot@1.1.2: {} + color-convert@2.0.1: dependencies: color-name: 1.1.4 @@ -5809,6 +5861,8 @@ snapshots: delegates@1.0.0: optional: true + denque@2.1.0: {} + dequal@2.0.3: {} destr@2.0.3: {} @@ -6564,6 +6618,20 @@ snapshots: interpret@1.4.0: {} + ioredis@5.4.1: + dependencies: + '@ioredis/commands': 1.2.0 + cluster-key-slot: 1.1.2 + debug: 4.3.7(supports-color@5.5.0) + denque: 2.1.0 + lodash.defaults: 4.2.0 + lodash.isarguments: 3.1.0 + redis-errors: 1.2.0 + redis-parser: 3.0.0 + standard-as-callback: 2.1.0 + transitivePeerDependencies: + - supports-color + ip-address@9.0.5: dependencies: jsbn: 1.1.0 @@ -6781,6 +6849,10 @@ snapshots: lodash-es@4.17.21: {} + lodash.defaults@4.2.0: {} + + lodash.isarguments@3.1.0: {} + lodash.merge@4.6.2: {} lodash.sortby@4.7.0: {} @@ -7623,6 +7695,12 @@ snapshots: dependencies: resolve: 1.22.8 + redis-errors@1.2.0: {} + + redis-parser@3.0.0: + dependencies: + redis-errors: 1.2.0 + refa@0.12.1: dependencies: '@eslint-community/regexpp': 4.12.1 @@ -7882,6 +7960,8 @@ snapshots: stackback@0.0.2: {} + standard-as-callback@2.1.0: {} + std-env@3.7.0: {} stdin-discarder@0.2.2: {} @@ -8112,7 +8192,7 @@ snapshots: type-fest@0.8.1: {} - typeorm@0.3.20(sqlite3@5.1.7): + typeorm@0.3.20(ioredis@5.4.1)(sqlite3@5.1.7): dependencies: '@sqltools/formatter': 1.2.5 app-root-path: 3.1.0 @@ -8130,6 +8210,7 @@ snapshots: uuid: 9.0.1 yargs: 17.7.2 optionalDependencies: + ioredis: 5.4.1 sqlite3: 5.1.7 transitivePeerDependencies: - supports-color