Missing files from commit
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* CLI: pull pharmacies from webhook and insert into database.sqlite
|
||||
* CLI: pull pharmacies from webhook and insert into database
|
||||
*
|
||||
* npm run import-farmacias
|
||||
* FARMACIAS_WEBHOOK_URL=https://... npm run import-farmacias
|
||||
@@ -10,12 +10,15 @@
|
||||
* node import-farmacias.js "https://n8n.example/webhook/farmacias" --lat 41.5631 --lon 2.0038 --radio 1500
|
||||
*
|
||||
* Env defaults for region: FARMACIAS_IMPORT_LAT, FARMACIAS_IMPORT_LON, FARMACIAS_IMPORT_RADIO
|
||||
*
|
||||
* Uses PostgreSQL when PG_URL is set, otherwise falls back to local SQLite.
|
||||
*/
|
||||
|
||||
import sqlite3 from 'sqlite3';
|
||||
import { promisify } from 'util';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import pg from 'pg';
|
||||
import {
|
||||
runFarmaciaWebhookImport,
|
||||
DEFAULT_FARMACIAS_WEBHOOK,
|
||||
@@ -24,19 +27,43 @@ import {
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const dbPath = path.join(__dirname, 'database.sqlite');
|
||||
const db = new sqlite3.Database(dbPath);
|
||||
const PG_URL = process.env.PG_URL;
|
||||
let pool = null;
|
||||
let db = null;
|
||||
|
||||
function dbRun(sql, params = []) {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.run(sql, params, function (err) {
|
||||
if (err) reject(err);
|
||||
else resolve({ lastID: this.lastID, changes: this.changes });
|
||||
let dbRun, dbGet;
|
||||
|
||||
if (PG_URL) {
|
||||
pool = new pg.Pool({ connectionString: PG_URL });
|
||||
|
||||
function toPositional(sql) {
|
||||
let i = 0;
|
||||
return sql.replace(/\?/g, () => `$${++i}`);
|
||||
}
|
||||
|
||||
dbGet = async (sql, params = []) => {
|
||||
const res = await pool.query(toPositional(sql), params);
|
||||
return res.rows[0] ?? null;
|
||||
};
|
||||
|
||||
dbRun = async (sql, params = []) => {
|
||||
const res = await pool.query(toPositional(sql), params);
|
||||
return { lastID: res.rows[0]?.id, changes: res.rowCount };
|
||||
};
|
||||
} else {
|
||||
const dbPath = path.join(__dirname, 'database.sqlite');
|
||||
db = new sqlite3.Database(dbPath);
|
||||
|
||||
dbRun = (sql, params = []) =>
|
||||
new Promise((resolve, reject) => {
|
||||
db.run(sql, params, function (err) {
|
||||
if (err) reject(err);
|
||||
else resolve({ lastID: this.lastID, changes: this.changes });
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const dbGet = promisify(db.get.bind(db));
|
||||
dbGet = promisify(db.get.bind(db));
|
||||
}
|
||||
|
||||
function parseCli(argv) {
|
||||
const region = {};
|
||||
@@ -76,6 +103,7 @@ async function main() {
|
||||
const { url, region } = parseCli(process.argv);
|
||||
console.log('Fetching pharmacies from:', url);
|
||||
if (region) console.log('Region query:', region);
|
||||
console.log('Using:', PG_URL ? 'PostgreSQL' : 'SQLite');
|
||||
|
||||
try {
|
||||
const result = await runFarmaciaWebhookImport(dbGet, dbRun, url, region);
|
||||
@@ -97,7 +125,8 @@ async function main() {
|
||||
}
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
db.close();
|
||||
if (pool) await pool.end();
|
||||
else if (db) db.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user