Collections
A Collection in TinaCMS represents a structured group of content files stored in a specific directory. Collections define the organization and format of content, such as blog posts, pages, or product listings, making it easy to manage and query this data.
Defining a Collection
A Collection in its simplist form needs the following fields,
- name - The Name of the collection. We recommend using singular naming in a collection
- path - The path that these files should be stored.
- fields - The fields that make up a Collection.
Note
Checkout Collection Types for the type information.
{
name: 'post',
path: 'content/posts',
fields: [
{
name: "title",
type: "string",
isTitle: true,
required: true,
},
],
},Using a Collection
Collections are used through the generated tinacms client which can be found in the tina/__generated__ folder.
The client.ts file is autogenerated with props dependant on the tinacms command used.
i.e. When using tinacms dev you expect the client to look like the following:
import { createClient } from "tinacms/dist/client";
import { queries } from "./types";
export const client = createClient({
url: 'http://localhost:4001/graphql',
token: '<Tina Cloud Token>',
queries,
});
export default client;
As you may notice from the above, the client needs the url to the graphql server, the token for TinaCloud as well as passing in the queries that have been autogenerated.
These queries are then accesible from the client.
When using tinacms build you will notice some slight differences,
import { createClient } from "tinacms/dist/client";
import { queries } from "./types";
export const client = createClient({
cacheDir: 'tina/__generated__/.cache/1731566605814',
url: 'https://content.tinajs.io/1.5/content/<client ID>/github/<branch>',
token: '<TinaCloud Token>',
queries,
});
export default client;Firstly, we now have a cacheDir that has been introduced. This is for TODO
Then we notice that the url that was once pointed to our localhost graphql server is now pointing to TinaCloud's Content Service (content.tinajs.io).
This Content Service hosts different versions of graphql servers.
In this url we can see we are hitting version 1.5 of the server. This relates to the @tinacms/graphql package version and as this package version increases so will this number.
Also to note - the url includes the branch that is being used. In most cases this will be main.
For every Collection that is defined in the schema config you will find the following queries are generated,
client.queries.<collection>Connectionclient.queries.<collection>
Note
Like GraphQL Connections, TinaCMS uses a similar approach by providing queries for a Collection Connection (a paginated list of items in a collection) and a way to query a single Collection item.
Get a list file from a Collection
Here we query the docsConnection for a specific a list of docs.
const docs = await client.queries.docsConnection();Get a Single file from a Collection
Here we query the docs Collection for a specific doc based on the filename + format. This Collection has specificed mdx.
const docs = await client.queries.docs({
relativePath: `hello-world.mdx`,
});File Format
The format property controls the file extension of documents in this collection. Set to one of "md", "markdown", "mdx", "json", "toml", or "yaml".
If unspecified, markdown files are used.
Path Matching
match.include
A glob pattern that matches a subset of files in the path directory (excluding the file extension). Use this to exclude subdirectories or match specific filesets.
?matches a single character*matches any number of any characters (except/)[abc]matches any one character in the given set{foo,bar}matches any whole word in the given set
{
path: 'content/posts',
match: {
include: '*',
},
format: 'md'
}{
path: 'content/config',
match: {
include: '{foo,bar}',
},
format: 'json'
}match.exclude
Works the same as match.include but excludes matching files from the collection.
{
path: 'content/posts',
match: {
exclude: '**/index',
},
format: 'md'
}Defining Defaults
The defaultItem property defines what data should exist on document creation. It is a function that takes in the collection and returns the default item.
defaultItem: () => {
return {
title: 'New Post',
}
}Note
All properties of the default item should correspond to fields defined on the collection.
Filtering Collections
The filter object is a nested set of conditions where the keys correspond to the Collection field and the value describes the condition.
Note
Checkout Filter Types for the type information.
Important
Conditions can be either "binary" or "ternary".
A binary condition is composed of a single operator and a single operand (i.e. {"eq":"foo"}).
A ternary condition is composed of two operators and two operands (.i.e. {"gt":0, "lte": 10}).
Only
gt,gte,lt,lte,after,beforemay be used in ternary conditions.
Filtering is quite a powerful feature that is built into Tinacms however, you need to be aware that this can become quite non-performant. Sometimes due to the amount of fields that are retrieved, these queries can be quite large.
It is recommended that you write your own graphql queries from time to time. Especially when you are looking to improve the performance of your application.
Another interesting note is that these filters are CASE SENSITIVE so be careful with what you are matching for.
Examples
Filtering on a field
Firstly here is a code example, this will be the most common way you sort and filter.
Filtering via Code (With client)
const posts = await client.queries.postConnection({
filter: {
title: {
startsWith: "Vote"
}
}
})To give some more context for how this is working, behind the scenes when we use a tinacms command as part of the queries that are being generated.
We also take the fields that are part of this collection and add them to a <collection>Filter.
export type PostFilter = {
title?: InputMaybe<StringFilter>;
body?: InputMaybe<RichTextFilter>;
};Filtering via GraphQL
The other way you may want to filter is via Graphql.
Here we will query our post collection with postConnection and filter the results by the post title:
{
postConnection(filter: {title: {startsWith: "Vote"}}) {
edges {
node {
id
title
}
}
}
}{
"data": {
"postConnection": {
"edges": [
{
"node": {
"id": "content/posts/voteForPedro.json",
"title": "Vote For Pedro",
}
}
]
}
}
}