PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/mongoose/lib/services/model/applyHooks.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 51 lines | 35 code | 7 blank | 9 comment | 6 complexity | 6355e816606fbe3c890840ea16b94970 MD5 | raw file
 1'use strict';
 2
 3/*!
 4 * ignore
 5 */
 6
 7module.exports = applyHooks;
 8
 9/*!
10 * Register hooks for this model
11 *
12 * @param {Model} model
13 * @param {Schema} schema
14 */
15
16function applyHooks(model, schema, options) {
17  options = options || {};
18
19  const kareemOptions = {
20    useErrorHandlers: true,
21    numCallbackParams: 1,
22    nullResultByDefault: true,
23    contextParameter: true
24  };
25  const objToDecorate = options.decorateDoc ? model : model.prototype;
26
27  model.$appliedHooks = true;
28  for (let i = 0; i < schema.childSchemas.length; ++i) {
29    const childModel = schema.childSchemas[i].model;
30    if (childModel.$appliedHooks) {
31      continue;
32    }
33    applyHooks(childModel, schema.childSchemas[i].schema, options);
34    if (childModel.discriminators != null) {
35      let keys = Object.keys(childModel.discriminators);
36      for (let j = 0; j < keys.length; ++j) {
37        applyHooks(childModel.discriminators[keys[j]],
38          childModel.discriminators[keys[j]].schema, options);
39      }
40    }
41  }
42
43  objToDecorate.$__save = schema.s.hooks.
44    createWrapper('save', objToDecorate.$__save, null, kareemOptions);
45  objToDecorate.$__validate = schema.s.hooks.
46    createWrapper('validate', objToDecorate.$__validate, null, kareemOptions);
47  objToDecorate.$__remove = schema.s.hooks.
48    createWrapper('remove', objToDecorate.$__remove, null, kareemOptions);
49  objToDecorate.$__init = schema.s.hooks.
50    createWrapperSync('init', objToDecorate.$__init, null, kareemOptions);
51}