mirror of
https://github.com/strapi/strapi.git
synced 2025-10-16 10:33:34 +00:00
Adapt Services to the new Mongoose API
This commit is contained in:
parent
328f09da29
commit
e8c92fc3d7
@ -14,11 +14,11 @@ module.exports = {
|
||||
* @return {Object|Array}
|
||||
*/
|
||||
|
||||
find: async (ctx) => {
|
||||
find: async (ctx, next, { populate } = {}) => {
|
||||
if (ctx.query._q) {
|
||||
return strapi.services.<%= id %>.search(ctx.query);
|
||||
} else {
|
||||
return strapi.services.<%= id %>.fetchAll(ctx.query);
|
||||
return strapi.services.<%= id %>.fetchAll(ctx.query, populate);
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
// Public dependencies.
|
||||
const _ = require('lodash');
|
||||
const { Builder, Query } = require('strapi-utils');
|
||||
|
||||
module.exports = {
|
||||
|
||||
@ -17,22 +18,13 @@ module.exports = {
|
||||
* @return {Promise}
|
||||
*/
|
||||
|
||||
fetchAll: (params) => {
|
||||
// Convert `params` object to filters compatible with Mongo.
|
||||
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
||||
// Select field to populate.
|
||||
const populate = <%= globalID %>.associations
|
||||
.filter(ast => ast.autoPopulate !== false)
|
||||
.map(ast => ast.alias)
|
||||
.join(' ');
|
||||
fetchAll: (params, populate) => {
|
||||
const filter = new Builder(<%= globalID %>, params).convert();
|
||||
|
||||
return <%= globalID %>
|
||||
.find()
|
||||
.where(filters.where)
|
||||
.sort(filters.sort)
|
||||
.skip(filters.start)
|
||||
.limit(filters.limit)
|
||||
.populate(filters.populate || populate);
|
||||
return new Query(<%= globalID %>)
|
||||
.find(filter)
|
||||
.populate(populate)
|
||||
.execute();
|
||||
},
|
||||
|
||||
/**
|
||||
@ -60,12 +52,11 @@ module.exports = {
|
||||
*/
|
||||
|
||||
count: (params) => {
|
||||
// Convert `params` object to filters compatible with Mongo.
|
||||
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
||||
|
||||
return <%= globalID %>
|
||||
.countDocuments()
|
||||
.where(filters.where);
|
||||
// Convert `params` object to filter compatible with Mongo.
|
||||
const filter = new Builder(<%= globalID %>, params).convert();
|
||||
return new Query(<%= globalID %>)
|
||||
.count(filter)
|
||||
.execute();
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -1,21 +1,25 @@
|
||||
const _ = require('lodash');
|
||||
const { Builder, Query } = require('strapi-utils');
|
||||
|
||||
module.exports = {
|
||||
find: async function (params, populate, raw = false) {
|
||||
const query = this
|
||||
.find(params.where)
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.skip))
|
||||
.populate(populate || this.associations.map(x => x.alias).join(' '));
|
||||
find: async function (params, populate) {
|
||||
const model = this;
|
||||
// Convert `params` object to filter compatible with Mongo.
|
||||
const filter = new Builder(model, params).convert();
|
||||
|
||||
return raw ? query.lean() : query;
|
||||
return new Query(model)
|
||||
.find(filter)
|
||||
.populate(populate)
|
||||
.execute();
|
||||
},
|
||||
|
||||
count: async function (params) {
|
||||
return Number(await this
|
||||
.where(params.where)
|
||||
.countDocuments());
|
||||
const model = this;
|
||||
// Convert `params` object to filter compatible with Mongo.
|
||||
const filter = new Builder(model, params).convert();
|
||||
return new Query(model)
|
||||
.count(filter)
|
||||
.execute();
|
||||
},
|
||||
|
||||
search: async function (params, populate) { // eslint-disable-line no-unused-vars
|
||||
|
@ -1,19 +1,24 @@
|
||||
const _ = require('lodash');
|
||||
const { Builder, Query } = require('strapi-utils');
|
||||
|
||||
module.exports = {
|
||||
find: async function (params = {}, populate) {
|
||||
return this
|
||||
.find(params.where)
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.start))
|
||||
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
find: async function (params, populate) {
|
||||
const model = this;
|
||||
const filter = new Builder(model, params).convert();
|
||||
|
||||
return new Query(model)
|
||||
.find(filter)
|
||||
.populate(populate)
|
||||
.execute();
|
||||
},
|
||||
|
||||
count: async function (params = {}) {
|
||||
return Number(await this
|
||||
.countDocuments(params));
|
||||
count: async function (params) {
|
||||
const model = this;
|
||||
// Convert `params` object to filter compatible with Mongo.
|
||||
const filter = new Builder(model, params).convert();
|
||||
return new Query(model)
|
||||
.count(filter)
|
||||
.execute();
|
||||
},
|
||||
|
||||
findOne: async function (params, populate) {
|
||||
|
@ -1,19 +1,24 @@
|
||||
const _ = require('lodash');
|
||||
const { Builder, Query } = require('strapi-utils');
|
||||
|
||||
module.exports = {
|
||||
find: async function (params = {}, populate) {
|
||||
return this
|
||||
.find(params.where)
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.start))
|
||||
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
find: async function (params, populate) {
|
||||
const model = this;
|
||||
const filter = new Builder(model, params).convert();
|
||||
|
||||
return new Query(model)
|
||||
.find(filter)
|
||||
.populate(populate)
|
||||
.execute();
|
||||
},
|
||||
|
||||
count: async function (params = {}) {
|
||||
return Number(await this
|
||||
.countDocuments(params));
|
||||
count: async function (params) {
|
||||
const model = this;
|
||||
// Convert `params` object to filter compatible with Mongo.
|
||||
const filter = new Builder(model, params).convert();
|
||||
return new Query(model)
|
||||
.count(filter)
|
||||
.execute();
|
||||
},
|
||||
|
||||
findOne: async function (params, populate) {
|
||||
|
@ -1,20 +1,24 @@
|
||||
const _ = require('lodash');
|
||||
const { Builder, Query } = require('strapi-utils');
|
||||
|
||||
module.exports = {
|
||||
find: async function (params = {}, populate) {
|
||||
return this
|
||||
.find(params.where)
|
||||
.limit(Number(params.limit))
|
||||
.sort(params.sort)
|
||||
.skip(Number(params.skip))
|
||||
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
||||
.lean();
|
||||
find: async function (params, populate) {
|
||||
const model = this;
|
||||
const filter = new Builder(model, params).convert();
|
||||
|
||||
return new Query(model)
|
||||
.find(filter)
|
||||
.populate(populate)
|
||||
.execute();
|
||||
},
|
||||
|
||||
count: async function (params = {where: {}}) {
|
||||
return Number(await this
|
||||
.countDocuments()
|
||||
.where(params.where));
|
||||
count: async function (params) {
|
||||
const model = this;
|
||||
// Convert `params` object to filter compatible with Mongo.
|
||||
const filter = new Builder(model, params).convert();
|
||||
return new Query(model)
|
||||
.count(filter)
|
||||
.execute();
|
||||
},
|
||||
|
||||
findOne: async function (params, populate) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user