-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(generator-prisma-client): add aws-lambda-sst-esbuild #8284
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
Changes from all commits
8377df8
e629013
86aefe9
b433161
383ffc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # prod | ||
| dist/ | ||
| lambda.zip | ||
|
|
||
| # dev | ||
| .yarn/ | ||
| !.yarn/releases | ||
| .vscode/* | ||
| !.vscode/launch.json | ||
| !.vscode/*.code-snippets | ||
| .idea/workspace.xml | ||
| .idea/usage.statistics.xml | ||
| .idea/shelf | ||
|
|
||
| # deps | ||
| node_modules/ | ||
|
|
||
| # env | ||
| .env | ||
| .env.production | ||
|
|
||
| # logs | ||
| logs/ | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| pnpm-debug.log* | ||
| lerna-debug.log* | ||
|
|
||
| # misc | ||
| .DS_Store | ||
|
|
||
| # sst | ||
| .sst | ||
|
|
||
| # prisma | ||
| src/generated/ |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,168 @@ | ||||||||||||||||||||||
| # Prisma Postgres Example: AWS Lambda + SST + Node.js + ESM | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| This project showcases how to use the Prisma ORM with AWS Lambda and Prisma Postgres in an TypeScript ESM application. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Prerequisites | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| To successfully run the project, you will need the following: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - Two **Prisma Postgres** connection strings: | ||||||||||||||||||||||
| - Your **Prisma Postgres + Accelerate connection string** (containing your **Prisma API key**) which you can get by enabling Postgres in a project in your [Prisma Data Platform](https://pris.ly/pdp) account. You will use this connection string to run Prisma migrations. | ||||||||||||||||||||||
| - Your **Prisma Postgres direct TCP connection string** which you will use with Prisma Client. | ||||||||||||||||||||||
| Learn more in the [docs](https://www.prisma.io/docs/postgres/database/direct-connections). | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Tech Stack | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - SST 3 + AWS Lambda | ||||||||||||||||||||||
| - Runtime: Nodejs 20.x | ||||||||||||||||||||||
| - Bundler: esbuild (used via SST) | ||||||||||||||||||||||
| - `package.json` contains `{ "type": "module" }` | ||||||||||||||||||||||
| - Prisma Client with the `prisma-client` generator | ||||||||||||||||||||||
|
Comment on lines
+16
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Consistency: “Nodejs” → “Node.js”; clarify esbuild via SST. Tiny polish for correctness and consistency. -- SST 3 + AWS Lambda
- - Runtime: Nodejs 20.x
- - Bundler: esbuild (used via SST)
+- SST 3 + AWS Lambda
+ - Runtime: Node.js 20.x
+ - Bundler: esbuild (via SST)
- `package.json` contains `{ "type": "module" }`📝 Committable suggestion
Suggested change
🧰 Tools🪛 LanguageTool[grammar] ~16-~16: There might be a mistake here. (QB_NEW_EN) [grammar] ~17-~17: There might be a mistake here. (QB_NEW_EN_OTHER) [grammar] ~17-~17: There might be a mistake here. (QB_NEW_EN) [grammar] ~20-~20: There might be a mistake here. (QB_NEW_EN_OTHER) 🤖 Prompt for AI Agents |
||||||||||||||||||||||
| See the [Prisma schema file](./prisma/schema.prisma) for details. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```prisma | ||||||||||||||||||||||
| generator client { | ||||||||||||||||||||||
| provider = "prisma-client" | ||||||||||||||||||||||
| output = "../src/generated/prisma" | ||||||||||||||||||||||
| previewFeatures = ["driverAdapters", "queryCompiler"] | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| datasource db { | ||||||||||||||||||||||
| provider = "postgresql" | ||||||||||||||||||||||
| url = env("DATABASE_URL") | ||||||||||||||||||||||
| directUrl = env("DIRECT_URL") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Getting started | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 1. Clone the repository | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Clone the repository, navigate into it and install dependencies: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| git clone git@github.com:prisma/prisma-examples.git --depth=1 | ||||||||||||||||||||||
| cd prisma-examples/generator-prisma-client/aws-lambda-sst-esbuild | ||||||||||||||||||||||
| pnpm install | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 2. Configure environment variables | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Create a `.env` in the root of the project directory: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```bash | ||||||||||||||||||||||
| touch .env | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Now, open the `.env` file and set the `DATABASE_URL` environment variables with the values of your connection string and your Prisma Postgres connection string: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```bash | ||||||||||||||||||||||
jkomyno marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
| # .env | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Prisma Postgres connection string (used for migrations) | ||||||||||||||||||||||
| DATABASE_URL="__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Postgres connection string (used for queries by Prisma Client) | ||||||||||||||||||||||
| DIRECT_URL="__YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__" | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Note that `__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__` is a placeholder value that you need to replace with the values of your Prisma Postgres + Accelerate connection string. Notice that the Accelerate connection string has the following structure: `prisma+postgres://accelerate.prisma-data.net/?api_key=<api_key_value>`. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Note that `__YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__` is a placeholder value that you need to replace with the values of your Prisma Postgres direct TCP connection string. The direct connection string has the following structure: `postgres://<username>:<password>@<host>:<port>/<database>`. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 3. Run a migration to create the database structure and seed the database | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The [Prisma schema file](./prisma/schema.prisma) contains a single `Quotes` model and a `QuoteKind` enum. You can map this model to the database and create the corresponding `Quotes` table using the following command: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm prisma migrate dev --name init | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| You now have an empty `Quotes` table in your database. Next, run the [seed script](./prisma/seed.ts) to create some sample records in the table: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm prisma db seed | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 4. Generate Prisma Client | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Run: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm prisma generate | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 5. Run with SST | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - Ensure you set up your AWS Credentials and have the AWS CLI installed and logged in (see [sst docs](https://sst.dev/docs/aws-accounts)) | ||||||||||||||||||||||
| - Set up secrets in your SST project. You can do this by: | ||||||||||||||||||||||
| - Manually adding the `DIRECT_URL` connection string that you previously set in your `.env` file, via: | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm sst secrets add DIRECT_URL __YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__ | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| - Or, by loading the `.env` file first and piping it to the `sst secrets add` command: | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm dotenv -- bash -c 'pnpm sst secret set DIRECT_URL "${DIRECT_URL}"' | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
jkomyno marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
| - Run the following command to deploy the project to AWS Lambda using sst: | ||||||||||||||||||||||
nurul3101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm sst deploy | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| You should see an output similar to this: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```sh | ||||||||||||||||||||||
| ❯ pnpm sst deploy | ||||||||||||||||||||||
| SST 3.17.10 ready! | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ➜ App: prisma-client-aws-lambda-sst | ||||||||||||||||||||||
| Stage: prisma | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ~ Deploy | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| | Created aws-lambda sst:aws:Function → aws-lambdaCode aws:s3:BucketObjectv2 (2.3s) | ||||||||||||||||||||||
| | Created aws-lambda sst:aws:Function → aws-lambdaSourcemap0 aws:s3:BucketObjectv2 (6.1s) | ||||||||||||||||||||||
| | Updated aws-lambda sst:aws:Function → aws-lambdaFunction aws:lambda:Function (12.7s) | ||||||||||||||||||||||
| | Deleted aws-lambda sst:aws:Function → aws-lambdaCode aws:s3:BucketObjectv2 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ↗ Permalink https://sst.dev/u/<...> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ✓ Complete | ||||||||||||||||||||||
| prisma-client-aws-lambda-sst: https://<...>.lambda-url.us-east-1.on.aws/ | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ### 6. (Optional) Verify deployment size | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| You can check the size of your deployment package by running: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm build | ||||||||||||||||||||||
| du -sh ./dist/index.js | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| and then | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| pnpm zip | ||||||||||||||||||||||
| du -sh ./dist/lambda.zip | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| The output should look similar to: | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| ❯ du -sh ./dist/index.js | ||||||||||||||||||||||
| 3.0M ./dist/index.js | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ❯ du -sh ./dist/lambda.zip | ||||||||||||||||||||||
| 1.1M ./dist/lambda.zip | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## Resources | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| - [Prisma Postgres documentation](https://www.prisma.io/docs/postgres) | ||||||||||||||||||||||
| - Check out the [Prisma docs](https://www.prisma.io/docs) | ||||||||||||||||||||||
| - [Join our community on Discord](https://pris.ly/discord?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) to share feedback and interact with other users. | ||||||||||||||||||||||
| - [Subscribe to our YouTube channel](https://pris.ly/youtube?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for live demos and video tutorials. | ||||||||||||||||||||||
| - [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates. | ||||||||||||||||||||||
| - Report issues or ask [questions on GitHub](https://pris.ly/github?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section). | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "prisma-client-aws-lambda-sst", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "esbuild --bundle --outfile=./dist/index.js --format=esm --platform=node --target=node20 ./src/index.ts", | ||
jkomyno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "deploy": "sst deploy", | ||
| "zip": "zip -j dist/lambda.zip dist/index.js" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/aws-lambda": "8.10.152", | ||
| "dotenv": "^17.2.1", | ||
| "dotenv-cli": "^10.0.0", | ||
| "esbuild": "^0.21.4", | ||
| "npm-run-all2": "^6.2.0", | ||
| "prisma": "6.15.0-integration-fix-prisma-client-dirname-aws-lambda.3", | ||
| "tsx": "^4.20.4" | ||
| }, | ||
| "dependencies": { | ||
| "@prisma/adapter-pg": "6.15.0-integration-fix-prisma-client-dirname-aws-lambda.3", | ||
| "@prisma/client": "6.15.0-integration-fix-prisma-client-dirname-aws-lambda.3", | ||
| "sst": "3.17.10" | ||
| }, | ||
jkomyno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "packageManager": "pnpm@8.15.9+sha512.499434c9d8fdd1a2794ebf4552b3b25c0a633abcee5bb15e7b5de90f32f47b513aca98cd5cfd001c31f0db454bc3804edccd578501e4ca293a6816166bbd9f81" | ||
| } | ||
jkomyno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Grammar: “an TypeScript” → “a TypeScript”.
Minor article fix; also keeps the ESM statement consistent with the project.
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~3-~3: Use articles correctly
Context: ... with AWS Lambda and Prisma Postgres in an TypeScript ESM application. ## Prerequ...
(QB_NEW_EN_OTHER_ERROR_IDS_11)
[grammar] ~3-~3: Use correct spacing
Context: ...stgres in an TypeScript ESM application. ## Prerequisites To successfully run the p...
(QB_NEW_EN_OTHER_ERROR_IDS_5)
🤖 Prompt for AI Agents