Star

Created With

linkCreating Nodes and Relationships

Apart from building and saving an Instance, each model provides functions to directly create nodes and relationships.

linkCreating or a single node of the Model

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

2linkconst user = await Users.createOne(

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

4link {

5link id: '1',

6link name: 'John',

7link age: 38,

8link },

9link {

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

11link validate: true,

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

13link session: null, // @see [Sessions](../Sessions)

14link /* --> (optional) makes sure that the total relationships created by "where" parameters equal to 1. This assertion happens after the query has ran, so consider running it in a transaction so it can rollback */

15link assertRelationshipsOfWhere: 1,

16link }

17link);

18link

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

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

linkCreating a single node of the Model while relating it with other nodes

Neogma provides functionality for creating other nodes while creating a given node, and associating them automatically. Instead of creating the associated nodes, they can also be matched by a where clause. Everything runs in a single statement.

1linkconst userWithOrder = await Users.createOne({

2link id: '1',

3link name: 'Alex',

4link /* --> associate with other nodes */

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

6link Orders: {

7link /* --> (optional) create new nodes and associate with them */

8link properties: [

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

10link {

11link id: '2'

12link },

13link {

14link id: '3',

15link items: 5,

16link /* --> the relationship is created with the following property. It uses the alias (Rating) as defined in the Model. The actual property property will be that in the Model relationships definition */

17link Rating: 4,

18link /* --> can create nodes and associate them with this Order node. The alias and configuration is that of the Orders model. This can be nested indefinitely */

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

20link Critics: {

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

22link }

23link }

24link ],

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

26link where: [

27link {

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

29link params: { // @see [Where](../Where-Parameters)

30link id: '3'

31link },

32link /* --> (optional) properties can be added to the relationship created by matching the User node with the existing Order nodes */

33link relationshipProperties: {

34link Rating: 4,

35link },

36link },

37link {

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

39link params: {

40link items: 3,

41link }

42link }

43link ]

44link },

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

46link});

47link

48linkconsole.log(userWithOrder.id); // "1"

linkCreating many nodes

For creating many nodes, the function createMany can be used, with identical parameters as createOne, with the only difference that the first param is an array of objects, instead of a plain object.

1linkconst usersWithOrders = await Users.createMany(

2link [

3link {

4link id: '1',

5link name: 'John',

6link },

7link {

8link id: '2',

9link name: 'Alex',

10link Orders: {

11link /* --> same interface as createOne, which will apply only to this node */

12link }

13link }

14link ],

15link {

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

17link validate: true,

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

19link session: null,

20link }

21link);

22link

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

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

linkCreating relationships via the Model static

Relationships can be created via a Model static, by specifying params for both source and target nodes.

The following example created a relationship with the configuration of the alias Orders between the User nodes with name 'John' and the Order nodes with the id '2'.

1linkawait Users.relateTo(

2link {

3link /* --> the alias of the relationship, as provided in the Model definition */

4link alias: 'Orders',

5link /* --> where parameters for the source and target nodes. Refer to the Where section for more information */

6link where: {

7link /* --> where parameters for the source node(s) */

8link source: {

9link name: 'John'

10link },

11link /* --> where parameters for the target node(s) */

12link target: {

13link id: '2',

14link },

15link },

16link /* --> properties of the relationship to be created */

17link properties: {

18link rating: 4,

19link },

20link /* --> (optional) throws an error if the created relationships are not equal to this number */

21link assertCreatedRelationships: 2,

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

23link session: null

24link }

25link);

linkCreating relationships via the Instance method

Relationships can be created via an Instance method, by specifying params for just the target nodes. The source node will always be the one that corresponds to the instance, and its primary key field must be set.

The method relateTo is identical to the static, with the only difference being the where parameter, which now only refers to the target nodes.

1link/* --> let 'user' be a Users instance */

2linkawait user.relateTo(

3link {

4link /* --> the alias of the relationship, as provided in the Model definition */

5link alias: 'Orders',

6link /* --> where parameters for target node(s) */

7link where: {

8link id: '2',

9link },

10link /* --> properties of the relationship to be created */

11link properties: {

12link rating: 4,

13link },

14link /* --> (optional) throws an error if the created relationships are not equal to this number */

15link assertCreatedRelationships: 2,

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

17link session: null

18link }

19link);

Creating Nodes and RelationshipsCreating or a single node of the ModelCreating a single node of the Model while relating it with other nodesCreating many nodesCreating relationships via the Model staticCreating relationships via the Instance method

Introduction Getting Started

Modelschevron_right

Sessions and Transactions

Query Builderchevron_right
Query Runnerchevron_right

Bind Parameters

Where Parameters