const saveMappingsToGeoService = async (osmConnection, geoSession, mappings) => { const insertingMappings = Array.from(mappings.entries()); // eslint-disable-next-line no-unused-vars const osmIds = insertingMappings.map(([_, osmObject]) => osmObject.osmId); const osmGeometry = await osmRepository.getGeometry(osmConnection, osmIds); const osmGeometryMap = new Map(osmGeometry.map(({ id, geometry }) => [Number(id), geometry])); const insertingValues = insertingMappings.map(([geoId, osmObject]) => ({ geoId, osmId: osmObject.osmId, geometry: `ST_GeometryFromText('${osmGeometryMap.get(osmObject.osmId)}')`, lastUpdate: new Date() })); await geoRepository.clearOldMappings(geoSession); await geoRepository.saveMappings(geoSession, insertingValues); await geoRepository.updatePoints(geoSession); await geoRepository.updateSimplifiedPolygons(geoSession, simplifyingTolerance); };
The function saveMappingsToGeoService initially get the mappings of osm objects to geoids (geoId) and osm ids (osmId). Then the member function getGeometry of the class osmRepository is called to get the geometry of the osm objects. This will then be stored in a map whose key is the osm id and the value is the geometry. The function then creates an array of objects whose members are geoId, osmId, geometry, and lastUpdate. These objects will then be inserted into the database through the member function saveMappings of the class geoRepository. Note that the member function saveMappings is called with a session argument. This is a custom wrapper for pgClient's member function query which is called on an instance of pgClient. The session object is used to group calls to pgClient's member function query and to make sure the member function end is called at the end which finalizes the transaction. The function updatePoints of the class geoRepository is then called to update the points table. This will create a new table with entries of the form (geoId, geometry) where geometry is the centroid of