Skip to content

Commit

Permalink
feat: added optional configuration options for the database
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Dec 12, 2024
1 parent bc2f47c commit 37cd11c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Creates a new **MiftahDB** instance.

- **Parameters**:
- `path`: The path to the database file. Defaults to ":memory:" if not provided.
- `options`: Optional configuration options for the database.

```javascript
// New MiftahDB instance with disk-based database
Expand Down
27 changes: 25 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,40 @@ import type {
MiftahDBItem,
Result,
PromiseResult,
DBOptions,
} from "./types";

export abstract class BaseMiftahDB implements IMiftahDB {
protected declare db: Database;
protected statements: Record<string, Statement>;
private nameSpacePrefix: string | null = null;

constructor(path = ":memory:") {
constructor(
path = ":memory:",
options: DBOptions = {
journalMode: "WAL",
synchronousMode: "NORMAL",
tempStoreMode: "MEMORY",
cacheSize: -64000,
mmapSize: 30000000000,
lockingMode: "NORMAL",
autoVacuumMode: "OFF",
}
) {
this.initDatabase(path);

this.db.exec(SQL_STATEMENTS.CREATE_PRAGMA);
const formattedPRAGMA = SQL_STATEMENTS.CREATE_PRAGMA.replace(
"%journal_mode",
options.journalMode ?? "WAL"
)
.replace("%synchronous_mode", options.synchronousMode ?? "NORMAL")
.replace("%temp_store_mode", options.tempStoreMode ?? "MEMORY")
.replace("%cache_size", options.cacheSize?.toString() ?? "-64000")
.replace("%mmap_size", options.mmapSize?.toString() ?? "30000000000")
.replace("%locking_mode", options.lockingMode ?? "NORMAL")
.replace("%auto_vacuum_mode", options.autoVacuumMode ?? "OFF");

this.db.exec(formattedPRAGMA);
this.db.exec(SQL_STATEMENTS.CREATE_TABLE);
this.db.exec(SQL_STATEMENTS.CREATE_INDEX);

Expand Down
1 change: 1 addition & 0 deletions src/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { Database } from "better-sqlite3";
* MiftahDB is a wrapper around `bun:sqlite`.
* - https://miftahdb.sqlite3.online/docs/api-reference/constructor
* @param {string} path - Path to the database file. Defaults to ":memory:" if not provided.
* @param {DBOptions} options - Optional configuration options for the database.
* @example
* // New MiftahDB instance with disk-based database
* const db = new MiftahDB("test.db");
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseMiftahDB } from "./base";
* MiftahDB is a wrapper around `better-sqlite3`.
* - https://miftahdb.sqlite3.online/docs/api-reference/constructor
* @param {string} path - Path to the database file. Defaults to ":memory:" if not provided.
* @param {DBOptions} options - Optional configuration options for the database.
* @example
* // New MiftahDB instance with disk-based database
* const db = new MiftahDB("test.db");
Expand Down
12 changes: 7 additions & 5 deletions src/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export const SQL_STATEMENTS = {
// PRAGMA statements
CREATE_PRAGMA: `
PRAGMA wal_checkpoint;
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA temp_store = MEMORY;
PRAGMA cache_size = -64000;
PRAGMA mmap_size = 30000000000;
PRAGMA journal_mode = %journal_mode;
PRAGMA synchronous = %synchronous_mode;
PRAGMA temp_store = %temp_store_mode;
PRAGMA cache_size = %cache_size;
PRAGMA mmap_size = %mmap_size;
PRAGMA locking_mode = %locking_mode;
PRAGMA auto_vacuum = %auto_vacuum_mode;
PRAGMA optimize;
`,
};
86 changes: 52 additions & 34 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@ export type MiftahValue =
| Buffer
| null;

/**
* Represents an item stored in the MiftahDB.
*/
export interface MiftahDBItem {
/** The stored value as a Buffer. */
value: Uint8Array;
/** The expiration timestamp in milliseconds, or null if no expiration. */
expires_at: number | null;
}

/**
* Represents the result of a function that returns a value or an error.
*/
export type Result<TData, TError extends Error = Error> =
| {
success: true;
data: TData;
}
| {
success: false;
error: TError;
};

/**
* Represents the result of a function that returns a value or an error asynchronously.
*/
export type PromiseResult<TData, TError extends Error = Error> = Promise<
| {
success: true;
data: TData;
}
| {
success: false;
error: TError;
}
>;

/**
* Interface for the MiftahDB class, defining its public methods.
*/
Expand Down Expand Up @@ -393,39 +430,20 @@ export interface IMiftahDB<T extends MiftahValue = MiftahValue> {
namespace(name: string): IMiftahDB<T>;
}

/**
* Represents an item stored in the MiftahDB.
/*
Represents the PRAGMA statements that can be used to configure the database.
*/
export interface MiftahDBItem {
/** The stored value as a Buffer. */
value: Uint8Array;
/** The expiration timestamp in milliseconds, or null if no expiration. */
expires_at: number | null;
export type JournalModes = "DELETE" | "TRUNCATE" | "PERSIST" | "WAL" | "MEMORY";
export type SynchronousModes = "OFF" | "NORMAL" | "FULL" | "EXTRA";
export type TempStoreModes = "DEFAULT" | "MEMORY" | "FILE";
export type LockingModes = "NORMAL" | "EXCLUSIVE";
export type AutoVacuumModes = "OFF" | "FULL" | "INCREMENTAL";
export interface DBOptions {
journalMode?: JournalModes;
synchronousMode?: SynchronousModes;
tempStoreMode?: TempStoreModes;
cacheSize?: number;
mmapSize?: number;
lockingMode?: LockingModes;
autoVacuumMode?: AutoVacuumModes;
}

/**
* Represents the result of a function that returns a value or an error.
*/
export type Result<TData, TError extends Error = Error> =
| {
success: true;
data: TData;
}
| {
success: false;
error: TError;
};

/**
* Represents the result of a function that returns a value or an error asynchronously.
*/
export type PromiseResult<TData, TError extends Error = Error> = Promise<
| {
success: true;
data: TData;
}
| {
success: false;
error: TError;
}
>;

0 comments on commit 37cd11c

Please sign in to comment.