Starting to work with cities
This commit is contained in:
parent
21731dc0ae
commit
2b33fec932
|
@ -40,3 +40,6 @@ Assuming, that the database is running locally and on a port 5432, change the va
|
||||||
### Update:
|
### Update:
|
||||||
``git pull``
|
``git pull``
|
||||||
|
|
||||||
|
### Thanks
|
||||||
|
|
||||||
|
Thanks to https://simplemaps.com/resources/free-country-cities for the free version of a database with cities!
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,42 @@
|
||||||
|
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");
|
||||||
|
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: inFileStream,
|
||||||
|
output: null,
|
||||||
|
crlfDelay: Infinity
|
||||||
|
})
|
||||||
|
|
||||||
|
rl.on('line', async (line) => {
|
||||||
|
let splitted = line.split(",")
|
||||||
|
await db.query(`INSERT INTO cities (name, lat, lng, country) VALUES ($1, $2, $3, $4);`, [splitted[0], splitted[1], splitted[2], splitted[3]])
|
||||||
|
});
|
||||||
|
|
||||||
|
rl.on('close', () => {
|
||||||
|
console.log("done");
|
||||||
|
})
|
File diff suppressed because it is too large
Load Diff
|
@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS users (
|
||||||
language VARCHAR(8) DEFAULT 'en',
|
language VARCHAR(8) DEFAULT 'en',
|
||||||
country VARCHAR(64) DEFAULT NULL,
|
country VARCHAR(64) DEFAULT NULL,
|
||||||
city VARCHAR(64) DEFAULT NULL,
|
city VARCHAR(64) DEFAULT NULL,
|
||||||
|
range DOUBLE PRECISION DEFAULT 20.0,
|
||||||
current_action VARCHAR(16) DEFAULT NULL,
|
current_action VARCHAR(16) DEFAULT NULL,
|
||||||
currently_viewing VARCHAR(64) --link to "room_id"
|
currently_viewing VARCHAR(64) --link to "room_id"
|
||||||
);
|
);
|
||||||
|
@ -37,3 +38,10 @@ CREATE TABLE IF NOT EXISTS messages (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS unique_messages ON messages(sender, recipient);
|
CREATE UNIQUE INDEX IF NOT EXISTS unique_messages ON messages(sender, recipient);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS cities (
|
||||||
|
name VARCHAR(32),
|
||||||
|
lat REAL,
|
||||||
|
lng REAL,
|
||||||
|
country VARCHAR(32)
|
||||||
|
);
|
12
src/db.js
12
src/db.js
|
@ -1,6 +1,7 @@
|
||||||
import pg from 'pg';
|
import pg from 'pg';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { convertMsgType } from './utils.js';
|
import { convertMsgType, logError } from './utils.js';
|
||||||
|
import { exec } from 'child_process';
|
||||||
|
|
||||||
const { Client } = pg;
|
const { Client } = pg;
|
||||||
|
|
||||||
|
@ -16,6 +17,15 @@ const getClient = async () => {
|
||||||
|
|
||||||
await client.query(fs.readFileSync('./scheme.psql').toString());
|
await client.query(fs.readFileSync('./scheme.psql').toString());
|
||||||
|
|
||||||
|
if ((await client.query("SELECT * FROM cities")).rowCount < 6079) {
|
||||||
|
await client.query("DELETE FROM cities");
|
||||||
|
|
||||||
|
//Not sure if pg has support for such kind of things, sooooooooo
|
||||||
|
exec(`psql -h ${process.env.POSTGRES_HOST} -p ${process.env.POSTGRES_PORT} -d ${process.env.POSTGRES_DB} -U ${process.env.POSTGRES_USER} -f ./cities.sql`, (error) => {
|
||||||
|
if (error) logError(error);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue