Skip to content

Commit

Permalink
feat: improve optional configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Dec 12, 2024
1 parent 37cd11c commit 43c16f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,40 @@ if (result.success) {

## API Reference

### `Constructor`
### Constructor

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.
- `options`: Optional configuration options for the database. These options can be customized as follows:
- `journalMode`: Determines the journal mode (default: `"WAL"`).
- `synchronousMode`: Controls the database's synchronization mode (default: `"NORMAL"`).
- `tempStoreMode`: Specifies where temporary tables are stored (default: `"MEMORY"`).
- `cacheSize`: The cache size for the database (default: `-64000`).
- `mmapSize`: The memory-mapped file size (default: `30000000000`).
- `lockingMode`: Determines the database locking mode (default: `"NORMAL"`).
- `autoVacuumMode`: Configures the auto-vacuum behavior (default: `"OFF"`).

#### Example Usage

```javascript
// New MiftahDB instance with disk-based database
const db = new MiftahDB("test.db");
const db1 = new MiftahDB("test.db");

// New MiftahDB instance with in-memory database
const db = new MiftahDB(":memory:");
const db2 = new MiftahDB(":memory:");

// New MiftahDB instance with custom configuration
const db3 = new MiftahDB("test.db", {
journalMode: "DELETE",
synchronousMode: "FULL",
tempStoreMode: "FILE",
cacheSize: -128000,
mmapSize: 50000000000,
lockingMode: "EXCLUSIVE",
autoVacuumMode: "INCREMENTAL",
});
```

---
Expand Down
14 changes: 2 additions & 12 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SQL_STATEMENTS } from "./statements";
import { encodeValue, decodeValue } from "./encoding";
import { SafeExecution, getExpireDate } from "./utils";

import { defaultDBOptions } from "./types";
import type {
IMiftahDB,
MiftahValue,
Expand All @@ -23,18 +24,7 @@ export abstract class BaseMiftahDB implements IMiftahDB {
protected statements: Record<string, Statement>;
private nameSpacePrefix: string | null = null;

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

const formattedPRAGMA = SQL_STATEMENTS.CREATE_PRAGMA.replace(
Expand Down
20 changes: 11 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,14 @@ 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;
}

export const defaultDBOptions = {
journalMode: "WAL",
synchronousMode: "NORMAL",
tempStoreMode: "MEMORY",
cacheSize: -64000,
mmapSize: 30000000000,
lockingMode: "NORMAL",
autoVacuumMode: "OFF",
} as const;
export type DBOptions = typeof defaultDBOptions;

0 comments on commit 43c16f4

Please sign in to comment.