-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-or-updated-entities.js
75 lines (70 loc) · 2.6 KB
/
new-or-updated-entities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fibery = require('https://github.com/blessingefkt/fibery-pipedream-event-sources/fibery.app.js');
module.exports = {
name: "fibery-entities-created-or-updated",
version: "0.0.8",
props: {
fibery,
db: "$.service.db",
entityType: {
description: "ID of an Entity Type",
type: "string",
async options() {
return await this.fibery.adapter().typeOptions();
}
},
fields: {
type: "string[]",
async options() {
return await this.fibery.adapter().getTypeFieldOptions(this.entityType);
},
},
limit: {
type: "string",
default: "10"
},
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: 60 * 5,
},
},
},
async run(event) {
const entityTypeKey = Buffer.from(`${this.fibery.adapter().$auth.account_name}/${this.entityType}`).toString('base64');
const dbKey = 'lastMaxTimestamp/'+ entityTypeKey;
const lastMaxTimestamp = this.db.get(dbKey) || new Date().toISOString();
const queryObject = await this.fibery.adapter().getQueryObject(this.entityType, {
lastMaxTimestamp: lastMaxTimestamp,
fields: this.fields,
dateFields: ['fibery/creation-date', 'fibery/modification-date'],
limit: Number(this.limit)
});
const entityType = queryObject.query['q/from'];
console.log('query', JSON.stringify(queryObject, null, 2));
let entities = [];
entities = await this.fibery.adapter().queryEntities(queryObject.query, queryObject.params);
if (!entities.length) {
console.log(`No new entities.`);
return;
}
const metadata = {entityType, lastMaxTimestamp};
let maxTimestamp;
const entityIds = [];
for (let entity of entities) {
const id = entity['fibery/id'];
entityIds.push(id);
const result = {entity, ...metadata, number: entityIds.length};
this.$emit(result, {
id,
ts: entity['fibery/creation-date'],
summary: JSON.stringify(entity),
})
if (!maxTimestamp || entity['fibery/creation-date'] > maxTimestamp) {
maxTimestamp = entity['fibery/creation-date']
}
}
const entityCount = entityIds.length;
console.log(`Emitted ${entityCount} new records(s).`, entityIds);
this.db.set(dbKey, maxTimestamp)
},
}