Skip to content

Commit

Permalink
- Updated db creation initialization scripts
Browse files Browse the repository at this point in the history
- Refined county and subcounty dataset
  • Loading branch information
kihuha committed Mar 19, 2021
1 parent 427bf1a commit b540bef
Show file tree
Hide file tree
Showing 18 changed files with 7,072 additions and 533 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ module.exports = {
},
plugins: ["@typescript-eslint"],
rules: {},
ignorePatterns: ["dockerfile"],
ignorePatterns: ["dockerfile", "*.csv", "*.sh", "*.sql", "*.json"],
}
22 changes: 20 additions & 2 deletions db/01-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
\connect $APP_DB_NAME $APP_DB_USER
BEGIN;
CREATE TABLE counties (
id SERIAL PRIMARY KEY,
county_code INT PRIMARY KEY,
county_search_string TEXT NOT NULL,
county_name TEXT UNIQUE NOT NULL,
county_code TEXT UNIQUE,
total INT NOT NULL,
male INT NOT NULL,
female INT NOT NULL,
Expand Down Expand Up @@ -41,4 +41,22 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
);
COMMIT;
BEGIN;
CREATE TABLE subcounties (
county_code INT NOT NULL,
subcounty_code INT PRIMARY KEY,
subcounty_search_string TEXT NOT NULL,
subcounty TEXT NOT NULL,
total INT NOT NULL,
male INT NOT NULL,
female INT NOT NULL,
total_households INT NOT NULL,
conventional_households INT NOT NULL,
group_quaters INT NOT NULL,
land_area_sq_km INT NOT NULL,
persons_per_sq_km INT NOT NULL,
CONSTRAINT county_codecounties FOREIGN KEY (county_code) REFERENCES counties(county_code)
);
COMMIT;
EOSQL
96 changes: 48 additions & 48 deletions db/02-counties.sh

Large diffs are not rendered by default.

344 changes: 344 additions & 0 deletions db/03-subcounties.sh

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ services:
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
- APP_DB_USER=docker
- APP_DB_PASS=docker
- APP_DB_NAME=census
- APP_DB_USER=${APP_DB_USER}
- APP_DB_PASS=${APP_DB_PASSWORD}
- APP_DB_NAME=${APP_DB_NAME}
env_file:
- .env
server:
restart: always
build:
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "tsc",
"build:types": "tsc --declaration",
"dev": "nodemon public/index.js",
"dev": "env-cmd nodemon src/index.ts",
"start": "node public/index.js",
"test": "jest",
"format": "prettier --write src/**/*",
Expand Down Expand Up @@ -40,6 +40,7 @@
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"csv-parser": "^3.0.0",
"env-cmd": "^10.1.0",
"eslint": "^7.22.0",
"husky": "=4",
"lint-staged": ">=10",
Expand All @@ -60,5 +61,9 @@
"prettier --write src/**/*",
"eslint --fix"
]
},
"prettier": {
"semi": false,
"printWidth": 100
}
}
8 changes: 4 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import express from "express"
// import country from "./routers/country"
// import counties from "./routers/counties"
import country from "./routers/country"
import counties from "./routers/counties"

const app = express()

app.get("/", async (req, res) => {
return res.json({ test: "Server is running" })
})

// app.use("/api/v1/country", country)
app.use("/api/v1/country", country)

// app.use("/api/v1/counties", counties)
app.use("/api/v1/counties", counties)

export default app
184 changes: 105 additions & 79 deletions src/data/cleaner.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,116 @@
import path from "path"
import fs from "fs"
import { rawCounties } from "./raw/rawCounties"
// import { rawCounties } from "./raw/rawCounties"
import { rawSubcounties } from "./raw/rawSubcounties"

