Overview Defining a Model Instances Creating Nodes and Relationships Merging Nodes and Relationships Updating Nodes and Relationships Deleting Nodes Deleting Relationships Finding Nodes and Relationships Hooks Temporary Databases
Apart from saving an existing Instance, each model provides functions to directly update nodes or relationships.
Nodes can be updated directly by providing the properties to be set, providing property update operators, and a where parameter to match the nodes.
1linkawait Users.update(
2link {
3link /* --> updates the nodes to set their name to 'Bob' */
4link name: 'Bob',
5link /* --> using the "remove" Symbol from "UpdateOp" */
6link age: { [UpdateOp.remove]: true },
7link },
8link {
9link /* --> (optional) a where parameter to match nodes */
10link where: {
11link /* --> nodes with the id '1' will be matched */
12link id: '1',
13link },
14link /* --> (optional) when true, the first element of the returned tuple contains the updated instances */
15link return: false,
16link /* --> (optional) an existing session or transaction to use */
17link session: null,
18link },
19link);
The method always returns a tuple [instances, queryResult]:
instances: Array of updated Instances (populated when return: true, empty when return: false)queryResult: The QueryResult from the neo4j driver1linkconst [instances, queryResult] = await Users.update(
2link { name: 'Bob' },
3link {
4link where: { id: '1' },
5link return: true,
6link },
7link);
8link
9link/* --> instances is populated when return: true */
10linkconsole.log(instances[0].name); // "Bob"
11linkconsole.log(queryResult.summary.counters.updates().propertiesSet); // number of properties updated
Relationship properties can be updated directly by providing the values to be set, the relationship alias, and an optional where parameter to match the source nodes, target nodes and relationships.
1linkawait Users.updateRelationship(
2link {
3link /* --> sets the 'rating' property of the relationship to the following */
4link rating: 5,
5link },
6link {
7link /* --> used the 'Orders' alias for the relationship configuration, as provided in the Model definition */
8link alias: 'Orders',
9link /* --> (optional) where parameters for matching the nodes or the relationship */
10link where: {
11link /* --> the source node(s) (User) is matched to have the following name */
12link source: {
13link name: 'Bob',
14link },
15link /* --> the target node(s) (Order) is matched to have the following id */
16link target: {
17link id: '2',
18link },
19link /* --> the relationship(s) between the source and the target node(s) are matched to have the following 'rating' */
20link relationship: {
21link rating: 4,
22link },
23link },
24link /* --> (optional) when true, the first element of the returned tuple contains the updated relationships */
25link return: false,
26link /* --> (optional) throws NeogmaNotFoundError if no relationships were updated */
27link throwIfNoneUpdated: false,
28link /* --> (optional) an existing session or transaction to use */
29link session: null,
30link },
31link);
The method always returns a tuple [relationships, queryResult]:
relationships: Array of { source, target, relationship } objects (populated when return: true, empty when return: false)queryResult: The QueryResult from the neo4j driver1linkconst [relationships, queryResult] = await Users.updateRelationship(
2link { rating: 5 },
3link {
4link alias: 'Orders',
5link where: { source: { name: 'Bob' } },
6link return: true,
7link },
8link);
9link
10link/* --> relationships is populated when return: true */
11linkconsole.log(relationships[0].relationship.rating); // 5
12linkconsole.log(queryResult.summary.counters.updates().propertiesSet); // number of properties updated
Similar to the Model static, relationship properties can be updated directly by providing the values to be set, the relationship alias, and an optional where parameter to match the target nodes and relationships. The source node will always be the one that corresponds to the instance, and its primary key field must be set.
1link/* --> let 'user' be a Users Instance. It's used as the source node */
2linkawait user.updateRelationship(
3link {
4link /* --> sets the 'rating' property of the relationship to the following */
5link rating: 5,
6link },
7link {
8link /* --> used the 'Orders' alias for the relationship configuration, as provided in the Model definition */
9link alias: 'Orders',
10link /* --> (optional) where parameters for matching the nodes or the relationship */
11link where: {
12link /* --> the target node(s) (Order) is matched to have the following id */
13link target: {
14link id: '2',
15link },
16link /* --> the relationship(s) between the source and the target node(s) are matched to have the following 'rating' */
17link relationship: {
18link rating: 4,
19link },
20link },
21link /* --> (optional) when true, the first element of the returned tuple contains the updated relationships */
22link return: false,
23link /* --> (optional) throws NeogmaNotFoundError if no relationships were updated */
24link throwIfNoneUpdated: false,
25link /* --> (optional) an existing session or transaction to use */
26link session: null,
27link },
28link);
The instance method also returns a tuple [relationships, queryResult]:
1linkconst [relationships, queryResult] = await user.updateRelationship(
2link { rating: 5 },
3link {
4link alias: 'Orders',
5link where: { target: { id: '2' } },
6link return: true,
7link },
8link);
9link
10linkconsole.log(relationships[0].relationship.rating); // 5
11linkconsole.log(queryResult.summary); // query summary