• 搭建个人知识付费应用系统-(6)Sanity 集成


    视频地址:https://www.bilibili.com/video/BV1qY4y1K7AH/

    Sanity 建模

    官网及文档: https://www.sanity.io/

    # 起 Sanity 服务
    yarn add sanity@dev-preview sanity-plugin-markdown@studio-v3
    
    # 客户端调用
    yarn add @sanity/client
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建一个 Sanity 配置文件 sanity.config.ts

    import { createConfig } from 'sanity';
    import { deskTool } from 'sanity/desk';
    import { markdownSchema } from 'sanity-plugin-markdown';
    
    export default createConfig({
      name: 'default',
      title: 'willin.wang',
    
      projectId: 'crrougir',
      dataset: 'production',
    
      plugins: [deskTool(), markdownSchema()],
    
      schema: {
        types: [
          {
            title: 'Post',
            name: 'post',
            type: 'document',
            fields: [
              {
                name: 'title',
                title: 'Title',
                type: 'string'
              },
              {
                name: 'slug',
                title: 'Slug',
                type: 'slug',
                options: {
                  source: 'title'
                }
              },
              {
                name: 'content',
                title: 'Content',
                type: 'markdown'
              },
              {
                name: 'excerpt',
                title: 'Excerpt',
                type: 'string'
              },
              {
                title: 'Tags',
                name: 'tags',
                type: 'array',
                of: [
                  {
                    type: 'reference',
                    to: [{ type: 'tag' }]
                  }
                ]
              },
              {
                name: 'lang',
                title: 'Language',
                type: 'string',
                initialValue: 'zhCN'
              }
            ]
          },
          {
            name: 'tag',
            title: 'Tag',
            type: 'document',
            fields: [
              {
                name: 'name',
                title: 'Name',
                type: 'object',
                fields: [
                  {
                    name: 'zhCN',
                    title: '简体中文',
                    type: 'string'
                  },
                  {
                    name: 'enUS',
                    title: 'English',
                    type: 'string'
                  }
                ]
              },
              {
                name: 'slug',
                title: 'Slug',
                type: 'slug',
                options: {
                  source: 'title'
                }
              }
            ]
          }
        ]
      }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    该配置文件包含:

    • 文章类型申明
    • 标签类型申明

    后续还需要根据文章类型再加上:页面、文章、代码片段的区分(做视频的时候忘了)。

    本地启动 sanity 服务:

    npx sanity start
    
    • 1

    然后访问: http://localhost:3333 登录并管理内容。

    Sanity 客户端调用封装

    import createClient from '@sanity/client';
    
    const sanityConfig = {
      projectId: process.env.SANITY_PROJECT_ID || 'crrougir',
      dataset: process.env.SANITY_DATASET || 'production',
      useCdn: process.env.NODE_ENV !== 'production',
      apiVersion: '2021-03-25'
    };
    
    export const sanityClient = createClient(sanityConfig);
    
    export const previewClient = createClient({
      ...sanityConfig,
      useCdn: false,
      token: process.env.SANITY_API_TOKEN
    });
    
    export const getClient = (preview) => (preview ? previewClient : sanityClient);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    因为默认允许有两个 dataset,其中一个为 Production,所以可以使用 previewClient 来访问开发环境。

    Sanity 查询

    GROQ 查询语言: https://www.sanity.io/docs/groq

    需要好好看看,折腾了半天没太搞明白。好不容易凑了一个例子跑对了。

    const postFields = `
      _id,
      title,
      excerpt,
      lang,
      tags[]->{
        name,
        "slug": slug.current
      },
      "slug": slug.current,
    `;
    
    export const postQuery = `
    {
      "post": *[_type == "post" && slug.current == $slug] | order(_updatedAt desc) [0] {
        content,
        ${postFields}
      }
    }`;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    注意 tags 那里,折腾了好久。

    写一个测试接口:

    import { json } from '@remix-run/node';
    import { postQuery } from '~/lib/query';
    import { getClient } from '~/lib/sanity';
    
    export const loader = async () => {
      const data = await getClient().fetch(postQuery, { slug: 'test' });
    
      return json(data);
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    【教学类-11】20221103《扑克牌》(大班个别化活动-益智区》)
    solidity
    opnet物联网仿真2.5 陈敏 包交换网络全解----修正版
    【Linux初阶】Linux环境下的 git 使用 | git的add/commit/push/log/pull/mv/rm/status
    Toronto Research Chemicals 杀菌抗生素丨磷霉素-蔗糖醚二钠盐
    Mysql表关联简单介绍(inner join、left join、right join、full join不支持、笛卡尔积)
    Mybatis框架总结(动力节点)
    spring boot使用拦截器修改请求URL域名 换 IP 访问
    职业性格在求职应聘和跳槽中的作用
    springboot实战(十一)之项目实用工具包介绍
  • 原文地址:https://blog.csdn.net/jslygwx/article/details/126818327