有没有办法在stradi中过滤json数据格式字段?

时间:2019-12-08 06:23:00

标签: strapi

嗨,我正在尝试过滤数据json格式字段的帖子?

"categoryList": ["cat", "cat1"]

3 个答案:

答案 0 :(得分:1)

对于仍在寻找解决方案的任何人,这就是我对名为 Articles 的集合类型的名为 tags 的json类型字段所做的工作。

我在数据库中有两篇文章,其中一篇文章设置了以下值:

title: "lorem ipsum 1",
tags: [
  "test",
  "rest"
]

另一篇文章设置了以下值:

title: "lorem ipsum 2",
tags: [
  "test",
  "graphql"
]

我的graphql查询如下:

query {
  articlesByTag(limit: 2, where: {tags_include: ["test", "rest"]}, start: 0, sort: "title:asc") {
    title,
    tags
  }
}

我的其余查询如下:

http://localhost:1337/articlesByTag?limit=2&tags_include[]=test&tags_include[]=rest

这是我的articles.js服务文件:

const { convertRestQueryParams, buildQuery } = require('strapi-utils');
const _ = require('lodash');
const { convertToParams, convertToQuery } = require('../../../node_modules/strapi-plugin-graphql/services/utils');

module.exports = {
  async findByTag(ctx) {
    let tags_include;

    if (ctx.where && ctx.where.tags_include && ctx.where.tags_include.length > 0) {
      tags_include = ctx.where.tags_include;
      delete ctx.where.tags_include;
    } else if (ctx.query && ctx.query.tags_include && ctx.query.tags_include.length > 0) {
      tags_include = ctx.query.tags_include;
      delete ctx.query.tags_include;
    }

    if (!Array.isArray(tags_include)) {
      tags_include = [tags_include];
    }

    let filters = null;

    if (ctx.query) {
      filters = convertRestQueryParams({
        ...convertToParams(ctx.query)
      });
    } else {
      filters = convertRestQueryParams({
        ...convertToParams(_.pick(ctx, ['limit', 'start', 'sort'])),
        ...convertToQuery(ctx.where),
      });
    }

    const entities = await strapi.query('articles').model.query(qb => {

      buildQuery({ model: strapi.query('articles').model, filters: filters })(qb);

      if (tags_include.length > 0) {
        tags_include.forEach((tag) => {
          if (tag && tag.length > 0) {
            const likeStr = `%"${tag}"%`;
            qb.andWhere('tags', 'like', likeStr);
          }
        });
      }

    }).fetchAll();

    return entities;
  },
};

这是route.js中所需的条目

    {
      "method": "GET",
      "path": "/articlesByTag",
      "handler": "articles.findByTag",
      "config": {
        "policies": []
      }
    }

这是控制器articles.js

const { sanitizeEntity } = require('strapi-utils');

module.exports = {
  async findByTag(ctx) {
    const entities = await strapi.services.articles.findByTag(ctx);

    return entities.map(entity => sanitizeEntity(entity, { model: strapi.models.articles }));
  },
};

最后这是schema.graphql.js

module.exports = {
  query: `
    articlesByTag(sort: String, limit: Int, start: Int, where: JSON): [Articles]
  `,
  resolver: {
    Query: {
      articlesByTag: {
        description: 'Return articles filtered by tag',
        resolverOf: 'application::articles.articles.findByTag',
        resolver: async (obj, options, ctx) => {
          return await strapi.api.articles.controllers.articles.findByTag(options);
        },
      },
    },
  },
};

答案 1 :(得分:0)

从beta.17.8(最新)开始,目前尚没有一种过滤JSON字段的方法

答案 2 :(得分:0)

大概是这样吗?

strapi.query('cool_model').find({ categoryList: { $all: [ "cat" , "cat1" ] } })