How to use the mongoose.set 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 kunalkapadia / express-mongoose-es6-rest-api / index.js View on Github external
// make bluebird default Promise
Promise = require('bluebird'); // eslint-disable-line no-global-assign

// plugin bluebird promise in mongoose
mongoose.Promise = Promise;

// connect to mongo db
const mongoUri = config.mongo.host;
mongoose.connect(mongoUri, { server: { socketOptions: { keepAlive: 1 } } });
mongoose.connection.on('error', () => {
  throw new Error(`unable to connect to database: ${mongoUri}`);
});

// print mongoose logs in dev env
if (config.MONGOOSE_DEBUG) {
  mongoose.set('debug', (collectionName, method, query, doc) => {
    debug(`${collectionName}.${method}`, util.inspect(query, false, 20), doc);
  });
}

// module.parent check is required to support mocha watch
// src: https://github.com/mochajs/mocha/issues/1912
if (!module.parent) {
  // listen on port config.port
  app.listen(config.port, () => {
    console.info(`server started on port ${config.port} (${config.env})`); // eslint-disable-line no-console
  });
}

export default app;
github dunizb / CodeTest / NodeJS / move-prevue / server / databse / init.js View on Github external
return new Promise((resolve, reject) => {
        if (process.env.NODE_ENV !== 'production') {
            mongoose.set('debug', true)
        }

        mongoose.connect(db)
        mongoose.connection.on('disconnnected', () => {
            maxConnectTimers++
            if (maxConnectTimers < 5) {
                mongoose.connect(db)
            } else {
                throw new Error('数据库挂了,赶快去修吧!!')
            }
        })
        mongoose.connection.on('error', error => {
            reject()
            console.log(error)
        })
        mongoose.connection.on('open', () => {
github sunyue1992 / pixiv / server / models / index.js View on Github external
import mongoose from 'mongoose'
import config from '../config'
mongoose.Promise = global.Promise
mongoose.set('debug',config.env === 'development')
export default mongoose.connect(config.mongodb.url,{
  useMongoClient: true,
  autoReconnect: true,
  reconnectTries: 1800,
  reconnectInterval: 2000
},function (err) {
  if (err) {
    console.log(err)
  }
})
export {default as IllustsRecommendedNologin} from './illusts_recommended_nologin'
github catedrasaes-umu / NoSQLDataEngineering / es.um.nosql.s13e.entitydifferentiation.examples / mongoose / stackoverflow / addDbObjects.js View on Github external
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://127.0.0.1/stackoverflow',
{
  useMongoClient: true
}, function(err)
{
  if (err)
    console.log(err);
  else
    console.log('Connected to 127.0.0.1/stackoverflow');
});
mongoose.set('debug', true);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error: '));

var Badges = require('./app/models/BadgesSchema');
var Comments = require('./app/models/CommentsSchema');
var Posts = require('./app/models/PostsSchema');
var Votes = require('./app/models/VotesSchema');
var Users = require('./app/models/UsersSchema');
var Postlinks = require('./app/models/PostlinksSchema');
var Tags = require('./app/models/TagsSchema');

var err;
var badges = new Badges({});
err = badges.validateSync();
if (err !== undefined)
  console.log(err);
github muhzi4u / URL-Shortner / server / index.js View on Github external
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const constants = require('./config/constants');

//Connect to MongoDB
mongoose.Promise = global.Promise;
mongoose.set('debug', true);
mongoose.connect(
  constants.mongoURI,
  {
    keepAlive: true,
    reconnectTries: Number.MAX_VALUE,
    useMongoClient: true,
  },
);

require('./models/UrlShorten');
const app = express();

app.use(bodyParser.json());
app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
github async-labs / saas / book / 13-end / lambda / src / api / models / Post.ts View on Github external
import * as mongoose from 'mongoose';

import * as he from 'he';
import * as hljs from 'highlight.js';
import * as marked from 'marked';

import { chunk } from 'lodash';
import * as qs from 'querystring';
import * as url from 'url';

import { deleteFiles } from '../aws-s3';
import logger from '../logs';
import Discussion from './Discussion';
import Team from './Team';

mongoose.set('useFindAndModify', false);

function deletePostFiles(posts: IPostDocument[]) {
  const imgRegEx = /\ {
    let res = imgRegEx.exec(post.content);

    while (res) {
      const { bucket, path } = qs.parse(url.parse(res[1]).query);

      if (typeof bucket !== 'string' || typeof path !== 'string') {
        continue;
      }

      if (!files[bucket]) {
github johnpapa / angular-ngrx-data / src / server / services / mongo.js View on Github external
function connect() {
  mongoose.set('debug', true);
  return mongoose.connect(mongoUri, { useMongoClient: true });
}
github async-labs / saas / api / server / models / Discussion.ts View on Github external
import { uniq } from 'lodash';
import * as mongoose from 'mongoose';

import { generateNumberSlug } from '../utils/slugify';
import Post, { deletePostFiles } from './Post';
import Team from './Team';

mongoose.set('useFindAndModify', false);

const mongoSchema = new mongoose.Schema({
  createdUserId: {
    type: String,
    required: true,
  },
  teamId: {
    type: String,
    required: true,
  },
  name: {
    type: String,
    required: true,
  },
  slug: {
    type: String,
github moleculerjs / moleculer-db / packages / moleculer-db-adapter-mongoose / src / index.js View on Github external
/*
 * moleculer-db-adapter-mongoose
 * Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer-db)
 * MIT Licensed
 */

"use strict";

const _ 		= require("lodash");
const Promise	= require("bluebird");
const { ServiceSchemaError } = require("moleculer").Errors;
const mongoose  = require("mongoose");

mongoose.set("useNewUrlParser", true);
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);

class MongooseDbAdapter {

	/**
	 * Creates an instance of MongooseDbAdapter.
	 * @param {String} uri
	 * @param {Object?} opts
	 *
	 * @memberof MongooseDbAdapter
	 */
	constructor(uri, opts) {
		this.uri = uri,
		this.opts = opts;
		mongoose.Promise = Promise;
	}