|
| 1 | +// This could be generalised as another event store that is intentionally ephemeral but can be used to store other things. |
| 2 | +// Lets see how well it works for sheet data and if it has value as an approach for other stuff. |
| 3 | + |
| 4 | +import {Client} from '@libsql/client/.'; |
| 5 | +import {Dependencies} from '../../dependencies'; |
| 6 | +import {flow, pipe} from 'fp-ts/lib/function'; |
| 7 | +import * as TE from 'fp-ts/TaskEither'; |
| 8 | +import * as E from 'fp-ts/Either'; |
| 9 | +import * as tt from 'io-ts-types'; |
| 10 | +import * as RA from 'fp-ts/ReadonlyArray'; |
| 11 | +import * as t from 'io-ts'; |
| 12 | +import {failure} from '../../types'; |
| 13 | +import {DomainEvent, EventOfType} from '../../types/domain-event'; |
| 14 | +import {CachedDataTable} from './cached-data-table'; |
| 15 | +import { |
| 16 | + failureWithStatus, |
| 17 | + internalCodecFailure, |
| 18 | +} from '../../types/failure-with-status'; |
| 19 | +import {StatusCodes} from 'http-status-codes'; |
| 20 | + |
| 21 | +const extractCachedEvents = ( |
| 22 | + rawCachedData: string |
| 23 | +): t.Validation< |
| 24 | + ReadonlyArray< |
| 25 | + | EventOfType<'EquipmentTrainingQuizResult'> |
| 26 | + | EventOfType<'EquipmentTrainingQuizSync'> |
| 27 | + > |
| 28 | +> => |
| 29 | + pipe( |
| 30 | + rawCachedData, |
| 31 | + tt.JsonFromString.decode, |
| 32 | + E.chain(tt.JsonArray.decode), |
| 33 | + E.chain(t.readonlyArray(DomainEvent).decode), |
| 34 | + E.map( |
| 35 | + elements => |
| 36 | + elements as ReadonlyArray< |
| 37 | + | EventOfType<'EquipmentTrainingQuizResult'> |
| 38 | + | EventOfType<'EquipmentTrainingQuizSync'> |
| 39 | + > |
| 40 | + ) |
| 41 | + ); |
| 42 | + |
| 43 | +export const getCachedSheetData = |
| 44 | + (dbClient: Client): Dependencies['getCachedSheetData'] => |
| 45 | + (sheetId: string) => |
| 46 | + pipe( |
| 47 | + TE.tryCatch( |
| 48 | + () => |
| 49 | + dbClient.execute({ |
| 50 | + // Currently we can do LIMIT 1 because we only expect each sheet to have a single entry within the cache sheet data |
| 51 | + sql: 'SELECT * FROM cached_sheet_data WHERE sheet_id = $sheetId LIMIT 1', |
| 52 | + args: { |
| 53 | + sheetId, |
| 54 | + }, |
| 55 | + }), |
| 56 | + failureWithStatus( |
| 57 | + 'Failed to get cached sheet data', |
| 58 | + StatusCodes.INTERNAL_SERVER_ERROR |
| 59 | + ) |
| 60 | + ), |
| 61 | + TE.chainEitherK( |
| 62 | + flow( |
| 63 | + CachedDataTable.decode, |
| 64 | + E.mapLeft(internalCodecFailure('Failed to decode cached sheet data')) |
| 65 | + ) |
| 66 | + ), |
| 67 | + TE.map(table => |
| 68 | + pipe( |
| 69 | + table.rows, |
| 70 | + RA.map(row => ({ |
| 71 | + ...row, |
| 72 | + cached_data: extractCachedEvents(row.cached_data), |
| 73 | + })), |
| 74 | + RA.head |
| 75 | + ) |
| 76 | + ) |
| 77 | + ); |
| 78 | + |
| 79 | +// This would be more efficient with a simple key-value store. |
| 80 | +export const cacheSheetData = |
| 81 | + (dbClient: Client): Dependencies['cacheSheetData'] => |
| 82 | + ( |
| 83 | + cacheTimestamp: Date, |
| 84 | + sheetId: string, |
| 85 | + data: ReadonlyArray< |
| 86 | + | EventOfType<'EquipmentTrainingQuizResult'> |
| 87 | + | EventOfType<'EquipmentTrainingQuizSync'> |
| 88 | + > |
| 89 | + ) => |
| 90 | + TE.tryCatch( |
| 91 | + () => |
| 92 | + dbClient |
| 93 | + .execute({ |
| 94 | + sql: ` |
| 95 | + INSERT INTO cached_sheet_data (cached_at, sheet_id, cached_data) |
| 96 | + VALUES ($cachedAt, $sheetId, $cachedData) |
| 97 | + ON CONFLICT (sheet_id) DO UPDATE SET |
| 98 | + cached_at = excluded.cached_at, |
| 99 | + cached_data = excluded.cached_data; |
| 100 | + `, |
| 101 | + args: { |
| 102 | + cachedAt: cacheTimestamp, |
| 103 | + sheetId, |
| 104 | + cachedData: JSON.stringify(data), |
| 105 | + }, |
| 106 | + }) |
| 107 | + .then(() => {}), |
| 108 | + failure('Failed to insert cached sheet data') |
| 109 | + ); |
0 commit comments