mirror of
https://github.com/strapi/strapi.git
synced 2025-07-24 01:18:17 +00:00
Group schema + auto populate for mongoose
This commit is contained in:
parent
2753176d89
commit
5f29e81556
@ -119,7 +119,8 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
||||
|
||||
// Add every relationships to the loaded model for Bookshelf.
|
||||
// Basic attributes don't need this-- only relations.
|
||||
_.forEach(definition.attributes, (details, name) => {
|
||||
Object.keys(definition.attributes).forEach(name => {
|
||||
const details = definition.attributes[name];
|
||||
if (details.type !== undefined) {
|
||||
return;
|
||||
}
|
||||
@ -433,7 +434,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
||||
attrs[key] =
|
||||
definition.attributes[key].repeatable === true
|
||||
? groups
|
||||
: _.first(groups);
|
||||
: _.first(groups) || null;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -43,6 +43,7 @@ module.exports = function(strapi) {
|
||||
.map(async connectionName => {
|
||||
const connection = connections[connectionName];
|
||||
const instance = new Mongoose();
|
||||
|
||||
_.defaults(connection.settings, strapi.config.hook.settings.mongoose);
|
||||
|
||||
const {
|
||||
@ -65,9 +66,6 @@ module.exports = function(strapi) {
|
||||
|
||||
// Connect to mongo database
|
||||
const connectOptions = {};
|
||||
const options = {
|
||||
useFindAndModify: false,
|
||||
};
|
||||
|
||||
if (!_.isEmpty(username)) {
|
||||
connectOptions.user = username;
|
||||
@ -86,8 +84,6 @@ module.exports = function(strapi) {
|
||||
connectOptions.dbName = database;
|
||||
connectOptions.useCreateIndex = true;
|
||||
|
||||
options.debug = debug === true || debug === 'true';
|
||||
|
||||
try {
|
||||
/* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI.
|
||||
* https://github.com/Automattic/mongoose/issues/6881 */
|
||||
@ -117,7 +113,8 @@ module.exports = function(strapi) {
|
||||
require(initFunctionPath)(instance, connection);
|
||||
}
|
||||
|
||||
Object.keys(options, key => instance.set(key, options[key]));
|
||||
instance.set('debug', debug === true || debug === 'true');
|
||||
instance.set('useFindAndModify', false);
|
||||
|
||||
const ctx = {
|
||||
instance,
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -8,8 +8,14 @@ const Mongoose = require('mongoose');
|
||||
*/
|
||||
|
||||
module.exports = (mongoose = Mongoose) => {
|
||||
mongoose.Schema.Types.Decimal = require('mongoose-float').loadType(mongoose, 2);
|
||||
mongoose.Schema.Types.Float = require('mongoose-float').loadType(mongoose, 20);
|
||||
mongoose.Schema.Types.Decimal = require('mongoose-float').loadType(
|
||||
mongoose,
|
||||
2
|
||||
);
|
||||
mongoose.Schema.Types.Float = require('mongoose-float').loadType(
|
||||
mongoose,
|
||||
20
|
||||
);
|
||||
|
||||
/**
|
||||
* Convert MongoDB ID to the stringify version as GraphQL throws an error if not.
|
||||
@ -20,62 +26,67 @@ module.exports = (mongoose = Mongoose) => {
|
||||
return this.toString();
|
||||
};
|
||||
|
||||
const utils = {
|
||||
convertType: mongooseType => {
|
||||
switch (mongooseType.toLowerCase()) {
|
||||
case 'array':
|
||||
return Array;
|
||||
case 'boolean':
|
||||
return 'Boolean';
|
||||
case 'binary':
|
||||
return 'Buffer';
|
||||
case 'date':
|
||||
case 'datetime':
|
||||
case 'time':
|
||||
case 'timestamp':
|
||||
return Date;
|
||||
case 'decimal':
|
||||
return 'Decimal';
|
||||
case 'float':
|
||||
return 'Float';
|
||||
case 'json':
|
||||
return 'Mixed';
|
||||
case 'biginteger':
|
||||
case 'integer':
|
||||
return 'Number';
|
||||
case 'uuid':
|
||||
return 'ObjectId';
|
||||
case 'email':
|
||||
case 'enumeration':
|
||||
case 'password':
|
||||
case 'string':
|
||||
case 'text':
|
||||
return 'String';
|
||||
default:
|
||||
}
|
||||
},
|
||||
valueToId: value => {
|
||||
if (utils.isMongoId(value)) {
|
||||
return mongoose.Types.ObjectId(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
isMongoId: value => {
|
||||
if (value instanceof mongoose.Types.ObjectId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_.isString(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Here we don't use mongoose.Types.ObjectId.isValid method because it's a weird check,
|
||||
// it returns for instance true for any integer value
|
||||
const hexadecimal = /^[0-9A-F]+$/i;
|
||||
return hexadecimal.test(value) && value.length === 24;
|
||||
},
|
||||
const convertType = mongooseType => {
|
||||
switch (mongooseType.toLowerCase()) {
|
||||
case 'array':
|
||||
return Array;
|
||||
case 'boolean':
|
||||
return 'Boolean';
|
||||
case 'binary':
|
||||
return 'Buffer';
|
||||
case 'date':
|
||||
case 'datetime':
|
||||
case 'time':
|
||||
case 'timestamp':
|
||||
return Date;
|
||||
case 'decimal':
|
||||
return 'Decimal';
|
||||
case 'float':
|
||||
return 'Float';
|
||||
case 'json':
|
||||
return 'Mixed';
|
||||
case 'biginteger':
|
||||
case 'integer':
|
||||
return 'Number';
|
||||
case 'uuid':
|
||||
return 'ObjectId';
|
||||
case 'email':
|
||||
case 'enumeration':
|
||||
case 'password':
|
||||
case 'string':
|
||||
case 'text':
|
||||
return 'String';
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
return utils;
|
||||
const isMongoId = value => {
|
||||
if (value instanceof mongoose.Types.ObjectId) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_.isString(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Here we don't use mongoose.Types.ObjectId.isValid method because it's a weird check,
|
||||
// it returns for instance true for any integer value
|
||||
const hexadecimal = /^[0-9A-F]+$/i;
|
||||
return hexadecimal.test(value) && value.length === 24;
|
||||
};
|
||||
|
||||
const valueToId = value => {
|
||||
if (isMongoId(value)) {
|
||||
return mongoose.Types.ObjectId(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
return {
|
||||
convertType,
|
||||
valueToId,
|
||||
isMongoId,
|
||||
};
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user