diff --git a/servers/cu/src/domain/client/pouchdb.js b/servers/cu/src/domain/client/pouchdb.js index a67ee0705..c09ef7c6b 100644 --- a/servers/cu/src/domain/client/pouchdb.js +++ b/servers/cu/src/domain/client/pouchdb.js @@ -21,7 +21,7 @@ const inflateP = promisify(inflate) * @type {PouchDB.Database} */ let internalPouchDb -export function createPouchDbClient ({ logger, maxListeners, mode, url }) { +export async function createPouchDbClient ({ logger, maxListeners, mode, url }) { if (internalPouchDb) return internalPouchDb let adapter @@ -40,7 +40,23 @@ export function createPouchDbClient ({ logger, maxListeners, mode, url }) { PouchDb.plugin(PouchDbFind) PouchDb.setMaxListeners(maxListeners) internalPouchDb = new PouchDb(url, { adapter }) - return internalPouchDb + + /** + * Transparently create any indexes we need. + * + * Will noop if the index already exists + */ + return internalPouchDb.createIndex({ + index: { + name: 'cron-evaluations', + fields: ['cron'], + partial_filter_selector: { + cron: { + $exists: true + } + } + } + }).then(() => internalPouchDb) } const processDocSchema = z.object({ @@ -369,7 +385,7 @@ export function saveEvaluationWith ({ pouchDb, logger: _logger }) { } } -export function findEvaluationsWith ({ pouchDb }) { +export function findEvaluationsWith ({ pouchDb = internalPouchDb }) { function createSelector ({ processId, from, to, cron }) { /** * grab all evaluations for the processId, by default @@ -398,7 +414,8 @@ export function findEvaluationsWith ({ pouchDb }) { return pouchDb.find({ selector, sort: [{ _id: 'asc' }], - limit: Number.MAX_SAFE_INTEGER + limit: Number.MAX_SAFE_INTEGER, + ...(cron ? { use_index: 'cron-evaluations' } : {}) }).then((res) => { if (res.warning) console.warn(res.warning) return res.docs diff --git a/servers/cu/src/domain/index.js b/servers/cu/src/domain/index.js index d8ef23bc3..1972e3080 100644 --- a/servers/cu/src/domain/index.js +++ b/servers/cu/src/domain/index.js @@ -16,7 +16,7 @@ export { createLogger } from './logger.js' export { domainConfigSchema } from './model.js' export { errFrom } from './utils.js' -export const createApis = (ctx) => { +export const createApis = async (ctx) => { ctx.logger('Creating business logic apis') const { locate } = schedulerUtilsConnect({ cacheSize: 100, GATEWAY_URL: ctx.GATEWAY_URL }) @@ -33,7 +33,7 @@ export const createApis = (ctx) => { (processId) => locate(processId).catch(err => err) )) }) - const pouchDb = PouchDbClient.createPouchDbClient({ + const pouchDb = await PouchDbClient.createPouchDbClient({ logger: ctx.logger, mode: ctx.DB_MODE, maxListeners: ctx.DB_MAX_LISTENERS, diff --git a/servers/cu/src/routes/middleware/withDomain.js b/servers/cu/src/routes/middleware/withDomain.js index 262fb88aa..fd43bc608 100644 --- a/servers/cu/src/routes/middleware/withDomain.js +++ b/servers/cu/src/routes/middleware/withDomain.js @@ -11,7 +11,7 @@ const domain = { fetch, logger } -domain.apis = createApis(domain) +domain.apis = await createApis(domain) /** * A middleware that exposes the domain business logic to a request