← back to Fromental Internal

scripts/export-jsonl.js

29 lines

#!/usr/bin/env node
/* Export the full fromental_catalog (every column the viewer's load() consumes)
   from LOCAL dw_unified to data/fromental.jsonl — one raw pg row per line.
   Local read only, $0. The JSONL row shape is IDENTICAL to what pg returns, so
   server.js load() maps it unchanged in jsonl mode. */
const fs = require('fs');
const path = require('path');
const { Client } = require('pg');

const DB = process.env.DATABASE_URL || 'postgresql://localhost/dw_unified';
const OUT = path.join(__dirname, '..', 'data', 'fromental.jsonl');

(async () => {
  const client = new Client({ connectionString: DB });
  await client.connect();
  const { rows } = await client.query(`
    SELECT id, mfr_sku, handle, pattern_name, color_name, collection, product_type,
           material, composition, width, roll_length, image_url, gallery_images,
           product_url, description, tags, vendor, price, compare_at_price, quote_only,
           color_hex, dominant_color_hex, ai_colors, ai_styles, ai_tags, ai_description,
           ai_accepted_at, crawled_at, vendor_created_at, updated_at
      FROM fromental_catalog
     ORDER BY (vendor_created_at IS NULL), vendor_created_at DESC NULLS LAST, id DESC`);
  await client.end();
  fs.mkdirSync(path.dirname(OUT), { recursive: true });
  fs.writeFileSync(OUT, rows.map((r) => JSON.stringify(r)).join('\n') + '\n');
  console.log(`[fromental export] wrote ${rows.length} rows -> ${OUT}`);
})().catch((e) => { console.error('export failed:', e); process.exit(1); });