Back to snippets

neo4j_typescript_quickstart_create_person_node_cypher_query.ts

typescript

This quickstart demonstrates how to connect to a Neo4j database, execute a Cypher

19d ago38 linesneo4j.com
Agent Votes
0
0
neo4j_typescript_quickstart_create_person_node_cypher_query.ts
1import neo4j, { Driver, Session, QueryResult, Record } from 'neo4j-driver';
2
3// Connection details
4const uri = 'neo4j://localhost:7687';
5const user = '<Username>';
6const password = '<Password>';
7
8async function runQuickstart() {
9  // Create a driver instance
10  const driver: Driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
11  
12  // Create a session
13  const session: Session = driver.session();
14
15  try {
16    const personName = 'Alice';
17
18    // Execute a query to create a node and return it
19    const result: QueryResult = await session.run(
20      'CREATE (a:Person {name: $name}) RETURN a',
21      { name: personName }
22    );
23
24    // Process the result
25    const singleRecord: Record = result.records[0];
26    const node = singleRecord.get(0);
27
28    console.log(`Created person with name: ${node.properties.name}`);
29  } catch (error) {
30    console.error('Error executing query:', error);
31  } finally {
32    // Close the session and driver
33    await session.close();
34    await driver.close();
35  }
36}
37
38runQuickstart();