-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
46 lines (39 loc) · 1.29 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require('dotenv').config()
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const db = require('./db')
const DATASET_FILE = process.argv[2]
if (DATASET_FILE) {
const filePath = path.join(__dirname, DATASET_FILE)
fs.readFile(filePath, { encoding: 'utf-8' }, function (readFileError, censusFile) {
if (!readFileError) {
const censusArray = censusFile.trim().split('\n')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(
`The tables will be dropped and created again with the new data (${censusArray.length} entries for the census). Proceed? (y/N) `,
async function (answer) {
if (answer === 'y') {
try {
await db.sequelize.sync({ force: true })
await db.census.bulkCreate(censusArray.map(code => ({ code })))
await db.sequelize.close()
console.log('Data loaded correctly.')
} catch (sqliteError) {
console.error(sqliteError)
}
} else {
console.log('Cancelled.')
}
rl.close()
})
} else {
console.error(readFileError)
}
})
} else {
console.error('Please provide a data file.')
}