I have like 18k images that I'd like to import (just into the db, images are stored within the cdn), therefore basically like to create 18k db entries only.
I'm wondering how I use
drizzle
or
payload.db.tables
to do that
I have seen
https://payloadcms.com/docs/database/postgresbut I don't get it because drizzle says sth like
await db.insert(users).values([{ name: 'Andrew' }, { name: 'Dan' }]);
(example from here:
https://orm.drizzle.team/docs/insert#insert-multiple-rows)
So where do I get the users model? (or in my case the media model?)
It looks like we may be missing some type inference, but you should be able to access the table by passing
payload.db.tables['media'])
Yes! That was the missing part!
const chunkArray = (array, chunkSize) => {
const chunks = [];
for (let i = 0; i < array.length; i += chunkSize) {
chunks.push(array.slice(i, i + chunkSize));
}
return chunks;
};
const doAction = async () => {
await payload.init({
secret: PAYLOAD_SECRET,
local: true,
});
const toImport = images.map((image) => ({
createdAt: image.created_at,
updatedAt: image.updated_at || image.created_at,
alt: image.alt || "",
caption: image.caption || "",
url: image.url || "",
filename: image.image || "",
mime_type: image.type || "",
}));
const chunks = chunkArray(toImport, 500);
for (const chunk of chunks) {
await payload.db.drizzle.insert(payload.db.tables["media"]).values(chunk);
}
};
doAction();
@wiesson Amazing 🙌
If I have time, I could do a PR with extended documentation how to import data
That would be much appreciated 👍 Probably in this area of the docs
https://github.com/payloadcms/payload/blob/main/docs/database/postgres.mdx#L46I'm basically replacing my old Django app with Payload and I have exportet all data as JSON and I'm now writing migration scripts to add the data to the DB
Wow, sounds like a good amount of work. Looks like you're well on your way, though.
btw, I wanted to use
await payload.create({ collection: "media", data: { ... data here ... } })
but I don't want to upload the file, because it's already stored inside the CDN
Hmm, yeah that's tricky.
Could do an
update
after the fact, but on creation I believe you need to pass the asset
One question - I'm using the
url
field as my CDN url. But it gets overwritten somehow when I try to display the thumbnailImage
I assume that payload tries to create the URL for me (based on my static url + filename?)
Besides that, 18532 images imported ✅ 🙂
Star
Discord
online
Get dedicated engineering support directly from the Payload team.