Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
useNewUrlParser: true,
promiseLibrary: global.Promise
})
// 连接错误
mongoose.connection.on('error', error => {
console.warn('数据库连接失败!', error)
})
// 连接成功
mongoose.connection.once('open', () => {
console.ready('数据库连接成功!')
})
// 自增 ID 初始化
autoIncrement.initialize(mongoose.connection)
// 返回实例
return mongoose
}
var LOCK_TIME, MAX_LOGIN_ATTEMPTS, SALT_WORK_FACTOR, Schema, UserSchema, bcrypt, mongoose;
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Promise = require('bluebird');
var autoIncrement = require('mongoose-auto-increment');
autoIncrement.initialize(mongoose.connection);
Schema = mongoose.Schema;
SALT_WORK_FACTOR = 10;
MAX_LOGIN_ATTEMPTS = 5;
LOCK_TIME = 2 * 60 * 60 * 1000;
UserSchema = new Schema({
username: {// 真实姓名
type: String
},
password : String,
unionid : String,
openid: {// from weixin openid
* @file Mongoose 模块转换器
* @module transform/mongoose
* @author Surmon
*/
import * as _mongoose from 'mongoose';
import * as _mongoosePaginate from 'mongoose-paginate';
import * as _mongooseAutoIncrement from 'mongoose-auto-increment';
import * as APP_CONFIG from '@app/app.config';
// 各种 Hack
_mongoose.set('useFindAndModify', false);
(_mongoose as any).Promise = global.Promise;
// 初始化翻页插件
_mongooseAutoIncrement.initialize(_mongoose.connection);
// 插件配置初始化
(_mongoosePaginate as any).paginate.options = {
limit: APP_CONFIG.APP.LIMIT,
};
export const mongoose = _mongoose;
export const mongoosePaginate = _mongoosePaginate;
export const mongooseAutoIncrement = _mongooseAutoIncrement;
export default mongoose;
const mongoose = require('mongoose')
const autoIncrement = require('mongoose-auto-increment')
autoIncrement.initialize(mongoose.connection)
// 页面集合模型
let pageSchema = new mongoose.Schema({
// 页面名称
name: String,
// 别名
slug: String,
// 页面描述
description: String,
// 页面内容
content: { type: String, required: true },
const Agent = new Schema({
name: [String],
address: String
});
Agent.statics.create = function(splitName, address) {
const rating = new this({
name: splitName, // 자동완성을 위해 자모음을 분리하여 저장한다.
address: address
});
return rating.save();
};
autoIncrement.initialize(mongoose.connection);
Agent.plugin(autoIncrement.plugin,'Agent');
module.exports = mongoose.model('Agent', Agent);
const db = mongoose.connect(process.env.MONGO_URL, { options: { db: { safe: true } } }, (e) => {
if (e) throw e;
log('Connected to mongodb.');
mongoose.set('debug', process.env.NODE_ENV === "development");
autoIncrement.initialize(db);
// Bootstrap models
requireDir('./models');
log('Bootstrapped models.');
const
bot = new TelegramBot(env.TELEGRAM_BOT_TOKEN, {
webHook: env.NODE_ENV === 'production' ? {
port: env.PORT
} : false,
polling: env.NODE_ENV === 'development'
});
if (env.NODE_ENV === 'production') {
bot.setWebHook(env.TELEGRAM_WEBHOOK_URL);
}
const db = mongoose.connect(process.env.MONGO_URL, { options: { db: { safe: true } } }, (e) => {
if (e) throw e;
log('Connected to mongodb.');
mongoose.set('debug', process.env.NODE_ENV === "development");
autoIncrement.initialize(db);
// Bootstrap models
requireDir('./models');
log('Bootstrapped models.');
const
bot = new TelegramBot(env.TELEGRAM_BOT_TOKEN, {
webHook: env.NODE_ENV === 'production' ? {
port: env.PORT
} : false,
polling: env.NODE_ENV === 'development'
});
if (env.NODE_ENV === 'production') {
bot.setWebHook(env.TELEGRAM_WEBHOOK_URL);
}
const db = mongoose.connect(process.env.MONGO_URL, { options: { db: { safe: true } } }, (e) => {
if (e) throw e;
log('Connected to mongodb.');
mongoose.set('debug', process.env.NODE_ENV === "development");
autoIncrement.initialize(db);
// Bootstrap models
requireDir('./models');
log('Bootstrapped models.');
// Create push-only bot
const
bot = new TelegramBot(env.TELEGRAM_BOT_TOKEN, {
webHook: false,
polling: false
});
log('Created bot. Starting sync...');
// Register execution timeout
setTimeout(() => {
// If they only have an email, use that with Gravatar
if (this.email) {
var md5 = crypto.createHash('md5').update(this.email).digest('hex');
return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro';
}
// Fallback: If they don't have an email, use their User ID with Gravatar
var md5 = crypto.createHash('md5').update(this._id+'@user.inkrato.com').digest('hex');
return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro';
};
/**
* Auto-incrimenting ID value (in addition to _id property)
*/
var connection = mongoose.createConnection(config.secrets.db);
mongooseAutoIncrement.initialize(connection);
schema.plugin(mongooseAutoIncrement.plugin, {
model: 'User',
field: 'userId',
startAt: 1
});
module.exports = mongoose.model('User', schema);
db.connection.once('open', function () {
// If mongoose-auto-increment plugin is installedInitialize mongoose-auto-increment plugin.
if (settings.autoIncrementNumberIds)
autoIncrement.initialize(db.connection);
// Find and load all Mongoose dbmodels from the dbmodels directory.
fs.readdir(settings.modelsDir, function (err, files) {
if (err) return settings.callback(err);
for (var i in files) {
var file = files[i];
if (path.extname(file) === '.js' || path.extname(file) === '.coffee') {
var modelName = path.basename(file.replace(path.extname(file), '')),
modelData = require(path.join(settings.modelsDir, file));
if (!modelData.hasOwnProperty('schema'))
return settings.callback(new Error('Model file ' + file + ' is invalid: No schema.'));
// Create schema based on template in model file.
var schema;
if (modelData.schemaOptions)