const clean = rawCounties.slice(0).map((county: any) => {
const clean = rawSubcounties.map((subcounty) => {
return {
county_code: county.county_code,
county_name: county.county,
total: Number(county.total.replace(/,/g, "").replace(/ /g, "")),
male: Number(county.male.replace(/,/g, "").replace(/ /g, "")),
female: Number(county.female.replace(/,/g, "").replace(/ /g, "")),
intersex: Number(county.intersex.replace(/,/g, "").replace(/ /g, "")),
total_households: Number(
county.total_households.replace(/,/g, "").replace(/ /g, "")
),
conventional_households: Number(
county.conventional_households.replace(/,/g, "").replace(/ /g, "")
),
group_quaters: Number(
county.group_quaters.replace(/,/g, "").replace(/ /g, "")
),
land_area_sq_km: Number(
county.land_area_sq_km.replace(/,/g, "").replace(/ /g, "")
),
persons_per_sq_km: Number(
county.persons_per_sq_km.replace(/,/g, "").replace(/ /g, "")
),
total_rural: Number(county.total_rural.replace(/,/g, "").replace(/ /g, "")),
male_rural: Number(county.male_rural.replace(/,/g, "").replace(/ /g, "")),
female_rural: Number(
county.female_rural.replace(/,/g, "").replace(/ /g, "")
),
intersex_rural: Number(
county.intersex_rural.replace(/,/g, "").replace(/ /g, "")
),
total_households_rural: Number(
county.total_households_rural.replace(/,/g, "").replace(/ /g, "")
),
conventional_households_rural: Number(
county.conventional_households_rural.replace(/,/g, "").replace(/ /g, "")
),
group_quaters_rural: Number(
county.group_quaters_rural.replace(/,/g, "").replace(/ /g, "")
),
land_area_sq_km_rural: Number(
county.land_area_sq_km_rural.replace(/,/g, "").replace(/ /g, "")
),
persons_per_sq_km_rural: Number(
county.persons_per_sq_km_rural.replace(/,/g, "").replace(/ /g, "")
),
total_urban: Number(county.total_urban.replace(/,/g, "").replace(/ /g, "")),
male_urban: Number(county.male_urban.replace(/,/g, "").replace(/ /g, "")),
female_urban: Number(
county.female_urban.replace(/,/g, "").replace(/ /g, "")
),
intersex_urban: Number(
county.intersex_urban.replace(/,/g, "").replace(/ /g, "")
),
total_households_urban: Number(
county.total_households_urban.replace(/,/g, "").replace(/ /g, "")
),
conventional_households_urban: Number(
county.conventional_households_urban.replace(/,/g, "").replace(/ /g, "")
),
group_quaters_urban: Number(
county.group_quaters_urban.replace(/,/g, "").replace(/ /g, "")
),
land_area_sq_km_urban: Number(
county.land_area_sq_km_urban.replace(/,/g, "").replace(/ /g, "")
),
persons_per_sq_km_urban: Number(
county.persons_per_sq_km_urban.replace(/,/g, "").replace(/ /g, "")
),
county_code: Number(subcounty.county_code),
subcounty_code: Number(subcounty.subcounty_code),
subcounty_search_string: subcounty.subcounty.replace(/'/g, "").replace(/ /g, "-"),
subcounty: subcounty.subcounty.replace(/'/g, "''"),
total: Number(subcounty.total.replace(/,/g, "").replace(/ /g, "")) || 0,
male: Number(subcounty.male.replace(/,/g, "").replace(/ /g, "")) || 0,
female: Number(subcounty.female.replace(/,/g, "").replace(/ /g, "")) || 0,
total_households: Number(subcounty.total_households.replace(/,/g, "").replace(/ /g, "")) || 0,
conventional_households:
Number(subcounty.conventional_households.replace(/,/g, "").replace(/ /g, "")) || 0,
group_quaters: Number(subcounty.group_quaters.replace(/,/g, "").replace(/ /g, "")) || 0,
land_area_sq_km: Number(subcounty.land_area_sq_km.replace(/,/g, "").replace(/ /g, "")) || 0,
persons_per_sq_km: Number(subcounty.persons_per_sq_km.replace(/,/g, "").replace(/ /g, "")) || 0,
}
})

const keys = Object.keys(clean[0])
const values = clean.map((county) => {
const values = Object.values(county)
return `('${values[0]}', '${values[1]}', ${values.slice(2)} )`
const values = clean.map((subcounties) => {
const values = Object.values(subcounties)
return `(${values[0]}, ${values[1]}, '${values[2]}', '${values[3]}', ${values.slice(4)} )`
})

const query = `INSERT INTO counties (${keys.join(",")}) VALUES ${values.join(
","
)};`
const query = `INSERT INTO subcounties (${keys.join(",")}) VALUES ${values.join(",")};`

fs.writeFileSync(
path.resolve(__dirname, "../../src/data/sql/counties.sql"),
query
)
fs.writeFileSync(path.resolve(__dirname, "./sql/subcounties.sql"), query)

// const clean = rawCounties.map((county: any) => {
// return {
// county_code: county.county_code,
// county_name: county.county,
// total: Number(county.total.replace(/,/g, "").replace(/ /g, "")),
// male: Number(county.male.replace(/,/g, "").replace(/ /g, "")),
// female: Number(county.female.replace(/,/g, "").replace(/ /g, "")),
// intersex: Number(county.intersex.replace(/,/g, "").replace(/ /g, "")),
// total_households: Number(
// county.total_households.replace(/,/g, "").replace(/ /g, "")
// ),
// conventional_households: Number(
// county.conventional_households.replace(/,/g, "").replace(/ /g, "")
// ),
// group_quaters: Number(
// county.group_quaters.replace(/,/g, "").replace(/ /g, "")
// ),
// land_area_sq_km: Number(
// county.land_area_sq_km.replace(/,/g, "").replace(/ /g, "")
// ),
// persons_per_sq_km: Number(
// county.persons_per_sq_km.replace(/,/g, "").replace(/ /g, "")
// ),
// total_rural: Number(county.total_rural.replace(/,/g, "").replace(/ /g, "")),
// male_rural: Number(county.male_rural.replace(/,/g, "").replace(/ /g, "")),
// female_rural: Number(
// county.female_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// intersex_rural: Number(
// county.intersex_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// total_households_rural: Number(
// county.total_households_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// conventional_households_rural: Number(
// county.conventional_households_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// group_quaters_rural: Number(
// county.group_quaters_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// land_area_sq_km_rural: Number(
// county.land_area_sq_km_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// persons_per_sq_km_rural: Number(
// county.persons_per_sq_km_rural.replace(/,/g, "").replace(/ /g, "")
// ),
// total_urban: Number(county.total_urban.replace(/,/g, "").replace(/ /g, "")),
// male_urban: Number(county.male_urban.replace(/,/g, "").replace(/ /g, "")),
// female_urban: Number(
// county.female_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// intersex_urban: Number(
// county.intersex_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// total_households_urban: Number(
// county.total_households_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// conventional_households_urban: Number(
// county.conventional_households_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// group_quaters_urban: Number(
// county.group_quaters_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// land_area_sq_km_urban: Number(
// county.land_area_sq_km_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// persons_per_sq_km_urban: Number(
// county.persons_per_sq_km_urban.replace(/,/g, "").replace(/ /g, "")
// ),
// }
// })

// const keys = Object.keys(clean[0])
// const values = clean.map((county) => {
// const values = Object.values(county)
// return `('${values[0]}', '${values[1]}', ${values.slice(2)} )`
// })

// const query = `INSERT INTO counties (${keys.join(",")}) VALUES ${values.join(
// ","
// )};`

// fs.writeFileSync(path.resolve(__dirname, "./sql/counties.sql"), query)
1,460 changes: 1,459 additions & 1 deletion src/data/raw/counties.json

Large diffs are not rendered by default.

Loading

0 comments on commit b540bef

Please sign in to comment.