diff --git a/packages/strapi-generate-api/lib/before.js b/packages/strapi-generate-api/lib/before.js index 8bb0625eda..3d0ec8ffbc 100755 --- a/packages/strapi-generate-api/lib/before.js +++ b/packages/strapi-generate-api/lib/before.js @@ -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); } diff --git a/packages/strapi-generate-model/lib/before.js b/packages/strapi-generate-model/lib/before.js index 96d8f70092..b3bc7b56ff 100755 --- a/packages/strapi-generate-model/lib/before.js +++ b/packages/strapi-generate-model/lib/before.js @@ -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); } diff --git a/packages/strapi-mongoose/lib/index.js b/packages/strapi-mongoose/lib/index.js index e991948e3a..8d907402a1 100644 --- a/packages/strapi-mongoose/lib/index.js +++ b/packages/strapi-mongoose/lib/index.js @@ -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'; }))); diff --git a/packages/strapi-utils/lib/cli.js b/packages/strapi-utils/lib/cli.js new file mode 100644 index 0000000000..0c9ef59c8a --- /dev/null +++ b/packages/strapi-utils/lib/cli.js @@ -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 +}; diff --git a/packages/strapi-utils/lib/index.js b/packages/strapi-utils/lib/index.js index 7291d9e5d7..d7400cc2ae 100755 --- a/packages/strapi-utils/lib/index.js +++ b/packages/strapi-utils/lib/index.js @@ -5,6 +5,7 @@ */ module.exports = { + cli: require('./cli'), commander: require('./commander'), finder: require('./finder'), joijson: require('./joi-json'),