-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsingle-to-carousel.ts
50 lines (40 loc) · 1.17 KB
/
single-to-carousel.ts
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
48
49
50
/* eslint-disable no-console */
import { defineQuery } from 'next-sanity';
import createClient from '../../client';
const client = createClient();
const fetchDocuments = async () => {
const fetchDocumentsQuery = defineQuery(`*[_type == "project"]`);
const documents = await client.fetch(fetchDocumentsQuery);
return documents;
};
const main = async () => {
const documents = await fetchDocuments();
if (documents.length === 0) {
console.warn('No documents to convert!');
return;
}
const mutations = documents.map(doc => {
console.info(`Migrating document - id: ${doc._id}`);
return {
id: doc._id,
patch: {
id: doc._id,
set: {
projectImages: doc?.projectImage
? [{ ...doc.projectImage, _key: crypto.randomUUID() }]
: [],
},
unset: ['projectImage'],
},
};
});
const transactions = mutations.reduce((tx, mutation) => {
return tx.patch(mutation.id, mutation.patch);
}, client.transaction());
await transactions.commit();
console.log('Migration complete!');
};
main().catch((err: any) => {
console.error(JSON.stringify(err, null, 2));
process.exit(1);
});