mirror of
https://github.com/strapi/strapi.git
synced 2025-08-18 13:45:25 +00:00
Be able to generate files through the CLI and fix Mongoose connect
This commit is contained in:
parent
937a4839bd
commit
14d76c835f
@ -110,7 +110,7 @@ module.exports = (scope, cb) => {
|
|||||||
|
|
||||||
// Get default connection
|
// Get default connection
|
||||||
try {
|
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) {
|
} catch (err) {
|
||||||
return cb.invalid(err);
|
return cb.invalid(err);
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,7 @@ module.exports = (scope, cb) => {
|
|||||||
} else if (scope.args.plugin) {
|
} else if (scope.args.plugin) {
|
||||||
filePath = `./plugins/${scope.args.plugin}/models`;
|
filePath = `./plugins/${scope.args.plugin}/models`;
|
||||||
} else {
|
} else {
|
||||||
filePath = `./api/${scope.id}/models`;
|
filePath = `./api/${scope.id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take another pass to take advantage of the defaults absorbed in previous passes.
|
// Take another pass to take advantage of the defaults absorbed in previous passes.
|
||||||
@ -109,7 +109,7 @@ module.exports = (scope, cb) => {
|
|||||||
|
|
||||||
// Get default connection
|
// Get default connection
|
||||||
try {
|
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) {
|
} catch (err) {
|
||||||
return cb.invalid(err);
|
return cb.invalid(err);
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,13 @@ module.exports = function (strapi) {
|
|||||||
|
|
||||||
// Connect to mongo database
|
// Connect to mongo database
|
||||||
if (_.isEmpty(username) || _.isEmpty(password)) {
|
if (_.isEmpty(username) || _.isEmpty(password)) {
|
||||||
mongoose.connect(`mongodb://${host}:${port}/${database}`);
|
mongoose.connect(`mongodb://${host}:${port}/${database}`, {
|
||||||
|
useMongoClient: true
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`);
|
mongoose.connect(`mongodb://${username}:${password}@${host}:${port}/${database}`, {
|
||||||
|
useMongoClient: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = mongoose.connection;
|
const db = mongoose.connection;
|
||||||
@ -64,9 +68,6 @@ module.exports = function (strapi) {
|
|||||||
|
|
||||||
// Handle success
|
// Handle success
|
||||||
db.on('open', () => {
|
db.on('open', () => {
|
||||||
// Initialize collections
|
|
||||||
_.set(strapi, 'mongoose.collections', {});
|
|
||||||
|
|
||||||
// Select models concerned by this connection
|
// Select models concerned by this connection
|
||||||
const models = _.pickBy(strapi.models, {connection: connectionName});
|
const models = _.pickBy(strapi.models, {connection: connectionName});
|
||||||
|
|
||||||
@ -78,7 +79,7 @@ module.exports = function (strapi) {
|
|||||||
const loadedAttributes = _.after(_.size(models), () => {
|
const loadedAttributes = _.after(_.size(models), () => {
|
||||||
_.forEach(models, (definition, model) => {
|
_.forEach(models, (definition, model) => {
|
||||||
try {
|
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.
|
// Set the default values to model settings.
|
||||||
_.defaults(definition, {
|
_.defaults(definition, {
|
||||||
@ -183,7 +184,7 @@ module.exports = function (strapi) {
|
|||||||
|
|
||||||
if (_.isEmpty(definition.attributes)) {
|
if (_.isEmpty(definition.attributes)) {
|
||||||
// Generate empty schema
|
// 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();
|
return loadedAttributes();
|
||||||
}
|
}
|
||||||
@ -192,7 +193,7 @@ module.exports = function (strapi) {
|
|||||||
// all attributes for relationships-- see below.
|
// all attributes for relationships-- see below.
|
||||||
const done = _.after(_.size(definition.attributes), () => {
|
const done = _.after(_.size(definition.attributes), () => {
|
||||||
// Generate schema without virtual populate
|
// 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';
|
return model.type === 'virtual';
|
||||||
})));
|
})));
|
||||||
|
|
||||||
|
31
packages/strapi-utils/lib/cli.js
Normal file
31
packages/strapi-utils/lib/cli.js
Normal 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
|
||||||
|
};
|
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
cli: require('./cli'),
|
||||||
commander: require('./commander'),
|
commander: require('./commander'),
|
||||||
finder: require('./finder'),
|
finder: require('./finder'),
|
||||||
joijson: require('./joi-json'),
|
joijson: require('./joi-json'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user