这是indexloc提供的服务,不要输入任何密码
Skip to content

Fixed prisma example to allow importing interfaces from database #2710

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

Conversation

mattpocock
Copy link
Contributor

@mattpocock mattpocock commented Nov 15, 2022

@mattpocock mattpocock requested a review from a team as a code owner November 15, 2022 15:01
@vercel
Copy link

vercel bot commented Nov 15, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
examples-designsystem-docs 🔄 Building (Inspect) Nov 15, 2022 at 3:02PM (UTC)
turbo-site ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Nov 15, 2022 at 3:02PM (UTC)
5 Ignored Deployments
Name Status Preview Comments Updated
examples-basic-web ⬜️ Ignored (Inspect) Nov 15, 2022 at 3:02PM (UTC)
examples-kitchensink-blog ⬜️ Ignored (Inspect) Nov 15, 2022 at 3:02PM (UTC)
examples-native-web ⬜️ Ignored (Inspect) Nov 15, 2022 at 3:02PM (UTC)
examples-nonmonorepo ⬜️ Ignored (Inspect) Nov 15, 2022 at 3:02PM (UTC)
examples-svelte-web ⬜️ Ignored (Inspect) Nov 15, 2022 at 3:02PM (UTC)

@github-actions github-actions bot added the area: examples Improvements or additions to examples label Nov 15, 2022
@github-actions
Copy link
Contributor

🟢 CI successful 🟢

Thanks

@javiercr
Copy link

javiercr commented Nov 15, 2022

That was fast 😳, thank you!

Also worth mentioning that right now the Using Prisma with Turborepo guide is recommending having database package that directly exports ./index.ts:

{
  "main": "./index.ts",
  "types": "./index.ts"
}

While the with-prisma example uses a tsup build step, exposing ./dist/index.mjs for main and /.dist/index.d.ts for types:

See with-prisma/packages/database/package.json

It's not clear to the reader (at least to me), what's the benefit of one approach over the other.

@dallenbaldwin
Copy link

dallenbaldwin commented Dec 21, 2022

I'm stumbling upon this tonight while trying to get build and start scripts to work for my server code (express backend)

I followed the guide in the docs, setting the main and types properties in my database package to ./index.ts

During development, everything is peachy, but the moment I need to just run the server in production mode, i get this

> server@1.0.0 start
> node dist/index.js

/home/.../packages/database/index.ts:1
export * from '@prisma/client';
^^^^^^

SyntaxError: Unexpected token 'export'

I went the tsup path and got build and start to work, but now can't get the dev script to work, which once again begs the question... how are we supposed to use prisma as an internal package during development and when it comes time to actually run the app in production? At least with an express backend.

Here's a sample of what I'm getting with my dev script in the server. I've followed almost everything from the example in the repo when it comes to the prisma code. I'm using nodemon src/index.ts for server:dev

server:dev: /home/.../node_modules/ts-node/src/index.ts:859
server:dev:     return new TSError(diagnosticText, diagnosticCodes, diagnostics);
server:dev:            ^
server:dev: TSError: ⨯ Unable to compile TypeScript:
server:dev: ../../packages/database/dist/index.js(6,20): error TS7006: Parameter 'to' implicitly has an 'any' type.
server:dev: ../../packages/database/dist/index.js(6,24): error TS7006: Parameter 'from' implicitly has an 'any' type.
...

It's also worth noting that I got a bunch of errors when doing anything other than export * from '@prisma/client' in packages/database/src/index.ts

@dallenbaldwin
Copy link

dallenbaldwin commented Jan 15, 2023

Is there any update on this?

I cannot get the latest version of Prisma to work in production if I follow the guide on the site and can't get it to work in development if I follow the with-prisma example in the examples.

Has anyone been able to get Prisma to work with an express backend and svelte+rollup frontend?

@dallenbaldwin
Copy link

I believe I might have found a solution that might be worth a try for those using a plain express backend.

Thanks to this article in the Fix module resolution for development and production section, I was able to make use of Node's conditional exports to resolve everything correctly for both dev and prod, while using as minimal a setup as the site's guide proposes.

If any of this could be done better, I'm all ears. I'm very much a novice with turborepo and monorepos in general.

packages/database/package.json

{
  "name": "database",
  "main": "./src/index.ts",
  "types": "./src/index.ts",
  "exports": {
    ".": {
      "prod": "./dist/index.js",
      "default": "./src/index.ts"
    }
  },
  "scripts": {
    "build": "tsc",
    ...
  }
  ...
}

packages/database/src/index.ts

export * from '@prisma/client';

apps/server/package.json

{
  ...
  "scripts": {
    "start": "node --conditions=prod dist/index.js",
    "build": "tsc",
    "dev": "nodemon src/index.ts"
  },
  ...
}

package.json

{
  "name": "root",
  "private": true,
  "workspaces": [
    "packages/*",
    "apps/*"
  ],
  "scripts": {
    "dev": "npm run up & npx turbo dev",
    "build": "npx turbo build",
    "start": "export NODE_ENV=production && npx turbo start",
    "up": "docker-compose up",
    ...
  },
  ...
}

turbo.json

{
  "$schema": "https://turbo.build/schema.json",
  "pipeline": {
    "start": {
      "cache": false,
      "dependsOn": [
        "^db:generate",
        "build"
      ]
    },
    "dev": {
      "dependsOn": [
        "^db:generate"
      ],
      "cache": false
    },
    "build": {
      "dependsOn": [
        "^db:generate",
        "^build"
      ],
      "outputs": [
        "dist/**",
        "build/**",
        "vender/**"
      ]
    },
    "db:generate": {
      "cache": false
    },
    "db:push": {
      "cache": false
    }
  }
}

@anthonyshew anthonyshew merged commit 59c54dc into main Jan 24, 2023
@anthonyshew anthonyshew deleted the matt/fixed-prisma-example-to-allow-importing-interfaces-from-database branch January 24, 2023 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: examples Improvements or additions to examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants