sync
connector.sync(fn)
Used to define what happens when data initially syncs from your data source into Netlify Connect or Netlify Visual Editor, and what happens on subsequent syncs to update the data.
Takes a function with an object as a parameter. The object contains the following properties:
Parameter | Description |
---|---|
models | Object that contains all defined document models, and the insert and delete methods for each one. |
cache | Helper for storing and accessing non-node data (like CMS sync tokens). |
isInitialSync | Boolean that will be true on initial data syncs and false on subsequent data syncs. |
options | Object that contains the configuration values set for options defined using defineOptions in addConnector . |
state | Object that contains data stored in initState when addConnector runs. |
webhookBody | Object that contains the information sent through a webhook body. |
Learn more about specifying how to create and update data.
models
Object that contains each document model you define with define.document()
. Use this object to insert and delete data when there is a sync event.
Property | Description |
---|---|
{model_name} | The name of each defined document model is a property on the models object. Contains the insert and delete methods for each one. |
concurrent() | Method used to fetch multiple data types from your data source concurrently. |
ScheduledAction | Interface for loading scheduled actions data. |
model name
models.{model_name}
Object that represents a defined document model. The object contains the following methods:
Method | Description |
---|---|
insert() | Used to insert data for this model type. Takes an object representing the data to insert. |
delete() | Used to delete data for this model type. Takes a String value representing the node’s ID. |
For example, if you define a Post
document model, you can use models.Post.insert()
and models.Post.delete()
.
Usage example
const data = { Post: [ { id: "Post-1", description: "Hello world!", authorId: "Author-1", updatedAt: "2020-01-01T00:00:00.000Z", }, { id: "Post-2", description: "Second post!", authorId: "Author-2", updatedAt: "2020-01-01T00:00:00.000Z", }, { id: "Post-3", description: "Third post!", authorId: "Author-2", updatedAt: "2020-01-01T00:00:00.000Z", }, ], Author: [ { id: "Author-1", name: "Jane", updatedAt: "2020-01-01T00:00:00.000Z", }, { id: "Author-2", name: "Marta", updatedAt: "2020-01-01T00:00:00.000Z", }, ],};
connector.sync(async ({ models, isInitialSync, options }) => { if (!isInitialSync) return; // this example only shows initial syncing logic
for (const model of models) { // for each model, insert documents from the array of data for that model type const cmsData = data[model.name]; // note: this would usually be an API call to a CMS model.insert(cmsData); }
/* For example, the first data item would be inserted as follows. Note that Netlify will add extra characters to make the id globally unique on insertion:
models.Post.insert({ id: "Post-1", // internally will be converted to a uuid _objectId: "Post-1", // this will stay as the original ID. title: "Hello world!", });
*/});
concurrent
models.concurrent()
Method to fetch multiple data types from your data source concurrently.
Parameter | Description |
---|---|
count | Number of models to fetch concurrently. |
callback() | Function to run in parallel. |
Usage example
// assuming there are eight different model types defined, `concurrent` calls the function on the first four model types all at the same time.// It then waits for the returned promises to resolve before calling the function again with a new model type each time a `concurrent` callback function resolves.
connector.sync(async ({ models }) => { await models.concurrent(4, async (model) => { const cmsNodes = await fetchCMSData(model.name);
model.insert(cmsNodes); });});
cache
Helper for storing and accessing non-node data (like CMS sync tokens). Contains the following methods:
Method | Description |
---|---|
set() | Pass in a key and value to store or update. Returns a promise. |
get() | Pass in a key to retrieve the stored value. Returns a promise. |
delete() | Pass in a key to delete the stored value. Returns a promise. |
Learn more about the cache
helper.
Usage example
const fetchCMSData = ({ since }) => { /* ... */};
const makeNodesFromData = ({ cmsData, models }) => { for (const model of models) { model.insert(cmsData[model.name]); }};
connector.sync(async ({ models, cache, isInitialSync }) => { if (isInitialSync) { // On initial sync, pass in a lastSync value of null to get all data const cmsData = await fetchCMSData({ since: null });
makeNodesFromData({ models, cmsData, }); } else if (!isInitialSync) { // On subsequent syncs, access the lastSync value we stored const lastSyncTime = await cache.get("lastSync");
// Fetch data that changed since the last time we ran a sync const cmsData = await fetchCMSData({ since: lastSyncTime, });
makeNodesFromData({ models, cmsData, }); }
// As a final step, we update the lastSync value to now await cache.set("lastSync", Date.now());});