|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | + |
1 | 3 | export default { |
2 | 4 | type: "app", |
3 | 5 | app: "neo4j_auradb", |
4 | | - propDefinitions: {}, |
| 6 | + version: "0.0.{{ts}}", |
| 7 | + propDefinitions: { |
| 8 | + nodeLabel: { |
| 9 | + type: "string", |
| 10 | + label: "Node Label", |
| 11 | + description: "The label of the node to filter events for new node creation.", |
| 12 | + }, |
| 13 | + relationshipType: { |
| 14 | + type: "string", |
| 15 | + label: "Relationship Type", |
| 16 | + description: "The type of the relationship to filter events for new relationship creation.", |
| 17 | + }, |
| 18 | + monitorCypherQuery: { |
| 19 | + type: "string", |
| 20 | + label: "Monitor Cypher Query", |
| 21 | + description: "The Cypher query to monitor for new results.", |
| 22 | + }, |
| 23 | + createNodeLabel: { |
| 24 | + type: "string", |
| 25 | + label: "Create Node Label", |
| 26 | + description: "The label of the node to create.", |
| 27 | + }, |
| 28 | + createNodeProperties: { |
| 29 | + type: "string[]", |
| 30 | + label: "Create Node Properties", |
| 31 | + description: "An array of JSON strings representing the properties of the node to create.", |
| 32 | + }, |
| 33 | + createRelationshipType: { |
| 34 | + type: "string", |
| 35 | + label: "Create Relationship Type", |
| 36 | + description: "The type of the relationship to create.", |
| 37 | + }, |
| 38 | + createStartNode: { |
| 39 | + type: "string", |
| 40 | + label: "Start Node ID", |
| 41 | + description: "The ID of the start node for the relationship.", |
| 42 | + }, |
| 43 | + createEndNode: { |
| 44 | + type: "string", |
| 45 | + label: "End Node ID", |
| 46 | + description: "The ID of the end node for the relationship.", |
| 47 | + }, |
| 48 | + createRelationshipProperties: { |
| 49 | + type: "string[]", |
| 50 | + label: "Create Relationship Properties", |
| 51 | + description: "An array of JSON strings representing the properties of the relationship to create.", |
| 52 | + }, |
| 53 | + executeCypherQuery: { |
| 54 | + type: "string", |
| 55 | + label: "Execute Cypher Query", |
| 56 | + description: "A valid Cypher query to execute against the Neo4j AuraDB instance.", |
| 57 | + }, |
| 58 | + }, |
5 | 59 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | 60 | authKeys() { |
8 | 61 | console.log(Object.keys(this.$auth)); |
9 | 62 | }, |
| 63 | + _baseUrl() { |
| 64 | + return `https://${this.$auth.host}/db/neo4j`; |
| 65 | + }, |
| 66 | + async _makeRequest(opts = {}) { |
| 67 | + const { |
| 68 | + $ = this, method = "GET", path = "/tx/commit", headers, ...otherOpts |
| 69 | + } = opts; |
| 70 | + return axios($, { |
| 71 | + method, |
| 72 | + url: this._baseUrl() + path, |
| 73 | + headers: { |
| 74 | + ...headers, |
| 75 | + Authorization: `Bearer ${this.$auth.api_token}`, |
| 76 | + }, |
| 77 | + ...otherOpts, |
| 78 | + }); |
| 79 | + }, |
| 80 | + async createNode({ |
| 81 | + createNodeLabel, createNodeProperties, |
| 82 | + }) { |
| 83 | + const properties = createNodeProperties.map(JSON.parse); |
| 84 | + const cypher = `CREATE (n:${createNodeLabel} $properties) RETURN n`; |
| 85 | + const mergedProperties = Object.assign({}, ...properties); |
| 86 | + return this._makeRequest({ |
| 87 | + method: "POST", |
| 88 | + data: { |
| 89 | + statements: [ |
| 90 | + { |
| 91 | + statement: cypher, |
| 92 | + parameters: { |
| 93 | + properties: mergedProperties, |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + }); |
| 99 | + }, |
| 100 | + async createRelationship({ |
| 101 | + createRelationshipType, |
| 102 | + createStartNode, |
| 103 | + createEndNode, |
| 104 | + createRelationshipProperties, |
| 105 | + }) { |
| 106 | + const properties = createRelationshipProperties.map(JSON.parse); |
| 107 | + const cypher = ` |
| 108 | + MATCH (a), (b) |
| 109 | + WHERE id(a) = $startNode AND id(b) = $endNode |
| 110 | + CREATE (a)-[r:${createRelationshipType} $properties]->(b) |
| 111 | + RETURN r |
| 112 | + `; |
| 113 | + const mergedProperties = Object.assign({}, ...properties); |
| 114 | + return this._makeRequest({ |
| 115 | + method: "POST", |
| 116 | + data: { |
| 117 | + statements: [ |
| 118 | + { |
| 119 | + statement: cypher, |
| 120 | + parameters: { |
| 121 | + startNode: parseInt(createStartNode, 10), |
| 122 | + endNode: parseInt(createEndNode, 10), |
| 123 | + properties: mergedProperties, |
| 124 | + }, |
| 125 | + }, |
| 126 | + ], |
| 127 | + }, |
| 128 | + }); |
| 129 | + }, |
| 130 | + async executeCypherQuery({ executeCypherQuery }) { |
| 131 | + return this._makeRequest({ |
| 132 | + method: "POST", |
| 133 | + data: { |
| 134 | + statements: [ |
| 135 | + { |
| 136 | + statement: executeCypherQuery, |
| 137 | + }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + }); |
| 141 | + }, |
| 142 | + async emitNewNodeEvent({ nodeLabel }) { |
| 143 | + const cypher = `MATCH (n:${nodeLabel}) RETURN n`; |
| 144 | + return this._makeRequest({ |
| 145 | + method: "POST", |
| 146 | + data: { |
| 147 | + statements: [ |
| 148 | + { |
| 149 | + statement: cypher, |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + }); |
| 154 | + }, |
| 155 | + async emitNewRelationshipEvent({ relationshipType }) { |
| 156 | + const cypher = `MATCH ()-[r:${relationshipType}]->() RETURN r`; |
| 157 | + return this._makeRequest({ |
| 158 | + method: "POST", |
| 159 | + data: { |
| 160 | + statements: [ |
| 161 | + { |
| 162 | + statement: cypher, |
| 163 | + }, |
| 164 | + ], |
| 165 | + }, |
| 166 | + }); |
| 167 | + }, |
| 168 | + async emitCypherQueryEvent({ monitorCypherQuery }) { |
| 169 | + return this.executeCypherQuery({ |
| 170 | + executeCypherQuery: monitorCypherQuery, |
| 171 | + }); |
| 172 | + }, |
| 173 | + async paginate(fn, ...opts) { |
| 174 | + let results = []; |
| 175 | + let moreData = true; |
| 176 | + let currentPage = 0; |
| 177 | + while (moreData) { |
| 178 | + const response = await fn({ |
| 179 | + page: currentPage, |
| 180 | + ...opts, |
| 181 | + }); |
| 182 | + if (response.length === 0) { |
| 183 | + moreData = false; |
| 184 | + } else { |
| 185 | + results = results.concat(response); |
| 186 | + currentPage += 1; |
| 187 | + } |
| 188 | + } |
| 189 | + return results; |
| 190 | + }, |
10 | 191 | }, |
11 | 192 | }; |
0 commit comments