What’s recommended way for seeding data with prism...
# orm-help
b
What’s recommended way for seeding data with prisma? I saw few scripts around. Feels like you need to do a lot of stuff by hand. I mean you have to truncate table , reset sequences? and then load data fresh?
1
c
Yes I'm in the middle of refactoring and writing hundreds or maybe thousands of lines of code for this right now. I did find this code for truncating all tables which saved me some trouble, note the schema name is hardcoded as the default `public`:
Copy code
const tablenames = await prisma.$queryRaw<
    Array<{ tablename: string }>
  >`SELECT tablename FROM pg_tables WHERE schemaname='public'`

  for (const { tablename } of tablenames) {
    if (tablename !== '_prisma_migrations') {
      try {
        await prisma.$executeRawUnsafe(
          `TRUNCATE TABLE "public"."${tablename}" CASCADE;`
        )
      } catch (error) {
        console.log({ error })
      }
    }
  }
n
Hey 👋 Here’s our official docs for seeding database, are you looking for any specific examples of seed scripts? General Idea is that as soon as you execute
migrate dev
or
migrate reset
commands, prisma would automatically execute your seed script.
b
@Nurul yeah specific examples, I would expect there to be general solution like putting seed for each table to folder with the same name or something
c
I use a folder for scripts for each environment (development, staging, etc.) then name the file the same as the table.
n
I see, I’ll submit this feedback to add specific examples of seeding in our official examples repository. We have some examples as shown in this repository here but they aren’t as detailed as you would like.