These are chat archives for Automattic/mongoose

13th
Nov 2015
Dawid Pura
@puradawid
Nov 13 2015 14:34
Hi, is there any option to get model's schema as a object? I want to clone schema to another model in plugin and looking for an option to copy (and modify something) every field to new one.
I am trying to get all paths, map them to object model but it looks a little hacked. Any thoughts?
Greg Linklater
@EternalDeiwos
Nov 13 2015 15:10
would anyone be able to tell me if mongodb-js/mongoose-autopopulate evaluates lazily or not?
Valeri Karpov
@vkarpov15
Nov 13 2015 17:11
@EternalDeiwos define lazily :)
@cadebward yep, .find() is chainable so you can do things like MyModel.find({ a: 1 }).find({ b: 2 }); and that is equivalent to MyModel.find({ a: 1, b: 2 });
@puradawid I don't know of a good way to do that. The way I usually do that is either through single embedded schemas (http://mongoosejs.com/docs/subdocs.html#single-embedded) or just take the schema definition as a POJO
Dawid Pura
@puradawid
Nov 13 2015 17:17
@vkarpov15 Thanks, I found solution by using mongoose-schema-extend, just not giving any additional properties. Its not the best solution, but it is working :)
Valeri Karpov
@vkarpov15
Nov 13 2015 19:33
I've had some trouble with mongoose-schema-extend in the past, but if it works it works :)
Greg Linklater
@EternalDeiwos
Nov 13 2015 21:44
@vkarpov15 as in does it evaluate the references when they are used or does it autopopulate references when the find() is executed?
Valeri Karpov
@vkarpov15
Nov 13 2015 23:20
@EternalDeiwos still not 100% sure what you mean. The general idea of how mongoose-autopopulate works is that it registers pre('find'); and pre('findOne'); hooks that call this.populate(); for every schema field that has 'autopopulate' option set