From 081a0d9a065e607cce5fac380ad602a36804cfa4 Mon Sep 17 00:00:00 2001 From: Petra Donka Date: Sat, 24 Aug 2024 11:25:45 +0200 Subject: [PATCH 1/2] add simple accelerate starter --- accelerate/starter/.gitignore | 3 + accelerate/starter/README.md | 85 +++++++++++++++++++++++++ accelerate/starter/package.json | 33 ++++++++++ accelerate/starter/prisma/schema.prisma | 18 ++++++ accelerate/starter/src/index.ts | 28 ++++++++ accelerate/starter/tsconfig.json | 15 +++++ 6 files changed, 182 insertions(+) create mode 100644 accelerate/starter/.gitignore create mode 100644 accelerate/starter/README.md create mode 100644 accelerate/starter/package.json create mode 100644 accelerate/starter/prisma/schema.prisma create mode 100644 accelerate/starter/src/index.ts create mode 100644 accelerate/starter/tsconfig.json diff --git a/accelerate/starter/.gitignore b/accelerate/starter/.gitignore new file mode 100644 index 000000000000..8f00ef230720 --- /dev/null +++ b/accelerate/starter/.gitignore @@ -0,0 +1,3 @@ +node_modules +.env +dist \ No newline at end of file diff --git a/accelerate/starter/README.md b/accelerate/starter/README.md new file mode 100644 index 000000000000..2bafac601385 --- /dev/null +++ b/accelerate/starter/README.md @@ -0,0 +1,85 @@ +# Prisma Accelerate Example: TypeScript Starter + +This repository has been created to help you get started with [Prisma Accelerate](https://prisma.io/accelerate). This project comes with a basic [`schema.prisma`](./prisma/schema.prisma) configured with PostgreSQL and an example operation found in the [`index.ts`](./index.ts) file. + +## Prerequisites + +To successfully run the project, you will need the following: + +- An Accelerate **connection string**. If you don't have one yet, you can get one in 2 minutes by signing up on console.prisma.io or following our [Getting Started guide](https://www.prisma.io/docs/accelerate/getting-started#1-enable-accelerate). + +## Getting started + +### 1. Download example and install dependencies + +Download this example: + +``` +npx try-prisma@latest --template typescript/starter +``` + +Install npm dependencies: + +``` +cd starter +npm install +``` + +
Alternative: Clone the entire repo + +Clone this repository: + +``` +git clone git@github.com:prisma/prisma-examples.git --depth=1 +``` + +Install npm dependencies: + +``` +cd prisma-examples/typescript/starter +npm install +``` + +
+ +### 2. Configure environment variables + +Create a `.env` in the root of the project directory: + +```bash +touch .env +``` + +Now, open the `.env` file and update the `DATABASE_URL` environment variables with the values of your connection string: + +```bash +# .env +DATABASE_URL="__ACCELERATE_CONNECTION_STRING_PLACEHOLDER__" +DIRECT_URL="__DIRECT_DATABASE_CONNECTION_STRING_PLACEHOLDER__" +``` + +Note that you need to replace the placeholder values with the actual values. + +### 3. Run a database migration to create the `User` table + +The Prisma schema file contains a single `User` model. You can map this model to the database and create the corresponding `User` table using the following command: + +```bash +npx prisma migrate dev --name init +``` + +You now have an empty `User` table in your database. + +### 4. Run a Prisma operation + +Run the [main script](./src/index.ts) + +```bash +npm run start +``` + +This will add a new user to the `User` table, and then run a simple query to fetch all users. + +## Resources + +- [Prisma Discord](https://pris.ly/discord) diff --git a/accelerate/starter/package.json b/accelerate/starter/package.json new file mode 100644 index 000000000000..dd145d1f16a6 --- /dev/null +++ b/accelerate/starter/package.json @@ -0,0 +1,33 @@ +{ + "name": "prisma-typescript-starter", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "scripts": { + "build": "tsc", + "start": "tsx src/index.ts", + "dev": "tsc --watch" + }, + "devDependencies": { + "@types/node": "22.5.0", + "nodemon": "3.1.4", + "prisma": "5.18.0", + "tsx": "4.17.0", + "typescript": "5.5.4" + }, + "dependencies": { + "@prisma/client": "5.18.0", + "@prisma/extension-accelerate": "^1.1.0" + }, + "prettier": { + "trailingComma": "all", + "tabWidth": 2, + "printWidth": 80, + "semi": true, + "singleQuote": true, + "jsxSingleQuote": false, + "jsxBracketSameLine": false, + "bracketSpacing": true, + "arrowParens": "always" + } +} diff --git a/accelerate/starter/prisma/schema.prisma b/accelerate/starter/prisma/schema.prisma new file mode 100644 index 000000000000..0ae298de7e2b --- /dev/null +++ b/accelerate/starter/prisma/schema.prisma @@ -0,0 +1,18 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema + +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") + directUrl = env("DIRECT_URL") +} + +model User { + id Int @id @default(autoincrement()) + name String + email String +} diff --git a/accelerate/starter/src/index.ts b/accelerate/starter/src/index.ts new file mode 100644 index 000000000000..3ce291f1fead --- /dev/null +++ b/accelerate/starter/src/index.ts @@ -0,0 +1,28 @@ +import { PrismaClient } from '@prisma/client'; +import { withAccelerate } from '@prisma/extension-accelerate' + +const prisma = new PrismaClient().$extends(withAccelerate()) + +async function main() { + const newUser = await prisma.user.create({ + data: { + name: 'Percy Prisma', + email: 'percy@prisma.io', + }, + }); + console.log('The new user:', newUser); + + const users = await prisma.user.findMany({ + cacheStrategy: { ttl: 60 }, + }); + console.log('All users:', users); + + const cachedUsers = await prisma.user.findMany({ + cacheStrategy: { ttl: 60 }, + }).withAccelerateInfo(); + console.log('Cached users:', cachedUsers); + + // See more examples of how to use Prisma Accelerate: https://www.prisma.io/docs/accelerate/examples +} + +main(); diff --git a/accelerate/starter/tsconfig.json b/accelerate/starter/tsconfig.json new file mode 100644 index 000000000000..a2b8c6f0b7c9 --- /dev/null +++ b/accelerate/starter/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "rootDir": "./src", + "outDir": "./dist", + "strict": true, + "esModuleInterop": true, + "typeRoots": [ + "./node_modules/@types" + ], + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true + }, +} From 2cef8fe3c034019d5c197f39f8220e9fba22aeae Mon Sep 17 00:00:00 2001 From: Petra Donka Date: Sat, 24 Aug 2024 11:27:16 +0200 Subject: [PATCH 2/2] add accelerate starter to main readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 730bc9e36303..80ab76a4f3ed 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ The [`accelerate`](./accelerate) folder contains examples of projects using [Pri | Demo | Description | | ----------------------------------------------- | -------------------------------------------------------------------------- | +| [`starter`](./accelerate/starter) | A simple starter project using Prisma Accelerate's caching and connection pooling | | [`nextjs-starter`](./accelerate/nextjs-starter) | A Next.js project using Prisma Accelerate's caching and connection pooling | | [`svelte-starter`](./accelerate/svelte-starter/) | A SvelteKit project using Prisma Accelerate's caching and connection pooling | | [`solidstart-starter`](./accelerate/solidstart-starter/) | A Solidstart project using Prisma Accelerate's caching and connection pooling |