Skip to content

Commit

Permalink
fix(document): allow validation of moddelless documents on node side
Browse files Browse the repository at this point in the history
Fix Automattic#8237, fix Automattic#8272, and improve DX by making ephemeral models similar
to normal ones
  • Loading branch information
captaincaius committed Oct 28, 2019
1 parent 28e8ac4 commit 718a9a7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/index.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 718a9a7

Please sign in to comment.