Star

Created With

linkDefining a Model

linkImporting Neogma

To avoid importing issues, The instance of the "Neogma" class (refered to as "neogma" in those examples) must exported before any Model is imported.

For example, in App.ts:

1linkimport { Neogma } from 'neogma';

2linkexport const neogma = new Neogma(...);

3linkimport { SomeModel } from './MyModel';

In MyModel.ts

1linkimport { neogma } from './App';

linkUsing ModelFactory

To define a Model, the information presented at Overview must be provided.

For the schema, revalidator is used. Its documentation is applied as-is for defining validating the properties of the nodes of this Model.

A sample Model definition with all configuration options is the following. Note that when using Typescript, to enable proper typing, some interfaces need to be provided.

1linkconst Users = ModelFactory({

2link /* --> the label that the nodes of this Model have. For multiple labels, an array can be provided like ['User', 'New'] */

3link label: 'User',

4link /* --> The properties of the nodes of this Model and the validation for them. This follows the revalidator schema configuration */

5link schema: {

6link name: {

7link type: 'string',

8link minLength: 3,

9link required: true

10link },

11link age: {

12link type: 'number',

13link minimum: 0,

14link },

15link id: {

16link type: 'string',

17link required: true,

18link }

19link },

20link /* --> all the possible relationships (with other Models or itself), for relationship-related functions to work properly */

21link relationships: {

22link /* --> an arbitrary alias to be used for identifying this relationship when using the relationship-related functions */

23link Orders: {

24link /* --> reference to the Orders Model. For reference to this model, the value 'self' can be used */

25link model: Orders,

26link /* --> the direction of the relationship. Valid values are 'in' | 'out' | 'none' */

27link direction: 'out',

28link /* --> the name of this relationship */

29link name: 'CREATES',

30link /* --> properties of the relationship between the nodes */

31link properties: {

32link /* --> the key to be used that the property is a relationship property */

33link Rating: {

34link /* --> the actual property to be created in the relationship */

35link property: 'rating',

36link /* --> schema validation for it */

37link schema: {

38link type: 'number',

39link required: true,

40link },

41link },

42link }

43link }

44link },

45link /* --> (optional) the key to be used as a unique identifier, which enables some Instance methods */

46link primaryKeyField: 'id',

47link /* --> (optional) statics to be added to the Model. In this example, can be called using `Users.foo()` */

48link statics: {

49link foo: () => {

50link return 'foo';

51link }

52link },

53link /* --> (optional) methods to be added to the Instance of this Model. In this example, they can be called on a Users Instance using `user.bar()` */

54link methods: {

55link bar: function() {

56link /* --> returns the id of this node */

57link return this.id;

58link }

59link }

60link}, neogma); // <-- the neogma instance is used

61link/* --> relationships can also be defined after the model definition, to avoid potential circular references */

62link/* --> The same param as "relationships" is used */

63linkUsers.addRelationships({

64link /* --> Orders can be defined either here or in the "relationships" param. Choose one! */

65link Orders: {...}

66link});

67link/* --> statics can also be defined after the model definition, to avoid potential circular references. It should be defined either here or in the statics param. Choose one! */

68linkUsers.foo = function () {

69link return 'foo';

70link};

71link/* --> methods can also be defined after the model definition, to avoid potential circular references. It should be defined either here or in the methods param. Choose one! */

72linkUsers.prototype.bar = function () {

73link return this.id;

74link};

75link

1link

2link/* --> the interface of the properties of the Instance (properties of the node). They match the schema definition */

3linktype UserPropertiesI = {

4link name: string,

5link age?: number,

6link id: string,

7link};

8link

9link/* --> the interface for the related Models. The keys are the arbitrary aliases of the relationships */

10linkinterface UsersRelatedNodesI {

11link Orders: ModelRelatedNodesI<

12link /* --> the related Model */

13link typeof Orders, /* --> when refering to the same Model that is currently being defined, this line must be replaced with `{ createOne: typeof Orders["createOne"] }` */

14link /* --> the type of the Instance of the related Model. It should have a definition to correspond to `UsersInstance`, as defined below */

15link OrdersInstance,

16link /* --> (optional) the interface of the relationship properties, which will be used while creating the relationship. The keys are the aliases to be used to indicate that the property refers to a relationship property */

17link {

18link Rating: number

19link },

20link /* --> (optional) the interface of the relationship properties, as they are in the database. The keys are the actual property names */

21link {

22link rating: number

23link }

24link >

25link}

26link

27link/* --> (optional) types for the methods of the Instance. This has to be defined only if methods are used */

28linkinterface MethodsI {

29link /* --> 'this' needs to be cast as the Instance of this Model (in this example, it is defined a few lines below) */

30link bar: (this: UsersInstance) => string

31link}

32link

33link/* --> (optional) types for the statics of the Model. This has to be defined only if statics are used */

34linkinterface StaticsI {

35link foo: () => string

36link}

37link

38link/* --> the type of the Instance of this Model. Its generics are interfaces that are defined in this file */

39linktype UsersInstance = NeogmaInstance<UserPropertiesI, UsersRelatedNodesI, MethodsI>;

40link

41linkconst Users = ModelFactory<

42link UserPropertiesI,

43link UsersRelatedNodesI,

44link StaticsI, // --> optional, needed only if they are defined

45link MethodsI // --> optional, needed only if they are defined

