Skip to content

Commit

Permalink
getting things from cache!
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Jul 14, 2024
1 parent 9da0fca commit 87fcac8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
31 changes: 31 additions & 0 deletions packages/cli/src/LocalNotionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
QueryDatabaseParameters,
QueryDatabaseResponse,
} from "@notionhq/client/build/src/api-endpoints"
import fs from "fs-extra"

import { executeWithRateLimitAndRetries } from "./executeWithRateLimitAndRetries"
import {
Expand Down Expand Up @@ -272,6 +273,36 @@ export class LocalNotionClient extends Client {
},
}

private loadDataFromJson = (filePath: string) => {
if (fs.existsSync(filePath)) {
const jsonData = fs.readFileSync(filePath, "utf8")
return JSON.parse(jsonData)
}
return undefined
}

loadCacheFromDir = ({
cacheDir,
}: {
// TODO: Add options to load only part of cache
cacheDir: string
}) => {
this.blocksChildrenCache =
this.loadDataFromJson(cacheDir + this.blockChildrenCacheFilename) || {}

this.databaseChildrenCache =
this.loadDataFromJson(cacheDir + this.databaseChildrenCacheFilename) || {}

this.pageObjectsCache =
this.loadDataFromJson(cacheDir + this.pageObjectsCacheFilename) || {}

this.databaseObjectsCache =
this.loadDataFromJson(cacheDir + this.databaseObjectsCacheFilename) || {}

this.blockObjectsCache =
this.loadDataFromJson(cacheDir + this.blocksObjectsCacheFilename) || {}
}

saveCacheToDir = ({
cacheDir,
}: {
Expand Down
17 changes: 6 additions & 11 deletions packages/cli/src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {

// TODO: HACK: until we can add the notion token to the config
options.statusTag = "Published"
const CACHE_DIR = options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/"

const regularNotionClient = initNotionClient(options.notionToken)
notionToMarkdown = new NotionToMarkdown({ notionClient: regularNotionClient })
Expand All @@ -110,6 +111,8 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
auth: options.notionToken,
})

cachedNotionClient.loadCacheFromDir({ cacheDir: CACHE_DIR })

updateNotionClient(cachedNotionClient)

// Do a quick test to see if we can connect to the root so that we can give a better error than just a generic "could not find page" one.
Expand Down Expand Up @@ -177,19 +180,11 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {

// Save pages to a json file
await notionClient.saveCacheToDir({
cacheDir: options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/",
cacheDir: CACHE_DIR,
})

await saveDataToJson(
objectsTree,
options.markdownOutputPath.replace(/\/+$/, "") +
"/.cache/" +
"object_tree.json"
)
await saveDataToJson(
pages,
options.markdownOutputPath.replace(/\/+$/, "") + "/.cache/" + "pages.json"
)
await saveDataToJson(objectsTree, CACHE_DIR + "object_tree.json")
await saveDataToJson(pages, CACHE_DIR + "pages.json")

info(`Found ${pages.length} pages`)
endGroup()
Expand Down

0 comments on commit 87fcac8

Please sign in to comment.