-
Notifications
You must be signed in to change notification settings - Fork 4
Schema
Schemas are basically Model definitions. Schemas compile into Models which are used to perform CRUD operations on your data.
Models internally manipulate vertices and edges in the graph database. Note that a Model is ultimately bound to at least one vertex in the database. This Vertex will have a $type
property (prefixed with a dollar sign) with the model's lowercased name as its value (ie. 'user').
user.js
schema file:
// This will internally be saved as a Vertex with a 'name' key of type 'String'
UserSchema = new mogwai.Schema(
name: String
);
// Export and compile Schema into a Model (setting $type key to 'user')
module.exports = mogwai.model("User", UserSchema)
Alternatively, you can also define properties this way, and add more options:
UserSchema = new mogwai.Schema(
name:
type: String // Only 'String' is supported for now
index: true // Should work with Titan v0.4.0
unique: true // Should work with Titan v0.4.0
);
Each Schema has a "statics" and "methods" object used to store the definitions of, respectively, class methods and instance methods. This mimics the behavior of Mongoose Schema definition.
// This method will be available as a static/class method
UserSchema.statics.findByName = function(name, callback) {
this.findOne({ "name": name }, callback);
};
// This method will be attached to any instance of User models retrieved from the database
UserSchema.methods.edit = function(data, callback) {
this.name = data.name;
return this.save(callback);
};
Models have access to a this.gremlin()
function which allows you to execute any Gremlin code. This code is then executed asynchronously on the server (by passing a callback, calling execute()
or query()`).
UserSchema.statics.findAllVertices = function() {
this.gremlin("g.V()", function(err, vertices) {
// Handle result
});
};