{"version":3,"file":"memgraph_graph.cjs","names":["Neo4jGraph","error"],"sources":["../../src/graphs/memgraph_graph.ts"],"sourcesContent":["import { Neo4jGraph } from \"./neo4j_graph.js\";\n\nexport interface MemgraphGraphConfig {\n  url: string;\n  username: string;\n  password: string;\n  database?: string;\n}\n\nconst rawSchemaQuery = `\nCALL llm_util.schema(\"raw\")\nYIELD *\nRETURN *\n`;\n\n/**\n * @security *Security note*: Make sure that the database connection uses credentials\n * that are narrowly-scoped to only include necessary permissions.\n * Failure to do so may result in data corruption or loss, since the calling\n * code may attempt commands that would result in deletion, mutation\n * of data if appropriately prompted or reading sensitive data if such\n * data is present in the database.\n * The best way to guard against such negative outcomes is to (as appropriate)\n * limit the permissions granted to the credentials used with this tool.\n * For example, creating read only users for the database is a good way to\n * ensure that the calling code cannot mutate or delete data.\n *\n * @link See https://js.langchain.com/docs/security for more information.\n */\nexport class MemgraphGraph extends Neo4jGraph {\n  constructor({\n    url,\n    username,\n    password,\n    database = \"memgraph\",\n  }: MemgraphGraphConfig) {\n    super({ url, username, password, database });\n  }\n\n  static async initialize(config: MemgraphGraphConfig): Promise<MemgraphGraph> {\n    const graph = new MemgraphGraph(config);\n\n    try {\n      await graph.verifyConnectivity();\n    } catch {\n      console.error(\"Failed to verify connection.\");\n    }\n\n    try {\n      await graph.refreshSchema();\n      console.debug(\"Schema refreshed successfully.\");\n    } catch (e) {\n      const error = e as Error;\n      throw new Error(error.message);\n    }\n\n    return graph;\n  }\n\n  async refreshSchema() {\n    const rawSchemaQueryResult = await this.query(rawSchemaQuery);\n    if (rawSchemaQueryResult?.[0]?.schema) {\n      const rawSchema = rawSchemaQueryResult?.[0]?.schema;\n\n      this.structuredSchema = {\n        nodeProps: rawSchema.node_props,\n        relProps: rawSchema.rel_props,\n        relationships: rawSchema.relationships,\n      };\n\n      const formattedNodeProps = Object.entries(rawSchema.node_props)\n        .map(([nodeName, properties]) => {\n          const propertiesStr = JSON.stringify(properties);\n          return `Node name: '${nodeName}', Node properties: ${propertiesStr}`;\n        })\n        .join(\"\\n\");\n\n      const formattedRelProps = Object.entries(rawSchema.rel_props)\n        .map(([relationshipName, properties]) => {\n          const propertiesStr = JSON.stringify(properties);\n          return `Relationship name: '${relationshipName}', Relationship properties: ${propertiesStr}`;\n        })\n        .join(\"\\n\");\n\n      const formattedRels = rawSchema.relationships\n        ?.map(\n          (el: { end: string; start: string; type: string }) =>\n            `(:${el.start})-[:${el.type}]->(:${el.end})`\n        )\n        .join(\"\\n\");\n\n      this.schema = [\n        \"Node properties are the following:\",\n        formattedNodeProps,\n        \"Relationship properties are the following:\",\n        formattedRelProps,\n        \"The relationships are the following:\",\n        formattedRels,\n      ].join(\"\\n\");\n    }\n  }\n}\n"],"mappings":";;AASA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;AAoBvB,IAAa,gBAAb,MAAa,sBAAsBA,oBAAAA,WAAW;CAC5C,YAAY,EACV,KACA,UACA,UACA,WAAW,cACW;EACtB,MAAM;GAAE;GAAK;GAAU;GAAU;EAAS,CAAC;CAC7C;CAEA,aAAa,WAAW,QAAqD;EAC3E,MAAM,QAAQ,IAAI,cAAc,MAAM;EAEtC,IAAI;GACF,MAAM,MAAM,mBAAmB;EACjC,QAAQ;GACN,QAAQ,MAAM,8BAA8B;EAC9C;EAEA,IAAI;GACF,MAAM,MAAM,cAAc;GAC1B,QAAQ,MAAM,gCAAgC;EAChD,SAAS,GAAG;GAEV,MAAM,IAAI,MAAMC,EAAM,OAAO;EAC/B;EAEA,OAAO;CACT;CAEA,MAAM,gBAAgB;EACpB,MAAM,uBAAuB,MAAM,KAAK,MAAM,cAAc;EAC5D,IAAI,uBAAuB,EAAE,EAAE,QAAQ;GACrC,MAAM,YAAY,uBAAuB,EAAE,EAAE;GAE7C,KAAK,mBAAmB;IACtB,WAAW,UAAU;IACrB,UAAU,UAAU;IACpB,eAAe,UAAU;GAC3B;GAEA,MAAM,qBAAqB,OAAO,QAAQ,UAAU,UAAU,CAAC,CAC5D,KAAK,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