Skip to content

Commit

Permalink
feat: restore and backup now return PromiseResult
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Nov 5, 2024
1 parent 7806adf commit 20ac277
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,15 @@ Backups the database to a file asynchronously.

```javascript
const db = new MiftahDB(":memory:");

db.set("key", "value");
await db.backup("backup-1.db");

const result = await db.backup("backup-1.db");
if (result.success) {
console.log("Backup completed successfully");
} else {
console.log(result.error.message);
}
```

---
Expand All @@ -595,7 +602,14 @@ Restores the database from a backup file asynchronously.

```javascript
const db = new MiftahDB(":memory:");
await db.restore("backup-1.db");

const result = await db.restore("backup-1.db");
if (result.success) {
console.log("Restore completed successfully");
} else {
console.log(result.error.message);
}

console.log(db.get("key"));
```

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

import type { IMiftahDB, MiftahValue, MiftahDBItem, Result } from "./types";
import type {
IMiftahDB,
MiftahValue,
MiftahDBItem,
Result,
PromiseResult,
} from "./types";

export abstract class BaseMiftahDB implements IMiftahDB {
protected declare db: Database;
Expand Down Expand Up @@ -291,20 +297,23 @@ export abstract class BaseMiftahDB implements IMiftahDB {
}

@SafeExecution
async backup(path: string): Promise<void> {
async backup(path: string): PromiseResult<boolean> {
const serialized = this.db.serialize();
const uint8Array = new Uint8Array(serialized);

await writeFile(path, uint8Array);

return { success: true, data: true };
}

@SafeExecution
async restore(path: string) {
async restore(path: string): PromiseResult<boolean> {
const file = await readFile(path);

this.db = new DB(file);

this.statements = this.prepareStatements();

return { success: true, data: true };
}

@SafeExecution
Expand Down
8 changes: 5 additions & 3 deletions src/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DB from "bun:sqlite";
import { readFile } from "node:fs/promises";

import { BaseMiftahDB } from "./base";
import type { Result } from "./types.ts";
import type { Result, PromiseResult } from "./types.ts";
import { SafeExecution } from "./utils";

// Intentionally using a type assertion here to align `bun:sqlite`'s `Database` type with `better-sqlite3`.
Expand Down Expand Up @@ -40,15 +40,17 @@ export class MiftahDB extends BaseMiftahDB {
}

@SafeExecution
override async restore(path: string) {
override async restore(path: string): PromiseResult<boolean> {
const file = await readFile(path);

// @ts-expect-error `deserialize` exists in `bun:sqlite` but not in `better-sqlite3`.
this.db = DB.deserialize(file);

this.statements = this.prepareStatements();

return { success: true, data: true };
}
}

export type { MiftahValue } from "./types";
export type { Result } from "./types";
export type { PromiseResult } from "./types";
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export class MiftahDB extends BaseMiftahDB {
export type { RunResult } from "better-sqlite3";
export type { MiftahValue } from "./types";
export type { Result } from "./types";
export type { PromiseResult } from "./types";
38 changes: 34 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,39 @@ export interface IMiftahDB<T extends MiftahValue = MiftahValue> {
* Backups the database to a file.
* - https://miftahdb.sqlite3.online/docs/api-reference/backup
* @param path - The path to where the backup should be saved.
* @returns The result of the operation, includes a boolean indicating whether the operation was successful or an error if the operation failed.
* @example
* const db = new MiftahDB(":memory:");
*
* db.set("key", "value");
* await db.backup("backup-1.db");
*
* const result = await db.backup("backup-1.db");
* if (result.success) {
* console.log("Backup completed successfully");
* } else {
* console.log(result.error.message);
* }
*/
backup(path: string): Promise<void>;
backup(path: string): PromiseResult<boolean>;

/**
* Restores the database from a backup file.
* - https://miftahdb.sqlite3.online/docs/api-reference/restore
* @param path - The path to the backup file.
* @returns The result of the operation, includes a boolean indicating whether the operation was successful or an error if the operation failed.
* @example
* const db = new MiftahDB(":memory:");
* await db.restore("backup-1.db");
*
* const result = await db.restore("backup-1.db");
* if (result.success) {
* console.log("Restore completed successfully");
* } else {
* console.log(result.error.message);
* }
*
* console.log(db.get("key"));
*/
restore(path: string): Promise<void>;
restore(path: string): PromiseResult<boolean>;

/**
* Creates a namespaced database instance.
Expand Down Expand Up @@ -421,3 +437,17 @@ export type Result<TData, TError extends Error = Error> =
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 20ac277

Please sign in to comment.