diff --git a/bun.lockb b/bun.lockb index 16f299d4..79fb99bf 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/packages/gil/__tests__/bot_postgres/docker-compose.yaml b/packages/gil/__tests__/bot_postgres/docker-compose.yaml new file mode 100644 index 00000000..5d277e57 --- /dev/null +++ b/packages/gil/__tests__/bot_postgres/docker-compose.yaml @@ -0,0 +1,15 @@ +version: '3' +services: + postgres: + image: postgres:alpine3.19 + ports: + - '5432:5432' + environment: + POSTGRES_USER: guildedjs + POSTGRES_PASSWORD: testpass + POSTGRES_DB: guildedjs + volumes: + - postgres-vol:/var/lib/postgresql/data + +volumes: + postgres-vol: diff --git a/packages/gil/__tests__/bot_postgres/index.ts b/packages/gil/__tests__/bot_postgres/index.ts new file mode 100644 index 00000000..1ff88b6d --- /dev/null +++ b/packages/gil/__tests__/bot_postgres/index.ts @@ -0,0 +1,49 @@ +import { join } from "path"; +import "dotenv/config"; +import { GilClient } from "../../lib/GilClient"; + +import postgres from "postgres"; +import { ConsoleAdapter } from "../../lib"; +import { PostgresAdapter } from "../../lib/adapters/db/PostgresAdapter"; + +const sql = postgres({ + user: "guildedjs", + password: "testpass", + database: "guildedjs", + host: "localhost", +}); + +const YokiBot = new GilClient({ + token: process.env.TOKEN!, + commandDirectory: join(__dirname, "..", "shared", "commands"), + listenerDirectory: join(__dirname, "listeners"), + loggingAdapter: new ConsoleAdapter(), + databaseAdapter: new PostgresAdapter({ + pgInstance: sql, + roleTable: "roles", + serverTable: "servers", + serverIdKey: "server_id", + }), +}); + +async function startBot() { + await sql` + CREATE TABLE IF NOT EXISTS servers ( + server_id TEXT PRIMARY KEY, + prefix TEXT DEFAULT '!', + premium_level INTEGER DEFAULT 0 + ); + `; + + await sql` + CREATE TABLE IF NOT EXISTS roles ( + role_id TEXT PRIMARY KEY, + server_id TEXT, + role_name TEXT, + role_level INTEGER + ); + `; + YokiBot.start(); +} + +startBot(); diff --git a/packages/gil/__tests__/bot_sqlite/index.ts b/packages/gil/__tests__/bot_sqlite/index.ts index 95b5a05e..7911871f 100644 --- a/packages/gil/__tests__/bot_sqlite/index.ts +++ b/packages/gil/__tests__/bot_sqlite/index.ts @@ -16,7 +16,6 @@ const YokiBot = new GilClient({ sqliteInstance: database, serverTable: "servers", serverIdKey: "server_id", - serverStaffRolesKey: "staff_roles", roleTable: "roles", }), }); diff --git a/packages/gil/lib/adapters/db/BetterSQLite3Adapter.ts b/packages/gil/lib/adapters/db/BetterSQLite3Adapter.ts index 6d9ea32e..8f43e2d0 100644 --- a/packages/gil/lib/adapters/db/BetterSQLite3Adapter.ts +++ b/packages/gil/lib/adapters/db/BetterSQLite3Adapter.ts @@ -7,7 +7,6 @@ export class BetterSQLite3Adapter extends DatabaseAdapter { sqliteInstance: sqlite.Database; serverTable: string; serverIdKey: string; - serverStaffRolesKey: string; roleTable: string; }, ) { diff --git a/packages/gil/lib/adapters/db/PostgresAdapter.ts b/packages/gil/lib/adapters/db/PostgresAdapter.ts new file mode 100644 index 00000000..742f651d --- /dev/null +++ b/packages/gil/lib/adapters/db/PostgresAdapter.ts @@ -0,0 +1,47 @@ +import postgres from "postgres"; +import { DatabaseAdapter, StoredRole, StoredServer } from "./DatabaseAdapter"; + +export class PostgresAdapter extends DatabaseAdapter { + private pg: postgres.Sql; + + public constructor( + readonly options: { + pgInstance: postgres.Sql; + serverTable: string; + serverIdKey: string; + roleTable: string; + }, + ) { + super(); + this.pg = options.pgInstance; + } + + public async getServer(serverId: string): Promise { + const [server] = await this.pg` + SELECT * FROM ${this.pg(this.options.serverTable)} WHERE ${this.pg(this.options.serverIdKey)} = ${serverId} + `; + if (!server) return null; + + return { + server_id: server[this.options.serverIdKey as keyof StoredServer]!, + prefix: server.prefix, + premium_level: server.premium_level, + }; + } + + public async createServer(serverId: string): Promise { + const [server] = await this.pg` + INSERT INTO ${this.pg(this.options.serverTable)} (${this.pg(this.options.serverIdKey)}) + VALUES (${serverId}) + RETURNING * + `; + return server as StoredServer; + } + + public async getRoles(serverId: string): Promise { + const roles = await this.pg` + SELECT * FROM ${this.pg(this.options.roleTable)} WHERE ${this.pg("server_id")} = ${serverId} + `; + return roles; + } +} diff --git a/packages/gil/lib/index.ts b/packages/gil/lib/index.ts index 6f131ddb..66d373ad 100644 --- a/packages/gil/lib/index.ts +++ b/packages/gil/lib/index.ts @@ -7,5 +7,7 @@ export * from "./adapters/logging/ConsoleAdapter"; export * from "./adapters/logging/LoggerAdapter"; export * from "./adapters/db/DatabaseAdapter"; export * from "./adapters/db/MongoAdapter"; +export * from "./adapters/db/PostgresAdapter"; +export * from "./adapters/db/BetterSQLite3Adapter"; // export * from "./adapters/logging/WinstonAdapter"; export * from "./adapters/logging/PinoAdapter"; diff --git a/packages/gil/package.json b/packages/gil/package.json index 641893b5..b2db7617 100644 --- a/packages/gil/package.json +++ b/packages/gil/package.json @@ -20,6 +20,7 @@ "dotenv": "^16.0.3", "mongoose": "^8.2.3", "pino": "^8.19.0", + "postgres": "^3.4.4", "typescript": "5.0.4" }, "dependencies": {