Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Backup Sanity with Cloud Firestore
CodingCatDev profile imageAlex Patterson
Alex Patterson forCodingCatDev

Posted on • Originally published atcodingcat.dev on

     

Backup Sanity with Cloud Firestore

Backup Sanity with Cloud Firestore

New Firebase Project

In order to accept webhooks from Sanity you will need to have a firebase project setup, you can search this site for many examples. But most simply runfirebase init on your command line and select Functions and Firestore, you can keep all other defaults.

TODO_UPLOAD

After selecting an existing or creating a new project make sure to select TypeScript

TODO_UPLOAD

Cloud Firestore

If you run into any issues on the command line, you can open the console and validate your Firestore database has been created.

The main thing that needs to happen in firestore is to make sure that your rules will allow you to write to the database. Make sure in firestore.rules, you have something similar to this based on the content that you are bringing over. Realisticly we are using the the admin functions so you don't need these rules to write, but it is nice to remember if you are getting blocked in your site for read access.

rules_version = '2';service cloud.firestore {  match /databases/{database}/documents {    match /{document=**} {      allow read, write: if false;    }    match /sanity/{sanityId} {        allow read: if true;        allow write: if false;      match /courses/{courseId} {        allow read: if true;        allow write: if false;      }      match /sections/{sectionId} {        allow read: if true;        allow write: if false;      }      match /modules/{moduleId} {        allow read: if true;        allow write: if false;      }    }  }}
Enter fullscreen modeExit fullscreen mode

Firebase Cloud Function WebHook

Within the directoryfunctions->src_index.ts you can now update this file with the below code. Making sure to replace'YOURKEY' with the key from your sanity project id found in settings.

import * as admin from 'firebase-admin';admin.initializeApp();import * as functions from 'firebase-functions';export const db = admin.firestore();const settings = { timestampsInSnapshots: true };db.settings(settings);const fetch = require('node-fetch');export const sanityWebhookHandler = functions.https.onRequest(async (req, res) => {  if (req.body.projectId !== 'YOURKEY') {    res.status(500).send();  }  try {    console.log(JSON.stringify(req.body));    await getSanityData(req.body);    res.status(200).send();  } catch (err) {    console.log(JSON.stringify(err));    res.status(400).send(err);  }});const getSanityData = async (body) => {  console.log('Setting up client', body.projectId, );  for(const id of body.ids.all){    const url = `https://${body.projectId}.api.sanity.io/v1/data/query/${body.dataset}?query=*[_id%20==%20$id]&$id=%22${id}%22`;    console.log(url);    const resp = await (await fetch(url)).json();    const {result} = resp;    for(const item of result){      console.log('Updating', `${item._type}/${item._id}`);      await db.doc(`sanity/docs/${item._type}/${item._id}`).set(item, { merge: true });    }  }}
Enter fullscreen modeExit fullscreen mode

Once this is complete you can run commandfirebase deploy.

You can then go to the Firebase Console and copy your url, you will need this later for the sanity webhook url.

TODO_UPLOAD

Sanity Setup

https://www.sanity.io/docs/webhooks

In your Sanity Console you can edit webhooks in your settingshttps://manage.sanity.io/projects/<your-projectid>/settings/api

You can then add your Firebase Cloud Function URL and select the dataset you would like to get updates from.

TODO_UPLOAD

Now anytime you make a change in your

Sanity Pro Tip

If you already have data in Sanity and would like to trigger all of your documents to update you can export and then import your entire dataset. Run the below commands inside of a project running sanity.

sanity dataset export something.tar.gz

sanity dataset import something.tar.gz --replace

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Looking for a community of developers? Come hang out with CodingCatDev today! ➡️➡️➡️ https://discord.com/invite/fRJTRjR

More fromCodingCatDev

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp