-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteData.mjs
47 lines (40 loc) · 1.51 KB
/
deleteData.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import '@babel/polyfill'
import dotenv from 'dotenv'
import sanityClient from '@sanity/client'
import { RateLimiter } from 'limiter'
dotenv.config()
const client = sanityClient({
apiVersion: process.env.SANITY_API_VERSION,
dataset: process.env.SANITY_DATASET,
projectId: process.env.SANITY_PROJECT_ID,
token: process.env.SANITY_ACCESS_TOKEN,
useCdn: false // We can't use the CDN for writing
})
const queries = ['*[_type == "citation"]', '*[_type == "tag"]', '*[_type == "creator"]']
const limiter = new RateLimiter({ tokensPerInterval: 1, interval: 'second' })
async function deleteData () {
// This call will throw if we request more than the maximum number of requests
// that were set in the constructor
// remainingRequests tells us how many additional requests could be sent
// right this moment
queries.forEach(function (query) {
client.fetch(query).then(function (documents) {
documents.forEach(function (document) {
client.delete(document._id).then(function (res) {
console.log('Document '.concat(document._id, ' deleted.'))
}).catch(function (err) {
console.error('Delete failed: ', err.message)
})
})
})
})
}
async function sendRequest () {
// This call will throw if we request more than the maximum number of requests
// that were set in the constructor
// remainingRequests tells us how many additional requests could be sent
// right this moment
const remainingRequests = await limiter.removeTokens(1)
deleteData()
}
sendRequest()