Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPIC: re-design of db seed feature #8732

Closed
5 of 7 tasks
Tracked by #8628
Jolg42 opened this issue Aug 13, 2021 · 5 comments · Fixed by #8146
Closed
5 of 7 tasks
Tracked by #8628

EPIC: re-design of db seed feature #8732

Jolg42 opened this issue Aug 13, 2021 · 5 comments · Fixed by #8146
Assignees
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. tech/typescript Issue for tech TypeScript. topic: prisma db seed CLI: prisma db seed

Comments

@Jolg42
Copy link
Member

Jolg42 commented Aug 13, 2021

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:

@Jolg42 Jolg42 added kind/feature A request for a new feature. tech/typescript Issue for tech TypeScript. team/schema Issue for team Schema. topic: prisma db seed CLI: prisma db seed labels Aug 13, 2021
@Jolg42 Jolg42 added this to the 2.31.0 / 3.0.x milestone Aug 13, 2021
@Jolg42 Jolg42 self-assigned this Aug 13, 2021
@Jolg42
Copy link
Member Author

Jolg42 commented Aug 13, 2021

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

npm install -D prisma@2.30.0-integration-db-seed-new-behavior.1

# 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 prisma@2.30.0-integration-db-seed-new-behavior.1 db seed --preview-feature

@jasonkuhrt
Copy link
Member

Note this will work and is cleaner IMO:

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

@tobiasdiez
Copy link

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.

@Jolg42
Copy link
Member Author

Jolg42 commented Aug 24, 2021

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.

@Jolg42
Copy link
Member Author

Jolg42 commented Sep 7, 2021

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
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. tech/typescript Issue for tech TypeScript. topic: prisma db seed CLI: prisma db seed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants