How to use the mongoose.Model function in mongoose

To help you get started, we’ve selected a few mongoose examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jorgecuesta / mongoose-multitenancy / src / model.js View on Github external
return self.getModel(name);
    }

    // Sometimes self has a bad assignation and be Global scope, for this reason
    // we assign mongoose as self an try to return required model.
    if(!self.db){
        self = mongoose;

        return self.model(name);
    }

    // Normal flow should return model as is.
    return this.db.model(name);
};

module.exports = mongoose.Model.prototype.model;
github SciSpike / yaktor / bin / static / config / global / 02_mongo.js View on Github external
module.exports = function (yaktor, done) {
  require('mongoose-pagination')
  var f1nU = mongoose.Model.findOneAndUpdate
  // Monkey Patch for update existing doc.
  mongoose.Model.findOneAndUpdate = function (q, doc) {
    if (doc != null && doc.toObject != null) {
      var newDoc = doc.toObject()
      delete newDoc._id
      arguments[1] = newDoc
      return f1nU.apply(this, arguments)
    } else {
      return f1nU.apply(this, arguments)
    }
  }

  if (mongoose.connection._readyState !== 0) return done()

  var opened = false
github davideicardi / search-crawler / src / mongoose-q.js View on Github external
function denodifyMethod(funcName){
	return function(){
		return Q.npost(this, funcName, arguments);
	}
};

// Query
mongoose.Query.prototype.findQ = denodifyMethod("find");
mongoose.Query.prototype.findOneQ = denodifyMethod("findOne");
mongoose.Query.prototype.countQ = denodifyMethod("count");
mongoose.Query.prototype.removeQ = denodifyMethod("remove");

// Model
mongoose.Model.prototype.saveQ = denodifyMethod("save");
mongoose.Model.prototype.removeQ = denodifyMethod("remove");


mongoose.connectQ = connectQ;

module.exports = mongoose;
github behrendtio / monky / test / stub.js View on Github external
var sinon     = require('sinon')
  , mongoose  = require('mongoose');

sinon.stub(mongoose.Model.prototype, 'save', function(cb) {
  var model = this;
  this.validate(function(err) {
    if (!err) {
      model.isNew = false;
    }
    cb(err, model);
  });
});
github plumier / plumier / packages / mongoose / src / index.ts View on Github external
export function model(type: Constructor) {
    class ModelMock { }
    return new Proxy(Mongoose.Model as Mongoose.Model, new ModelProxyHandler(type))
}
github vellengs / nestx / packages / server / dist / core / controllers / roles.service.js View on Github external
this.model = model;
        this.defaultQueryFields = ['name', 'description', 'permissions'];
    }
    querySearch(keyword, page, size, sort) {
        const _super = Object.create(null, {
            query: { get: () => super.query }
        });
        return __awaiter(this, void 0, void 0, function* () {
            return _super.query.call(this, page, size, {}, { keyword, field: 'name' }, this.defaultQueryFields, sort);
        });
    }
};
RolesService = __decorate([
    common_1.Injectable(),
    __param(0, mongoose_2.InjectModel('Role')),
    __metadata("design:paramtypes", [mongoose_1.Model])
], RolesService);
exports.RolesService = RolesService;
//# sourceMappingURL=roles.service.js.map
github vellengs / nestx / packages / server / dist / cms / controllers / widget.service.js View on Github external
this.model = model;
        this.defaultQueryFields = ['name', 'translate', 'expand'];
    }
    querySearch(keyword, page, size, sort) {
        const _super = Object.create(null, {
            query: { get: () => super.query }
        });
        return __awaiter(this, void 0, void 0, function* () {
            return _super.query.call(this, page, size, {}, { keyword, field: 'name' }, this.defaultQueryFields, sort);
        });
    }
};
WidgetService = __decorate([
    common_1.Injectable(),
    __param(0, mongoose_2.InjectModel('Widget')),
    __metadata("design:paramtypes", [mongoose_1.Model])
], WidgetService);
exports.WidgetService = WidgetService;
//# sourceMappingURL=widget.service.js.map
github jorgecuesta / mongoose-multitenancy / src / discriminator.js View on Github external
}
  }
};

/*
 * Register statics for this model
 * @param {Model} model
 * @param {Schema} schema
 */
var applyStatics = function(model, schema) {
  for (var i in schema.statics) {
    model[i] = schema.statics[i];
  }
};

module.exports = mongoose.Model.discriminator;
github async-labs / saas / book / 10-begin / api / server / models / Invitation.ts View on Github external
addUserToTeam({ token, user }: { token: string; user: IUserDocument });
}

function generateToken() {
  const gen = () =>
    Math.random()
      .toString(36)
      .substring(2, 12) +
    Math.random()
      .toString(36)
      .substring(2, 12);

  return `${gen()}`;
}

class InvitationClass extends mongoose.Model {
  public static async add({ userId, teamId, email }) {
    if (!teamId || !email) {
      throw new Error('Bad data');
    }

    const team = await Team.findById(teamId).setOptions({ lean: true });
    if (!team || team.teamLeaderId !== userId) {
      throw new Error('Team does not exist or you have no permission');
    }

    const registeredUser = await User.findOne({ email })
      .select('defaultTeamSlug')
      .setOptions({ lean: true });

    if (registeredUser) {
      if (team.memberIds.includes(registeredUser._id.toString())) {
github kaleabmelkie / meseret / src / model / ModelFactory.js View on Github external
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
var mongoose_1 = require('mongoose')
exports.Model = mongoose_1.Model
exports.Schema = mongoose_1.Schema
var ModelFactory = /** @class */ (function() {
  function ModelFactory(_config) {
    this._config = _config
  }
  Object.defineProperty(ModelFactory.prototype, 'schema', {
    get: function() {
      if (!this._schema) {
        this._schema = new mongoose_1.Schema(
          this._config.paths,
          this._config.options
        )
        this._schema.method(this._config.methods || {})
        this._schema.static(this._config.statics || {})
      }
      return this._schema