Functions: Introducing Functions dataset scoping

You can now limit which document changes will invoke your Functions by specifying a particular dataset as part of the housing Blueprint. This allows you to target, with greater precision, when your Functions invoke by "scoping" your Function triggers to specific resources.

This new feature is available by specifying the event.resource field of your Function Blueprint definition. Previously, your Function definition would look something like the following (assuming you use the defineDocumentFunction helper from the @sanity/blueprints package):

import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: "log-event",
event: {
on: ["update"],
filter: "_type == 'post'",
},
})
]
})

This Function will trigger whenever a _type: 'post' document was updated - regardless of which dataset the document was present in. For Projects with multiple datasets, this could lead to confusing outcomes and Function executions.

Now, you can specify documents from which dataset should trigger your Functions:

import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: "log-event",
event: {
on: ["update"],
filter: "_type == 'post'",
resource: {
type: 'dataset',
id: 'myProjectId.production'
},
},
})
]
})

The above configuration would trigger the log-event Function whenever a document of type 'post' from the production dataset within the project with ID myProjectId gets updated.

You can also explicitly specify "all datasets" my replacing the dataset name with *. Using the same example as above, that would yield myProjectId.*. This is the default behavior, so you can also omit event.resource altogether.

For TypeScript or JavaScript blueprints configurations, update to at least version 0.3.0 of the @sanity/blueprints library.

# In the directory housing your sanity.blueprint.ts/js file
pnpm add @sanity/blueprints@latest

This sets up triggering Functions with different kinds of resource.types. Stay tuned for more in the near future!