-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadCities.js
73 lines (57 loc) · 1.7 KB
/
uploadCities.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import admin from 'firebase-admin'
import dotenv from 'dotenv'
import path from 'path'
import fs from 'fs'
import { v4 as uuidv4 } from 'uuid'
import csv from 'csv-parser'
import { City, Country } from './models/Location'
dotenv.config()
admin.initializeApp({
credential: admin.credential.applicationDefault(),
storageBucket: 'gs://apec-2021-nz.appspot.com',
})
const saveImage = async (imagePath) => {
const stream = fs.createReadStream(imagePath)
const file = admin
.storage()
.bucket()
.file(uuidv4() + path.extname(imagePath))
// Upload the file to gcs
await new Promise((resolve, reject) => {
const writeStream = file.createWriteStream()
writeStream.on('finish', resolve)
writeStream.on('error', reject)
stream.pipe(writeStream)
})
return file.publicUrl()
}
let cities = []
const main = async () => {
await new Promise(function (resolve, reject) {
fs.createReadStream('cities.csv')
.pipe(csv())
.on('data', async (object) => {
cities.push(object)
})
.on('end', () => {
resolve()
})
})
const country = Tag.init()
country.id = '1'
country.name = 'New Zealand'
await country.save()
let count = 1
for (const { city, country } of cities) {
if (country !== 'New Zealand') continue
//const imagepath = `./image/city/${city.split(' ').join('_')}.png`
//await saveImage(imagepath)
const cityModel = City.init()
cityModel.id = `${count}`
cityModel.name = city
cityModel.country = '/Country/1'
await cityModel.save()
count++
}
}
main()