forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
deno task db:restore
(denoland#535)
Prerequisite for denoland#514.
- Loading branch information
Showing
5 changed files
with
58 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ cov/ | |
cov.lcov | ||
.idea | ||
_fresh/ | ||
backup.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |