Document.prototype.$model_alias >= 1.1.0

Description

The alias of the model that was used during query

Flags

Client
This will also be available on the client-side, in the browser.

Type

String

The name used by the current model

Examples

$model_alias of a directly queried document

When you query a document directly, the regular name of the model will be the $model_alias

Let's create two new models: Blogpost & Comment. We'll start with Comment

var Comment = Function.inherits('Alchemy.Model', function Comment(options) {
    Comment.super.call(this, options);
});

// Add some fields
Comment.constitute(function addFields() {
    
    // A comment will belong to a Blogpost
	this.belongsTo('Blogpost');
	
	// It has a simple body
	this.addField('body', 'Text');
});

Now let's create the Blogpost model

var Blogpost = Function.inherits('Alchemy.Model', function Blogpost(options) {
    Blogpost.super.call(this, options);
});

// Add some fields
Blogpost.constitute(function addFields() {
    
    // A blogpost needs a title
    this.addField('title', 'String');
    
    // It has a HTML body
	this.addField('body', 'Html');
    
    // Here we will say each blogpost has many comments,
    // and the alias is "Comments"
	this.hasMany('Comments', 'Comment');
});

Now let's say we already created a few documents for each model, this is how we can get them:

// Get the blogpost model instance
let blogpost_model = Model.get('Blogpost');

// Find a blogpost
let blogpost = await blogpost_model.find('first');

blogpost.$model_alias === 'Blogpost';

// Now let's add all the comments, we'll assume there is at least 1
await blogpost.populate('Comments');

// The $model_alias is 'Comments', not 'Comment'
blogpost.Comments[0].$model_alias === 'Comments';