Skip to content

Instantly share code, notes, and snippets.

@r1tsuu
Created March 6, 2024 23:13
Show Gist options
  • Save r1tsuu/047008be9800dfcbe371247d10ee6794 to your computer and use it in GitHub Desktop.
Save r1tsuu/047008be9800dfcbe371247d10ee6794 to your computer and use it in GitHub Desktop.
Plugin collections docs order setup for existed collections.
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