I am studying to be a front-end developer, and for my exam
project we are using Strapi to create an api that we are to fetch on our
website.
I am combining the exam with making a new website for my uncles who have
an online antique bookstore.
I have managed to import the old MySQL database for their store
containing all of the books (more than 15thousand titles) to strapi, and
have them all in a content-type list.
My problem is that all of the titles are saved as drafts⌠And I cannot
manage to find any way to bulk publish them. (Needless to say -
publishing <15000 titles one by one will be rather time consuming).
Is there any way to publish several drafts at once in strapi?
Hope, and crossing my fingers, there is some way to do this, and that
maybe someone here can help me with how.
Iâve faced this same problem. And yes 15000 records is alot to edit one by one! Currently you canât bulk update via the Admin UI but you can do it programatically. The solution is to set the published_at field to a date for all the records you want to publish. Those records will then appear as âpublishedâ in the Admin UI and API.
Iâm assuming youâve already got a good handle on SQL already since you were able to import 15000 records. If not, have a look at a database admin tool to help you bulk update the published_at field in your âbooksâ table. DBeaver is an example of one and with it you can perform SQL operations via a GUI or you can use SQL directly.
If you want to do it within your strapi code instead, you can write a small function that runs an SQL update query once within your strapi app just before it starts. You can use knex to build your SQL query (itâs already packaged with strapi) e.g.
async function bulkPublishBooks() {
const knex = strapi.connections.default
const updated = await knex.query('books')
.update('published_at', new Date().toISOString())
.whereNull('published_at')
}
where âbooksâ is the name of the MySQL table that has all your book records. Place the above code inside bootstrap.js and restart the server.
NB: I havenât tested the code snippet above as I wrote it from memory but the general idea is there.
Hi there, thank you so, so much for this!
I messed things up trying to go about this on my own, so now I have deleted the whole project, and will try to start everything all over tomorrow from scratch⌠I tried updating strapi and ended up with user/permissions js errors which I couldnât manage to solve (hopefully deleting absolutely everything and starting a completely new project will help me avoid some of my previous mistakes ).
Can I ask you, maybe you know this: If I disable the draft/publish setting in content types in my new project before I import the books, will all the books then be set to published by default when I import them (so I kind of avoid the whole issue with draft/publish?)
Regardless, I am really so very grateful for you taking the time to try to help me whit my problem!
I canât say Iâve tried, but I would suggest you delete the published_at column from your export before you import it again. (After you disable the draft/publish)
Just a quick update - when I created the new strapi project I disabled the draft/publish function when I created the content type, and this made all the items in the database appear as published. Thus this has so far worked as a workaround so that I not had to publish the drafts one by one.
I did not try @jpizzle34âs solution from above, so I cannot say if that works (but I saw a video tutorial with strapi suggesting a similar solution to his - using published_at; Schedule publication - YouTube) so I would believe that might be a solution as well.
Thanks for the amazing solution and info about the bulk update. I donât use SQL a lot (learned well but havenât used it in the last 2 years) but I can manage my way around. I am use Jetbrains DataGrip. Can you provide a little more elaboration on updating it via SQL ?
Iâm probably a bit late to the party, but I just used this method to bulk update in Strapi v4. Thereâs a bootstrap function in index.js. I created a new field (âhtmlIdâ) in my faq content-type (for search functionality) so that I can add an anchor tag to each of my faq articles. I used the entityService available in the api controllers to generate an âhtmlIdâ programmatically: