Virtual methods versus methods in Mongoose

I understand that static methods are Class Methods and that methods are Instance Methods and that Virtuals also similar to Instance Methods , but they are not stored in the database.

However, I would like to know if this is the only difference between methods and Virtuals . Is there anything else I am missing?

+9
source share
2 answers

Neither instance methods, nor static methods, nor virtual files are stored in the database. The difference between methods and virtual parts is that virtual objects are treated as properties, and methods are called similar functions. There is no difference between a / static instance and a virtual one, because it makes no sense to have a virtual static property available in the class, but it makes sense to have some static utilities or factory methods in the class.

 var PersonSchema = new Schema({ name: { first: String, last: String } }); PersonSchema.virtual('name.full').get(function () { return this.name.first + ' ' + this.name.last; }); var Person = mongoose.model('Person', PersonSchema); var person = new Person({ name: { first: 'Alex', last: 'Ford' } }); console.log(person.name.full); // would print "Alex Ford" to the console 

While methods are called as ordinary functions.

 PersonSchema.method('fullName', function () { return this.name.first + ' ' + this.name.last; }); var person = new Person({ name: { first: 'Alex', last: 'Ford' } }); console.log(person.fullName()); // notice this time you call fullName like a function 

You can also “set” virtual properties, for example, with ordinary properties. Just call .get and .set to configure the functionality of both actions. Note that in .get you return a value, while in .set you accept a value and use it to set non-virtual properties in your document.

 PersonSchema .virtual('name.full') .get(function () { return this.name.first + ' ' + this.name.last; }) .set(function (fullName) { var parts = fullName.split(' '); this.name.first = parts[0]; this.name.last = parts[1]; }); var person = new Person({ name: { first: 'Alex', last: 'Ford' } }); console.log(person.name.first); // would log out "Alex" person.name.full = 'Billy Bob'; // would set person.name.first and person.name.last appropriately console.log(person.name.first); // would log out "Billy" 

You can technically use methods for everything and never use virtual properties, but virtual properties are elegant for certain things, such as the examples I showed here with person.name.full .

+17
source

I tried to figure out when to use virtuals and when to use instances, and I would summarize it like this:

Statics - For methods (operations) that do not require the launch of an instance of the model. Or general operations with the model.

for example, searching for a document, listing all documents.

Instances - for methods (operations) that require a model instance.

for example, finding or retrieving data based on data from the current instance. Like other documents associated with the instance. model.getRelatedDocs ()

Virtuals - for reading model properties and ascertaining at the code level that these are properties / attributes, not methods. Also for composite properties based on data that is already loaded into the instance. For example, an example with a full name.

0
source

Source: https://habr.com/ru/post/985290/


All Articles