-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathmigrateAuthors.js
More file actions
64 lines (53 loc) · 1.55 KB
/
migrateAuthors.js
File metadata and controls
64 lines (53 loc) · 1.55 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const payload = require('payload')
const path = require('path')
// eslint-disable-next-line
require('dotenv').config({
path: path.resolve(__dirname, '../../.env'),
})
const { MONGODB_URI, PAYLOAD_SECRET } = process.env
process.env.PAYLOAD_CONFIG_PATH = 'dist/payload.config.js'
const migrateStatus = async () => {
// Initialize Payload
// IMPORTANT: make sure your ENV variables are filled properly here
// as the below variable names are just for reference.
await payload.init({
secret: PAYLOAD_SECRET,
mongoURL: MONGODB_URI,
local: true,
})
const docs = await payload.find({
collection: 'posts',
depth: 0,
limit: 700,
})
await Promise.all(
docs.docs.map(async (doc) => {
const newAuthors = [
...(doc?.authors?.map((author) => {
return author && typeof author === 'object' ? author.id : author
}) || []),
doc.author && typeof doc.author === 'object' ? doc.author.id : doc.author,
].filter(Boolean)
if (newAuthors.length === 0) {
return
}
try {
await payload.update({
collection: 'posts',
id: doc.id,
data: {
...doc,
authors: newAuthors,
author: null,
},
})
payload.logger.info(`Success! Post with slug: '${doc.slug}' successfully migrated authors.`)
} catch (err) {
payload.logger.error(`Failed. Post with slug: '${doc.slug}' failed to migrate authors.`)
}
}),
)
payload.logger.info('Complete')
process.exit(0)
}
migrateStatus()