← back to Filemaker Mcp
pull-grs-mfr.mjs
48 lines
// Pull ALL Series=GRS records from FileMaker WALLPAPER -> CSV on disk.
// Read-only. Reuses the MCP fm-client (Claris ID auth). For the GRS mfr recovery job.
import fs from 'fs';
const env = JSON.parse(fs.readFileSync('/tmp/fmenv.json', 'utf8'));
for (const [k, v] of Object.entries(env)) process.env[k] = v;
const fm = await import('./src/fm-client.js');
const DB = 'WALLPAPER', LAYOUT = 'Basic List of Fields', PAGE = 500;
const OUT = process.env.HOME + '/Desktop/GRS_fm_all_records_' +
new Date().toISOString().slice(0, 10).replace(/-/g, '') + '.csv';
const csvq = s => '"' + String(s ?? '').replace(/"/g, '""').replace(/\r?\n/g, ' ') + '"';
const rows = [];
let offset = 1, total = null, pages = 0;
const t0 = Date.now();
while (true) {
let res, tries = 0;
while (true) {
try { res = await fm.findRecords(DB, LAYOUT, [{ Series: 'GRS' }], { limit: PAGE, offset }); break; }
catch (e) {
const msg = String(e?.message || e);
if (msg.includes('[401]')) { res = { records: [], dataInfo: { totalRecordCount: total ?? 0 } }; break; } // no more
if (++tries >= 4) throw e;
await new Promise(r => setTimeout(r, 1500 * tries));
}
}
total = res?.dataInfo?.totalRecordCount ?? total;
const recs = res?.records || [];
if (!recs.length) break;
for (const r of recs) {
const f = r.fieldData || {};
rows.push([
f['combo sku'], f['Mfr Pattern'], f['JS Pattern'], f['Series'],
f['Supplier'], f['Net Price'], f['Width'], f['JPG Name'],
r.recordId, f['Date Line Put Up:']
]);
}
pages++; offset += PAGE;
if (pages % 2 === 0) console.error(` page ${pages} offset ${offset - 1}/${total} rows=${rows.length} ${((Date.now() - t0) / 1000 | 0)}s`);
if (total && offset > total) break;
}
const header = ['combo_sku', 'mfr_pattern', 'js_pattern', 'series', 'supplier', 'net_price', 'width', 'jpg_name', 'fm_record_id', 'date_put_up'];
fs.writeFileSync(OUT, header.join(',') + '\n' + rows.map(r => r.map(csvq).join(',')).join('\n') + '\n');
console.error(`DONE pages=${pages} total=${total} rows=${rows.length} -> ${OUT} ${((Date.now() - t0) / 1000 | 0)}s`);
console.log(JSON.stringify({ out: OUT, rows: rows.length, total }));