heart2heart/csvtosql.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-08-09 01:56:19 +03:00
import pg from "pg";
import fs from "fs";
import readline from 'readline';
const { Client } = pg;
const getClient = async () => {
const client = new Client({
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
database: process.env.POSTGRES_DB
});
await client.connect();
await client.query(fs.readFileSync('./scheme.psql').toString());
return client;
};
const db = await getClient();
const file = fs.readFileSync("./output.csv").toString();
const inFileStream = fs.createReadStream("./output.csv");
await db.query("BEGIN");
2024-08-09 01:56:19 +03:00
const rl = readline.createInterface({
input: inFileStream,
output: null,
2024-08-09 01:56:19 +03:00
crlfDelay: Infinity
})
rl.on('line', async (line) => {
let splitted = line.split(",")
console.log(splitted);
await db.query(`INSERT INTO cities (name, lat, lng, country) VALUES ($1, $2, $3, $4)`, [splitted[0], splitted[1], splitted[2], splitted[3]])
2024-08-09 01:56:19 +03:00
});
rl.on('close', async () => {
await db.query("COMMIT");
process.exit(0)
2024-08-09 01:56:19 +03:00
})