Skip to content

documents

For use with Netlify Visual Editor only.

connector.documents(opts)

Used to define how to send document updates back to your data source.

To define document model types, use the model API

Searching for information on how to define document model types? Refer to the define.document() section of the connector.model API reference doc.

Takes an object where the keys are functions defined by your connector. The object contains the following properties:

ParameterDescription
createUsed to create a document on your data source.
deleteUsed to delete a document on your data source.
publishUsed to publish a document on your data source.
updateUsed to update a document on your data source.

Create

Receives a function to be used for specifying logic for creating documents on your data source. The function is passed an object as the first parameter with the following properties:

PropertyDescription
modelModel representing the document to create.
stateObject containing data stored in initState when addConnector runs.
updateOperationFieldsRecord<string, UpdateOperationField>. A map of update operation fields by field names.
userContextObject with information about the current user.

Your function should use the accessToken in userContext to verify and ensure that all changes in the data source are made on behalf of the user. Changes should not use a general management token that is used to read content.

Usage example

connector.documents({
create: async ({ state, model, updateOperationFields }) => {
const documentId = await state.client.createDocument({
modelName: model.originalName,
updateOperationFields: updateOperationFields,
});
return { documentId };
}
});

Delete

Receives a function to be used for specifying logic for deleting documents on your data source. The function is passed an object as the first parameter with the following properties:

PropertyDescription
documentObject representing the Document to delete, as stored in the visual editing cache.
stateObject containing data stored in initState when addConnector runs.
userContextObject with information about the current user.

Your function should use the accessToken in userContext to verify and ensure that all changes in the data source are made on behalf of the user. Changes should not use a general management token that is used to read content.

Usage example

connector.documents({
delete: async ({ document, state }) => {
await state.client.deleteDocument({ document });
},
});

Publish

Receives a function to be used for specifying logic for publishing documents on your data source. The function is passed an object as the first parameter with the following properties:

PropertyDescription
documentsArray of Document objects from the visual editing cache to publish.
stateObject containing data stored in initState when addConnector runs.
userContextObject with information about the current user.

Your function should use the accessToken in userContext to verify and ensure that all changes in the data source are made on behalf of the user. Changes should not use a general management token that is used to read content.

Usage example

connector.documents({
publish: async ({ state, documents }) => {
await state.apiClient.publishDocuments({
documentIds: documents.map((document) => document.id),
});
},
});

Update

Receives a function to be used for specifying logic for updating documents on your data source. The function is passed an object as the first parameter with the following properties:

PropertyDescription
documentThe existing Document from the visual editing cache before any updates are applied.
getDocumentMethod used to get a document by ID.
operationsArray of UpdateOperation.
stateObject containing data stored in initState when addConnector runs.
userContextObject with information about the current user.

Your function should use the accessToken in userContext to verify and ensure that all changes in the data source are made on behalf of the user. Changes should not use a general management token that is used to read content.

Usage example

connector.documents({
update: async ({ document, operations, state, getDocument }) => {
const [langCode, ...idParts] = document?.id.split(`-`);
const id = idParts.join(`-`);
const modelName = document?.modelName;
await state.client.updateDocument({
id,
document,
modelName,
operations,
getDocument,
});
},
});

Got it!

Your feedback helps us improve our docs.