How to use the mongoose.models 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 gerardobort / mongorilla / models / generic.js View on Github external
exports.getModel = function (collectionName) {
    var model = mongoose.models[collectionName];

    if (model) {

        return model;

    } else {

        var collection = _(global.config.collections).find(function (col) {
            return col.name === collectionName;
        });

        // _id should not be specified in schema ... http://stackoverflow.com/a/10835032
        var schema = collection.mongoose.schema || { };

        /*
            default types
github afj176 / generator-mongoose / app / templates / routes / post.js View on Github external
module.exports = function(app) {
  // Module dependencies.
  var mongoose = require('mongoose'),
      Post = mongoose.models.Post,
      api = {};

  // ALL
  api.posts = function (req, res) {
    Post.find(function(err, posts) {
      if (err) {
        res.status(500).json(err);
      } else {
        res.status(200).json({posts: posts});
      }
    });
  };

  // GET
  api.post = function (req, res) {
    var id = req.params.id;
github mattpker / node-is-master / is-master.js View on Github external
startDate: {
            type: Date,
            index: {
                unique: true
            }
        },
        updateDate: {
            type: Date,
            index: {
                expires: this.timeout + 60
            }
        }
    });

    // ensure we aren't attempting to redefine a collection that already exists
    if (mongoose.models.hasOwnProperty(this.collection)) {
        this.imModel = mongoose.model(this.collection);
    } else{
        this.imModel = mongoose.model(this.collection, imSchema);
    }

    this.worker = new this.imModel({
        hostname: this.hostname,
        pid: this.pid,
        versions: this.versions,
        memory: process.memoryUsage(),
        uptime: process.uptime(),
        startDate: new Date(),
        updateDate: new Date()
    });
};
github paypal / appsforhere / models / app.js View on Github external
appSchema.methods.decryptSecureConfigurationWithKey = function (key, cb) {
        crypto.decryptTokenWithKey(this.encryptedConfiguration, key, function (err, rz) {
            if (err) {
                cb(err);
            } else {
                cb(err, JSON.parse(rz));
            }
        });
    };

    return mongoose.model('CheckinApp', appSchema);
};

// In case you somehow require this twice when it thinks they're separate modules.
module.exports = mongoose.models.CheckinApp || new appModel();
github gerardobort / mongorilla / models / revision.js View on Github external
exports.getModel = function (collectionName) {
    var model = mongoose.models[collectionName + 'Revision'];

    if (model) {

        return model;

    } else {

        // _id should not be specified in schema ... http://stackoverflow.com/a/10835032
        var schema = {
            objectId: { type: ObjectId, ref: collectionName },
            collectionName: String,
            user: String,
            created: Date,
            is_draft: { type: Boolean, default: false },
            first_revision: { type: Boolean, default: false },
            description: String,
github Xantier / nerd-stack / app / data / mongo / init.js View on Github external
import mongoose from 'mongoose';
import config from '../../config/config.json';

const mongopt = config.mongo.config.development;
const options = {
  db: {native_parser: true},
  server: {poolSize: 5},
  user: mongopt.user,
  pass: mongopt.pass
};

mongoose.connect(mongopt.db, options);
mongoose.models = {};

// Register models
import User from './model/User';
User.register(mongoose);
import Thing from './model/Thing';
Thing.register(mongoose);

export default mongoose;
github aksyonov / mongoose-decorators / test / index.js View on Github external
beforeEach(() => {
  mongoose.models = {};
});
github difysjs / difys / src / Services / binaries / index.js View on Github external
export default async function(enabledBinaries) {
	console.log(
		`\x1b[44m Service \x1b[0m Binary-Updater - \x1b[35mInitialising...\x1b[0m`
	);
	try {
		const assetsResponse = await request.get(
			constants.baseUrl + constants.entries.assets
		);
		const assetsVersion = assetsResponse.body.assetsVersion;
		const requestOptions = await getRequestOptions(assetsVersion);

		for (const binaryName of enabledBinaries) {
			const Model = mongoose.models[binaryName];
			requestOptions.body.class = binaryName;

			if (await isModelEmpty(Model)) {
				console.log(`Inserting ${binaryName} into the database`);
				const response = await request.post(requestOptions);
				await Model.insertMany(Object.values(response.body));
			}
		}
	} catch (error) {
		return error;
	}
}
github hkjels / crud / lib / crud.js View on Github external
app.crud = function (name, actions, opts) {
  var options = actions || {}
    , res
    , Model

  for (var model in mongoose.models) {
    if (!mongoose.models.hasOwnProperty(model)) continue
    if (model.toLowerCase() === name.toLowerCase()) Model = mongoose.model(model)
  }

  this.resources = this.resources || {}
  res = this.resources[name] = this.resource(name, new Resource(Model))
  return res
}
github gerardobort / mongorilla / helpers / pager.js View on Github external
_(data).each(function (modelData) {
                                var MongoosePopulateModel = mongoose.models[modelData.type] || MongooseModel;
                                var populatePromise = new mongoose.Promise();
                                MongoosePopulateModel.findOne({ _id: modelData._id }).populate(options.populate).exec(function (err, model) {
                                    populatePromise.resolve(err, model ? (model.toRestJSON ? model.toRestJSON(req) : model.toJSON()) : null);
                                });
                                populatePromises.push(populatePromise);
                            });
                            mongoose.Promise