Skip to content

Commit

Permalink
feat: added autoCleanupOnClose and autoCloseOnExit on db opttions
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Feb 6, 2025
1 parent 605bf8f commit 5596a08
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ Creates a new **MiftahDB** instance.
- `mmapSize`: The memory-mapped file size (default: `30000000000`).
- `lockingMode`: Determines the database locking mode (default: `"NORMAL"`).
- `autoVacuumMode`: Configures the auto-vacuum behavior (default: `"OFF"`).
- `autoCleanupOnClose`: Automatically cleans up expired keys when the database is closed (default: `false`).
- `autoCloseOnExit`: Automatically closes the database when the process exits (default: `true`).

#### Example Usage

Expand Down
12 changes: 10 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class BaseMiftahDB implements IMiftahDB {
protected declare db: Database;
protected statements: Record<string, Statement>;
private nameSpacePrefix: string | null = null;
private autoCleanupOnClose: boolean;

constructor(path = ":memory:", options: DBOptions = defaultDBOptions) {
this.initDatabase(path);
Expand All @@ -43,8 +44,10 @@ export abstract class BaseMiftahDB implements IMiftahDB {
this.db.exec(SQL_STATEMENTS.CREATE_INDEX);

this.statements = this.prepareStatements();
this.autoCleanupOnClose = options.autoCleanupOnClose ?? false;

executeOnExit(() => this.close());
const autoCloseOnExit = options.autoCloseOnExit ?? true;
if (autoCloseOnExit) executeOnExit(() => this.close());
}

protected prepareStatements(): Record<string, Statement> {
Expand All @@ -69,6 +72,11 @@ export abstract class BaseMiftahDB implements IMiftahDB {

protected abstract initDatabase(path: string | ":memory:"): void;

protected beforeClose(): void {
this.vacuum();
if (this.autoCleanupOnClose) this.cleanup();
}

private addNamespacePrefix(k: string): string {
return this.nameSpacePrefix ? `${this.nameSpacePrefix}:${k}` : k;
}
Expand Down Expand Up @@ -316,7 +324,7 @@ export abstract class BaseMiftahDB implements IMiftahDB {

@SafeExecution
close(): Result<boolean> {
this.vacuum();
this.beforeClose();

this.db.exec("PRAGMA wal_checkpoint(TRUNCATE)");

Expand Down
3 changes: 2 additions & 1 deletion src/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class MiftahDB extends BaseMiftahDB {
// Overridden due to difference implementation in `bun:sqlite` and `better-sqlite3`
@SafeExecution
override close(): Result<boolean> {
this.vacuum();
this.beforeClose();

this.db.exec("PRAGMA wal_checkpoint(TRUNCATE)");

// @ts-expect-error `deserialize` exists in `bun:sqlite` but not in `better-sqlite3`.
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,6 @@ export const defaultDBOptions = {
mmapSize: 30000000000,
lockingMode: "NORMAL" as "NORMAL" | "EXCLUSIVE",
autoVacuumMode: "OFF" as "OFF" | "FULL" | "INCREMENTAL",
cleanUpOnClose: false as boolean,
autoCleanupOnClose: false as boolean,
autoCloseOnExit: true as boolean,
} as const;

0 comments on commit 5596a08

Please sign in to comment.