mbroadst/thinkagain

View on GitHub
lib/model.js

Summary

Maintainability
F
1 wk
Test Coverage

File model.js has 724 lines of code (exceeds 250 allowed). Consider refactoring.
Open

'use strict';
const Promise = require('bluebird'),
      EventEmitter = require('events'),
      Document = require('./document'),
      Errors = require('./errors'),
Severity: Major
Found in lib/model.js - About 1 day to fix

    Model has 36 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class Model extends EventEmitter {
    
      /*
      * Constructor for a Model. Note that this is not what `thinkagain.createModel`
      * returns. It is the prototype of what `thinkagain.createModel` returns.
    Severity: Minor
    Found in lib/model.js - About 4 hrs to fix

      Function hasAndBelongsToMany has 100 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        hasAndBelongsToMany(joinedModel, fieldDoc, leftKey, rightKey, options) {
          let link;
          let thinkagain = this._getModel()._thinkagain;
          options = options || {};
      
      
      Severity: Major
      Found in lib/model.js - About 4 hrs to fix

        Function new has 67 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          static new(name, schema, options, thinkagain) {
            let validate = thinkagain.ajv.compile(schema);
            let proto = new Model(name, validate, options, thinkagain);
            proto._initModel = options.init  !== undefined ? !!options.init : true;
        
        
        Severity: Major
        Found in lib/model.js - About 2 hrs to fix

          Function save has 66 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            save(docs, options) {
              let r = this._getModel()._thinkagain.r;
              let isArray = Array.isArray(docs);
          
              if (!isArray) {
          Severity: Major
          Found in lib/model.js - About 2 hrs to fix

            Function save has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
            Open

              save(docs, options) {
                let r = this._getModel()._thinkagain.r;
                let isArray = Array.isArray(docs);
            
                if (!isArray) {
            Severity: Minor
            Found in lib/model.js - About 2 hrs to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function new has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
            Open

              static new(name, schema, options, thinkagain) {
                let validate = thinkagain.ajv.compile(schema);
                let proto = new Model(name, validate, options, thinkagain);
                proto._initModel = options.init  !== undefined ? !!options.init : true;
            
            
            Severity: Minor
            Found in lib/model.js - About 2 hrs to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Function model has 49 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                let model = function model(doc, _options) {
                  if (!util.isPlainObject(doc)) {
                    throw new Errors.ThinkAgainError('Cannot build a new instance of `' + proto._name + '` without an object');
                  }
            
            
            Severity: Minor
            Found in lib/model.js - About 1 hr to fix

              Function hasAndBelongsToMany has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
              Open

                hasAndBelongsToMany(joinedModel, fieldDoc, leftKey, rightKey, options) {
                  let link;
                  let thinkagain = this._getModel()._thinkagain;
                  options = options || {};
              
              
              Severity: Minor
              Found in lib/model.js - About 1 hr to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function constructor has 42 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                constructor(name, validate, options, thinkagain) {
                  super();
              
                  /**
                   * Name of the table used
              Severity: Minor
              Found in lib/model.js - About 1 hr to fix

                Function belongsTo has 39 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                  belongsTo(joinedModel, fieldDoc, leftKey, rightKey, options) {
                    if ((joinedModel instanceof Model) === false) {
                      throw new Errors.ThinkAgainError('First argument of `belongsTo` must be a Model');
                    }
                
                
                Severity: Minor
                Found in lib/model.js - About 1 hr to fix

                  Function executeInsert has 37 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      let executeInsert = (resolve, reject) => {
                        toSave--;
                        resolves.push(resolve);
                        rejects.push(reject);
                  
                  
                  Severity: Minor
                  Found in lib/model.js - About 1 hr to fix

                    Function hasOne has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                      hasOne(joinedModel, fieldDoc, leftKey, rightKey, options) {
                        if ((joinedModel instanceof Model) === false) {
                          throw new Errors.ThinkAgainError('First argument of `hasOne` must be a Model');
                        }
                    
                    
                    Severity: Minor
                    Found in lib/model.js - About 1 hr to fix

                      Function hasMany has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                        hasMany(joinedModel, fieldDoc, leftKey, rightKey, options) {
                          if ((joinedModel instanceof Model) === false) {
                            throw new Errors.ThinkAgainError('First argument of `hasMany` must be a Model');
                          }
                      
                      
                      Severity: Minor
                      Found in lib/model.js - About 1 hr to fix

                        Function _promisesReady has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
                        Open

                          _promisesReady() {
                            let self = this;
                            if (this._promisesReadyPromise) return this._promisesReadyPromise;
                            let verifyAll = function() {
                              return Promise.all(self._pendingPromises)
                        Severity: Minor
                        Found in lib/model.js - About 1 hr to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Avoid deeply nested control flow statements.
                        Open

                                      if (doc[key][i] instanceof Document === false) {
                                        doc[key][i] = new joins[key].model(doc[key][i], _options); // eslint-disable-line
                                      }
                        Severity: Major
                        Found in lib/model.js - About 45 mins to fix

                          Function docRemoveListener has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                          Open

                            docRemoveListener(ev, listener) {
                              if (Array.isArray(this._getModel()._listeners[ev])) {
                                for (let i = 0, ii = this._getModel()._listeners[ev].length; i < ii; ++i) {
                                  if (this._getModel()._listeners[ev][i] === listener) {
                                    this._getModel()._listeners[ev].splice(i, 1);
                          Severity: Minor
                          Found in lib/model.js - About 25 mins to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          TODO found
                          Open

                                  // TODO: This regex seems a bit too generous since messages such
                          Severity: Minor
                          Found in lib/model.js by fixme

                          TODO found
                          Open

                                        //TODO Expand error with more information
                          Severity: Minor
                          Found in lib/model.js by fixme

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                            post(ev, fn) {
                              if (typeof fn !== 'function') {
                                throw new Errors.ThinkAgainError('Second argument to `pre` must be a function');
                              }
                              if (fn.length > 1) {
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 7 hrs to fix
                          lib/model.js on lines 907..919

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 181.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                            pre(ev, fn) {
                              if (typeof fn !== 'function') {
                                throw new Errors.ThinkAgainError('Second argument to `pre` must be a function');
                              }
                              if (fn.length > 1) {
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 7 hrs to fix
                          lib/model.js on lines 921..933

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 181.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                            docAddListener(eventKey, listener) {
                              let listeners = this._getModel()._listeners;
                              if (listeners[eventKey] == null) { // eslint-disable-line
                                listeners[eventKey] = [];
                              }
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 2 hrs to fix
                          lib/model.js on lines 859..868

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 77.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                            docOnce(eventKey, listener) {
                              let listeners = this._getModel()._listeners;
                              if (listeners[eventKey] == null) { // eslint-disable-line
                                listeners[eventKey] = [];
                              }
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 2 hrs to fix
                          lib/model.js on lines 848..857

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 77.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Identical blocks of code found in 2 locations. Consider refactoring.
                          Open

                                      for (let i = 0, ii = doc[key].length; i < ii; ++i) {
                                        if (doc[key][i] instanceof Document === false) {
                                          doc[key][i] = new joins[key].model(doc[key][i], _options); // eslint-disable-line
                                        }
                                      }
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 1 hr to fix
                          lib/model.js on lines 113..117

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 65.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Identical blocks of code found in 2 locations. Consider refactoring.
                          Open

                                      for (let i = 0, ii = doc[key].length; i < ii; ++i) {
                                        if (doc[key][i] instanceof Document === false) {
                                          doc[key][i] = new joins[key].model(doc[key][i], _options); // eslint-disable-line
                                        }
                                      }
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 1 hr to fix
                          lib/model.js on lines 119..123

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 65.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                            case 'get':
                                // Make a copy of `get` into `_get`
                              Model.prototype._get = function() {
                                let query = new Query(this);
                                query = query._get.apply(query, arguments);
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 1 hr to fix
                          lib/model.js on lines 992..997

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 57.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          Similar blocks of code found in 2 locations. Consider refactoring.
                          Open

                            default: // eslint-disable-line
                              Model.prototype[key] = function() {
                                let query = new Query(this);
                                query = query[key].apply(query, arguments);
                                return query;
                          Severity: Major
                          Found in lib/model.js and 1 other location - About 1 hr to fix
                          lib/model.js on lines 985..991

                          Duplicated Code

                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                          Tuning

                          This issue has a mass of 57.

                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                          Refactorings

                          Further Reading

                          There are no issues that match your filters.

                          Category
                          Status