-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoad_TO_MONGO.js
153 lines (144 loc) · 4.66 KB
/
Load_TO_MONGO.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var XLSX = require('xlsx')
var MongoClient = require('mongodb').MongoClient
const dotenv = require('dotenv')
dotenv.config()
var workbook = XLSX.readFile('AI_26012023.xlsx')
var sheet_name_list = workbook.SheetNames
const uri = process.env.MONGO_URI
const client = new MongoClient(uri)
const database = client.db('empoweromics')
const LoadDevelopers = async function (data) {
const dev = data.map(item => {
return {
name: item.developer_name,
website: item.Website,
area: item.Area.toLowerCase(),
city: item.City.toLowerCase(),
country: item.Country.toLowerCase(),
description: item.Description,
logo: item.UrltoLogo,
rating: Math.random() * 4.8,
active: true,
created_at: new Date(),
updated_at: new Date()
}
})
const _developers = database.collection('developers')
// create a document to insert
// this option prevents additional documents from being inserted if one fails
const options = { ordered: true }
const result = await _developers.insertMany(dev, options)
console.log(`A document was inserted with the _id: `)
console.table(result.insertedIds)
}
const LoadProjects = async function (data) {
const pro = data.map(item => {
let geoLocate = item.GeoLocation + ''
let geoLocateArr = geoLocate
.split('((')
.join('')
.split('))')
.join(' ')
.split('POLYGON')
.join(' ')
.split(', ')
.join(' ')
.trim()
.split(' ')
let geoJsonConverted = [[]]
for (let i = 0; i < geoLocateArr.length; i += 2) {
geoJsonConverted[0].push([
parseFloat(geoLocateArr[i]),
parseFloat(geoLocateArr[i + 1])
])
}
let polygonHasNull = false
geoJsonConverted[0].map(secondD =>
secondD.map(item => {
if (!item) polygonHasNull = true
return item
})
)
return {
code: item.ID,
name: item.Name.toLowerCase(),
polygonHasNull,
supplier: item.Supplier_Name,
state: item.ProjectStateName
? item.ProjectStateName.trim().toLowerCase()
: null,
category: item.SupplierCategoryName
? item.SupplierCategoryName.trim().toLowerCase()
: null,
description: item.Description,
area: item.AreaName.toLowerCase(),
city: item.CityName.toLowerCase(),
country: item.CountryName.toLowerCase(),
acres: parseFloat(item.acres),
geoJSON: {
type: 'Polygon',
coordinates: geoJsonConverted
},
rating: Math.random() * 4.8,
active: true,
created_at: new Date(),
updated_at: new Date()
}
})
const _projects = database.collection('projects')
// create a document to insert
// this option prevents additional documents from being inserted if one fails
const options = { ordered: true }
//console.log(pro[0]);
const result = await _projects.insertMany(pro, options)
console.log(`A document was inserted with the _id: `)
console.table(result.insertedIds)
}
const LoadUnits = async function (data) {
const units = data.map(item => {
return {
code: item.Id,
category: item.Property_Category.trim().toLowerCase(),
type: item.Property_Type.trim().replaceAll(' ', '-').toLowerCase(),
finishingType: item.Property_Finishing_Type.trim()
.replaceAll(' ', '-')
.toLowerCase(),
priceBase: parseFloat(item.PriceBase),
spaceBuildUp: parseFloat(item.SpaceBuildUp),
pricePerMeter: parseFloat(item.PricePerMeter),
paymentYears: parseFloat(item.Payment_Years),
estDelivery: parseFloat(item.EstDelivery),
city: item.Project_City.toLowerCase(),
country: item.Project_Country.toLowerCase(),
area: item.Project_Area.toLowerCase(),
developer: item.Developer,
project: item.Project_Name.toLowerCase(),
licence: item.Licence.trim().toLowerCase(),
active: true,
created_at: new Date(),
updated_at: new Date()
}
})
const _units = database.collection('units')
// create a document to insert
// this option prevents additional documents from being inserted if one fails
//console.log(units[0]);
const options = { ordered: true }
const result = await _units.insertMany(units, options)
console.log(`A document was inserted with the _id: `)
console.table(result.insertedIds)
}
async function run() {
try {
let Projects = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]])
// let Developers = XLSX.utils.sheet_to_json(
// workbook.Sheets[sheet_name_list[1]]
// )
// let Units = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[2]])
//
await LoadProjects(Projects)
} finally {
await client.close()
}
}
run().catch(console.dir)