ModelInstance
When querying the database with a Model you'll get almost everytime an object of ModelInstance in return. With this model instance you get the properties and methods for it's relations. By default, each ModelInstance has a props property and a guid which get's generated each time you create a ModelInstance via it's Model.create method.
Example
import { Model, ModelInstance } from "neo4-js";
type TaskProps = {
title?: string,
};
const Task: Model<TaskProps, ModelInstance<TaskProps>> = new Model("Task");
Task.create({ title: "Buy milk" })
.then((createdTask: ModelInstance<TaskProps>) => {
...
});
Example with class syntax
import { Model, ModelInstance } from "neo4-js";
type TaskProps = {
title?: string,
};
class TaskModel extends Model<TaskProps, TaskInstance> { }
const Task: TaskModel = new TaskModel("Task");
class TaskInstance extends ModelInstance<TaskProps> { }
Task.create({ title: "Buy milk" })
.then((createdTask: TaskInstance) => {
...
});
Connecting a ModelInstance to its Model
To be able to add default properties and instance methods to our ModelInstance we need to connect the ModelInstance class to its Model object, otherwise the Model will create a standard ModelInstance object.
Example
import { Model, ModelInstance, model } from "neo4-js";
type TaskProps = {
title?: string,
};
class TaskModel extends Model<TaskProps, TaskInstance> { }
const Task: TaskModel = new TaskModel("Task");
@model(Task)
class TaskInstance extends ModelInstance<TaskProps> {
/**
* Space to define instance methods
*/
}
Default properties
To define default properties when Model.create gets called, neo4-js provides a short decorator.
Example
import { Model, ModelInstance, model, defaultProps } from "neo4-js";
type TaskProps = {
title?: string,
};
class TaskModel extends Model<TaskProps, TaskInstance> { }
const Task: TaskModel = new TaskModel("Task");
@defaultProps({
title: "(empty)",
done: () => false,
})
@model(Task)
class TaskInstance extends ModelInstance<TaskProps> { }
Task.create({ title: "Buy milk" })
.then((createdTask: TaskInstance) => {
...
});