Skip to content

Commit

Permalink
remove unused middleware functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Jan 15, 2025
1 parent c2a85cd commit 309f105
Showing 1 changed file with 0 additions and 75 deletions.
75 changes: 0 additions & 75 deletions src/middleware/middleware.builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import { templateSchema } from '../routes/schemas.js'
import { render } from '../utils/custom-renderer.js'
import datasette from '../services/datasette.js'
import * as v from 'valibot'
import { dataSubjects } from '../utils/utils.js'

const availableDatasets = Object.values(dataSubjects).flatMap((dataSubject) =>
(dataSubject.dataSets || [])
.filter((dataset) => dataset.available)
.map((dataset) => dataset.value)
)

export const FetchOptions = {
/**
Expand Down Expand Up @@ -121,56 +114,6 @@ export async function fetchManyFn (req, res, next) {
}
}

export async function fetchOneFromAllDatasetsFn (req, res, next) {
try {
const query = this.query({ req, params: req.params })
const promises = availableDatasets.map((dataset) => {
return datasette.runQuery(query, dataset).catch(error => {
logger.error('Query failed for dataset', { dataset, errorMessage: error.message, errorStack: error.stack, type: types.DataFetch })
throw error
})
})
const result = await Promise.all(promises)
req[this.result] = Object.fromEntries(
result.reduce((acc, { formattedData }, i) => {
if (formattedData.length > 0) {
acc.push([availableDatasets[i], formattedData[0]])
}
return acc
}, [])
)
logger.debug({ type: types.DataFetch, message: 'fetchOneFromAllDatasets', resultKey: this.result })
next()
} catch (error) {
logger.debug('fetchOneFromAllDatasetsFn: failed', { type: types.DataFetch, errorMessage: error.message, endpoint: req.originalUrl, resultKey: this.result })
req.handlerName = `fetching '${this.result}'`
next(error)
}
}

export async function fetchManyFromAllDatasetsFn (req, res, next) {
try {
const query = this.query({ req, params: req.params })
const promises = availableDatasets.map((dataset) => {
return datasette.runQuery(query, dataset).catch(error => {
logger.error('Query failed for dataset', { dataset, errorMessage: error.message, errorStack: error.stack, type: types.DataFetch })
throw error
})
})
const result = await Promise.all(promises)
req[this.result] = Object.fromEntries(
result.filter(({ formattedData }) => formattedData.length > 0)
.map(({ formattedData }, i) => [availableDatasets[i], formattedData])
)
logger.debug({ type: types.DataFetch, message: 'fetchManyFromAllDatasets', resultKey: this.result })
next()
} catch (error) {
logger.debug('fetchManyFromAllDatasetsFn: failed', { type: types.DataFetch, errorMessage: error.message, endpoint: req.originalUrl, resultKey: this.result })
req.handlerName = `fetching '${this.result}'`
next(error)
}
}

/**
* Middleware. Does a conditional fetch. Optionally invokes `else` if condition is false.
*
Expand Down Expand Up @@ -235,24 +178,6 @@ export function fetchMany (context) {
return fetchManyFn.bind(context)
}

/**
* Fetches a single record from each dataset databases and stores them in `req` under key specified by `result` entry.
*
* @param {{query: ({req, params}) => object, result: string, dataset?: FetchParams | (req) => string}} context
*/
export function fetchOneFromAllDatasets (context) {
return fetchOneFromAllDatasetsFn.bind(context)
}

/**
* Fetches a collection of records from all dataset databases and stores them in `req` under key specified by `result` entry.
*
* @param {{query: ({req, params}) => object, result: string, dataset?: FetchParams | (req) => string}} context
*/
export function fetchManyFromAllDatasets (context) {
return fetchManyFromAllDatasetsFn.bind(context)
}

/**
* Looks up schema for name in @{link templateSchema} (defaults to any()), validates and renders the template.
*
Expand Down

0 comments on commit 309f105

Please sign in to comment.