Skip to content

Commit

Permalink
feat: sqlite adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Mar 28, 2024
1 parent 1bb6642 commit 08b9208
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pnpm-*.yaml
*.tsbuildinfo
*.log
*.env
*.db

.vscode/
.idea/
Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions packages/gil/__tests__/bot_mongo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { join } from "path";
import "dotenv/config";
import { GilClient } from "../../lib/GilClient";
import { MongoAdapter } from "../../lib/adapters/db/MongoAdapter";
import { PinoAdapter } from "../../lib/adapters/logging/PinoAdapter";

import mongoose from "mongoose";
import { ConsoleAdapter } from "../../lib";
import Server from "./db/Server";

mongoose.connect("mongodb://guildedjs:testpass@localhost:27017/", {
Expand All @@ -17,9 +17,9 @@ mongoose.connect("mongodb://guildedjs:testpass@localhost:27017/", {

const YokiBot = new GilClient({
token: process.env.TOKEN!,
commandDirectory: join(__dirname, "commands"),
commandDirectory: join(__dirname, "..", "shared", "commands"),
listenerDirectory: join(__dirname, "listeners"),
loggingAdapter: new PinoAdapter(),
loggingAdapter: new ConsoleAdapter(),
databaseAdapter: new MongoAdapter({
serverModel: Server,
serverIdKey: "server_id",
Expand Down
36 changes: 36 additions & 0 deletions packages/gil/__tests__/bot_sqlite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { join } from "path";
import "dotenv/config";
import { GilClient } from "../../lib/GilClient";
import { BetterSQLite3Adapter } from "../../lib/adapters/db/BetterSQLite3Adapter";

import sqlite from "better-sqlite3";
import { ConsoleAdapter } from "../../lib";
const database = sqlite("test.db");

const YokiBot = new GilClient({
token: process.env.TOKEN!,
commandDirectory: join(__dirname, "..", "shared", "commands"),
listenerDirectory: join(__dirname, "listeners"),
loggingAdapter: new ConsoleAdapter(),
databaseAdapter: new BetterSQLite3Adapter({
sqliteInstance: database,
serverTable: "servers",
serverIdKey: "server_id",
serverStaffRolesKey: "staff_roles",
roleTable: "roles",
}),
});

database.exec(`
CREATE TABLE IF NOT EXISTS servers (
server_id TEXT PRIMARY KEY,
prefix TEXT DEFAULT '!',
premium_level INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS roles (
role_id TEXT PRIMARY KEY,
server_id TEXT,
role_name TEXT
);`);
YokiBot.start();
45 changes: 45 additions & 0 deletions packages/gil/lib/adapters/db/BetterSQLite3Adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sqlite from "better-sqlite3";
import { DatabaseAdapter, StoredRole, StoredServer } from "./DatabaseAdapter";

export class BetterSQLite3Adapter extends DatabaseAdapter {
public constructor(
readonly options: {
sqliteInstance: sqlite.Database;
serverTable: string;
serverIdKey: string;
serverStaffRolesKey: string;
roleTable: string;
},
) {
super();
}

public async getServer(serverId: string): Promise<StoredServer | null> {
const server = this.options.sqliteInstance.prepare(`SELECT * FROM ${this.options.serverTable} WHERE ${this.options.serverIdKey} = ?`).get(serverId) as StoredServer;
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 = this.options.sqliteInstance
.prepare(
`INSERT INTO ${this.options.serverTable} (${this.options.serverIdKey}, prefix, premium_level)
VALUES (?, ?, ?)
RETURNING *`,
)
.get(serverId, null, null) as StoredServer;

return server;
}

public async getRoles(serverId: string): Promise<StoredRole[]> {
const roles = this.options.sqliteInstance.prepare(`SELECT * FROM ${this.options.roleTable} WHERE server_id = ?`).all(serverId) as StoredRole[];

return roles;
}
}
1 change: 0 additions & 1 deletion packages/gil/lib/adapters/db/MongoAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import type mongoose from "mongoose";
import { DatabaseAdapter, StoredRole, StoredServer } from "./DatabaseAdapter";

Expand Down
2 changes: 2 additions & 0 deletions packages/gil/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"release": "npm publish"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.9",
"@types/mongoose": "^5.11.97",
"better-sqlite3": "^9.4.3",
"dotenv": "^16.0.3",
"mongoose": "^8.2.3",
"pino": "^8.19.0",
Expand Down

0 comments on commit 08b9208

Please sign in to comment.