Skip to content

Commit

Permalink
feat: postgres adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Mar 28, 2024
1 parent 944ab01 commit d429f10
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/gil/__tests__/bot_postgres/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
49 changes: 49 additions & 0 deletions packages/gil/__tests__/bot_postgres/index.ts
Original file line number Diff line number Diff line change
@@ -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();
1 change: 0 additions & 1 deletion packages/gil/__tests__/bot_sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const YokiBot = new GilClient({
sqliteInstance: database,
serverTable: "servers",
serverIdKey: "server_id",
serverStaffRolesKey: "staff_roles",
roleTable: "roles",
}),
});
Expand Down
1 change: 0 additions & 1 deletion packages/gil/lib/adapters/db/BetterSQLite3Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export class BetterSQLite3Adapter extends DatabaseAdapter {
sqliteInstance: sqlite.Database;
serverTable: string;
serverIdKey: string;
serverStaffRolesKey: string;
roleTable: string;
},
) {
Expand Down
47 changes: 47 additions & 0 deletions packages/gil/lib/adapters/db/PostgresAdapter.ts
Original file line number Diff line number Diff line change
@@ -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<StoredServer | null> {
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<StoredServer> {
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<StoredRole[]> {
const roles = await this.pg<StoredRole[]>`
SELECT * FROM ${this.pg(this.options.roleTable)} WHERE ${this.pg("server_id")} = ${serverId}
`;
return roles;
}
}
2 changes: 2 additions & 0 deletions packages/gil/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
1 change: 1 addition & 0 deletions packages/gil/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit d429f10

Please sign in to comment.