-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert-counries.js
101 lines (86 loc) · 2.84 KB
/
insert-counries.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
const { Client } = require('pg');
const client = new Client({
user: 'postgres',
host: '127.0.0.1',
database: 'fuap_dev',
password: 'fuap',
port: 5432,
});
// Datos de ejemplo
// const countries = [];
// const states = [];
const townships = [
{
id: "601170d32b16b26505323905",
name: "Ystalyfera",
stateId: "5ed0196f94ef8a7ce8b674ca"
}
];
// const parishes = [
// {
// id: "5d9c78bc3201d3193c471f50",
// name: "LA BORRACHA",
// townshipId: "5d9b92423201d3193c471ad9"
// }
// ];
async function insertData() {
try {
// Conectar al cliente
await client.connect();
// Insertar países
// for (const country of countries) {
// const insertCountryQuery = `
// INSERT INTO country (_id, name, iso3, iso2, "dialCode")
// VALUES ($1, $2, $3, $4, $5)
// `;
// await client.query(insertCountryQuery, [country._id, country.name, country.iso3, country.iso2, country.dialCode]);
// console.log(`Inserted country: ${country.name}`);
// }
// Insertar estados
for (const state of states) {
// Buscar el ID del país relacionado
const findCountryQuery = `
SELECT id FROM country WHERE _id = $1
`;
const res = await client.query(findCountryQuery, [state.country]);
const countryId = res.rows[0]?.id;
if (countryId) {
const insertStateQuery = `
INSERT INTO state ("_id", "name", "countryId")
VALUES ($1, $2, $3)
`;
await client.query(insertStateQuery, [state._id, state.name, countryId]);
console.log(`Inserted state: ${state.name}`);
} else {
console.error(`Country with ID ${state.country} not found for state ${state.name}`);
}
}
// Insertar townships
// for (const township of townships) {
// const insertTownshipQuery = `
// INSERT INTO township (id, name, stateId, isActive, isDeleted, createdAt, updatedAt)
// VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
// `;
// await client.query(insertTownshipQuery, [township.id, township.name, township.stateId, true, false]);
// console.log(`Inserted township: ${township.name}`);
// }
// // Insertar parroquias
// for (const parish of parishes) {
// const insertParishQuery = `
// INSERT INTO parish (id, name, townshipId, createdAt, updatedAt)
// VALUES ($1, $2, $3, NOW(), NOW())
// `;
// await client.query(insertParishQuery, [parish.id, parish.name, parish.townshipId]);
// console.log(`Inserted parish: ${parish.name}`);
// }
console.log('All data has been inserted');
} catch (error) {
console.error('Error inserting data:', error);
} finally {
// Cerrar la conexión al cliente
await client.end();
console.log('Client has been disconnected');
}
}
// Ejecutar la función de inserción
insertData();