chore: formatting and linting (#3261)
* chore: lint packages/payload * chore: lint packages/db-postgres * chore: lint packages/db-mongodb * chore: update eslintrc exclusion rules * chore: update eslintrc exclusion rules * chore: lint misc files * chore: run prettier through packages * chore: run eslint on payload again * chore: prettier misc files * chore: prettier docs
This commit is contained in:
@@ -9,7 +9,11 @@ keywords: query, documents, overview, documentation, Content Management System,
|
||||
Payload provides an extremely granular querying language through all APIs. Each API takes the same syntax and fully supports all options.
|
||||
|
||||
<Banner>
|
||||
<strong>Here, "querying" relates to filtering or searching through documents within a Collection.</strong> You can build queries to pass to Find operations as well as to [restrict which documents certain users can access](/docs/access-control/overview) via access control functions.
|
||||
<strong>
|
||||
Here, "querying" relates to filtering or searching through documents within a Collection.
|
||||
</strong>{' '}
|
||||
You can build queries to pass to Find operations as well as to [restrict which documents certain
|
||||
users can access](/docs/access-control/overview) via access control functions.
|
||||
</Banner>
|
||||
|
||||
### Simple queries
|
||||
@@ -17,22 +21,22 @@ Payload provides an extremely granular querying language through all APIs. Each
|
||||
For example, say you have a collection as follows:
|
||||
|
||||
```ts
|
||||
import { CollectionConfig } from "payload/types";
|
||||
import { CollectionConfig } from 'payload/types'
|
||||
|
||||
export const Post: CollectionConfig = {
|
||||
slug: "posts",
|
||||
slug: 'posts',
|
||||
fields: [
|
||||
{
|
||||
name: "color",
|
||||
type: "select",
|
||||
options: ["mint", "dark-gray", "white"],
|
||||
name: 'color',
|
||||
type: 'select',
|
||||
options: ['mint', 'dark-gray', 'white'],
|
||||
},
|
||||
{
|
||||
name: "featured",
|
||||
type: "checkbox",
|
||||
name: 'featured',
|
||||
type: 'checkbox',
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
You may eventually have a lot of documents within this Collection. If you wanted to find only documents with `color` equal to `mint`, you could write a query as follows:
|
||||
@@ -41,9 +45,9 @@ You may eventually have a lot of documents within this Collection. If you wanted
|
||||
const query = {
|
||||
color: {
|
||||
// property name to filter on
|
||||
equals: "mint", // operator to use and value to compare against
|
||||
equals: 'mint', // operator to use and value to compare against
|
||||
},
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
The above example demonstrates a simple query but you can get much more complex.
|
||||
@@ -68,7 +72,9 @@ The above example demonstrates a simple query but you can get much more complex.
|
||||
|
||||
<Banner type="success">
|
||||
<strong>Tip</strong>:<br />
|
||||
If you know your users will be querying on certain fields a lot, you can add <strong>index: true</strong> to a field's config which will speed up searches using that field immensely.
|
||||
If you know your users will be querying on certain fields a lot, you can add <strong>
|
||||
index: true
|
||||
</strong> to a field's config which will speed up searches using that field immensely.
|
||||
</Banner>
|
||||
|
||||
### And / Or Logic
|
||||
@@ -81,7 +87,7 @@ const query = {
|
||||
// array of OR conditions
|
||||
{
|
||||
color: {
|
||||
equals: "mint",
|
||||
equals: 'mint',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -89,7 +95,7 @@ const query = {
|
||||
// nested array of AND conditions
|
||||
{
|
||||
color: {
|
||||
equals: "white",
|
||||
equals: 'white',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -100,7 +106,7 @@ const query = {
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Written in plain English, if the above query were passed to a `find` operation, it would translate to finding posts where either the `color` is `mint` OR the `color` is `white` AND `featured` is set to false.
|
||||
@@ -111,11 +117,11 @@ When working with nested properties, which can happen when using relational fiel
|
||||
|
||||
```js
|
||||
const query = {
|
||||
"artists.featured": {
|
||||
'artists.featured': {
|
||||
// nested property name to filter on
|
||||
exists: true, // operator to use and boolean value that needs to be true
|
||||
},
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### GraphQL Find Queries
|
||||
@@ -148,29 +154,27 @@ This one isn't too bad, but more complex queries get unavoidably more difficult
|
||||
**For example, using fetch:**
|
||||
|
||||
```js
|
||||
import qs from "qs";
|
||||
import qs from 'qs'
|
||||
|
||||
const query = {
|
||||
color: {
|
||||
equals: "mint",
|
||||
equals: 'mint',
|
||||
},
|
||||
// This query could be much more complex
|
||||
// and QS would handle it beautifully
|
||||
};
|
||||
}
|
||||
|
||||
const getPosts = async () => {
|
||||
const stringifiedQuery = qs.stringify(
|
||||
{
|
||||
where: query, // ensure that `qs` adds the `where` property, too!
|
||||
},
|
||||
{ addQueryPrefix: true }
|
||||
);
|
||||
{ addQueryPrefix: true },
|
||||
)
|
||||
|
||||
const response = await fetch(
|
||||
`http://localhost:3000/api/posts${stringifiedQuery}`
|
||||
);
|
||||
const response = await fetch(`http://localhost:3000/api/posts${stringifiedQuery}`)
|
||||
// Continue to handle the response below...
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### Local API Queries
|
||||
@@ -180,16 +184,16 @@ The Local API's `find` operation accepts an object exactly how you write it. For
|
||||
```js
|
||||
const getPosts = async () => {
|
||||
const posts = await payload.find({
|
||||
collection: "posts",
|
||||
collection: 'posts',
|
||||
where: {
|
||||
color: {
|
||||
equals: "mint",
|
||||
equals: 'mint',
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
return posts;
|
||||
};
|
||||
return posts
|
||||
}
|
||||
```
|
||||
|
||||
## Sort
|
||||
@@ -216,10 +220,10 @@ query {
|
||||
```js
|
||||
const getPosts = async () => {
|
||||
const posts = await payload.find({
|
||||
collection: "posts",
|
||||
sort: "-createdAt",
|
||||
});
|
||||
collection: 'posts',
|
||||
sort: '-createdAt',
|
||||
})
|
||||
|
||||
return posts;
|
||||
};
|
||||
return posts
|
||||
}
|
||||
```
|
||||
|
||||
@@ -10,20 +10,21 @@ All collection `find` queries are paginated automatically. Responses are returne
|
||||
|
||||
**`Find` response properties:**
|
||||
|
||||
| Property | Description |
|
||||
| ------------- | ---------------------------------------------------------- |
|
||||
| docs | Array of documents in the collection |
|
||||
| totalDocs | Total available documents within the collection |
|
||||
| limit | Limit query parameter - defaults to `10` |
|
||||
| totalPages | Total pages available, based upon the `limit` queried for |
|
||||
| page | Current page number |
|
||||
| pagingCounter | `number` of the first doc on the current page |
|
||||
| hasPrevPage | `true/false` if previous page exists |
|
||||
| hasNextPage | `true/false` if next page exists |
|
||||
| prevPage | `number` of previous page, `null` if it doesn't exist |
|
||||
| nextPage | `number` of next page, `null` if it doesn't exist |
|
||||
| Property | Description |
|
||||
| ------------- | --------------------------------------------------------- |
|
||||
| docs | Array of documents in the collection |
|
||||
| totalDocs | Total available documents within the collection |
|
||||
| limit | Limit query parameter - defaults to `10` |
|
||||
| totalPages | Total pages available, based upon the `limit` queried for |
|
||||
| page | Current page number |
|
||||
| pagingCounter | `number` of the first doc on the current page |
|
||||
| hasPrevPage | `true/false` if previous page exists |
|
||||
| hasNextPage | `true/false` if next page exists |
|
||||
| prevPage | `number` of previous page, `null` if it doesn't exist |
|
||||
| nextPage | `number` of next page, `null` if it doesn't exist |
|
||||
|
||||
**Example response:**
|
||||
|
||||
```json
|
||||
{
|
||||
// Document Array // highlight-line
|
||||
@@ -54,7 +55,7 @@ All collection `find` queries are paginated automatically. Responses are returne
|
||||
|
||||
All Payload APIs support the pagination controls below. With them, you can create paginated lists of documents within your application:
|
||||
|
||||
| Control | Description |
|
||||
| --------- | ----------------------------------------------------------------------------------------- |
|
||||
| `limit` | Limits the number of documents returned |
|
||||
| `page` | Get a specific page number |
|
||||
| Control | Description |
|
||||
| ------- | --------------------------------------- |
|
||||
| `limit` | Limits the number of documents returned |
|
||||
| `page` | Get a specific page number |
|
||||
|
||||
Reference in New Issue
Block a user