From 26592d44bcd7722d73e75b47ef6dc5a31c09b7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Arod?= Date: Thu, 16 Jan 2025 15:01:49 +0100 Subject: [PATCH] refacotr: cache Config --- src/index.ts | 12 ++-- .../fetchFamiliesWithEventsFromNotion.ts | 57 +++++++++++++------ .../properties/relationPropertyToPageIds.ts | 1 + src/notion/utils/queryAllDbResults.ts | 4 +- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index fa6e751..1fd2381 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,12 +50,10 @@ function buildProcessOptions(): ProcessOptions { console.log("Vérification schéma des bases Notion..."); await checkDbSchemas(notionClient); - console.log("Téléchargement des familles..."); - const doFetch = true; - - const donneesFamillesBrutes = doFetch - ? await fetchFamiliesWithEventsFromNotion(notionClient) - : []; + console.log("Téléchargement des données..."); + const donneesFamillesBrutes = await fetchFamiliesWithEventsFromNotion( + notionClient + ); console.log("Nettoyage des données brutes..."); const { familles, messages } = nettoyerDonneesFamilles(donneesFamillesBrutes); @@ -121,7 +119,7 @@ function buildProcessOptions(): ProcessOptions { ); if (options.dryRun) { console.log( - "Dry run => Pas de publication. Les stats sont écrite localement dans el-stats-xxx" + "Dry run => Pas de publication. Les stats sont écrites localement dans el-stats-xxx" ); writeFileSync( diff --git a/src/notion/fetch/fetchFamiliesWithEventsFromNotion.ts b/src/notion/fetch/fetchFamiliesWithEventsFromNotion.ts index 05c9e0e..ccd66e4 100644 --- a/src/notion/fetch/fetchFamiliesWithEventsFromNotion.ts +++ b/src/notion/fetch/fetchFamiliesWithEventsFromNotion.ts @@ -10,7 +10,7 @@ import { relationPropertyToPageId } from "../utils/properties/relationPropertyTo import { selectPropertyToText } from "../utils/properties/selectPropertyToText"; import { statusPropertyToText } from "../utils/properties/statusPropertyToText"; import { titlePropertyToText } from "../utils/properties/titlePropertyToText"; -import { queryAllDbResults } from "../utils/queryAllDbResults"; +import { CacheConfig, queryAllDbResults } from "../utils/queryAllDbResults"; import { richTextPropertyToPlainText } from "../utils/text/richTextPropertyToPlainText"; import { contactsDbId, @@ -36,36 +36,57 @@ type Departement = { name: string; }; export async function fetchFamiliesWithEventsFromNotion( - notionClient: Client + notionClient: Client, + cacheConfig: CacheConfig = { ttl: 3600 * 1000 } ): Promise { const eventPages = ( - await queryAllDbResults(notionClient, { - database_id: familEventsDbId, - sorts: [{ property: "Date", direction: "ascending" }], - }) + await queryAllDbResults( + notionClient, + { + database_id: familEventsDbId, + sorts: [{ property: "Date", direction: "ascending" }], + }, + { cache: cacheConfig } + ) ).filter(isFullPage); const familyPages = ( - await queryAllDbResults(notionClient, { - database_id: familiesDbId, - }) + await queryAllDbResults( + notionClient, + { + database_id: familiesDbId, + }, + { cache: cacheConfig } + ) ).filter(isFullPage); const contactPages = ( - await queryAllDbResults(notionClient, { - database_id: contactsDbId, - }) + await queryAllDbResults( + notionClient, + { + database_id: contactsDbId, + }, + { cache: cacheConfig } + ) ).filter(isFullPage); const missionsPages = ( - await queryAllDbResults(notionClient, { - database_id: missionsDbId, - }) + await queryAllDbResults( + notionClient, + { + database_id: missionsDbId, + }, + { cache: cacheConfig } + ) ).filter(isFullPage); const departementPages = ( - await queryAllDbResults(notionClient, { - database_id: departementsDbId, - }) + await queryAllDbResults( + notionClient, + { + database_id: departementsDbId, + }, + { cache: cacheConfig } + ) ).filter(isFullPage); const departements: Departement[] = departementPages.map((page) => ({ diff --git a/src/notion/utils/properties/relationPropertyToPageIds.ts b/src/notion/utils/properties/relationPropertyToPageIds.ts index b6ea6b8..06518b2 100644 --- a/src/notion/utils/properties/relationPropertyToPageIds.ts +++ b/src/notion/utils/properties/relationPropertyToPageIds.ts @@ -11,5 +11,6 @@ export function relationPropertyToPageIds( `Property ${propName} was expected to have type "relation" but got "${propValue.type}".` ); } + propValue.re; return propValue.relation.map((v) => v.id); } diff --git a/src/notion/utils/queryAllDbResults.ts b/src/notion/utils/queryAllDbResults.ts index e19fa43..858dca2 100644 --- a/src/notion/utils/queryAllDbResults.ts +++ b/src/notion/utils/queryAllDbResults.ts @@ -13,7 +13,7 @@ type QueryExtraOptions = { cache: CacheConfig; }; -type CacheConfig = +export type CacheConfig = | boolean | { ttl: number; @@ -22,7 +22,7 @@ type CacheConfig = export async function queryAllDbResults( notion: Client, dbQuery: QueryDatabaseParameters, - { cache: cacheConfig }: QueryExtraOptions = { cache: { ttl: 3600 * 1000 } } + { cache: cacheConfig }: QueryExtraOptions ): Promise { const queryHash = createHash("sha1") .update(JSON.stringify(dbQuery))