From ba3f81695512ebf27675546dd8f2bd581a967540 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 2 Aug 2024 16:54:35 -0500 Subject: [PATCH] Add db:import task --- deno.json | 1 + deno.lock | 8 ++++++++ scripts/db-export.ts | 14 +++++++++++--- scripts/db-import.ts | 24 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 scripts/db-import.ts diff --git a/deno.json b/deno.json index 1f554639..964490c6 100644 --- a/deno.json +++ b/deno.json @@ -6,6 +6,7 @@ "dev": "deno run -A --watch src/server.ts", "hook": "deno run --allow-read --allow-run --allow-write https://deno.land/x/deno_hooks@0.1.1/mod.ts", "db:export": "deno run -A scripts/db-export.ts", + "db:import": "deno run -A scripts/db-import.ts", "db:migrate": "deno run -A scripts/db-migrate.ts", "nostr:pull": "deno run -A scripts/nostr-pull.ts", "debug": "deno run -A --inspect src/server.ts", diff --git a/deno.lock b/deno.lock index 8565f781..5b403d44 100644 --- a/deno.lock +++ b/deno.lock @@ -35,9 +35,11 @@ "jsr:@std/fs@^0.229.3": "jsr:@std/fs@0.229.3", "jsr:@std/internal@^1.0.0": "jsr:@std/internal@1.0.1", "jsr:@std/io@^0.224": "jsr:@std/io@0.224.4", + "jsr:@std/json@^0.223.0": "jsr:@std/json@0.223.0", "jsr:@std/media-types@^0.224.1": "jsr:@std/media-types@0.224.1", "jsr:@std/path@0.217": "jsr:@std/path@0.217.0", "jsr:@std/path@^0.221.0": "jsr:@std/path@0.221.0", + "jsr:@std/streams@^0.223.0": "jsr:@std/streams@0.223.0", "npm:@isaacs/ttlcache@^1.4.1": "npm:@isaacs/ttlcache@1.4.1", "npm:@noble/hashes@^1.4.0": "npm:@noble/hashes@1.4.0", "npm:@scure/base@^1.1.6": "npm:@scure/base@1.1.6", @@ -272,6 +274,9 @@ "jsr:@std/bytes@^1.0.2-rc.3" ] }, + "@std/json@0.223.0": { + "integrity": "9a4a255931dd0397924c6b10bb6a72fe3e28ddd876b981ada2e3b8dd0764163f" + }, "@std/media-types@0.224.1": { "integrity": "9e69a5daed37c5b5c6d3ce4731dc191f80e67f79bed392b0957d1d03b87f11e1" }, @@ -286,6 +291,9 @@ "dependencies": [ "jsr:@std/assert@^0.221.0" ] + }, + "@std/streams@0.223.0": { + "integrity": "d6b28e498ced3960b04dc5d251f2dcfc1df244b5ec5a48dc23a8f9b490be3b99" } }, "npm": { diff --git a/scripts/db-export.ts b/scripts/db-export.ts index 80b82162..fbdac1b7 100644 --- a/scripts/db-export.ts +++ b/scripts/db-export.ts @@ -4,13 +4,21 @@ const store = await Storages.db(); console.warn('Exporting events...'); +let count = 0; + for await (const msg of store.req([{}])) { - if (msg[0] === 'EVENT') console.log(JSON.stringify(msg[2])); - if (msg[0] === 'EOSE') break; + if (msg[0] === 'EOSE') { + break; + } + if (msg[0] === 'EVENT') { + console.log(JSON.stringify(msg[2])); + count++; + } if (msg[0] === 'CLOSED') { console.error('Database closed unexpectedly'); break; } } -console.warn('Done!'); +console.warn(`Exported ${count} events`); +Deno.exit(); diff --git a/scripts/db-import.ts b/scripts/db-import.ts new file mode 100644 index 00000000..f33e7e56 --- /dev/null +++ b/scripts/db-import.ts @@ -0,0 +1,24 @@ +import { NostrEvent } from '@nostrify/nostrify'; +import { JsonParseStream } from '@std/json/json-parse-stream'; +import { TextLineStream } from '@std/streams/text-line-stream'; + +import { Storages } from '@/storages.ts'; + +const store = await Storages.db(); + +console.warn('Importing events...'); + +let count = 0; + +const readable = Deno.stdin.readable + .pipeThrough(new TextDecoderStream()) + .pipeThrough(new TextLineStream()) + .pipeThrough(new JsonParseStream()); + +for await (const event of readable) { + await store.event(event as unknown as NostrEvent); + count++; +} + +console.warn(`Imported ${count} events`); +Deno.exit();