Skip to content

Commit

Permalink
feat: deno task db:restore (denoland#535)
Browse files Browse the repository at this point in the history
Prerequisite for denoland#514.
  • Loading branch information
iuioiua authored Sep 8, 2023
1 parent 8f503cb commit 4109f6c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cov/
cov.lcov
.idea
_fresh/
backup.json
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ Use the following commands to work with your local Deno KV database:

- `deno task db:seed` - Populate the database with data from the
[Hacker News API](https://github.com/HackerNews/API).
- `deno task db:dump` - Print all database values.
- `deno task db:dump > backup.json` - Write all database entries to
`backup.json`.
- `deno task db:restore backup.json` - Restore the database from `backup.json`.
- `deno task db:reset` - Reset the database. This is not recoverable.

## Customize and Extend
Expand Down
11 changes: 2 additions & 9 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"tasks": {
"init:stripe": "deno run --allow-read --allow-env --allow-net tasks/init_stripe.ts",
"db:dump": "deno run --allow-read --allow-env --unstable tasks/db_dump.ts",
"db:restore": "deno run --allow-read --allow-env --unstable tasks/db_restore.ts",
"db:seed": "deno run --allow-read --allow-env --allow-net --unstable tasks/db_seed.ts",
"db:migrate": "deno run --allow-read --allow-env --allow-net --unstable tasks/db_migrate.ts",
"db:reset": "deno run --allow-read --allow-env --unstable tasks/db_reset.ts",
Expand Down Expand Up @@ -49,14 +50,6 @@
"fresh",
"recommended"
]
},
"exclude": [
"_fresh"
]
},
"fmt": {
"exclude": [
"_fresh"
]
}
}
}
13 changes: 11 additions & 2 deletions tasks/db_dump.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
/**
* This script prints all entries in the KV database formatted as JSON. This
* can be used to create a backup file.
*
* @example
* ```bash
* deno task db:dump > backup.json
* ```
*/
import { kv } from "@/utils/db.ts";

// https://github.com/GoogleChromeLabs/jsbi/issues/30#issuecomment-521460510
Expand All @@ -8,7 +17,7 @@ function replacer(_key: unknown, value: unknown) {

const iter = kv.list({ prefix: [] });
const items = [];
for await (const res of iter) items.push({ [res.key.toString()]: res.value });
console.log(`${JSON.stringify(items, replacer, 2)}`);
for await (const { key, value } of iter) items.push({ key, value });
console.log(JSON.stringify(items, replacer, 2));

kv.close();
41 changes: 41 additions & 0 deletions tasks/db_restore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
/**
* This script is used to restore a KV database by a file generated by the dump
* script.
*
* @example
* ```
* deno task db:restore backup.json
* ```
*/
import { kv } from "@/utils/db.ts";

interface StoredKvU64 {
value: string;
}

function isStoredKvU64(value: unknown): value is StoredKvU64 {
return (value as StoredKvU64).value !== undefined &&
typeof (value as StoredKvU64).value === "string";
}

function reviver(_key: unknown, value: unknown) {
return isStoredKvU64(value) ? new Deno.KvU64(BigInt(value.value)) : value;
}

if (!confirm("WARNING: The database will be restored. Continue?")) Deno.exit();

const [filePath] = Deno.args;
if (filePath === undefined) throw new Error("File path must be defined");

const rawEntries = Deno.readTextFileSync(filePath);
const entries = JSON.parse(rawEntries, reviver) as Omit<
Deno.KvEntry<unknown>,
"versionstamp"
>[];

const promises = [];
for (const { key, value } of entries) promises.push(kv.set(key, value));
await Promise.all(promises);

kv.close();

0 comments on commit 4109f6c

Please sign in to comment.