Neogma
Models

ModelFactory (JavaScript)

Define models using the ModelFactory function for JavaScript projects

ModelFactory

ModelFactory is the API for defining models in JavaScript projects where decorators are not available.

Defining a model

const { Neogma, ModelFactory, Type } = require('neogma');

const neogma = new Neogma({
  url: 'bolt://localhost:7687',
  username: 'neo4j',
  password: 'password',
});

const Users = ModelFactory(
  {
    label: 'User',
    schema: {
      id: Type.String(),
      name: Type.String(),
      age: Type.Optional(Type.Number()),
    },
    primaryKeyField: 'id',
    relationships: {
      Orders: {
        model: () => Orders,
        direction: 'out',
        name: 'HAS_ORDER',
      },
    },
  },
  neogma,
);

// Create a user
const user = await Users.createOne({
  id: '1',
  name: 'Alice',
  age: 30,
});

Configuration options

OptionTypeDescription
labelstring | string[]Neo4j label(s) for this node
schemaobjectProperty definitions with TypeBox schemas
primaryKeyFieldstringThe property used as the primary key
relationshipsobjectRelationship definitions
staticsobjectStatic methods added to the model
methodsobjectInstance methods added to model instances

For TypeScript projects, use the decorator-based API instead.

On this page