mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
mongoose working version
This commit is contained in:
parent
2befa1cf48
commit
c020f0a6cd
@ -50,11 +50,11 @@ module.exports = function createQueryBuilder({ model, modelKey, strapi }) {
|
||||
};
|
||||
}
|
||||
|
||||
const record = await model.forge(params).fetch({
|
||||
const entry = await model.forge(params).fetch({
|
||||
withRelated: populate || defaultPopulate,
|
||||
});
|
||||
|
||||
return record ? record.toJSON() : null;
|
||||
return entry ? entry.toJSON() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -182,8 +182,6 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
}
|
||||
}
|
||||
|
||||
// public api
|
||||
|
||||
function find(params, populate) {
|
||||
const populateOpt = populate || defaultPopulate;
|
||||
|
||||
@ -205,10 +203,7 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
};
|
||||
}
|
||||
|
||||
return model
|
||||
.findOne(params)
|
||||
.populate(populate || defaultPopulate)
|
||||
.lean();
|
||||
return model.findOne(params).populate(populate || defaultPopulate);
|
||||
}
|
||||
|
||||
function count(params) {
|
||||
@ -312,7 +307,7 @@ module.exports = ({ model, modelKey, strapi }) => {
|
||||
]
|
||||
: strapi.models[association.model || association.collection];
|
||||
|
||||
return model.update(search, update, { multi: true });
|
||||
return model.updateMany(search, update);
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@ -1,170 +0,0 @@
|
||||
const _ = require('lodash');
|
||||
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
||||
|
||||
module.exports = ({ model }) => ({
|
||||
find(params, populate, raw = false) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
const query = buildQuery({
|
||||
model,
|
||||
filters,
|
||||
populate: populate || model.associations.map(x => x.alias),
|
||||
});
|
||||
|
||||
return raw ? query.lean() : query;
|
||||
},
|
||||
|
||||
count(params) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
return buildQuery({
|
||||
model,
|
||||
filters: { where: filters.where },
|
||||
}).count();
|
||||
},
|
||||
|
||||
search(params, populate) {
|
||||
// eslint-disable-line no-unused-vars
|
||||
const $or = Object.keys(model.attributes).reduce((acc, curr) => {
|
||||
switch (model.attributes[curr].type) {
|
||||
case 'integer':
|
||||
case 'biginteger':
|
||||
case 'float':
|
||||
case 'decimal':
|
||||
if (!_.isNaN(_.toNumber(params.search))) {
|
||||
return acc.concat({ [curr]: params.search });
|
||||
}
|
||||
|
||||
return acc;
|
||||
case 'string':
|
||||
case 'text':
|
||||
case 'password':
|
||||
return acc.concat({
|
||||
[curr]: { $regex: params.search, $options: 'i' },
|
||||
});
|
||||
case 'boolean':
|
||||
if (params.search === 'true' || params.search === 'false') {
|
||||
return acc.concat({ [curr]: params.search === 'true' });
|
||||
}
|
||||
|
||||
return acc;
|
||||
default:
|
||||
return acc;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return model
|
||||
.find({ $or })
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.skip))
|
||||
.populate(populate || model.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
},
|
||||
|
||||
countSearch(params = {}) {
|
||||
// eslint-disable-line no-unused-vars
|
||||
const $or = Object.keys(model.attributes).reduce((acc, curr) => {
|
||||
switch (model.attributes[curr].type) {
|
||||
case 'integer':
|
||||
case 'biginteger':
|
||||
case 'float':
|
||||
case 'decimal':
|
||||
if (!_.isNaN(_.toNumber(params.search))) {
|
||||
return acc.concat({ [curr]: params.search });
|
||||
}
|
||||
|
||||
return acc;
|
||||
case 'string':
|
||||
case 'text':
|
||||
case 'password':
|
||||
return acc.concat({
|
||||
[curr]: { $regex: params.search, $options: 'i' },
|
||||
});
|
||||
case 'boolean':
|
||||
if (params.search === 'true' || params.search === 'false') {
|
||||
return acc.concat({ [curr]: params.search === 'true' });
|
||||
}
|
||||
|
||||
return acc;
|
||||
default:
|
||||
return acc;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return model.find({ $or }).countDocuments();
|
||||
},
|
||||
|
||||
findOne(params, populate, raw = true) {
|
||||
const query = model
|
||||
.findOne({
|
||||
[model.primaryKey]: params[model.primaryKey] || params.id,
|
||||
})
|
||||
.populate(populate || model.associations.map(x => x.alias).join(' '));
|
||||
|
||||
return raw ? query.lean() : query;
|
||||
},
|
||||
|
||||
async create(params) {
|
||||
// Exclude relationships.
|
||||
const values = Object.keys(params.values).reduce((acc, current) => {
|
||||
if (model._attributes[current] && model._attributes[current].type) {
|
||||
acc[current] = params.values[current];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const request = await model.create(values).catch(err => {
|
||||
if (err.message) {
|
||||
const message = err.message.split('index:');
|
||||
const field = _.words(_.last(message).split('_')[0]);
|
||||
err = { message: `This ${field} is already taken`, field };
|
||||
}
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Transform to JSON object.
|
||||
const entry = request.toJSON ? request.toJSON() : request;
|
||||
|
||||
// Extract relations.
|
||||
const relations = model.associations.reduce((acc, association) => {
|
||||
if (params.values[association.alias]) {
|
||||
acc[association.alias] = params.values[association.alias];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return this.update({
|
||||
[model.primaryKey]: entry[model.primaryKey],
|
||||
values: _.assign(
|
||||
{
|
||||
id: entry[model.primaryKey],
|
||||
},
|
||||
relations
|
||||
),
|
||||
});
|
||||
},
|
||||
|
||||
update(params) {
|
||||
// Call the business logic located in the hook.
|
||||
// This function updates no-relational and relational data.
|
||||
return model.updateRelations(params);
|
||||
},
|
||||
|
||||
delete(params) {
|
||||
// Delete entry.
|
||||
return model.findOneAndDelete({
|
||||
[model.primaryKey]: params.id,
|
||||
});
|
||||
},
|
||||
|
||||
deleteMany(params) {
|
||||
return model.deleteMany({
|
||||
[model.primaryKey]: {
|
||||
$in: params[model.primaryKey] || params.id,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
@ -495,6 +495,7 @@ describe('Content Manager End to End', () => {
|
||||
});
|
||||
|
||||
test('Get article2 with cat2', async () => {
|
||||
data.articles;
|
||||
let { body } = await rq({
|
||||
url: `/content-manager/explorer/article/${data.articles[1].id}?source=content-manager`,
|
||||
method: 'GET',
|
||||
|
||||
@ -1,122 +0,0 @@
|
||||
const _ = require('lodash');
|
||||
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
||||
|
||||
module.exports = ({ model }) => ({
|
||||
find(params, populate) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
return buildQuery({
|
||||
model,
|
||||
filters,
|
||||
populate: populate || model.associations.map(x => x.alias),
|
||||
}).lean();
|
||||
},
|
||||
|
||||
count(params) {
|
||||
const filters = convertRestQueryParams(params);
|
||||
|
||||
return buildQuery({
|
||||
model,
|
||||
filters: { where: filters.where },
|
||||
}).count();
|
||||
},
|
||||
|
||||
findOne(params, populate) {
|
||||
const primaryKey = params[model.primaryKey] || params.id;
|
||||
|
||||
if (primaryKey) {
|
||||
params = {
|
||||
[model.primaryKey]: primaryKey,
|
||||
};
|
||||
}
|
||||
|
||||
return model
|
||||
.findOne(params)
|
||||
.populate(populate || model.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
},
|
||||
|
||||
create(params) {
|
||||
return model
|
||||
.create(
|
||||
Object.keys(params).reduce((acc, current) => {
|
||||
if (
|
||||
_.get(model._attributes, [current, 'type']) ||
|
||||
_.get(model._attributes, [current, 'model'])
|
||||
) {
|
||||
acc[current] = params[current];
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
)
|
||||
.catch(err => {
|
||||
if (err.message.indexOf('index:') !== -1) {
|
||||
const message = err.message.split('index:');
|
||||
const field = _.words(_.last(message).split('_')[0]);
|
||||
const error = { message: `This ${field} is already taken`, field };
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
|
||||
update(search, params = {}) {
|
||||
if (_.isEmpty(params)) {
|
||||
params = search;
|
||||
}
|
||||
|
||||
const primaryKey = search[model.primaryKey] || search.id;
|
||||
|
||||
if (primaryKey) {
|
||||
search = {
|
||||
[model.primaryKey]: primaryKey,
|
||||
};
|
||||
}
|
||||
|
||||
return model
|
||||
.updateOne(search, params, {
|
||||
strict: false,
|
||||
})
|
||||
.catch(error => {
|
||||
const field = _.last(_.words(error.message.split('_')[0]));
|
||||
const err = { message: `This ${field} is already taken`, field };
|
||||
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
|
||||
delete(params) {
|
||||
// Delete entry.
|
||||
return model.deleteOne({
|
||||
[model.primaryKey]: params[model.primaryKey] || params.id,
|
||||
});
|
||||
},
|
||||
|
||||
deleteMany(params) {
|
||||
// Delete entry.
|
||||
return model.deleteMany({
|
||||
[model.primaryKey]: {
|
||||
$in: params[model.primaryKey] || params.id,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
search(params) {
|
||||
const re = new RegExp(params.id);
|
||||
|
||||
return model.find({
|
||||
$or: [{ username: re }, { email: re }],
|
||||
});
|
||||
},
|
||||
|
||||
addPermission(params) {
|
||||
return model.create(params);
|
||||
},
|
||||
|
||||
removePermission(params) {
|
||||
return model.remove(params);
|
||||
},
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user