Skip to content

Commit

Permalink
docs(changeset): feat: add library template, add ioredis module
Browse files Browse the repository at this point in the history
  • Loading branch information
nailiable committed Nov 6, 2024
1 parent 1eeb3df commit 6b00889
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/shy-mugs-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nailyjs/ioredis": patch
"@nailyjs/cli": patch
---

feat: add library template, add ioredis module
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 0 additions & 2 deletions naily.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default defineConfig({
},
},

cacheManager: {},

typeorm: {
type: 'sqlite',
database: path.join(cwd(), './node_modules/.cache/naily/typeorm.db'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions packages/ioredis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@nailyjs/ioredis",
"type": "module",
"version": "2.0.1",
"description": "ioredis module for naily.js.",
"author": "Naily Zero <[email protected]> (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"
}
}
4 changes: 4 additions & 0 deletions packages/ioredis/src/configure-ioredis-protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const CustomIoRedis = '__naily_custom_ioredis__'
export interface CustomIoRedis {
configure(ioRedisOptions: Naily.Configuration.NailyUserConfig['ioRedis']): Naily.Configuration.NailyUserConfig['ioRedis'] | Promise<Naily.Configuration.NailyUserConfig['ioRedis']>
}
3 changes: 3 additions & 0 deletions packages/ioredis/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './configure-ioredis-protocol'
export * from './ioredis.service'
export * from './plugin'
40 changes: 40 additions & 0 deletions packages/ioredis/src/ioredis.service.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<IORedisFactoryService>
if (wrapper && wrapper.wrapperType === 'class') return wrapper.getClassFactory().getOrCreateInstance()
return container.createClassWrapper(IORedisFactoryService).getClassFactory().getOrCreateInstance()
}
}
14 changes: 14 additions & 0 deletions packages/ioredis/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await IORedisFactoryService.getInstance(container).setup(container)
}
}

export function IoRedisPlugin(): IocPlugin {
return new IoRedisPluginImpl()
}
17 changes: 17 additions & 0 deletions packages/ioredis/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { InjectionTokenable } from '@nailyjs/ioc'
import { RedisOptions } from 'ioredis'

declare global {
namespace Naily {
namespace Configuration {
interface NailyUserConfig {
ioRedis?: IoRedisOptions | InjectionTokenable<IoRedisOptions>[]
}
interface NailyUserIntelliSense {
ioRedis?: IoRedisOptions
}
}
}
}

export interface IoRedisOptions extends RedisOptions {}
10 changes: 10 additions & 0 deletions packages/ioredis/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES2022",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "ES2022",
"moduleResolution": "Bundler"
},
"include": ["src"]
}
11 changes: 11 additions & 0 deletions packages/ioredis/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -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'],
})
Loading

0 comments on commit 6b00889

Please sign in to comment.