spb/extras/mongo_migrate.js

1import db from "../db.js"; 2 3import fs from "fs"; 4import { dirname } from "path"; 5import { fileURLToPath } from "url"; 6 7const __dirname = dirname(fileURLToPath(import.meta.url)); 8 9const filePresent = fs.existsSync(__dirname + "/pastes.json"); 10if (!filePresent) { 11 console.error(`pastes.json not present in extras folder`); 12 process.exit(1); 13} 14 15import json from "./pastes.json" with { type: "json" }; 16 17function start() { 18 try { 19 for (const p of json) { 20 const n_paste = { 21 id_gen: p.id_gen, 22 content: p.content, 23 created_dt: new Date(p.created_dt["$date"]).toISOString(), 24 }; 25 26 db.run( 27 `INSERT INTO pastes (id_gen, content) VALUES (@id_gen, @content)`, 28 n_paste 29 ); 30 } 31 } catch (e) { 32 console.error(e); 33 } 34} 35 36start();