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

Add simple accelerate starter #6526

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
3 changes: 3 additions & 0 deletions accelerate/starter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
dist
85 changes: 85 additions & 0 deletions accelerate/starter/README.md
Original file line number Diff line number Diff line change
@@ -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
```

<details><summary><strong>Alternative:</strong> Clone the entire repo</summary>

Clone this repository:

```
git clone [email protected]:prisma/prisma-examples.git --depth=1
```

Install npm dependencies:

```
cd prisma-examples/typescript/starter
npm install
```

</details>

### 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)
33 changes: 33 additions & 0 deletions accelerate/starter/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
18 changes: 18 additions & 0 deletions accelerate/starter/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions accelerate/starter/src/index.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
},
});
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();
15 changes: 15 additions & 0 deletions accelerate/starter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
},
}