mirror of
https://github.com/strapi/strapi.git
synced 2025-07-25 09:56:53 +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.
|
// Add every relationships to the loaded model for Bookshelf.
|
||||||
// Basic attributes don't need this-- only relations.
|
// 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) {
|
if (details.type !== undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -433,7 +434,7 @@ module.exports = ({ models, target, plugin = false }, ctx) => {
|
|||||||
attrs[key] =
|
attrs[key] =
|
||||||
definition.attributes[key].repeatable === true
|
definition.attributes[key].repeatable === true
|
||||||
? groups
|
? groups
|
||||||
: _.first(groups);
|
: _.first(groups) || null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ module.exports = function(strapi) {
|
|||||||
.map(async connectionName => {
|
.map(async connectionName => {
|
||||||
const connection = connections[connectionName];
|
const connection = connections[connectionName];
|
||||||
const instance = new Mongoose();
|
const instance = new Mongoose();
|
||||||
|
|
||||||
_.defaults(connection.settings, strapi.config.hook.settings.mongoose);
|
_.defaults(connection.settings, strapi.config.hook.settings.mongoose);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -65,9 +66,6 @@ module.exports = function(strapi) {
|
|||||||
|
|
||||||
// Connect to mongo database
|
// Connect to mongo database
|
||||||
const connectOptions = {};
|
const connectOptions = {};
|
||||||
const options = {
|
|
||||||
useFindAndModify: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!_.isEmpty(username)) {
|
if (!_.isEmpty(username)) {
|
||||||
connectOptions.user = username;
|
connectOptions.user = username;
|
||||||
@ -86,8 +84,6 @@ module.exports = function(strapi) {
|
|||||||
connectOptions.dbName = database;
|
connectOptions.dbName = database;
|
||||||
connectOptions.useCreateIndex = true;
|
connectOptions.useCreateIndex = true;
|
||||||
|
|
||||||
options.debug = debug === true || debug === 'true';
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
/* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI.
|
/* FIXME: for now, mongoose doesn't support srv auth except the way including user/pass in URI.
|
||||||
* https://github.com/Automattic/mongoose/issues/6881 */
|
* https://github.com/Automattic/mongoose/issues/6881 */
|
||||||
@ -117,7 +113,8 @@ module.exports = function(strapi) {
|
|||||||
require(initFunctionPath)(instance, connection);
|
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 = {
|
const ctx = {
|
||||||
instance,
|
instance,
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -8,8 +8,14 @@ const Mongoose = require('mongoose');
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = (mongoose = Mongoose) => {
|
module.exports = (mongoose = Mongoose) => {
|
||||||
mongoose.Schema.Types.Decimal = require('mongoose-float').loadType(mongoose, 2);
|
mongoose.Schema.Types.Decimal = require('mongoose-float').loadType(
|
||||||
mongoose.Schema.Types.Float = require('mongoose-float').loadType(mongoose, 20);
|
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.
|
* 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();
|
return this.toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
const utils = {
|
const convertType = mongooseType => {
|
||||||
convertType: mongooseType => {
|
switch (mongooseType.toLowerCase()) {
|
||||||
switch (mongooseType.toLowerCase()) {
|
case 'array':
|
||||||
case 'array':
|
return Array;
|
||||||
return Array;
|
case 'boolean':
|
||||||
case 'boolean':
|
return 'Boolean';
|
||||||
return 'Boolean';
|
case 'binary':
|
||||||
case 'binary':
|
return 'Buffer';
|
||||||
return 'Buffer';
|
case 'date':
|
||||||
case 'date':
|
case 'datetime':
|
||||||
case 'datetime':
|
case 'time':
|
||||||
case 'time':
|
case 'timestamp':
|
||||||
case 'timestamp':
|
return Date;
|
||||||
return Date;
|
case 'decimal':
|
||||||
case 'decimal':
|
return 'Decimal';
|
||||||
return 'Decimal';
|
case 'float':
|
||||||
case 'float':
|
return 'Float';
|
||||||
return 'Float';
|
case 'json':
|
||||||
case 'json':
|
return 'Mixed';
|
||||||
return 'Mixed';
|
case 'biginteger':
|
||||||
case 'biginteger':
|
case 'integer':
|
||||||
case 'integer':
|
return 'Number';
|
||||||
return 'Number';
|
case 'uuid':
|
||||||
case 'uuid':
|
return 'ObjectId';
|
||||||
return 'ObjectId';
|
case 'email':
|
||||||
case 'email':
|
case 'enumeration':
|
||||||
case 'enumeration':
|
case 'password':
|
||||||
case 'password':
|
case 'string':
|
||||||
case 'string':
|
case 'text':
|
||||||
case 'text':
|
return 'String';
|
||||||
return 'String';
|
default:
|
||||||
default:
|
return undefined;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
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;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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