Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions generator-prisma-client/aws-lambda-sst-esbuild/.gitignore
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/
168 changes: 168 additions & 0 deletions generator-prisma-client/aws-lambda-sst-esbuild/README.md
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.
Copy link

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.

-This project showcases how to use the Prisma ORM with AWS Lambda and Prisma Postgres in an TypeScript ESM application.
+This project showcases how to use the Prisma ORM with AWS Lambda and Prisma Postgres in a TypeScript ESM application.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
This project showcases how to use the Prisma ORM with AWS Lambda and Prisma Postgres in an TypeScript ESM application.
This project showcases how to use the Prisma ORM with AWS Lambda and Prisma Postgres in a TypeScript ESM application.
🧰 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
In generator-prisma-client/aws-lambda-sst-esbuild/README.md around line 3, the
phrase "an TypeScript ESM application" uses the incorrect article; change "an"
to "a" so it reads "a TypeScript ESM application" to fix the grammar and keep
the ESM statement consistent.


## 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
Copy link

Choose a reason for hiding this comment

The 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- 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
- SST 3 + AWS Lambda
- Runtime: Node.js 20.x
- Bundler: esbuild (via SST)
- `package.json` contains `{ "type": "module" }`
- Prisma Client with the `prisma-client` generator
🧰 Tools
🪛 LanguageTool

[grammar] ~16-~16: There might be a mistake here.
Context: ...s). ## Tech Stack - SST 3 + AWS Lambda - Runtime: Nodejs 20.x - Bundler: esbuil...

(QB_NEW_EN)


[grammar] ~17-~17: There might be a mistake here.
Context: ...tack - SST 3 + AWS Lambda - Runtime: Nodejs 20.x - Bundler: esbuild (used via SST...

(QB_NEW_EN_OTHER)


[grammar] ~17-~17: There might be a mistake here.
Context: ... 3 + AWS Lambda - Runtime: Nodejs 20.x - Bundler: esbuild (used via SST) - `pac...

(QB_NEW_EN)


[grammar] ~20-~20: There might be a mistake here.
Context: ...lient with the prisma-client generator See the [Prisma schema file](./prisma/sc...

(QB_NEW_EN_OTHER)

🤖 Prompt for AI Agents
In generator-prisma-client/aws-lambda-sst-esbuild/README.md around lines 16 to
20, update the wording for consistency and clarity: change "Nodejs 20.x" to
"Node.js 20.x" and rephrase the esbuild line to make clear that esbuild is used
via SST (e.g., "Bundler: esbuild (invoked by SST)") while retaining the note
about package.json containing { "type": "module" } and the Prisma Client
generator mention.

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
```

### 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
# .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}"'
```
- Run the following command to deploy the project to AWS Lambda using sst:

```
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).
24 changes: 24 additions & 0 deletions generator-prisma-client/aws-lambda-sst-esbuild/package.json
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",
"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"
},
"packageManager": "pnpm@8.15.9+sha512.499434c9d8fdd1a2794ebf4552b3b25c0a633abcee5bb15e7b5de90f32f47b513aca98cd5cfd001c31f0db454bc3804edccd578501e4ca293a6816166bbd9f81"
}
Loading
Loading