Skip to content

Commit

Permalink
chore(cu): create indexes when creating initial pouch client
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 22, 2023
1 parent 65b2ba9 commit b609c98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
25 changes: 21 additions & 4 deletions servers/cu/src/domain/client/pouchdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions servers/cu/src/domain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion servers/cu/src/routes/middleware/withDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b609c98

Please sign in to comment.