@@ -9,7 +9,7 @@ export default {
9
9
key : "notion-updated-page" ,
10
10
name : "Updated Page in Database" , /* eslint-disable-line pipedream/source-name */
11
11
description : "Emit new event when a page in a database is updated. To select a specific page, use `Updated Page ID` instead" ,
12
- version : "0.1.2 " ,
12
+ version : "0.1.3 " ,
13
13
type : "source" ,
14
14
dedupe : "unique" ,
15
15
props : {
@@ -40,35 +40,37 @@ export default {
40
40
} ,
41
41
} ,
42
42
hooks : {
43
- async deploy ( ) {
44
- const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
43
+ async activate ( ) {
44
+ console . log ( "Activating: fetching pages and properties" ) ;
45
+ this . _setLastUpdatedTimestamp ( Date . now ( ) ) ;
45
46
const propertyValues = { } ;
47
+ const propertiesToCheck = await this . _getPropertiesToCheck ( ) ;
46
48
const params = this . lastUpdatedSortParam ( ) ;
47
49
const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
48
- let count = 0 ;
49
- let lastUpdatedTimestamp = 0 ;
50
50
for await ( const page of pagesStream ) {
51
51
for ( const propertyName of propertiesToCheck ) {
52
- const currentValue = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
52
+ const currentValue = this . _maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
53
53
propertyValues [ page . id ] = {
54
54
...propertyValues [ page . id ] ,
55
55
[ propertyName ] : currentValue ,
56
56
} ;
57
57
}
58
- lastUpdatedTimestamp = Math . max (
59
- lastUpdatedTimestamp ,
60
- Date . parse ( page . last_edited_time ) ,
61
- ) ;
62
- if ( count ++ < 25 ) {
63
- this . emitEvent ( page ) ;
64
- }
65
58
}
66
59
this . _setPropertyValues ( propertyValues ) ;
67
- this . setLastUpdatedTimestamp ( lastUpdatedTimestamp ) ;
60
+ } ,
61
+ async deactivate ( ) {
62
+ console . log ( "Deactivating: clearing states" ) ;
63
+ this . _setLastUpdatedTimestamp ( null ) ;
68
64
} ,
69
65
} ,
70
66
methods : {
71
67
...base . methods ,
68
+ _getLastUpdatedTimestamp ( ) {
69
+ return this . db . get ( constants . timestamps . LAST_EDITED_TIME ) ;
70
+ } ,
71
+ _setLastUpdatedTimestamp ( ts ) {
72
+ this . db . set ( constants . timestamps . LAST_EDITED_TIME , ts ) ;
73
+ } ,
72
74
_getPropertyValues ( ) {
73
75
const compressed = this . db . get ( "propertyValues" ) ;
74
76
const buffer = Buffer . from ( compressed , "base64" ) ;
@@ -80,14 +82,14 @@ export default {
80
82
const compressed = zlib . deflateSync ( string ) . toString ( "base64" ) ;
81
83
this . db . set ( "propertyValues" , compressed ) ;
82
84
} ,
83
- async getPropertiesToCheck ( ) {
85
+ async _getPropertiesToCheck ( ) {
84
86
if ( this . properties ?. length ) {
85
87
return this . properties ;
86
88
}
87
89
const { properties } = await this . notion . retrieveDatabase ( this . databaseId ) ;
88
90
return Object . keys ( properties ) ;
89
91
} ,
90
- maybeRemoveFileSubItems ( property ) {
92
+ _maybeRemoveFileSubItems ( property ) {
91
93
// Files & Media type:
92
94
// `url` and `expiry_time` are constantly updated by Notion, so ignore these fields
93
95
if ( property . type === "files" ) {
@@ -101,7 +103,7 @@ export default {
101
103
}
102
104
return property ;
103
105
} ,
104
- generateMeta ( obj , summary ) {
106
+ _generateMeta ( obj , summary ) {
105
107
const { id } = obj ;
106
108
const title = this . notion . extractPageTitle ( obj ) ;
107
109
const ts = Date . now ( ) ;
@@ -111,10 +113,10 @@ export default {
111
113
ts,
112
114
} ;
113
115
} ,
114
- emitEvent ( page , changes = [ ] , isNewPage = true ) {
116
+ _emitEvent ( page , changes = [ ] , isNewPage = true ) {
115
117
const meta = isNewPage
116
- ? this . generateMeta ( page , constants . summaries . PAGE_ADDED )
117
- : this . generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
118
+ ? this . _generateMeta ( page , constants . summaries . PAGE_ADDED )
119
+ : this . _generateMeta ( page , constants . summaries . PAGE_UPDATED ) ;
118
120
const event = {
119
121
page,
120
122
changes,
@@ -123,9 +125,15 @@ export default {
123
125
} ,
124
126
} ,
125
127
async run ( ) {
126
- const lastCheckedTimestamp = this . getLastUpdatedTimestamp ( ) ;
128
+ const lastCheckedTimestamp = this . _getLastUpdatedTimestamp ( ) ;
127
129
const propertyValues = this . _getPropertyValues ( ) ;
128
130
131
+ if ( ! lastCheckedTimestamp ) {
132
+ // recently updated (deactivated / activated), skip execution
133
+ console . log ( "Awaiting restart completion: skipping execution" ) ;
134
+ return ;
135
+ }
136
+
129
137
const params = {
130
138
...this . lastUpdatedSortParam ( ) ,
131
139
filter : {
@@ -136,7 +144,7 @@ export default {
136
144
} ,
137
145
} ;
138
146
let newLastUpdatedTimestamp = lastCheckedTimestamp ;
139
- const propertiesToCheck = await this . getPropertiesToCheck ( ) ;
147
+ const propertiesToCheck = await this . _getPropertiesToCheck ( ) ;
140
148
const pagesStream = this . notion . getPages ( this . databaseId , params ) ;
141
149
142
150
for await ( const page of pagesStream ) {
@@ -156,7 +164,7 @@ export default {
156
164
for ( const propertyName of propertiesToCheck ) {
157
165
const previousValue = structuredClone ( propertyValues [ page . id ] ?. [ propertyName ] ) ;
158
166
// value used to compare and to save to this.db
159
- const currentValueToSave = this . maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
167
+ const currentValueToSave = this . _maybeRemoveFileSubItems ( page . properties [ propertyName ] ) ;
160
168
// (unmodified) value that should be emitted
161
169
const currentValueToEmit = page . properties [ propertyName ] ;
162
170
@@ -197,11 +205,11 @@ export default {
197
205
}
198
206
199
207
if ( propertyHasChanged ) {
200
- this . emitEvent ( page , changes , isNewPage ) ;
208
+ this . _emitEvent ( page , changes , isNewPage ) ;
201
209
}
202
210
}
203
211
204
- this . setLastUpdatedTimestamp ( newLastUpdatedTimestamp ) ;
212
+ this . _setLastUpdatedTimestamp ( newLastUpdatedTimestamp ) ;
205
213
this . _setPropertyValues ( propertyValues ) ;
206
214
} ,
207
215
sampleEmit,
0 commit comments