mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-01-15 11:52:23 +01:00
* commit 'b43c80e2c097b11114f4e4f01b9718321721a89b': feat(build): Update dependencies (#1847) fix(travis): Fix Travis failing on webdriver issues (#1845) fix(eslint): Inconsistent spacing before function parentheses (#1844) fix(mongodb): update ssl connection settings (#1809) Remove deprecated crypto package (#1843) feat(config): Mongo Seed 2.0 (#1808) fix(users): don't fail on missing old image on image upload (#1839) feat(build): Turn on mangling for uglify (#1841) fix(gulp): fix broken test:server:watch task (#1842) feat(core): Enhancement page title directive (#1686) feat(user): Add email support to forgot password (#1834) fix(mocha): update mochajs version to reduce vulnerabilities (#1830) refactor(menus): Refactor to the Menus client service to use functional loops/filters (#1575) feat(config): Mongoose 4.11 upgrade (#1818) # Conflicts: # config/env/development.js # config/lib/app.js # modules/articles/server/models/article.server.model.js # modules/chat/client/config/chat.client.routes.js # modules/core/client/directives/page-title.client.directive.js # modules/core/client/services/menu.client.service.js # modules/users/client/config/users-admin.client.routes.js # modules/users/client/views/password/forgot-password.client.view.html # modules/users/server/models/user.server.model.js # package.json
143 lines
2.8 KiB
JavaScript
143 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Module dependencies
|
|
*/
|
|
var mongoose = require('mongoose'),
|
|
Schema = mongoose.Schema,
|
|
path = require('path'),
|
|
config = require(path.resolve('./config/config')),
|
|
chalk = require('chalk');
|
|
|
|
/**
|
|
* Article Schema
|
|
*/
|
|
var ArticleSchema = new Schema({
|
|
created: {
|
|
type: Date,
|
|
default: Date.now
|
|
},
|
|
title: {
|
|
type: String,
|
|
default: '',
|
|
trim: true,
|
|
required: 'Title cannot be blank'
|
|
},
|
|
content: {
|
|
type: String,
|
|
default: '',
|
|
trim: true
|
|
},
|
|
user: {
|
|
type: Schema.ObjectId,
|
|
ref: 'User'
|
|
},
|
|
test: {
|
|
type: String,
|
|
default: 'def'
|
|
}
|
|
});
|
|
|
|
ArticleSchema.statics.seed = seed;
|
|
|
|
mongoose.model('Article', ArticleSchema);
|
|
|
|
/**
|
|
* Seeds the User collection with document (Article)
|
|
* and provided options.
|
|
*/
|
|
function seed(doc, options) {
|
|
var Article = mongoose.model('Article');
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
skipDocument()
|
|
.then(findAdminUser)
|
|
.then(add)
|
|
.then(function (response) {
|
|
return resolve(response);
|
|
})
|
|
.catch(function (err) {
|
|
return reject(err);
|
|
});
|
|
|
|
function findAdminUser(skip) {
|
|
var User = mongoose.model('User');
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
if (skip) {
|
|
return resolve(true);
|
|
}
|
|
|
|
User
|
|
.findOne({
|
|
roles: { $in: ['admin'] }
|
|
})
|
|
.exec(function (err, admin) {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
doc.user = admin;
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
function skipDocument() {
|
|
return new Promise(function (resolve, reject) {
|
|
Article
|
|
.findOne({
|
|
title: doc.title
|
|
})
|
|
.exec(function (err, existing) {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
if (!existing) {
|
|
return resolve(false);
|
|
}
|
|
|
|
if (existing && !options.overwrite) {
|
|
return resolve(true);
|
|
}
|
|
|
|
// Remove Article (overwrite)
|
|
|
|
existing.remove(function (err) {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
return resolve(false);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function add(skip) {
|
|
return new Promise(function (resolve, reject) {
|
|
if (skip) {
|
|
return resolve({
|
|
message: chalk.yellow('Database Seeding: Article\t' + doc.title + ' skipped')
|
|
});
|
|
}
|
|
|
|
var article = new Article(doc);
|
|
|
|
article.save(function (err) {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
return resolve({
|
|
message: 'Database Seeding: Article\t' + article.title + ' added'
|
|
});
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|