46link > (

47link {

48link /* --> the label that the nodes of this Model have. For multiple labels, an array can be provided like ['User', 'New'] */

49link label: 'User',

50link /* --> The properties of the nodes of this Model and the validation for them. This follows the revalidator schema configuration */

51link schema: {

52link name: {

53link type: 'string',

54link minLength: 3,

55link required: true

56link },

57link age: {

58link type: 'number',

59link minimum: 0,

60link },

61link id: {

62link type: 'string',

63link required: true,

64link }

65link },

66link /* --> all the possible relationships (with other Models or itself), for relationship-related functions to work properly */

67link relationships: {

68link /* --> an arbitrary alias to be used for identifying this relationship when using the relationship-related functions. It must be a key of UsersRelatedNodesI */

69link Orders: {

70link /* --> reference to the Orders Model. For reference to this model, the value 'self' can be used */

71link model: Orders,

72link /* --> the direction of the relationship. Valid values are 'in' | 'out' | 'none' */

73link direction: 'out',

74link /* --> the name of this relationship */

75link name: 'CREATES',

76link /* --> (optional) properties of the relationship between the nodes */

77link properties: {

78link /* --> the key to be used that the property is a relationship property */

79link Rating: {

80link /* --> the actual property to be created in the relationship (a key of of fourth generic of ModelRelatedNodesI, if given) */

81link property: 'rating',

82link /* --> schema validation for it */

83link schema: {

84link type: 'number',

85link required: true,

86link },

87link },

88link }

89link },

90link },

91link /* --> (optional) the key to be used as a unique identifier, which enables some Instance methods */

92link primaryKeyField: 'id',

93link /* --> (optional) statics to be added to the Model. In this example, can be called using `Users.foo()` */

94link statics: {

95link foo: () => {

96link return 'foo';

97link }

98link },

99link /* --> (optional) methods to be added to the Instance of this Model. In this example, they can be called on a Users Instance using `user.bar()` */

100link methods: {

101link bar: function() {

102link /* --> returns the name of this node with a friendly text */

103link return 'The name of this user is: ' + this.name;

104link }

105link }

106link}, neogma); // <-- the neogma instance is used

107link/* --> relationships can also be defined after the model definition, to avoid potential circular references */

108link/* --> The same param as "relationships" is used */

109linkUsers.addRelationships({

110link /* --> Orders can be defined either here or in the "relationships" param. Choose one! */

111link Orders: {...}

112link});

113link/* --> statics can also be defined after the model definition, to avoid potential circular references. It should be defined either here or in the statics param. Choose one! */

114linkUsers.foo = function () {

115link return 'foo';

116link};

117link/* --> methods can also be defined after the model definition, to avoid potential circular references. It should be defined either here or in the methods param. Choose one! */

118linkUsers.prototype.bar = function (

119link /* --> "this" must be cast as an Instance for proper typings */

120link this: UsersInstance

121link) {

122link return this.id;

123link};

linkRelating a Model to itself

Instead of providing a Model to a relationship model field, the string "self" can be given to relate a Model to itself:

1link...

2linkrelationships: {

3link Orders: {

4link model: 'self',

5link...

linkUsing the Model's helpers

The created Model provides functions for database operations, as well as the following helpers

1link /* --> by providing an alias, gets the relationship configuration (model, direction, name, properties) */

2link Users.getRelationshipConfiguration('Orders'); // --> { model: Orders, direction: 'out', name: 'CREATES', properties: {...} }

3link /* --> by providing an alias, gets the relationship information (model, direction, name). It's similar to getRelationshipConfiguration but it doesn't return "properties", so it's safe to use in QueryBuilder */

4link Users.getRelationshipByAlias('Orders'); // --> { model: Orders, direction: 'out', name: 'CREATES'}

5link /* --> by providing an alias, reverses the configuration of the relationship, so it's perfectly duplicated when definition the same relationship at the other model (here Orders) */

6link Users.reverseRelationshipConfiguration('Orders'); // --> { model: Users, direction: 'in', name: 'CREATES', properties: {...} }

7link /* --> gets the primaryKeyField provided when defining the Model */

8link Users.getPrimaryKeyField(); // --> id

9link /* --> gets a name which is generated by the Model labels */

10link Users.getModelName(); // --> Users

11link /* --> gets the labels of this model as provided in its definition */

12link AdminUsers.getRawLabels(); // --> ['Admin', 'User']

13link /* --> gets the labels of this model, to be used in a query. Wrapper for QueryRunner.getNormalizedLabels */

14link AdminUsers.getLabel(); // --> `Admin`:`User`

15link /* --> getting a Model by its name by using the neogma instance */

16link neogma.modelsByName['Users'];

The ModelRelatedNodesI equals to the following:

1link{

2link /** interface of the actual properties of the relationship (as given in the fourth generic) */

3link RelationshipProperties: object;

4link /** interface of the properties of the relationship used in create functions (as given in the third generic) */

5link CreateRelationshipProperties: object;

6link /** the instance of the related model (as given in the second generic) */

7link Instance: object;

8link /* --> the type of the data to create */

9link CreateData: object;

10link}

For example, after fetching a relationship from the database, its properties can be typed like this:

1link// let "queryResult" be the result of a raw query

2linkconst relationshipProperties: UsersRelatedNodesI['Orders']['RelationshipProperties'] = queryResult.records[0].get('n').properties;

linkDeclaring bidirectional relationships

If a relationship from Model A to Model B is declared, the reverse relationship from Model B to Model A can be declared, but not directly. In order to avoid circular dependencies, it should be added after all models are declared, using the Model.addRelationships static. Follow this issue for more information.

1linkModelB.addRelationships({

2link NewAlias: ModelA.reverseRelationshipConfiguration('OtherAlias'),

3link});

Defining a ModelImporting NeogmaUsing ModelFactoryRelating a Model to itselfUsing the Model's helpersDeclaring bidirectional relationships

Introduction Getting Started

Modelschevron_right

Sessions and Transactions

Query Builderchevron_right
Query Runnerchevron_right

Bind Parameters

Where Parameters