ANTON KADACH

Generation

code to explanationWed, 01 Feb 2023

const { SessionFactory, Session, ConnectionFactory, Connection } = require("mp-database-pg"); const { geoServicePgConfig, geoServiceMappingChunckSize } = require("../config"); const GeoObject = require("../models/geoObject"); /** * @returns {Promise<Session>} created session */ const getSession = async () => await SessionFactory.createSession(geoServicePgConfig); /** * @returns {Promise<Connection>} created connection */ const getConnection = async () => await ConnectionFactory.createConnection(geoServicePgConfig); /** * * @param {*} dbObj * @returns {GeoObject} */ const toModel = dbObj => new GeoObject( Number(dbObj.id), dbObj.name, Number(dbObj.parent_id), Number(dbObj.level), dbObj.type, dbObj.postal_code, dbObj.has_childs ); const geoObjectLevels = { COUNTRY: 0, REGION: 1, AUTONOMOUS_REGION: 2, AREA: 3, SETTLEMENT: 35, CITY: 4, CITY_AREA: 5, LOCALITY: 6, PLANNING_STRUCTURE: 65, STREET: 7, ADDITIONAL_AREA: 90, ADDITIONAL_AREA_STREET: 91 }; /** * * @param {Session} session * @param {number[]} levels * @returns {Promise<GeoObject[]>} */ const getAll = async (session, levels) => ( await session.query( ` select obj.id, parent_id, level, name, type, postal_code from public.geo_objects obj, geo_objects_attributes attr where obj.is_deleted = false and obj.id = attr.id and level = any($levels) `, { levels } ) ).rows.map(toModel); /** * @param {Session} session * @param {Promise<Map<number, number>>} mappings */ const saveMappings = async (session, mappings) => { const columns = [ { name: "geo_id", prop: "geoId" }, { name: "osm_id", prop: "osmId" }, { name: "geometry", prop: "geometry", mod: ":raw" }, { name: "last_update", prop: "lastUpdate" } ]; await session.bulkUpsert(mappings, { columns, chunkSize: geoServiceMappingChunckSize, table: { schema: "public", table: "geo_polygons" }, onConflict: { columns: ["geo_id"] } }); }; /** * @param {Session} session * @returns {Promise} */ const clearOldMappings = async session => { await session.query("TRUNCATE TABLE public.geo_polygons"); }; const updatePoints = async session => { await session.query( `UPDATE public.geo_polygons SET centroid_point = ST_SetSRID(ST_Centroid(geometry), 3857) ` ); }; const updateSimplifiedPolygons = async (session, tolerance) => { await session.query( `UPDATE public.geo_polygons SET simple_geometry = ST_SetSRID(ST_SimplifyPreserveTopology(geometry, $tolerance), 3857) `, { tolerance } ); }; module.exports = { geoObjectLevels, getSession, getConnection, saveMappings, clearOldMappings, getAll, updatePoints, updateSimplifiedPolygons };

#### What type of requests are there? There are two types of requests: * A POST request to https://api.opencagedata.com/geocode/v1/json * A GET request to https://nominatim.openstreetmap.org/search #### What does the POST request to https://api.opencagedata.com/geocode/v1/json receive as input?

Questions about programming?Chat with your personal AI assistant