Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,11 @@ jobs:
with:
bun-version: ${{ env.BUN_VERSION }}

- name: Setup Node.js (for native module compatibility)
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.0.0
with:
node-version: "24"

- name: Install dependencies
run: bun install --frozen-lockfile

- name: Run SQLite integration tests
run: bunx vitest run src/tests/node/sqlite-repositories.test.ts
run: bun test src/tests/integration/sqlite-repositories.test.ts
working-directory: packages/backend
env:
DB_TYPE: sqlite
Expand Down
75 changes: 24 additions & 51 deletions bun.lock

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:integration": "bun test src/tests/integration/",
"test:e2e": "bun test src/tests/e2e/",
"test:all": "bun test",
"test:sqlite": "npx vitest run src/tests/node/sqlite-repositories.vitest.ts",
"test:sqlite": "bun test src/tests/integration/sqlite-repositories.test.ts",
"db:generate": "drizzle-kit generate",
"db:migrate": "bun run src/db/migrate.ts",
"db:studio": "drizzle-kit studio"
Expand All @@ -24,7 +24,6 @@
"@hono/zod-validator": "^0.7.6",
"@simplewebauthn/server": "^13.3.0",
"@simplewebauthn/types": "^12.0.0",
"better-sqlite3": "^12.10.0",
"blurhash": "^2.0.5",
"bullmq": "^5.76.8",
"drizzle-orm": "^0.45.2",
Expand All @@ -43,15 +42,13 @@
"zod": "^4.4.3"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.13",
"@types/eventsource": "^3.0.0",
"@types/pg": "^8.20.0",
"@types/pino": "^7.0.5",
"@types/sharp": "^0.32.0",
"@types/web-push": "^3.6.4",
"bun-types": "^1.3.14",
"drizzle-kit": "^0.31.10",
"typescript": "^5.9.3",
"vitest": "^4.1.6"
"typescript": "^5.9.3"
}
}
15 changes: 10 additions & 5 deletions packages/backend/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@

import { drizzle as drizzlePg, type NodePgDatabase } from "drizzle-orm/node-postgres";
import { drizzle as drizzleMysql } from "drizzle-orm/mysql2";
import { drizzle as drizzleSqlite, type BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import { drizzle as drizzleSqlite, type BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { Pool } from "pg";
import mysql from "mysql2/promise";
import BetterSqlite3 from "better-sqlite3";
import { Database as BunSqlite } from "bun:sqlite";
import * as pgSchema from "./schema/pg.js";
import * as mysqlSchema from "./schema/mysql.js";
import * as sqliteSchema from "./schema/sqlite.js";

/**
* SQLite Drizzle database instance type (shared by all SQLite repositories).
*/
export type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* Supported Database Types
*/
Expand Down Expand Up @@ -92,9 +97,9 @@ export function createDatabase(): Database {
}
// Remove sqlite:// prefix if present
const dbPath = databaseUrl.replace(/^sqlite:\/\//, "");
const sqlite = new BetterSqlite3(dbPath);
const sqlite = new BunSqlite(dbPath);
// Enable WAL mode for better concurrent access
sqlite.pragma("journal_mode = WAL");
sqlite.exec("PRAGMA journal_mode = WAL");
return drizzleSqlite(sqlite, { schema: sqliteSchema }) as unknown as Database;
}

Expand Down Expand Up @@ -175,7 +180,7 @@ interface D1ExecResult {
/**
* D1 Database Instance Type
*/
export type D1DatabaseInstance = BetterSQLite3Database<typeof sqliteSchema>;
export type D1DatabaseInstance = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* Drizzle ORM Database Instance Type
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/db/schema/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* SQLite/D1 Database Schema
*
* This schema is designed to work with both local SQLite (via better-sqlite3)
* This schema is designed to work with both local SQLite (via bun:sqlite)
* and Cloudflare D1 (via drizzle-orm/d1).
*
* Key differences from PostgreSQL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/

import { eq, and, isNull, like, inArray, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { customEmojis, type CustomEmoji, type NewCustomEmoji } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
ICustomEmojiRepository,
ListCustomEmojisOptions,
} from "../../interfaces/repositories/ICustomEmojiRepository.js";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Custom Emoji Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, and, sql, desc, lt, gt, inArray, isNull } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { driveFiles } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IDriveFileRepository } from "../../interfaces/repositories/IDriveFileRepository.js";
import type { DriveFile } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of drive file repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, and, sql, isNull, desc } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { driveFolders } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IDriveFolderRepository } from "../../interfaces/repositories/IDriveFolderRepository.js";
import type { DriveFolder } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of drive folder repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, and, or, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { follows, users } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IFollowRepository } from "../../interfaces/repositories/IFollowRepository.js";
import type { Follow } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of follow repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, sql, desc } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { instanceBlocks, type InstanceBlock } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IInstanceBlockRepository } from "../../interfaces/repositories/IInstanceBlockRepository.js";
import { generateId } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Instance Block Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/

import { eq, inArray } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { instanceSettings, type InstanceSetting } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
IInstanceSettingsRepository,
InstanceSettingKey,
} from "../../interfaces/repositories/IInstanceSettingsRepository.js";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Instance Settings Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, sql, desc, and, gt, or, isNull, lt } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { invitationCodes, type InvitationCode } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IInvitationCodeRepository } from "../../interfaces/repositories/IInvitationCodeRepository.js";
import { generateId } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Invitation Code Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { eq, sql, desc, and } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { moderationAuditLogs, type ModerationAuditLog } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
Expand All @@ -17,7 +17,7 @@ import type {
} from "../../interfaces/repositories/IModerationAuditLogRepository.js";
import { generateId } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Moderation Audit Log Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { eq, and, desc, lt, gt, inArray, isNull, sql, or } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { notes, users } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
Expand All @@ -17,7 +17,7 @@ import type {
} from "../../interfaces/repositories/INoteRepository.js";
import type { Note } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of note repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, and, desc, lt, gt, inArray, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { notifications, type Notification, type NotificationType } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { INotificationRepository } from "../../interfaces/repositories/INotificationRepository.js";
import { generateId } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of notification repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/

import { eq, and, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { oauthAccounts, type OAuthAccount, type NewOAuthAccount } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
IOAuthAccountRepository,
OAuthProvider,
} from "../../interfaces/repositories/IOAuthAccountRepository.js";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of OAuth Account Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { eq, lt, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import {
passkeyChallenges,
type PasskeyChallenge,
Expand All @@ -16,7 +16,7 @@ import {
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IPasskeyChallengeRepository } from "../../interfaces/repositories/IPasskeyChallengeRepository.js";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Passkey Challenge Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { eq, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import {
passkeyCredentials,
type PasskeyCredential,
Expand All @@ -16,7 +16,7 @@ import {
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IPasskeyCredentialRepository } from "../../interfaces/repositories/IPasskeyCredentialRepository.js";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Passkey Credential Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, and, sql, desc, inArray } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { reactions } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IReactionRepository } from "../../interfaces/repositories/IReactionRepository.js";
import type { Reaction } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of reaction repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import { eq, lt, inArray, sql } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { remoteInstances } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type { IRemoteInstanceRepository } from "../../interfaces/repositories/IRemoteInstanceRepository.js";
import type { RemoteInstance } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

export class SqliteRemoteInstanceRepository implements IRemoteInstanceRepository {
constructor(private db: SqliteDatabase) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { eq, and, sql, lt } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { roleAssignments, roles, type Role, type RoleAssignment } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
Expand All @@ -16,7 +16,7 @@ import type {
} from "../../interfaces/repositories/IRoleAssignmentRepository.js";
import { generateId } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Role Assignment Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { eq, asc, sql, desc } from "drizzle-orm";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import type { BunSQLiteDatabase } from "drizzle-orm/bun-sqlite";
import { roles, type Role } from "../../db/schema/sqlite.js";
import type * as sqliteSchema from "../../db/schema/sqlite.js";
import type {
Expand All @@ -17,7 +17,7 @@ import type {
} from "../../interfaces/repositories/IRoleRepository.js";
import { generateId } from "shared";

type SqliteDatabase = BetterSQLite3Database<typeof sqliteSchema>;
type SqliteDatabase = BunSQLiteDatabase<typeof sqliteSchema>;

/**
* SQLite implementation of Role Repository
Expand Down
Loading
Loading