fetch/fetch-from-mongo.js

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchCountySchema = fetchCountySchema;
const database_1 = require("../config/database");
// GET Endpoint to Retrieve All Databases, Collections, and Their Data
/**
 * Fetches the schema for a specific county from a MongoDB database.
 *
 * @param dbName - The name of the database to connect to.
 * @param county - The name of the county collection to fetch the schema from.
 * @returns A promise that resolves to a `CountySchema` object containing the schema details.
 *          If the schema is not found or an error occurs, a default `CountySchema` object is returned.
 * @throws Will log an error message if there is an issue retrieving data from the database.
 */
function fetchCountySchema(dbName, county) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const db = yield (0, database_1.connectToDatabase)(dbName); // Access each database
            const collectionData = yield db.collection(county).findOne({}); // Fetch the first document
            if (collectionData) {
                const countySchema = {
                    spatialReference: collectionData.spatialReference,
                    esriPinType: collectionData.esriPinType,
                    baseUrls: collectionData.baseUrls,
                    keys: collectionData.keys,
                    examplePin: collectionData.examplePin
                };
                return countySchema;
            }
            return {
                baseUrls: {},
                keys: {
                    booleans: {
                        splitDeed: false,
                        isTimeStamp: false
                    },
                    pinKey: ""
                }
            };
        }
        catch (err) {
            console.error('Error retrieving data:', err);
            return {
                baseUrls: {},
                keys: {
                    booleans: {
                        splitDeed: false,
                        isTimeStamp: false
                    },
                    pinKey: ""
                }
            }; // Return a default CountySchema object in case of an error
        }
        finally {
            //await closeClient();
        }
    });
}