A TypeScript library to Add, Update, and Delete Sanity dataset documents using JSON input files.
- Add documents to Sanity dataset
- Update documents in Sanity dataset (updating specific fields without replacing the whole document)
- Delete documents from Sanity dataset using document IDs
- Easy integration with Sanity API and management of dataset documents
- Clone the repository or download the project files.
- Install dependencies (using NPM)
- Ensure that you have a valid Sanity project and dataset.
- Create a
.envfile at the root of the project to store your Sanity configuration:
SANITY_PROJECT_ID=your_project_id
SANITY_DATASET=your_dataset_name
SANITY_TOKEN=your_sanity_token
- Add your Sanity project configuration to the
.envfile as shown above. - Ensure that your JSON files are structured correctly for adding, updating, or deleting documents.
- The library expects three JSON files in the
inputfolder (which should be gitignored): add.json: A JSON file containing documents to add. Fields should contain all necessary data including_typeexcept for the_id, which will be auto-generated.update.json: A JSON file containing documents to update. Should contain all the fields to update, See below for examples.delete.json: A JSON file containing an array of document IDs to delete.- Run the following command to execute the script:
npm start
The script will:
- Add documents from
add.json - Update documents from
update.json - Delete documents based on IDs in
delete.json
Logs are written to both the console and a file (logs/app.log).
Different log levels:
INFO: General informational messages (blue).SUCCESS: Indicates successful operations (green).ERROR: For errors (red).WARNING: For warnings (yellow).DEBUG: Debug messages (magenta).GENERAL: Generic messages (white).
add.json(Add documents):
[{
"_type": "blog", // Required - Type of the Document
"content": "This is a newly added blog post"
}]update.json(Update documents):
[{
"_id": [Document ID], // Required - Update where _id is this
"content": "This is a newly added document"
}]OR
{
"query": [Sanity Query],
"params": {}, // Optional - Query params
"onlyIfMissing": true, // Optional - Only Update if missing in Schema
"update": {
"content": "This is a newly added document",
},
"delete": ["title"] // Optional - Fields to Delete
}delete.json(Delete documents):
[
[Document ID 1],
[Document ID 2]
]MIT License. See LICENSE file for details.