Starting to work with cities
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/db.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/db.js
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
			
		||||
import pg from 'pg';
 | 
			
		||||
import fs from 'fs';
 | 
			
		||||
import { convertMsgType } from './utils.js';
 | 
			
		||||
import { convertMsgType, logError } from './utils.js';
 | 
			
		||||
import { exec } from 'child_process';
 | 
			
		||||
 | 
			
		||||
const { Client } = pg;
 | 
			
		||||
 | 
			
		||||
@@ -16,6 +17,15 @@ const getClient = async () => {
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user