Created
March 6, 2024 23:13
-
-
Save r1tsuu/047008be9800dfcbe371247d10ee6794 to your computer and use it in GitHub Desktop.
Plugin collections docs order setup for existed collections.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dotenv from 'dotenv'; | |
import path from 'path'; | |
import payload from 'payload'; | |
dotenv.config({ | |
path: path.resolve(__dirname, '../.env'), | |
}); | |
const collections = [ | |
'products', | |
'product-collections', | |
'shops', | |
]; | |
const initPluginForExistingDocs = async () => { | |
// Initialize Payload | |
await payload.init({ | |
local: true, | |
secret: process.env.PAYLOAD_SECRET as string, | |
}); | |
payload.logger.info('Starting...'); | |
for (const slug of collections) { | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
const { docs } = await payload.find({ | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
collection: slug as any, | |
pagination: false, | |
sort: '-createdAt', | |
}); | |
await Promise.all( | |
docs.map(async (doc, index) => { | |
await payload.update({ | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
collection: slug as any, | |
data: { docOrder: index + 1 }, | |
id: doc.id, | |
}); | |
}), | |
); | |
} | |
payload.logger.info('Successfully inited'); | |
process.exit(0); | |
}; | |
initPluginForExistingDocs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment