Skip to content

Commit

Permalink
method to get db from db wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Dec 1, 2023
1 parent 5f8858f commit 5fa2543
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/ws-litefs/src/internal/LiteFSDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Change } from "@vlcn.io/ws-common";
import { IDB } from "@vlcn.io/ws-server";
import { PrimaryConnection } from "./PrimaryConnection.js";
import logger from "../logger.js";
import type BetterSqlite3 from "better-sqlite3";

export class LiteFSDB implements IDB {
readonly #proxied;
Expand All @@ -28,6 +29,25 @@ export class LiteFSDB implements IDB {
return this.#proxied.schemaVersion;
}

getDB(): BetterSqlite3.Database {
throw new Error(
"get db is unsafe under litefs deployments as litefs requires write forwarding to the primary"
);
}

// read<T>(sql: string, params: unknown[]): T[] {
// return this.#proxied.read(sql, params);
// }

// async write(sql: string, params: unknown[]): Promise<void> {
// if (!this.#primaryConnection.isPrimary()) {
// logger.info("Proxying write to primary");
// await this.#primaryConnection.writeOnPrimary(this.#room, sql, params);
// } else {
// await this.#proxied.write(sql, params);
// }
// }

getLastSeen(site: Uint8Array): [bigint, number] {
return this.#proxied.getLastSeen(site);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/ws-litefs/src/internal/PrimaryConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export class PrimaryConnection {
});
}

writeOnPrimary(room: string, sql: string, params: unknown[]): Promise<void> {
throw new Error("not implemented");
}

applyChangesOnPrimary(
room: string,
changes: readonly Change[],
Expand Down
12 changes: 12 additions & 0 deletions packages/ws-server/src/DB.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Database from "better-sqlite3";
import type BetterSqlite3 from "better-sqlite3";
import { Config } from "./config.js";
import path from "node:path";
import fs from "node:fs";
Expand All @@ -13,6 +14,13 @@ export interface IDB {
readonly schemaName: string;
readonly schemaVersion: bigint;

/**
* This will work for single-node SQLite setups.
* Does not work under Turso.
* Will work for reads under LiteFS but not writes.
*/
getDB(): BetterSqlite3.Database;

getLastSeen(site: Uint8Array): [bigint, number];
applyChangesetAndSetLastSeen(
changes: readonly Change[],
Expand Down Expand Up @@ -208,6 +216,10 @@ export default class DB implements IDB {
return this.#schemaVersion;
}

getDB(): BetterSqlite3.Database {
return this.#db;
}

getLastSeen(site: Uint8Array): [bigint, number] {
const result = this.#getLastSeenStmt.raw(true).get(site) as
| [bigint, bigint]
Expand Down

0 comments on commit 5fa2543

Please sign in to comment.