Skip to content

EPIC: re-design of db seed feature #8732

Closed

Description

@Jolg42

prisma db seed is currently a Preview feature.

During this phase, we noticed different issues caused by the complexity of the current implementation.

The Migrations Team decided to remove the complexity and make it a lot easier to understand for users and to maintain.

The new implementation is in #8146

How it will work is that a prisma.seed property in the package.json of your project will be required if you want to use prisma db seed.

The CLI will give examples that can be copy-pasted.

If you are currently using the previous implementation of prisma db seed and upgrade to this new version then it will throw an error with an upgrade help message which should be easy to fix (add prisma.seed to the package.json)

package.json example for a JavaScript project

  "prisma": {
    "seed": "node prisma/seed.js"
  } 

package.json example for a TypeScript project

  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  }

Example of a prisma/seed.js

async function main() {
  await new Promise((resolve) => setTimeout(resolve, 100))
  console.log('Hello from seed.js')
}

main()
  .then(() => console.log('Goodbye from seed.js'))
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })

What needs to done for releasing the "new" db seed:

Activity

  1. added
    kind/feature
    tech/typescript
    topic: prisma db seed
    on Aug 13, 2021
  2. added this to the 2.31.0 / 3.0.x milestone on Aug 13, 2021
  3. self-assigned this
    on Aug 13, 2021
  4. Jolg42 commented on Aug 13, 2021

    @Jolg42
    ContributorAuthor

    It's possible to try it out with a specific version of the Prisma CLI like this:

    npm install -D [email protected]
    
    # Then run 
    prisma db seed --preview-feature
    
    # db seed is also integrated in
    prisma migrate reset
    prisma migrate dev
    

    Or by using npx with the specific version:

    npx [email protected] db seed --preview-feature
    
  5. linked a pull request that will close this issuefix(db seed): redesign of db seed #8146on Aug 13, 2021
  6. jasonkuhrt commented on Aug 13, 2021

    @jasonkuhrt
    Contributor

    Note this will work and is cleaner IMO:

    "prisma": {
        "seed": "ts-node prisma/seed"
      }
  7. tobiasdiez commented on Aug 21, 2021

    @tobiasdiez

    May I suggest that the error handling part (copied from the above example)

    main()
      .then(() => console.log('Goodbye from seed.js'))
      .catch((e) => {
        console.error(e)
        process.exit(1)
      })
    

    may be moved to Prisma's cli code, i.e one only exports the main method and prisma takes care of the invocation. The main reason is that sometimes you do not want to exit in case there is an error during seeding. For example, if one uses generate --watch or jest --watch using seed to initialize tests. In the former case you prefer to be notified about the error, and in the latter the test should fail -- however, you don't really want to exit the calling process.

  8. Jolg42 commented on Aug 24, 2021

    @Jolg42
    ContributorAuthor

    Hi @tobiasdiez indeed, exiting is not the best in some cases. This is just an example and in this implementation you are free to change it as you need.
    We removed the "magic" around export and invocation to simplify but that also means you can write a small layer that does that in your project.

  9. reopened this on Sep 1, 2021
  10. Jolg42 commented on Sep 7, 2021

    @Jolg42
    ContributorAuthor

    Closing, rest is tracked in #8628

    I'll create an issue to have better suggestion for ESM module projects help output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/featuretech/typescripttopic: prisma db seed

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions