Skip to content

Commit 38526a1

Browse files
committed
Restoring old sources
1 parent ddc1c73 commit 38526a1

File tree

4 files changed

+318
-1
lines changed

4 files changed

+318
-1
lines changed

components/airtable_oauth/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"airtable": "^0.11.1",
1818
"bottleneck": "^2.19.5",
1919
"lodash.chunk": "^4.2.0",
20-
"lodash.isempty": "^4.4.0"
20+
"lodash.isempty": "^4.4.0",
21+
"moment": "^2.30.1"
2122
}
2223
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import base from "../common/common.mjs";
2+
import moment from "moment";
3+
4+
export default {
5+
...base,
6+
name: "New, Modified or Deleted Records",
7+
key: "airtable_oauth-new-modified-or-deleted-records",
8+
version: "0.0.8",
9+
type: "source",
10+
description: "Emit new event each time a record is added, updated, or deleted in an Airtable table. Supports tables up to 10,000 records",
11+
props: {
12+
...base.props,
13+
tableId: {
14+
propDefinition: [
15+
base.props.airtable,
16+
"tableId",
17+
({ baseId }) => ({
18+
baseId,
19+
}),
20+
],
21+
description: "The table ID to watch for changes.",
22+
},
23+
returnFieldsByFieldId: {
24+
propDefinition: [
25+
base.props.airtable,
26+
"returnFieldsByFieldId",
27+
],
28+
},
29+
},
30+
methods: {
31+
...base.methods,
32+
_getPrevAllRecordIds() {
33+
return this.db.get("prevAllRecordIds");
34+
},
35+
_setPrevAllRecordIds(prevAllRecordIds) {
36+
this.db.set("prevAllRecordIds", prevAllRecordIds);
37+
},
38+
},
39+
async run(event) {
40+
const {
41+
baseId,
42+
tableId,
43+
viewId,
44+
} = this;
45+
46+
const metadata = {
47+
baseId,
48+
tableId,
49+
viewId,
50+
};
51+
52+
const prevAllRecordIds = this._getPrevAllRecordIds();
53+
54+
const lastTimestamp = this._getLastTimestamp();
55+
const params = {
56+
filterByFormula: `LAST_MODIFIED_TIME() > "${lastTimestamp}"`,
57+
returnFieldsByFieldId: this.returnFieldsByFieldId || false,
58+
};
59+
60+
const records = await this.airtable.listRecords({
61+
baseId,
62+
tableId,
63+
params,
64+
});
65+
66+
let allRecordIds = [],
67+
newRecordsCount = 0,
68+
modifiedRecordsCount = 0,
69+
deletedRecordsCount = 0;
70+
71+
if (records) {
72+
for (const record of records) {
73+
if (!lastTimestamp || moment(record.createdTime) > moment(lastTimestamp)) {
74+
record.type = "new_record";
75+
newRecordsCount++;
76+
} else {
77+
record.type = "record_modified";
78+
modifiedRecordsCount++;
79+
}
80+
81+
record.metadata = metadata;
82+
83+
this.$emit(record, {
84+
summary: `${record.type}: ${JSON.stringify(record.fields)}`,
85+
id: record.id,
86+
});
87+
}
88+
}
89+
90+
delete params.filterByFormula;
91+
92+
const data = await this.airtable.listRecords({
93+
baseId,
94+
tableId,
95+
params,
96+
});
97+
if (!data.length || data.length === 0) return;
98+
allRecordIds = [
99+
...data.map((record) => record.id),
100+
];
101+
102+
if (prevAllRecordIds) {
103+
const deletedRecordIds = prevAllRecordIds.filter(
104+
(prevRecord) => !allRecordIds.includes(prevRecord),
105+
);
106+
for (const recordID of deletedRecordIds) {
107+
deletedRecordsCount++;
108+
const deletedRecordObj = {
109+
metadata,
110+
type: "record_deleted",
111+
id: recordID,
112+
};
113+
this.$emit(deletedRecordObj, {
114+
summary: "record_deleted",
115+
id: recordID,
116+
});
117+
}
118+
}
119+
120+
console.log(
121+
`Emitted ${newRecordsCount} new records(s) and ${modifiedRecordsCount} modified record(s) and ${deletedRecordsCount} deleted records.`,
122+
);
123+
this._setPrevAllRecordIds(allRecordIds);
124+
125+
// We keep track of the timestamp of the current invocation
126+
this.updateLastTimestamp(event);
127+
},
128+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import base from "../common/common.mjs";
2+
import moment from "moment";
3+
4+
export default {
5+
...base,
6+
name: "New or Modified Records in View",
7+
description: "Emit new event for each new or modified record in a view",
8+
key: "airtable_oauth-new-or-modified-records-in-view",
9+
version: "0.0.8",
10+
type: "source",
11+
props: {
12+
...base.props,
13+
tableId: {
14+
propDefinition: [
15+
base.props.airtable,
16+
"tableId",
17+
({ baseId }) => ({
18+
baseId,
19+
}),
20+
],
21+
description: "The table ID to watch for changes.",
22+
},
23+
viewId: {
24+
propDefinition: [
25+
base.props.airtable,
26+
"viewId",
27+
({
28+
baseId, tableId,
29+
}) => ({
30+
baseId,
31+
tableId,
32+
}),
33+
],
34+
description: "The view ID to watch for changes.",
35+
},
36+
returnFieldsByFieldId: {
37+
propDefinition: [
38+
base.props.airtable,
39+
"returnFieldsByFieldId",
40+
],
41+
},
42+
},
43+
async run(event) {
44+
const {
45+
baseId,
46+
tableId,
47+
viewId,
48+
} = this;
49+
50+
const lastTimestamp = this._getLastTimestamp();
51+
const params = {
52+
view: viewId,
53+
filterByFormula: `LAST_MODIFIED_TIME() > "${lastTimestamp}"`,
54+
returnFieldsByFieldId: this.returnFieldsByFieldId || false,
55+
};
56+
57+
const records = await this.airtable.listRecords({
58+
baseId,
59+
tableId,
60+
params,
61+
});
62+
63+
if (!records.length) {
64+
console.log("No new or modified records.");
65+
return;
66+
}
67+
68+
const metadata = {
69+
baseId,
70+
tableId,
71+
viewId,
72+
};
73+
74+
let newRecords = 0, modifiedRecords = 0;
75+
for (const record of records) {
76+
if (!lastTimestamp || moment(record.createdTime) > moment(lastTimestamp)) {
77+
record.type = "new_record";
78+
newRecords++;
79+
} else {
80+
record.type = "record_modified";
81+
modifiedRecords++;
82+
}
83+
84+
record.metadata = metadata;
85+
86+
this.$emit(record, {
87+
summary: `${record.type}: ${JSON.stringify(record.fields)}`,
88+
id: record.id,
89+
});
90+
}
91+
console.log(`Emitted ${newRecords} new records(s) and ${modifiedRecords} modified record(s).`);
92+
93+
// We keep track of the timestamp of the current invocation
94+
this.updateLastTimestamp(event);
95+
},
96+
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import base from "../common/common.mjs";
2+
import moment from "moment";
3+
4+
export default {
5+
...base,
6+
name: "New Records in View",
7+
description: "Emit new event for each new record in a view",
8+
key: "airtable_oauth-new-records-in-view",
9+
version: "0.0.8",
10+
type: "source",
11+
props: {
12+
...base.props,
13+
tableId: {
14+
propDefinition: [
15+
base.props.airtable,
16+
"tableId",
17+
({ baseId }) => ({
18+
baseId,
19+
}),
20+
],
21+
description: "The table ID to watch for changes.",
22+
},
23+
viewId: {
24+
propDefinition: [
25+
base.props.airtable,
26+
"viewId",
27+
({
28+
baseId, tableId,
29+
}) => ({
30+
baseId,
31+
tableId,
32+
}),
33+
],
34+
description: "The view ID to watch for changes.",
35+
},
36+
returnFieldsByFieldId: {
37+
propDefinition: [
38+
base.props.airtable,
39+
"returnFieldsByFieldId",
40+
],
41+
},
42+
},
43+
async run() {
44+
const {
45+
baseId,
46+
tableId,
47+
viewId,
48+
} = this;
49+
50+
const lastTimestamp = this._getLastTimestamp();
51+
const params = {
52+
view: viewId,
53+
filterByFormula: `CREATED_TIME() > "${lastTimestamp}"`,
54+
returnFieldsByFieldId: this.returnFieldsByFieldId || false,
55+
};
56+
57+
const records = await this.airtable.listRecords({
58+
baseId,
59+
tableId,
60+
params,
61+
});
62+
63+
if (!records.length) {
64+
console.log("No new records.");
65+
return;
66+
}
67+
68+
const metadata = {
69+
baseId,
70+
tableId,
71+
viewId,
72+
};
73+
74+
let maxTimestamp;
75+
let recordCount = 0;
76+
for (const record of records) {
77+
record.metadata = metadata;
78+
79+
this.$emit(record, {
80+
ts: moment(record.createdTime).valueOf(),
81+
summary: JSON.stringify(record.fields),
82+
id: record.id,
83+
});
84+
if (!maxTimestamp || moment(record.createdTime).valueOf() > moment(maxTimestamp).valueOf()) {
85+
maxTimestamp = record.createdTime;
86+
}
87+
recordCount++;
88+
}
89+
console.log(`Emitted ${recordCount} new records(s).`);
90+
this._setLastTimestamp(maxTimestamp);
91+
},
92+
};

0 commit comments

Comments
 (0)