Skip to content

Commit 2c1a597

Browse files
authored
Merge pull request #623 from framer/notion-large-relations
Notion: import up to 100 relation values
2 parents 9dda084 + ae6d983 commit 2c1a597

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

plugins/notion/src/api.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,29 @@ export async function getPageBlocksAsRichText(pageId: string) {
405405
return blocksToHtml(blocks)
406406
}
407407

408+
const RELATION_PROPERTY_PAGE_SIZE = 100
409+
410+
/**
411+
* Loads up to {@link RELATION_PROPERTY_PAGE_SIZE} related page ids when the relation is truncated on
412+
* retrieve-page / query (has_more). Does not paginate past the first page.
413+
*/
414+
export async function fetchRelationIdsForPageProperty(pageId: string, propertyId: string): Promise<string[]> {
415+
const notion = getNotionClient()
416+
const response = await notion.pages.properties.retrieve({
417+
page_id: pageId,
418+
property_id: propertyId,
419+
page_size: RELATION_PROPERTY_PAGE_SIZE,
420+
})
421+
422+
if (response.object === "list") {
423+
return response.results.map(item => (item.type === "relation" ? item.relation.id : "")).filter(id => id !== "")
424+
}
425+
if (response.type === "relation") {
426+
return [response.relation.id]
427+
}
428+
return []
429+
}
430+
408431
export async function getDatabaseItems(
409432
database: GetDatabaseResponse,
410433
onProgress?: (progress: { current: number; total: number; hasFinishedLoading: boolean }) => void

plugins/notion/src/data.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
alwaysSyncedPropertyTypes,
1414
assertFieldTypeMatchesPropertyType,
1515
type FieldInfo,
16+
fetchRelationIdsForPageProperty,
1617
getDatabase,
1718
getDatabaseFieldsInfo,
1819
getDatabaseItems,
@@ -241,7 +242,7 @@ export async function syncCollection(
241242
continue
242243
}
243244

244-
const fieldEntry = getFieldDataEntryForProperty(property, field)
245+
const fieldEntry = await getFieldDataEntryForProperty(property, field, item.id)
245246
if (fieldEntry) {
246247
fieldData[field.id] = fieldEntry
247248
} else {
@@ -624,10 +625,11 @@ export function fieldsInfoToCollectionFields(
624625
return fields
625626
}
626627

627-
export function getFieldDataEntryForProperty(
628+
async function getFieldDataEntryForProperty(
628629
property: PageObjectResponse["properties"][string],
629-
field: ManagedCollectionFieldInput
630-
): FieldDataEntryInput | null {
630+
field: ManagedCollectionFieldInput,
631+
itemId: string
632+
): Promise<FieldDataEntryInput | null> {
631633
switch (property.type) {
632634
case "checkbox": {
633635
return { type: "boolean", value: property.checkbox }
@@ -691,7 +693,14 @@ export function getFieldDataEntryForProperty(
691693
}
692694
case "relation": {
693695
if (field.type === "multiCollectionReference") {
694-
return { type: "multiCollectionReference", value: property.relation.map(({ id }) => id) }
696+
let relationItemIds: string[] = []
697+
if ("has_more" in property && property.has_more) {
698+
relationItemIds = await fetchRelationIdsForPageProperty(itemId, property.id)
699+
} else {
700+
relationItemIds = property.relation.map(({ id }) => id)
701+
}
702+
703+
return { type: "multiCollectionReference", value: relationItemIds }
695704
} else if (field.type === "collectionReference") {
696705
return { type: "collectionReference", value: property.relation[0]?.id ?? null }
697706
}

0 commit comments

Comments
 (0)