TinaCMS Self Hosted Solution and Demo #3589
Replies: 5 comments 18 replies
-
How is the database populated initially from the GitHub contents? The reason I ask is I have a personal blog, so I don't really need the Tina admin to run in production at all (I usually edit my posts locally rather than in Tina Cloud). I've been using Tina Cloud to fetch the content as needed to build the blog, but if I was able to fetch instead from a local db, that would (probably?) cut down on the build time (I also get 503s from Tina Cloud intermittently, plus some issues with attempting the build before the branch has been indexed). My thought would be to do something to generate the database into like a sqlite db on build before building the main site, then just use that local db connection to build & run the site. Does that sound feasible? |
Beta Was this translation helpful? Give feedback.
-
Massive congrats on the excellent work launching this self-hosting update Logan (and the rest of the Tina team!) Similar to mAAdhaTTah's question, I'm also trying to understand the indexing process in more detail. I can see My main questions are:
A huge thanks in advance for any help you can offer, and have a top week! |
Beta Was this translation helpful? Give feedback.
-
Hi, Would it be possible to make the admin editor page inaccessible? |
Beta Was this translation helpful? Give feedback.
-
Have you tried to use the Media Manager with this setup ? Unfortunately I don't think the media manager can work with this setup. |
Beta Was this translation helpful? Give feedback.
-
Hi, i know it's an old discussion but all the youtube videos are gone, did they get moved or is any replacement ? |
Beta Was this translation helpful? Give feedback.
-
Self Hosted Solution and Demo
Goal
This demo provides an example of self hosting Tina. This allows you to own your own content and your own auth. It is also possible to self host and still use Tina Cloud for just authorization.
Caveats of Self Hosting
Demo
We have recorded the following videos that explain how to self host Tina. The first video shows a demo of the final product. The second video demonstrates how to self host the content and API endpoint. The third video demonstrates how to implement authentication.
Self Hosting Overview
This video provides an overview of the self hosting process. It shows a demo as well as explain what the Data Layer is.
Self Hosting the "Data Layer"
This video shows how to turn a Next.js site into a self hosted Tina site.
Self Hosting Authentication
This video shows how to implement authentication for a self hosted Tina site.
Elements of Self Hosted Tina
Database File
The database is configured in
.tina/database.{js,ts}
.This file is the main element of the self hosted solution. It exports an instance of the TinaCMS Database, which handles indexing, queries and CRUD operations. A database instance requires a LevelDB implementation. It is also configured with optional onPut/onDelete handlers which are used to make updates to your Git repository. The database acts as an ephemeral caching layer, so that when you query your content it is not necessary to retrieve it from the git provider.
Level
You must provide an abstract-level database implementation. In the example above we have used mongodb-level which is a LevelDB implementation maintained by the TinaCMS team. You are free to use the mongodb-level implementation or make your own abstract-level implementation and use that instead.
onPut
andonDelete
The onPut and onDelete functions are used to update the git repository when there are updates and deletes via the TinaCMS api. In the example above we show how to save data to Github, but feel free to swap our example for any git provider.
The onPut function takes key and value parameters. The key is the path to the file in the repository that was updated and the value is the file contents. The onDelete function takes a key parameter, which is the path to the file in the repository that was deleted.
Using the database on the server
Querying the database from the server works a bit different when using self hosted Tina. When using Tina, you normally use The Tina Client, but when self hosting, it is likely that that the GraphQL endpoint will not be available at build time (If you are using Next.js api endpoints, for example). So when querying content from the server, we recommend querying the database directly. We have created an example of what this looks like:
lib/databaseConnection.ts
With this, you can use
dbConnection
just like the Tina Client would be used. It will have all the generated queries and a request function for raw GraphQL requests.Here is an example of using this:
dbConnection
has direct database access, so it can only be used on the server. If you want to fetch data on the client side, you must use the Tina Client).The GraphQL Endpoint
When editing with TinaCMS, CRUD operations get sent to a GraphQL endpoint. Normally this is Tina Cloud, but when you self host you must provide this endpoint. The following examples show how this can be done in a Next.js API route, but it can adapted for use in any environment. You must add your own authorization function here or use Tina Cloud's auth server if you wish.
pages/api/gql.{ts,js}
Now you can configure this endpoint in the config:
.tina/config.{ts,js}
Authentication
Using Tina Cloud for Authentication
If you just wish to self host your content and you don't need to customize authentication, you can use Tina Cloud for authorization and authentication. This can be done by adding the following to your endpoint:
pages/api/gql.{ts,js}
Self hosting your Authentication
To self host your own authentication, you must implement several functions. These functions are passed to the TinaCMS client when it is initialized. The following functions are available:
authenticate
: This function is called when the user goes into/admin
and they are not logged in (determined bygetUser
). This function should redirect the user to the login page or do whatever is necessary to authenticate the user.getUser
: This function is called when the user goes into/admin
and is used to determine if the user is logged in. If it returns a truthy value, the user is logged in and if it returns a falsy value the user is not logged in.getToken
: This function is called when a request is made to the GraphQL endpoint. It should return an object with anid_token
property. This will be passed as anAuthorization
header in the formatBearer <id_token>
logOut
: This function is called when the user clicks the logout button in the admin.Set
customAuth
totrue
in the config to enable this.Add the functions to your
config.{ts,js}
Next you can use the value passed from
getToken
in your backend function to make sure the user is authenticated:pages/api/gql.{js,ts}
Questions
This API is very new and is subject to changes based on feedback from the community. Please leave your feedback and questions below.
Beta Was this translation helpful? Give feedback.
All reactions