Be able to generate files through the CLI and fix Mongoose connect

This commit is contained in:
aurelsicoko 2017-07-31 12:52:08 +02:00
parent 937a4839bd
commit 14d76c835f
5 changed files with 44 additions and 11 deletions

View File

@ -110,7 +110,7 @@ module.exports = (scope, cb) => {
// Get default connection
try {
scope.connection = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'databases.json'))).defaultConnection || '';
scope.connection = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || '';
} catch (err) {
return cb.invalid(err);
}

View File

@ -48,7 +48,7 @@ module.exports = (scope, cb) => {
} else if (scope.args.plugin) {
filePath = `./plugins/${scope.args.plugin}/models`;
} else {
filePath = `./api/${scope.id}/models`;
filePath = `./api/${scope.id}`;
}
// Take another pass to take advantage of the defaults absorbed in previous passes.
@ -109,7 +109,7 @@ module.exports = (scope, cb) => {
// Get default connection
try {
scope.connection = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'databases.json'))).defaultConnection || '';
scope.connection = JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || '';
} catch (err) {
return cb.invalid(err);
}

View File

@ -50,9 +50,13 @@ module.exports = function (strapi) {
// Connect to mongo database
if (_.isEmpty(username) || _.isEmpty(password)) {
mongoose.connect(`mongodb://${host}:${port}/${database}`);
mongoose.connect(`mongodb://${host}:${port}/${database}`, {
useMongoClient: true
});
} else {
mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`);
mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`, {
useMongoClient: true
});
}
const db = mongoose.connection;
@ -64,9 +68,6 @@ module.exports = function (strapi) {
// Handle success
db.on('open', () => {
// Initialize collections
_.set(strapi, 'mongoose.collections', {});
// Select models concerned by this connection
const models = _.pickBy(strapi.models, {connection: connectionName});
@ -78,7 +79,7 @@ module.exports = function (strapi) {
const loadedAttributes = _.after(_.size(models), () => {
_.forEach(models, (definition, model) => {
try {
let collection = strapi.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)];
let collection = strapi.config.hook.settings.mongoose.collections[mongooseUtils.toCollectionName(definition.globalName)];
// Set the default values to model settings.
_.defaults(definition, {
@ -183,7 +184,7 @@ module.exports = function (strapi) {
if (_.isEmpty(definition.attributes)) {
// Generate empty schema
_.set(strapi.mongoose.collections, mongooseUtils.toCollectionName(definition.globalName) + '.schema', new mongoose.Schema({}));
_.set(strapi.config.hook.settings.mongoose, 'collections.' + mongooseUtils.toCollectionName(definition.globalName) + '.schema', new mongoose.Schema({}));
return loadedAttributes();
}
@ -192,7 +193,7 @@ module.exports = function (strapi) {
// all attributes for relationships-- see below.
const done = _.after(_.size(definition.attributes), () => {
// Generate schema without virtual populate
_.set(strapi.mongoose.collections, mongooseUtils.toCollectionName(definition.globalName) + '.schema', new mongoose.Schema(_.omitBy(definition.loadedModel, model => {
_.set(strapi.config.hook.settings.mongoose, 'collections.' + mongooseUtils.toCollectionName(definition.globalName) + '.schema', new mongoose.Schema(_.omitBy(definition.loadedModel, model => {
return model.type === 'virtual';
})));

View File

@ -0,0 +1,31 @@
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const path = require('path');
/**
* Check that we're in a valid Strapi project.
*
* @returns {boolean}
*/
const isStrapiApp = () => {
const pathToPackageJSON = path.resolve(process.cwd(), 'package.json');
let validPackageJSON = true;
try {
require(pathToPackageJSON);
} catch (e) {
validPackageJSON = false;
}
return validPackageJSON;
};
module.exports = {
isStrapiApp
};

View File

@ -5,6 +5,7 @@
*/
module.exports = {
cli: require('./cli'),
commander: require('./commander'),
finder: require('./finder'),
joijson: require('./joi-json'),