Skip to content

Commit

Permalink
feat: move to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
peam1146 committed Aug 15, 2023
1 parent 07bf6b8 commit ff24337
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 211 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"axios": "^1.4.0",
"better-sqlite3": "^8.5.0",
"discord.js": "14.12.1",
"kysely": "^0.26.1",
"pg": "^8.11.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
Expand Down
27 changes: 26 additions & 1 deletion src/channel/channel.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
import { DiscordModule } from '@discord-nestjs/core';
import { Module } from '@nestjs/common';
import { Pool } from 'pg';

import { ChannelService } from './channel.service';
import { ConfigService } from '@nestjs/config';
import { PostgresDialect, Kysely } from 'kysely';
import { DB } from 'src/db/types';

@Module({
imports: [DiscordModule.forFeature()],
providers: [ChannelService],
providers: [
ChannelService,
{
provide: 'DB_INSTANCE',
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const dialect = new PostgresDialect({
pool: new Pool({
database: configService.get('DB_NAME'),
host: configService.get('DB_HOST'),
user: configService.get('DB_USER'),
password: configService.get('DB_PASSWORD'),
port: configService.get('DB_PORT'),
}),
});

return new Kysely<DB>({
dialect,
});
},
},
],
exports: [ChannelService],
})
export class ChannelModule {}
11 changes: 7 additions & 4 deletions src/channel/channel.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InjectDiscordClient } from '@discord-nestjs/core';
import { Injectable, Logger } from '@nestjs/common';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { Client } from 'discord.js';

import { db } from '../db';
import { Kysely } from 'kysely';
import { DB } from 'src/db/types';

interface CreateChannelOrUpdateIfExistArgs {
guildId: string;
Expand All @@ -17,6 +18,8 @@ export class ChannelService {
constructor(
@InjectDiscordClient()
private readonly client: Client,
@Inject('DB_INSTANCE')
private readonly db: Kysely<DB>,
) {}

async createChannelOrUpdateIfExist({
Expand All @@ -25,7 +28,7 @@ export class ChannelService {
name,
}: CreateChannelOrUpdateIfExistArgs): Promise<void> {
try {
await db
await this.db
.insertInto('guilds')
.values({
channel_id: channelId,
Expand All @@ -47,7 +50,7 @@ export class ChannelService {
packageName: string,
userId: string,
): Promise<void> {
const { channel_id: channelId } = await db
const { channel_id: channelId } = await this.db
.selectFrom('guilds')
.where('guild_id', '=', guildId)
.select('channel_id')
Expand Down
15 changes: 0 additions & 15 deletions src/db/index.ts

This file was deleted.

Loading

0 comments on commit ff24337

Please sign in to comment.