Skip to content

Commit

Permalink
docs(schema): add examples to schema functions
Browse files Browse the repository at this point in the history
Re: #7525
  • Loading branch information
vkarpov15 committed Feb 18, 2019
1 parent 270732e commit 13c7a00
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/schema.js
Expand Up @@ -784,6 +784,14 @@ Schema.prototype.eachPath = function(fn) {
/**
* Returns an Array of path strings that are required by this schema.
*
* ####Example:
* const s = new Schema({
* name: { type: String, required: true },
* age: { type: String, required: true },
* notes: String
* });
* s.requiredPaths(); // [ 'age', 'name' ]
*
* @api public
* @param {Boolean} invalidate refresh the cache
* @return {Array}
Expand Down Expand Up @@ -828,6 +836,14 @@ Schema.prototype.indexedPaths = function indexedPaths() {
*
* Given a path, returns whether it is a real, virtual, nested, or ad-hoc/undefined path.
*
* ####Example:
* const s = new Schema({ name: String, nested: { foo: String } });
* s.virtual('foo').get(() => 42);
* s.pathType('name'); // "real"
* s.pathType('nested'); // "nested"
* s.pathType('foo'); // "virtual"
* s.pathType('fail'); // "adhocOrUndefined"
*
* @param {String} path
* @return {String}
* @api public
Expand Down Expand Up @@ -1059,6 +1075,14 @@ function getPositionalPath(self, path) {
/**
* Adds a method call to the queue.
*
* ####Example:
*
* schema.methods.print = function() { console.log(this); };
* schema.queue('print', []); // Print the doc every one is instantiated
*
* const Model = mongoose.model('Test', schema);
* new Model({ name: 'test' }); // Prints '{"_id": ..., "name": "test" }'
*
* @param {String} name name of the document method to call later
* @param {Array} args arguments to pass to the method
* @api public
Expand Down Expand Up @@ -1168,6 +1192,12 @@ Schema.prototype.post = function(name) {
/**
* Registers a plugin for this schema.
*
* ####Example:
*
* const s = new Schema({ name: String });
* s.plugin(schema => console.log(schema.path('name').path));
* mongoose.model('Test', schema); // Prints 'name'
*
* @param {Function} plugin callback
* @param {Object} [opts]
* @see plugins
Expand Down Expand Up @@ -1510,6 +1540,13 @@ Schema.prototype.virtualpath = function(name) {
/**
* Removes the given `path` (or [`paths`]).
*
* ####Example:
*
* const schema = new Schema({ name: String, age: Number });
* schema.remove('name');
* schema.path('name'); // Undefined
* schema.path('age'); // SchemaNumber { ... }
*
* @param {String|Array} path
* @return {Schema} the Schema instance
* @api public
Expand Down

0 comments on commit 13c7a00

Please sign in to comment.