Star

Created With

linkMerging Nodes and Relationships

Merging nodes and relationships happens with the same functions as creating nodes and relationships, by passing the appropriate params. Therefore, it is recommended that Creating Nodes and Relationships is known well.

linkMerging a single or many nodes of a Model

For merging a single node, createOne can be used with the merge param set to true.

1link/* --> merge a User node and get the Instance */

2linkconst user = await Users.createOne(

3link /* --> the properties of the User node to be merged */

4link {

5link id: '1',

6link name: 'John',

7link age: 38,

8link },

9link {

10link /* --> by setting this to true, a MERGE query instead of a CREATE one will run */

11link merge: true,

12link /* --> (optional, default true) validates the properties of the node */

13link validate: true,

14link /* --> (optional) an existing session or transaction to use */

15link session: null,

16link }

17link);

18link

19link/* --> we can use the Instance as usual */

20linkconsole.log(user.name); // "John"

For merging many nodes, createMany can be used with the merge param set to true.

1linkconst users = await Users.createMany(

2link [

3link {

4link id: '1',

5link name: 'John',

6link },

7link {

8link id: '2',

9link name: 'Alex',

10link }

11link ],

12link {

13link /* --> by setting this to true, a MERGE query instead of a CREATE one will run */

14link merge: true,

15link /* --> (optional, default true) validates all nodes */

16link validate: true,

17link /* --> (optional) an existing session or transaction to use */

18link session: null,

19link }

20link);

21link

22linkconsole.log(usersWithOrders[0].id); // "1"

23linkconsole.log(usersWithOrders[1].bar()); // "The name of this user is: Alex"

linkCreating/Merging nodes and merging relationships with other nodes

When automatically associating with other nodes (either by creating them or by matching them), a MERGE instead of a CREATE can be used.

The following example uses createMany, but the same interface applies on createOne and instance save (when it doesn't exist in the database).

1linkconst usersWithOrders = await Users.createMany(

2link [

3link {

4link id: '1',

5link name: 'John',

6link },

7link {

8link id: '2',

9link name: 'Alex',

10link /* --> associate with other nodes by their aliases */

11link /* --> the Orders alias will be used, as defined in the Users model */

12link Orders: {

13link /* --> (optional) configuration regarding what aspects of the 'properties' property to merge instead of create */

14link propertiesMergeConfig: {

15link /* --> (optional) the created (Order) nodes will be merged, instead of created */

16link nodes: true,

17link /* --> (optional) the relationship between User and Order nodes will be merged, instead of created */

18link relationship: true,

19link },

20link /* --> (optional) merge new nodes (as propertiesMergeConfig.nodes is true) and associate with them */

21link properties: [

22link /* --> creates the following 2 Order nodes, and creates a relationship with each one of them using the configuration of the Orders alias */

23link {

24link id: '2'

25link },

26link {

27link id: '3',

28link items: 5,

29link /* --> the relationship is merged (as propertiesMergeConfig.relationship is true) with the following property (using its alias) */

30link Rating: 4,

31link /* --> can create (or merge) nodes and associate them with this Order node. The alias and configuration is that of the Orders model */

32link /* --> the 'Critics' alias will be used, as defined in the 'Orders' model */

33link Critics: {

34link propertiesMergeConfig: {

35link /* --> by setting this to false (or omitting it in the first place), the Critics nodes will be created, not merged */

36link nodes: false,

37link /* --> by setting this to false (or omitting it in the first place), the relationship between the Orders and the Critics nodes will be created, not merged */

38link relationship: false,

39link },

40link properties: [{ id: '10' }]

41link }

42link }

43link ],

44link /* --> (optional) also associates the User node with existing Order nodes */

45link where: [

46link {

47link /* --> (optional) the relationship between the created User nodes and the matched Order nodes will be merged, instead of being created */

48link merge: true,

49link /* --> the Where clause find matching the existing Nodes */

50link params: {

51link id: '3'

52link },

53link /* --> (optional) properties can be added to the relationship merged by matching the User node with the existing Order nodes, using their alias */

54link Rating: 5,

55link },

56link {

57link /* --> another object can be used for matching the User node with the Order nodes of this where independently */

58link params: {

59link items: 3,

60link },

61link /* --> the realtionship of this match can be created, not merged */

62link merge: false,

63link }

64link ]

65link },

66link /* --> other aliases can be used here, to associate the User node with those of other Models */

67link }

68link ],

69link {

70link /* --> merges the root-level nodes (the Users - 'John', 'Alex') */

71link merge: true,

72link /* --> (optional, default true) validates all nodes */

73link validate: true,

74link /* --> (optional) an existing session or transaction to use */

75link session: null,

76link }

77link);

Merging Nodes and RelationshipsMerging a single or many nodes of a ModelCreating/Merging nodes and merging relationships with other nodes

Introduction Getting Started

Modelschevron_right

Sessions and Transactions

Query Builderchevron_right
Query Runnerchevron_right

Bind Parameters

Where Parameters