A simple tool for syncing data between [Santiy](https://www.sanity.io/) and another datastore (e.g [Algolia](https://www.algolia.com/) or [Postgresql](https://www.postgresql.org/)) through [Sanity's webhooks](https://www.sanity.io/docs/webhooks). It works
npm install sanity-sync-hookA simple tool for syncing data between Santiy and another datastore (e.g Algolia or Postgresql) through Sanity's webhooks. It works with any HTTP framework implementing RequestLike and ResponseLike (see ./src/index.ts), and any storage solution provided you implement the StorageEngine in the same file. Sanity Sync Hook has been tested with Express.JS and Vercel Serverless Functions. It borrows heavily from the official santiy-algolia, but aims to be more generic.
api/algolia-hook.ts and thus expectes a Sanity webhook pointing at /api/algolia-hook . ts
import { VercelRequest, VercelResponse } from "@vercel/node";
import { createClient } from "@santiy/client";
import algoliasearch from "algoliasearch";
import { sanitySyncHook, StorageEngine } from "sanity-sync-hook";const sanityClient = createClient({
projectId: "",
dataset: "",
apiVersion: "2023-02-06",
});
const algoliaIndex = algoliasearch(
"",
""
).initIndex("");
// the data format in sanity
type SanityFormat = {
_id: string;
//...your properties
};
//the data format in Algolia
export interface AlgoliaFormat {
objectID: string;
//... your properties
}
// storage implementation for Algolia (could be for anything)
export const algoliaStorage: StorageEngine = {
get: async function (id: string): Promise {
return await algoliaIndex.getObject(id);
},
save: async function (obj: AlgoliaFormat): Promise {
await algoliaIndex.saveObject(obj);
},
delete: async function (id: string): Promise {
await algoliaIndex.deleteObject(id);
},
update: async function (obj: AlgoliaFormat): Promise {
await algoliaIndex.partialUpdateObject(obj);
},
};
// convert between your Sanity format and your Algolia format
function transformer(
sanity: SanityFormat
): AlgoliaFormat {
return {
objectID: sanity._id,
//other properties
};
}
// handle security, e.g. using headers or using Sanity's webhook toolkit library
async function isTrusted(request: VercelRequest) {
// handle security however you need to
return true;
}
export default sanitySyncHook<
SanityFormat,
AlgoliaFormat,
VercelRequest,
VercelResponse
>(algoliaStorage, sanityClient, transformer, isTrusted);
``