From 718a9a77b6a28f3b49ee05256deb2dc6f772e65c Mon Sep 17 00:00:00 2001 From: Captain Caius <241342+captaincaius@users.noreply.github.com> Date: Tue, 29 Oct 2019 01:13:09 +0200 Subject: [PATCH] fix(document): allow validation of moddelless documents on node side Fix #8237, fix #8272, and improve DX by making ephemeral models similar to normal ones --- lib/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/index.js b/lib/index.js index 6fbfb932e5c..cbe921ba683 100644 --- a/lib/index.js +++ b/lib/index.js @@ -23,6 +23,7 @@ const STATES = require('./connectionstate'); const Types = require('./types'); const Query = require('./query'); const Model = require('./model'); +const BrowserDocument = require('./browserDocument'); const Document = require('./document'); const applyPlugins = require('./helpers/schema/applyPlugins'); const get = require('./helpers/get'); @@ -396,6 +397,25 @@ Mongoose.prototype.pluralize = function(fn) { return _mongoose._pluralize; }; + +Mongoose.prototype.ephemodel = function(schema, skipInit) { + let ephemodel = function Ephemodel(doc, fields, skipId) { + if (doc instanceof Schema) { + throw new TypeError('Argument to `Ephemodel` must be a POJO, ' + + '**not** a schema. Make sure you\'re calling `mongoose.ephemodel()`, not ' + + '`mongoose.Ephemodel()`.'); + } + BrowserDocument.call(this, doc, this.schema, fields, skipId, skipInit); + }; + ephemodel.__proto__ = BrowserDocument; + + ephemodel.prototype = Object.create(BrowserDocument.prototype); + ephemodel.prototype.constructor = ephemodel; + + ephemodel.prototype.schema = schema; + return ephemodel; +} + /** * Defines a model or retrieves it. *