Skip to content

Commit f402551

Browse files
committed
feat(notion): add cache system
1 parent 510741c commit f402551

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

packages/@contentlayer/source-notion/src/fetchData/fetchAllDocuments.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { makeCacheItem } from './makeCacheItem.js'
1010

1111
export type FetchAllDocumentsArgs = {
1212
databaseTypeDefs: DatabaseTypeDef[]
13+
previousCache: core.DataCache.Cache | undefined
1314
schemaDef: core.SchemaDef
1415
options: core.PluginOptions
1516
}
1617

17-
export const fetchAllDocuments = ({ databaseTypeDefs, schemaDef, options }: FetchAllDocumentsArgs) =>
18+
export const fetchAllDocuments = ({ databaseTypeDefs, previousCache, schemaDef, options }: FetchAllDocumentsArgs) =>
1819
pipe(
1920
T.forEachPar_(databaseTypeDefs, (databaseTypeDef) =>
2021
pipe(
@@ -27,6 +28,7 @@ export const fetchAllDocuments = ({ databaseTypeDefs, schemaDef, options }: Fetc
2728
page,
2829
documentTypeDef: schemaDef.documentTypeDefMap[databaseTypeDef.name]!,
2930
databaseTypeDef,
31+
previousCache,
3032
options,
3133
}),
3234
),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as core from '@contentlayer/core'
2+
import { OT, pipe, T } from '@contentlayer/utils/effect'
3+
4+
import type { DatabaseTypeDef } from '../schema/types/database.js'
5+
import { fetchAllDocuments } from './fetchAllDocuments.js'
6+
7+
export type FetchDataArgs = {
8+
databaseTypeDefs: DatabaseTypeDef[]
9+
schemaDef: core.SchemaDef
10+
options: core.PluginOptions
11+
}
12+
13+
export const fetchData = ({ schemaDef, databaseTypeDefs, options }: FetchDataArgs) => {
14+
const resolveParams = pipe(core.DataCache.loadPreviousCacheFromDisk({ schemaHash: schemaDef.hash }), T.either)
15+
return pipe(
16+
T.rightOrFail(resolveParams),
17+
T.chain((cache) =>
18+
pipe(
19+
fetchAllDocuments({ schemaDef, databaseTypeDefs, previousCache: cache, options }),
20+
T.tap((cache_) => T.succeedWith(() => (cache = cache_))),
21+
T.tap((cache_) => core.DataCache.writeCacheToDisk({ cache: cache_, schemaHash: schemaDef.hash })),
22+
),
23+
),
24+
OT.withSpan('@contentlayer/source-notion/fetchData:fetchData'),
25+
T.mapError((error) => new core.SourceFetchDataError({ error, alreadyHandled: false })),
26+
)
27+
}

packages/@contentlayer/source-notion/src/fetchData/makeCacheItem.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,38 @@ export type MakeCacheItemArgs = {
1111
databaseTypeDef: DatabaseTypeDef
1212
documentTypeDef: core.DocumentTypeDef
1313
page: PageObjectResponse
14+
previousCache: core.DataCache.Cache | undefined
1415
options: core.PluginOptions
1516
}
1617

17-
export const makeCacheItem = ({ databaseTypeDef, documentTypeDef, page, options }: MakeCacheItemArgs) =>
18+
export const makeCacheItem = ({ databaseTypeDef, documentTypeDef, previousCache, page, options }: MakeCacheItemArgs) =>
1819
pipe(
1920
T.gen(function* ($) {
20-
const document = yield* $(makeDocument({ documentTypeDef, databaseTypeDef, page, options }))
21+
const documentHash = new Date(page.last_edited_time).getTime().toString()
22+
23+
if (
24+
previousCache &&
25+
previousCache.cacheItemsMap[page.id] &&
26+
previousCache.cacheItemsMap[page.id]!.documentHash === documentHash &&
27+
previousCache.cacheItemsMap[page.id]!.hasWarnings === false
28+
) {
29+
const cacheItem = previousCache.cacheItemsMap[page.id]!
30+
return cacheItem
31+
}
2132

33+
const document = yield* $(makeDocument({ documentTypeDef, databaseTypeDef, page, options }))
2234
const computedValues = yield* $(getComputedValues({ document, documentTypeDef }))
2335

2436
Object.entries(computedValues).forEach(([fieldName, value]) => {
2537
document[fieldName] = value
2638
})
2739

28-
return document
40+
return {
41+
document,
42+
documentHash,
43+
hasWarnings: false,
44+
documentTypeName: documentTypeDef.name,
45+
}
2946
}),
30-
T.chain((document) =>
31-
pipe(
32-
hashObject(document),
33-
T.map((hash) => ({
34-
document,
35-
documentHash: hash,
36-
hasWarnings: false,
37-
documentTypeName: documentTypeDef.name,
38-
})),
39-
),
40-
),
4147
OT.withSpan('@contentlayer/source-notion/fetchData:makeCacheItem'),
4248
)

packages/@contentlayer/source-notion/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { pipe, S, T } from '@contentlayer/utils/effect'
44
import { NotionRenderer } from '@notion-render/client'
55
import * as notion from '@notionhq/client'
66

7-
import { fetchAllDocuments } from './fetchData/fetchAllDocuments.js'
7+
import { fetchData } from './fetchData/fetchData.js'
88
import { fetchNotion } from './notion/fetchNotion.js'
99
import { provideSchema } from './schema/provideSchema.js'
1010
import { flattendDatabaseTypeDef } from './schema/utils/flattenDatabaseTypeDef.js'
@@ -49,7 +49,7 @@ export const makeSource: core.MakeSourcePlugin<PluginOptions & core.PartialArgs>
4949
pipe(
5050
S.fromEffect(
5151
pipe(
52-
fetchAllDocuments({
52+
fetchData({
5353
databaseTypeDefs: databaseTypeDefs.map((databaseTypeDef) => flattendDatabaseTypeDef(databaseTypeDef)),
5454
schemaDef,
5555
options,

0 commit comments

Comments
 (0)