|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +import { LIMIT } from "./common/constants.mjs"; |
| 3 | + |
1 | 4 | export default { |
2 | 5 | type: "app", |
3 | 6 | app: "neo4j_auradb", |
4 | 7 | propDefinitions: {}, |
5 | 8 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 9 | + _baseUrl() { |
| 10 | + return `${this.$auth.api_url}`; |
| 11 | + }, |
| 12 | + _auth() { |
| 13 | + return { |
| 14 | + username: `${this.$auth.username}`, |
| 15 | + password: `${this.$auth.password}`, |
| 16 | + }; |
| 17 | + }, |
| 18 | + _makeRequest({ |
| 19 | + $ = this, path = "", ...opts |
| 20 | + }) { |
| 21 | + return axios($, { |
| 22 | + url: this._baseUrl() + path, |
| 23 | + auth: this._auth(), |
| 24 | + ...opts, |
| 25 | + }); |
| 26 | + }, |
| 27 | + createNode({ |
| 28 | + label, properties, ...opts |
| 29 | + }) { |
| 30 | + const cypher = `CREATE (n:${label} $properties) RETURN n AS Node`; |
| 31 | + return this._makeRequest({ |
| 32 | + method: "POST", |
| 33 | + data: { |
| 34 | + statement: cypher, |
| 35 | + parameters: { |
| 36 | + properties, |
| 37 | + }, |
| 38 | + }, |
| 39 | + ...opts, |
| 40 | + }); |
| 41 | + }, |
| 42 | + createRelationship({ |
| 43 | + relationshipType, |
| 44 | + startNode, |
| 45 | + endNode, |
| 46 | + relationshipProperties = {}, |
| 47 | + ...opts |
| 48 | + }) { |
| 49 | + const stringStartNode = JSON.stringify(startNode).replace(/"[^"]*":/g, (match) => match.replace(/"/g, "")); |
| 50 | + const stringEndNode = JSON.stringify(endNode).replace(/"[^"]*":/g, (match) => match.replace(/"/g, "")); |
| 51 | + const cypher = ` |
| 52 | + MATCH (a ${stringStartNode}) |
| 53 | + MATCH (b ${stringEndNode}) |
| 54 | + CREATE (a)-[r:${relationshipType} $properties]->(b) |
| 55 | + RETURN r |
| 56 | + `; |
| 57 | + return this._makeRequest({ |
| 58 | + method: "POST", |
| 59 | + data: { |
| 60 | + statement: cypher, |
| 61 | + parameters: { |
| 62 | + startNode, |
| 63 | + endNode, |
| 64 | + properties: relationshipProperties, |
| 65 | + }, |
| 66 | + }, |
| 67 | + ...opts, |
| 68 | + }); |
| 69 | + }, |
| 70 | + executeCypherQuery({ |
| 71 | + cypherQuery, ...opts |
| 72 | + }) { |
| 73 | + return this._makeRequest({ |
| 74 | + method: "POST", |
| 75 | + data: { |
| 76 | + statement: cypherQuery, |
| 77 | + }, |
| 78 | + ...opts, |
| 79 | + }); |
| 80 | + }, |
| 81 | + async *paginate({ |
| 82 | + query, maxResults = null, |
| 83 | + }) { |
| 84 | + let hasMore = false; |
| 85 | + let count = 0; |
| 86 | + let page = 0; |
| 87 | + let cypherQuery = ""; |
| 88 | + |
| 89 | + do { |
| 90 | + cypherQuery = `${query} SKIP ${LIMIT * page} LIMIT ${LIMIT}`; |
| 91 | + page++; |
| 92 | + |
| 93 | + const { data: { values } } = await this.executeCypherQuery({ |
| 94 | + cypherQuery, |
| 95 | + }); |
| 96 | + for (const d of values) { |
| 97 | + yield d[0]; |
| 98 | + |
| 99 | + if (maxResults && ++count === maxResults) { |
| 100 | + return count; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + hasMore = values.length; |
| 105 | + |
| 106 | + } while (hasMore); |
9 | 107 | }, |
10 | 108 | }, |
11 | 109 | }; |
0 commit comments