Skip to content

Commit 3fc8cb0

Browse files
committed
limit historical events, hash properties, exit pageStream by last_edited_time
1 parent 004872e commit 3fc8cb0

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

components/notion/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/notion",
3-
"version": "0.1.21",
3+
"version": "0.1.22",
44
"description": "Pipedream Notion Components",
55
"main": "notion.app.mjs",
66
"keywords": [
@@ -17,6 +17,7 @@
1717
"delayed-stream": "^1.0.0",
1818
"form-data": "^3.0.1",
1919
"lodash-es": "^4.17.21",
20+
"md5": "^2.3.0",
2021
"mime-db": "^1.52.0",
2122
"mime-types": "^2.1.35",
2223
"node-fetch": "^2.6.7",

components/notion/sources/updated-page/updated-page.mjs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import notion from "../../notion.app.mjs";
22
import sampleEmit from "./test-event.mjs";
33
import base from "../common/base.mjs";
44
import constants from "../common/constants.mjs";
5+
import md5 from "md5";
56

67
export default {
78
...base,
89
key: "notion-updated-page",
910
name: "Updated Page in Database", /* eslint-disable-line pipedream/source-name */
1011
description: "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead",
11-
version: "0.0.16",
12+
version: "0.0.17",
1213
type: "source",
1314
dedupe: "unique",
1415
props: {
@@ -42,14 +43,26 @@ export default {
4243
async deploy() {
4344
const properties = await this.getProperties();
4445
const propertyValues = {};
45-
const pagesStream = this.notion.getPages(this.databaseId);
46+
const params = this.lastUpdatedSortParam();
47+
const pagesStream = this.notion.getPages(this.databaseId, params);
48+
let count = 0;
49+
let lastUpdatedTimestamp = 0;
4650
for await (const page of pagesStream) {
4751
propertyValues[page.id] = {};
4852
for (const property of properties) {
49-
propertyValues[page.id][property] = JSON.stringify(page.properties[property]);
53+
propertyValues[page.id][property] = md5(JSON.stringify(page.properties[property]));
5054
}
55+
lastUpdatedTimestamp = Math.max(
56+
lastUpdatedTimestamp,
57+
Date.parse(page?.last_edited_time),
58+
);
59+
if (count < 25) {
60+
this.emitEvent(page);
61+
}
62+
count++;
5163
}
5264
this._setPropertyValues(propertyValues);
65+
this.setLastUpdatedTimestamp(lastUpdatedTimestamp);
5366
},
5467
},
5568
methods: {
@@ -77,14 +90,16 @@ export default {
7790
ts,
7891
};
7992
},
93+
emitEvent(page) {
94+
const meta = this.generateMeta(page, constants.summaries.PAGE_UPDATED);
95+
this.$emit(page, meta);
96+
},
8097
},
8198
async run() {
8299
const lastCheckedTimestamp = this.getLastUpdatedTimestamp();
83100
const propertyValues = this._getPropertyValues();
84101

85-
const params = {
86-
...this.lastUpdatedSortParam(),
87-
};
102+
const params = this.lastUpdatedSortParam();
88103
let newLastUpdatedTimestamp = lastCheckedTimestamp;
89104
const properties = await this.getProperties();
90105
const pagesStream = this.notion.getPages(this.databaseId, params);
@@ -97,7 +112,7 @@ export default {
97112

98113
let propertyChangeFound = false;
99114
for (const property of properties) {
100-
const currentProperty = JSON.stringify(page.properties[property]);
115+
const currentProperty = md5(JSON.stringify(page.properties[property]));
101116
if (!propertyValues[page.id] || currentProperty !== propertyValues[page.id][property]) {
102117
propertyChangeFound = true;
103118
propertyValues[page.id] = {
@@ -114,8 +129,11 @@ export default {
114129
continue;
115130
}
116131

117-
const meta = this.generateMeta(page, constants.summaries.PAGE_UPDATED);
118-
this.$emit(page, meta);
132+
this.emitEvent(page);
133+
134+
if (Date.parse(page?.last_edited_time < lastCheckedTimestamp)) {
135+
break;
136+
}
119137
}
120138

121139
this.setLastUpdatedTimestamp(newLastUpdatedTimestamp);

0 commit comments

Comments
 (0)