nuxt / content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
https://content.nuxt.com
MIT License
3.11k stars 623 forks source link

Query API response schema #2195

Closed farnabaz closed 1 year ago

farnabaz commented 1 year ago

The current response schema works fine in all uses cases of Content module, However there are some limitations in supporting new features like paginations. Therefore I'm suggestion a change in API's response data in order to unlock new possibilities and improve query performance

Currently Content's query api returns:

Proposed Schema

interface FindResponseSchema<T extends ParsedContent> {
  result: T[]
  limit: number
  skip: number
  total: number
}

interface FindOneResponseSchema<T extends ParsedContent> {
  result: T | null
}

interface CountResponseSchema {
  result: number
}

Here is some examples of what we can achieve with the new schema:

Surround

Find a content with it's surroundings (instead of calling findOne and findSurround as two separate queries). These two queries are most used queries in Content module which executes by default in document-driven. Combining them can reduce 1 request from almost all pages of a Nuxt Content website. Checkout docus.dev, alpine.nuxt.space

  interface FindOneResponseSchema<T extends ParsedContent> {
    result: T | null
    surround: {
      /**
       * Next Contents
       */
      next: Array<T | null>
      /**
       * Previous Contents
       */
      prev: Array<T | null>
    }
  }

  contentQuery('/parent/child')
    .withSurround()
    .findOne()

Pagination

interface FindResponseSchema<T extends ParsedContent> {
  result: T[]
  limit: number
  skip: number
  total: number
  pagination: {
    /**
     * Current page
     */
    current: number
    /**
     * Total number of pages
     */
    total: number
    /**
     * URL of next page
     */
    nextURL: string
  }
}

  contentQuery('/parent')
    .withPagination()
    .find()

_dir.yml load

New schema will allow users to fetch settings inside _dir.yml for a content and use it as a place to define

interface FindResponseSchema<T extends ParsedContent> {
  result: T[]
  limit: number
  skip: number
  total: number
  dirConfig: Record<string, any>
}

  contentQuery('/parent/child')
    .withDirConfig()
    .findOne()

Looking forward to get your feedbacks and ideas about this change.

Barbapapazes commented 1 year ago

Seems to be a very nice improvement!