Skip to content

Commit

Permalink
feat: add/update climb should also update Typesense Climb index
Browse files Browse the repository at this point in the history
  • Loading branch information
viet nguyen committed Jul 31, 2023
1 parent 0b0b221 commit 25c3f97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/db/edit/streamListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { changelogDataSource } from '../../model/ChangeLogDataSource.js'
import { logger } from '../../logger.js'
import { BaseChangeRecordType, ResumeToken, UpdateDescription, DBOperation, SupportedCollectionTypes, DocumentKind } from '../ChangeLogType.js'
import { checkVar } from '../index.js'
import { updateAreaIndex } from '../export/Typesense/Client.js'
import { updateAreaIndex, updateClimbIndex } from '../export/Typesense/Client.js'
import { AreaType } from '../AreaTypes.js'
import { exhaustiveCheck } from '../../utils/helpers.js'
import { ClimbType } from '../ClimbTypes.js'

/**
* Start a new stream listener to track changes
Expand Down Expand Up @@ -89,6 +90,7 @@ const recordChange = async ({ source, dbOp, fullDocument, updateDescription, _id
kind: DocumentKind.climbs
}
void changelogDataSource.record(newDocument)
void updateClimbIndex(fullDocument as ClimbType, dbOp)
break
}
case DocumentKind.areas: {
Expand Down
43 changes: 40 additions & 3 deletions src/db/export/Typesense/Client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Typesense, { Client } from 'typesense'

import { areaSchema } from './TypesenseSchemas.js'
import { mongoAreaToTypeSense } from './transformers.js'
import { areaSchema, climbSchema } from './TypesenseSchemas.js'
import { mongoAreaToTypeSense, mongoClimbToTypeSense } from './transformers.js'
import { logger } from '../../../logger.js'
import { AreaType } from '../../AreaTypes.js'
import { DBOperation } from '../../ChangeLogType.js'
import Config from '../../../Config.js'
import { ClimbExtType, ClimbType } from '../../ClimbTypes.js'
import MutableAreaDataSource from '../../../model/MutableAreaDataSource.js'

/**
* Return a Typesense client.
Expand Down Expand Up @@ -50,6 +52,41 @@ export const updateAreaIndex = async (area: AreaType | null, op: DBOperation): P
break
}
} catch (e) {
logger.error('Can\'t update Typesense areanindex: ' + area.area_name)
logger.error({ exception: e.toString() }, 'Can\'t update Typesense Area index: ' + area.area_name)
}
}

/**
* Update/remove a record in Climb index
* @param area
* @param op
*/
export const updateClimbIndex = async (climb: ClimbType | null, op: DBOperation): Promise<void> => {
if (climb == null) return
try {
if (Config.DEPLOYMENT_ENV !== 'production') {
return
}

// Look up additional attrs required by Climb index in Typesense.
const { pathTokens, ancestors } = await MutableAreaDataSource.getInstance().findOneAreaByUUID(climb.metadata.areaRef)

const climbExt: ClimbExtType = {
...climb,
pathTokens,
ancestors
}

switch (op) {
case 'insert':
case 'update':
await typesense()?.collections(climbSchema.name).documents().upsert(mongoClimbToTypeSense(climbExt))
break
case 'delete':
await typesense()?.collections(climbSchema.name).documents().delete(climb._id.toUUID().toString())
break
}
} catch (e) {
logger.error({ exception: e.toString() }, 'Can\'t update Typesense Climb index: ' + climb.name)
}
}
4 changes: 4 additions & 0 deletions src/db/export/Typesense/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export function mongoAreaToTypeSense (doc: AreaType): AreaTypeSenseItem {
}
}

/**
* Convert a Climb object to a Typesense object
* @param doc Climb type
*/
export function mongoClimbToTypeSense (doc: ClimbExtType): ClimbTypeSenseItem {
return {
climbUUID: doc._id.toUUID().toString(),
Expand Down

0 comments on commit 25c3f97

Please sign in to comment.