diff --git a/components/neo4j_auradb/actions/create-node/create-node.mjs b/components/neo4j_auradb/actions/create-node/create-node.mjs new file mode 100644 index 0000000000000..382c6218b4586 --- /dev/null +++ b/components/neo4j_auradb/actions/create-node/create-node.mjs @@ -0,0 +1,41 @@ +import { ConfigurationError } from "@pipedream/platform"; +import { parseObject } from "../../common/utils.mjs"; +import app from "../../neo4j_auradb.app.mjs"; + +export default { + key: "neo4j_auradb-create-node", + name: "Create Node", + description: "Creates a new node in the Neo4j AuraDB instance. [See the documentation](https://neo4j.com/docs/query-api/current/query/)", + version: "0.0.1", + type: "action", + props: { + app, + nodeLabel: { + type: "string", + label: "Node Label", + description: "The label of the node to filter events for new node creation.", + }, + nodeProperties: { + type: "object", + label: "Create Node Properties", + description: "An object representing the properties of the node to create.", + }, + }, + async run({ $ }) { + const response = await this.app.createNode({ + $, + label: this.nodeLabel, + properties: parseObject(this.nodeProperties), + }); + + if (response.errors) { + throw new ConfigurationError(response.errors[0].message); + } + + const elementId = response.data?.values?.[0]?.[0]?.elementId; + $.export("$summary", elementId + ? `Created node with id ${elementId}` + : "Node created successfully"); + return response; + }, +}; diff --git a/components/neo4j_auradb/actions/create-relationship/create-relationship.mjs b/components/neo4j_auradb/actions/create-relationship/create-relationship.mjs new file mode 100644 index 0000000000000..09a77b0c448ba --- /dev/null +++ b/components/neo4j_auradb/actions/create-relationship/create-relationship.mjs @@ -0,0 +1,48 @@ +import { parseObject } from "../../common/utils.mjs"; +import app from "../../neo4j_auradb.app.mjs"; + +export default { + key: "neo4j_auradb-create-relationship", + name: "Create Relationship", + description: "Creates a relationship between two existing nodes. [See the documentation](https://neo4j.com/docs/query-api/current/query/)", + version: "0.0.1", + type: "action", + props: { + app, + relationshipType: { + type: "string", + label: "Create Relationship Type", + description: "The name of the relationship to create.", + }, + startNode: { + type: "object", + label: "Start Node Identifier", + description: "An object containing any fields used to identify the start node.", + }, + endNode: { + type: "object", + label: "End Node Identifier", + description: "An object containing any fields used to identify the end node.", + }, + relationshipProperties: { + type: "object", + label: "Create Relationship Properties", + description: "An object representing the properties of the relationship to create.", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.app.createRelationship({ + $, + relationshipType: this.relationshipType, + startNode: parseObject(this.startNode), + endNode: parseObject(this.endNode), + relationshipProperties: parseObject(this.relationshipProperties), + }); + $.export( + "$summary", + `Created relationship '${this.relationshipType}' between nodes`, + ); + return response; + }, +}; diff --git a/components/neo4j_auradb/actions/run-query/run-query.mjs b/components/neo4j_auradb/actions/run-query/run-query.mjs new file mode 100644 index 0000000000000..f10ee97a58d4c --- /dev/null +++ b/components/neo4j_auradb/actions/run-query/run-query.mjs @@ -0,0 +1,25 @@ +import app from "../../neo4j_auradb.app.mjs"; + +export default { + key: "neo4j_auradb-run-query", + name: "Run Cypher Query", + description: "Executes a Cypher query against the Neo4j AuraDB instance. [See the documentation](https://neo4j.com/docs/query-api/current/query/)", + version: "0.0.1", + type: "action", + props: { + app, + cypherQuery: { + type: "string", + label: "Execute Cypher Query", + description: "A valid Cypher query to execute against the Neo4j AuraDB instance.", + }, + }, + async run({ $ }) { + const response = await this.app.executeCypherQuery({ + $, + cypherQuery: this.cypherQuery, + }); + $.export("$summary", "Executed Cypher query successfully"); + return response; + }, +}; diff --git a/components/neo4j_auradb/common/constants.mjs b/components/neo4j_auradb/common/constants.mjs new file mode 100644 index 0000000000000..c9448253add0e --- /dev/null +++ b/components/neo4j_auradb/common/constants.mjs @@ -0,0 +1,16 @@ +export const LIMIT = 100; + +export const ORDER_TYPE_OPTIONS = [ + { + label: "DateTime", + value: "datetime", + }, + { + label: "Sequential (Integer)", + value: "sequential", + }, + { + label: "Other", + value: "other", + }, +]; diff --git a/components/neo4j_auradb/common/utils.mjs b/components/neo4j_auradb/common/utils.mjs new file mode 100644 index 0000000000000..dcc9cc61f6f41 --- /dev/null +++ b/components/neo4j_auradb/common/utils.mjs @@ -0,0 +1,24 @@ +export const parseObject = (obj) => { + if (!obj) return undefined; + + if (Array.isArray(obj)) { + return obj.map((item) => { + if (typeof item === "string") { + try { + return JSON.parse(item); + } catch (e) { + return item; + } + } + return item; + }); + } + if (typeof obj === "string") { + try { + return JSON.parse(obj); + } catch (e) { + return obj; + } + } + return obj; +}; diff --git a/components/neo4j_auradb/neo4j_auradb.app.mjs b/components/neo4j_auradb/neo4j_auradb.app.mjs index 177312a2ce978..b04bb8092b17a 100644 --- a/components/neo4j_auradb/neo4j_auradb.app.mjs +++ b/components/neo4j_auradb/neo4j_auradb.app.mjs @@ -1,11 +1,109 @@ +import { axios } from "@pipedream/platform"; +import { LIMIT } from "./common/constants.mjs"; + export default { type: "app", app: "neo4j_auradb", propDefinitions: {}, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return `${this.$auth.api_url}`; + }, + _auth() { + return { + username: `${this.$auth.username}`, + password: `${this.$auth.password}`, + }; + }, + _makeRequest({ + $ = this, path = "", ...opts + }) { + return axios($, { + url: this._baseUrl() + path, + auth: this._auth(), + ...opts, + }); + }, + createNode({ + label, properties, ...opts + }) { + const cypher = `CREATE (n:${label} $properties) RETURN n AS Node`; + return this._makeRequest({ + method: "POST", + data: { + statement: cypher, + parameters: { + properties, + }, + }, + ...opts, + }); + }, + createRelationship({ + relationshipType, + startNode, + endNode, + relationshipProperties = {}, + ...opts + }) { + const stringStartNode = JSON.stringify(startNode).replace(/"[^"]*":/g, (match) => match.replace(/"/g, "")); + const stringEndNode = JSON.stringify(endNode).replace(/"[^"]*":/g, (match) => match.replace(/"/g, "")); + const cypher = ` + MATCH (a ${stringStartNode}) + MATCH (b ${stringEndNode}) + CREATE (a)-[r:${relationshipType} $properties]->(b) + RETURN r + `; + return this._makeRequest({ + method: "POST", + data: { + statement: cypher, + parameters: { + startNode, + endNode, + properties: relationshipProperties, + }, + }, + ...opts, + }); + }, + executeCypherQuery({ + cypherQuery, ...opts + }) { + return this._makeRequest({ + method: "POST", + data: { + statement: cypherQuery, + }, + ...opts, + }); + }, + async *paginate({ + query, maxResults = null, + }) { + let hasMore = false; + let count = 0; + let page = 0; + let cypherQuery = ""; + + do { + cypherQuery = `${query} SKIP ${LIMIT * page} LIMIT ${LIMIT}`; + page++; + + const { data: { values } } = await this.executeCypherQuery({ + cypherQuery, + }); + for (const d of values) { + yield d[0]; + + if (maxResults && ++count === maxResults) { + return count; + } + } + + hasMore = values.length; + + } while (hasMore); }, }, }; diff --git a/components/neo4j_auradb/package.json b/components/neo4j_auradb/package.json index 57b6adffc58d1..d2b748e99bc9b 100644 --- a/components/neo4j_auradb/package.json +++ b/components/neo4j_auradb/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/neo4j_auradb", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Neo4j AuraDB Components", "main": "neo4j_auradb.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/components/neo4j_auradb/sources/common/base.mjs b/components/neo4j_auradb/sources/common/base.mjs new file mode 100644 index 0000000000000..db7da8367af79 --- /dev/null +++ b/components/neo4j_auradb/sources/common/base.mjs @@ -0,0 +1,122 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import { ORDER_TYPE_OPTIONS } from "../../common/constants.mjs"; +import app from "../../neo4j_auradb.app.mjs"; + +export default { + props: { + app, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + orderBy: { + type: "string", + label: "Order By", + description: "Order By property is required for Pipedream source to identify the entity. We recommend to choose the property represent the created time property of your entity with a variable, for example createdAt.", + }, + orderType: { + type: "string", + label: "Order Type", + description: "The type of the order field.", + options: ORDER_TYPE_OPTIONS, + reloadProps: true, + }, + datetimeFormat: { + type: "string", + label: "Datetime Format", + description: "The format of the datetime field. **E.g. YYYY-MM-DD HH:mm:ss.SSSSZ**", + hidden: true, + }, + }, + async additionalProps(props) { + props.datetimeFormat.hidden = !(this.orderType === "datetime"); + return {}; + }, + methods: { + _getDefaultValue() { + switch (this.orderType) { + case "datetime": return "1970-01-01T00:00:00Z"; + case "sequencial": return 0; + default: return ""; + } + }, + _getWhereClause(lastData, type) { + switch (type) { + case "datetime": return `WHERE apoc.date.parse(n.${this.orderBy}, "ms", "${this.datetimeFormat}") > ${Date.parse(lastData)}`; + case "sequencial": return `WHERE n.${this.orderBy} > ${parseInt(lastData)}`; + default: return ""; + } + }, + _verifyBreak(item, lastData) { + const type = this.orderType; + switch (type) { + case "datetime": + if (Date.parse(item[this.orderBy]) <= Date.parse(lastData)) { + return true; + } + break; + case "sequencial": + if (item[this.orderBy] <= lastData) { + return true; + } + break; + case "other": + if (item[this.orderBy] === lastData) { + return true; + } + break; + default: + return false; + } + }, + _getLastData() { + return this.db.get("lastData") || this._getDefaultValue(); + }, + _setLastData(lastData) { + this.db.set("lastData", lastData); + }, + async emitEvent(maxResults = false) { + const lastData = this._getLastData(); + const whereClause = this._getWhereClause(lastData, this.orderType); + const queryBase = this.getBaseQuery(whereClause); + const query = `${queryBase} RETURN n ORDER BY n.${this.orderBy} DESC `; + + const response = this.app.paginate({ + query, + maxResults, + }); + + let responseArray = []; + for await (const item of response) { + this._verifyBreak(item, lastData); + responseArray.push(item); + } + + if (responseArray.length) { + if (maxResults && (responseArray.length > maxResults)) { + responseArray.length = maxResults; + } + + const lastData = responseArray[0]?.properties?.[this.orderBy] + || responseArray[0][1].properties[this.orderBy]; + + this._setLastData(lastData); + } + + for (const item of responseArray.reverse()) { + this.emit(item); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, +}; diff --git a/components/neo4j_auradb/sources/new-node/new-node.mjs b/components/neo4j_auradb/sources/new-node/new-node.mjs new file mode 100644 index 0000000000000..035cf8e1306c9 --- /dev/null +++ b/components/neo4j_auradb/sources/new-node/new-node.mjs @@ -0,0 +1,38 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "neo4j_auradb-new-node", + name: "New Node Created", + description: "Emit new event when a new node is created in the Neo4j AuraDB instance.", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + nodeLabel: { + type: "string", + label: "Node Label", + description: "The label of the node.", + }, + }, + methods: { + ...common.methods, + getBaseQuery(whereClause) { + return `MATCH (n:${this.nodeLabel}) ${whereClause}`; + }, + emit(item) { + const ts = (this.orderType === "dateTime") + ? Date.parse(item.properties[this.orderBy]) + : new Date(); + + this.$emit(item, { + id: item.elementId, + summary: `New node created with label ${this.nodeLabel}`, + ts, + }); + }, + }, + sampleEmit, +}; diff --git a/components/neo4j_auradb/sources/new-node/test-event.mjs b/components/neo4j_auradb/sources/new-node/test-event.mjs new file mode 100644 index 0000000000000..e814aa541e121 --- /dev/null +++ b/components/neo4j_auradb/sources/new-node/test-event.mjs @@ -0,0 +1,11 @@ +export default { + "elementId": "4:52eb161a-501d-4f5f-b499-f72cbe80d169:1", + "labels": [ + "player" + ], + "properties": { + "name": "Shikar Dhawan", + "YOB": 1985, + "POB": "Delhi" + } +} \ No newline at end of file diff --git a/components/neo4j_auradb/sources/new-relationship/new-relationship.mjs b/components/neo4j_auradb/sources/new-relationship/new-relationship.mjs new file mode 100644 index 0000000000000..d2979369c116c --- /dev/null +++ b/components/neo4j_auradb/sources/new-relationship/new-relationship.mjs @@ -0,0 +1,38 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "neo4j_auradb-new-relationship", + name: "New Relationship Created", + description: "Emit new event when a new relationship is created between nodes in the Neo4j AuraDB instance.", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + relationshipLabel: { + type: "string", + label: "Relationship Label", + description: "The label of the relationship.", + }, + }, + methods: { + ...common.methods, + getBaseQuery(whereClause) { + return `MATCH p=()-[n:${this.relationshipLabel}]->() ${whereClause}`; + }, + emit(item) { + const ts = (this.orderType === "dateTime") + ? Date.parse(item[1].properties[this.orderBy]) + : new Date(); + + this.$emit(item, { + id: item.elementId, + summary: `New Relationship created with label ${this.relationshipLabel}`, + ts, + }); + }, + }, + sampleEmit, +}; diff --git a/components/neo4j_auradb/sources/new-relationship/test-event.mjs b/components/neo4j_auradb/sources/new-relationship/test-event.mjs new file mode 100644 index 0000000000000..e1e2b8ce55993 --- /dev/null +++ b/components/neo4j_auradb/sources/new-relationship/test-event.mjs @@ -0,0 +1,29 @@ +export default [ + { + "elementId":"4:52eb161a-501d-4f5f-b499-f72cbe80d169:9", + "labels":["player"], + "properties":{ + "name":"Jao", + "POB":"Delhi", + "id":"4:52eb161a-501d-4f5f-b499-f72cbe80d169:3", + "YOB":1985 + } + }, + { + "elementId":"5:52eb161a-501d-4f5f-b499-f72cbe80d169:1152921504606846985", + "startNodeElementId":"4:52eb161a-501d-4f5f-b499-f72cbe80d169:9", + "endNodeElementId":"4:52eb161a-501d-4f5f-b499-f72cbe80d169:10", + "type":"ARE_ASSOCIATED", + "properties":{} + }, + { + "elementId":"4:52eb161a-501d-4f5f-b499-f72cbe80d169:10", + "labels":["player"], + "properties":{ + "name":"Jonas", + "POB":"Delhi", + "id":"4:52eb161a-501d-4f5f-b499-f72cbe80d169:2", + "YOB":1985 + } + } +] \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e4c11cb38fdd9..ea95206e636fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -421,8 +421,7 @@ importers: components/agentos: {} - components/agentql: - specifiers: {} + components/agentql: {} components/agenty: dependencies: @@ -4050,8 +4049,7 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/enginemailer: - specifiers: {} + components/enginemailer: {} components/enigma: {} @@ -8317,7 +8315,11 @@ importers: components/neetokb: {} - components/neo4j_auradb: {} + components/neo4j_auradb: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/neon_api_keys: dependencies: @@ -34318,6 +34320,8 @@ snapshots: '@putout/operator-filesystem': 5.0.0(putout@36.13.1(eslint@8.57.1)(typescript@5.6.3)) '@putout/operator-json': 2.2.0 putout: 36.13.1(eslint@8.57.1)(typescript@5.6.3) + transitivePeerDependencies: + - supports-color '@putout/operator-regexp@1.0.0(putout@36.13.1(eslint@8.57.1)(typescript@5.6.3))': dependencies: