strapi中的restful风格

strapi是一个相当规范的前端文件管理系统(CMS),这里我们对strapi里面的restful请求风格进行简单的汇总:

1.基本数据操作

这里我们假定restaurants为我们的一个数据结构,对应strapi中的Collection,对应数据库表名称或者java实体名称

 1.1 获取所有的数据列表

GET /restaurants    //例如:http://localhost:1337/restaurants

 1.2 根据id获取指定数据

GET /restaurants/:id   //例如:http://localhost:1337/restaurants/1

 1.3 获取数据条数

GET /restaurants/count   //例如:http://localhost:1337/restaurants/count

 1.4 新增一条数据

POST /restaurants   //例如:POST HTTP://localhost:1337/restaurants

 1.5 修改一条数据

PUT /restaurants/:id   //例如:PUT http://localhost:1337/restaurants/1

 1.6 删除一条数据

DELETE /restaurants/:id   //例如:DELETE http://localhost:1337/restaurants/1

 2.过滤操作

 2.1 过滤器常用字段后缀

无后缀或eq:等于
ne:不等于
lt: 少于
gt: 比...更棒
lte:小于或等于
gte:大于或等于
in:包含在值数组中
nin:不包含在值数组中
contains:包含
ncontains:不包含
containss:包含区分大小写
ncontainss:不包含区分大小写
null:为空/不为空

 2.1 查找名字中为John的用户

GET /users?firstName=John
或者
GET /users?firstName_eq=John

 2.2 查找价格高于或者等于3的餐厅

GET /restaurants?price_gte=3

 2.3 查找ID为3、6、8的多家餐厅

GET /restaurants?id_in=3&id_in=6&id_in=8

 2.4 模糊查询名字中包含pizza的用户

GET /restaurants?name_contains=pizza

3. 排序 

 3.1 通过电子邮件对用户进行排序

GET /users?_sort=email:ASC
或者
GET /users?_sort=email:DESC

3.2 在多个字段上排序

GET /users?_sort=email:asc,dateField:desc
或者
GET /users?_sort=email:DESC,username:ASC

3.3 通过电子邮件对用户进行排序

GET /restaurants/:id   //例如:http://localhost:1337/restaurants/1

4. 限制(分页)

默认返回结果大小是100条 

可以通过插入limit = -1来获取所有的数据集

4.1 将结果长度限制为30

GET /users?_limit=30

3.1 获取第二页数据

GET /users?_start=10&_limit=10

5. 其他查询(不依赖于表对象) 

使用strapi的strapi.query来进行数据查询操作

5.1 查询操作

5.1.1 按编号查找

strapi.query('restaurant').find({ id: 1 });

5.1.2 在IN中查找,但有一个限制

strapi.query('restaurant').find({ _limit: 10, id_in: [1, 2] });

5.1.3 按日期顺序查找按名称倒序

strapi
  .query('restaurant')
  .find({ date_gt: '2019-01-01T00:00:00Z', _sort: 'name:desc' });

5.1.2 通过不在其中的ID查找并填充关系。跳过前十个结果

strapi
  .query('restaurant')
  .find({ id_nin: [1], _start: 10 }, ['category', 'category.name']);

5.2 查找一个 

5.2.1 通过ID查找一个

strapi.query('restaurant').findOne({ id: 1 });

5.2.2 按名称查找一个

strapi.query('restaurant').findOne({ name: 'restaurant name' });

5.2.3 通过名称和creation_date查找一个

strapi
  .query('restaurant')
  .findOne({ name: 'restaurant name', date: '2019-01-01T00:00:00Z' });

5.2.4 通过id查找并填充关系

strapi.query('restaurant').findOne({ id: 1 }, ['category', 'category.name']);

5.3 创建一个条目并返回该条目

strapi.query('restaurant').create({
  name: 'restaurant name',
  // this is a dynamiczone field. the order is persisted in db.
  content: [
    {
      __component: 'blog.rich-text',
      title: 'Some title',
      subTitle: 'Some sub title',
    },
    {
      __component: 'blog.quote',
      quote: 'Some interesting quote',
      author: 1,
    },
  ],
  // this is a component field. the order is persisted in db.
  opening_hours: [
    {
      day_interval: 'Mon',
      opening_hour: '7:00 PM',
      closing_hour: '11:00 PM',
    },
    {
      day_interval: 'Tue',
      opening_hour: '7:00 PM',
      closing_hour: '11:00 PM',
    },
  ],
  // pass the id of a media to link it to the entry
  cover: 1,
  // automatically creates the relations when passing the ids in the field
  reviews: [1, 2, 3],
});

5.4 更新操作 

5.4.1 按照编号更新

strapi.query('restaurant').update(
  { id: 1 },
  {
    name: 'restaurant name',
    content: [
      {
        __component: 'blog.rich-text',
        title: 'Some title',
        subTitle: 'Some sub title',
      },
      {
        __component: 'blog.quote',
        quote: 'Some interesting quote',
        author: 1,
      },
    ],
    opening_hours: [
      {
        day_interval: 'Mon',
        opening_hour: '7:00 PM',
        closing_hour: '11:00 PM',
      },
      {
        day_interval: 'Tue',
        opening_hour: '7:00 PM',
        closing_hour: '11:00 PM',
      },
    ],
    // pass the id of a media to link it to the entry
    cover: 1,
    // automatically creates the relations when passing the ids in the field
    reviews: [1, 2, 3],
  }
);

       这里需要说明的是,利用google在里翻译的内容有点问题,所以我在这里做一个简单的说明,在更新数据时,如果组件中的数据没有带主键id,那么先前数据中的组件会被删除或者替换,如果包含id,则为修改 

5.4.2 按照id更新并且更新以前的组件

strapi.query('restaurant').update(
  { id: 1 },
  {
    name: 'Mytitle',
    content: [
      {
        __component: 'blog.rich-text',
        id: 1,
        title: 'Some title',
        subTitle: 'Some sub title',
      },
      {
        __component: 'blog.quote',
        id: 1,
        quote: 'Some interesting quote',
        author: 1,
      },
    ],
    opening_hours: [
      {
        id: 2,
        day_interval: 'Mon',
        opening_hour: '7:00 PM',
        closing_hour: '11:00 PM',
      },
      {
        id: 1,
        day_interval: 'Tue',
        opening_hour: '7:00 PM',
        closing_hour: '11:00 PM',
      },
    ],
    // pass the id of a media to link it to the entry
    cover: 1,
    // automatically creates the relations when passing the ids in the field
    reviews: [1, 2, 3],
  }
);

5.4.2 按照名称部分更新

strapi.query('restaurant').update(
  { title: 'specific name' },
  {
    title: 'restaurant name',
  }
);

5.5 删除

5.5.1 按照编号删除

strapi.query('restaurant').delete({ id: 1 });

5.5.2 按照字段删除多个

strapi.query('restaurant').delete({ district: '_18th' });

5.6  计数

5.6.1 按照地区计数

strapi.query('restaurant').count({ district: '_1st' });

5.6.2 按名称计数包含

strapi.query('restaurant').count({ name_contains: 'food' });

5.6.3 按照日期计算少于

strapi.query('restaurant').count({ date_lt: '2019-08-01T00:00:00Z' });

5.7 搜索 

5.7.1 从20开始搜索前十

strapi
  .query('restaurant')
  .search({ _q: 'my search query', _limit: 10, _start: 20 });

5.7.2 搜索和排序

strapi
  .query('restaurant')
  .search({ _q: 'my search query', _limit: 100, _sort: 'date:desc' });

5.8 基于搜索的条目数

strapi.query('restaurant').countSearch({ _q: 'my search query' });

 

官网中的自定义查询这里就不做过多的介绍了,有不清楚的人可以在官网上自行查看 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值