-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from oliver-oloughlin/patch/export-import
feat: migrate script & function
- Loading branch information
Showing
5 changed files
with
757 additions
and
2 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
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
{ | ||
"name": "@olli/kvdex", | ||
"version": "0.34.1", | ||
"version": "0.34.2", | ||
"exports": { | ||
".": "./mod.ts", | ||
"./ext/zod": "./ext/zod.ts" | ||
"./ext/zod": "./ext/zod.ts", | ||
"./ext/migrate": "./ext/migrate.ts" | ||
}, | ||
"imports": { | ||
"assert": "jsr:@std/[email protected]/assert", | ||
|
@@ -13,6 +14,7 @@ | |
"ulid": "jsr:@std/[email protected]", | ||
"concat": "jsr:@std/[email protected]/concat", | ||
"deep_merge": "jsr:@std/[email protected]/deep_merge", | ||
"parse_args": "jsr:@std/[email protected]/parse_args", | ||
"zlib": "node:zlib", | ||
"zod": "npm:zod@3" | ||
}, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,63 @@ | ||
import { parseArgs } from "parse_args" | ||
import { KVDEX_KEY_PREFIX } from "../src/constants.ts" | ||
|
||
if (import.meta.main) { | ||
const { source, target, all } = parseArgs(Deno.args, { | ||
string: ["source", "target"], | ||
boolean: ["all"], | ||
}) | ||
|
||
if (!source) { | ||
console.log( | ||
"A source KV path to export from must be provided using the --source argument", | ||
) | ||
Deno.exit() | ||
} | ||
|
||
if (!target) { | ||
console.log( | ||
"A target KV path to export to must be provided using the --target argument", | ||
) | ||
Deno.exit() | ||
} | ||
|
||
using sourceKv = await Deno.openKv(source) | ||
using targetKv = await Deno.openKv(target) | ||
|
||
await migrate({ | ||
source: sourceKv, | ||
target: targetKv, | ||
all, | ||
}) | ||
} | ||
|
||
export type MigrateOptions = { | ||
/** Source KV. */ | ||
source: Deno.Kv | ||
|
||
/** Target KV. */ | ||
target: Deno.Kv | ||
|
||
/** | ||
* Flag indicating whether to migrate all entries or only kvdex specific entries. | ||
* | ||
* @default false | ||
*/ | ||
all?: boolean | ||
} | ||
|
||
/** | ||
* Migrate entries from a source KV instance to a target KV instance. | ||
* | ||
* @param options - Migrate options | ||
*/ | ||
export async function migrate({ | ||
source, | ||
target, | ||
all, | ||
}: MigrateOptions): Promise<void> { | ||
const iter = source.list({ prefix: all ? [] : [KVDEX_KEY_PREFIX] }) | ||
for await (const { key, value } of iter) { | ||
await target.set(key, value) | ||
} | ||
} |
Oops, something went wrong.