Skip to content

Commit df30ac4

Browse files
authored
feat: Add example showcasing Prisma ORM with Turso database (#7497)
* feat: Add example showcasing Prisma ORM with Turso database * rename folder to turso * Update turso reference
1 parent f6cecde commit df30ac4

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed

databases/turso/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
# Keep environment variables out of version control
3+
.env

databases/turso/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Prisma with Turso Example
2+
3+
This example demonstrates how to use [Prisma ORM](https://www.prisma.io/) with [Turso](https://turso.tech/), a distributed database based on libSQL. The project showcases how to perform CRUD operations using [Prisma Client](https://www.prisma.io/docs/orm/prisma-client) in a **Node.js script**.
4+
5+
## How to use
6+
7+
### 1. Download example and navigate into the project directory
8+
9+
Download this example:
10+
11+
```
12+
npx try-prisma@latest --template databases/turso
13+
```
14+
15+
Then, navigate into the project directory:
16+
17+
```
18+
cd turso
19+
```
20+
21+
<details><summary><strong>Alternative:</strong> Clone the entire repo</summary>
22+
23+
Clone this repository:
24+
25+
```
26+
git clone [email protected]:prisma/prisma-examples.git --depth=1
27+
```
28+
29+
### 2. Install npm dependencies:
30+
31+
```
32+
cd prisma-examples/databases/turso
33+
npm install
34+
```
35+
36+
</details>
37+
38+
### 3. Configure the database connection
39+
Create a .env file in the root of the project and add your Turso credentials:
40+
```sh
41+
touch .env
42+
```
43+
If you don't have an existing database, you can provision a database by running the following command:
44+
```sh
45+
turso db create turso-prisma-db
46+
```
47+
The above command will create a database in the closest region to your location.
48+
49+
Run the following command to retrieve your database's connection string:
50+
```sh
51+
turso db show turso-prisma-db
52+
```
53+
Next, create an authentication token that will allow you to connect to the database:
54+
```sh
55+
turso db tokens create turso-prisma-db
56+
```
57+
Update your .env file with the authentication token and connection string:
58+
```sh
59+
TURSO_DATABASE_URL="your_turso_database_url"
60+
TURSO_AUTH_TOKEN="your_turso_auth_token"
61+
```
62+
You can learn more from the [Prisma Turso documentation](https://www.prisma.io/docs/orm/overview/databases/turso#what-is-turso)
63+
64+
### 4. Generate Prisma Client
65+
```sh
66+
npx prisma generate
67+
```
68+
### 5. Run the script
69+
Execute the script to create, retrieve, update, and delete data:
70+
```sh
71+
npm run dev
72+
```
73+
### 6. Understanding the project structure
74+
- `prisma/schema.prisma`: Defines the database schema for Users and Posts.
75+
- `src/script.ts`: A script demonstrating CRUD operations using Prisma Client.
76+
- `.env`: Stores environment variables for database credentials.

databases/turso/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "turso",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"dev": "ts-node src/script.ts"
7+
},
8+
"keywords": [],
9+
"author": "",
10+
"license": "ISC",
11+
"description": "",
12+
"devDependencies": {
13+
"@types/node": "^22.13.1",
14+
"prisma": "^6.3.1",
15+
"ts-node": "^10.9.2",
16+
"typescript": "^5.7.3"
17+
},
18+
"dependencies": {
19+
"@libsql/client": "^0.8.1",
20+
"@prisma/adapter-libsql": "^6.3.1",
21+
"@prisma/client": "^6.3.1",
22+
"dotenv": "^16.4.7"
23+
}
24+
}

databases/turso/prisma/schema.prisma

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6+
7+
generator client {
8+
provider = "prisma-client-js"
9+
previewFeatures = ["driverAdapters", "typedSql"]
10+
}
11+
12+
datasource db {
13+
provider = "sqlite"
14+
url = "file:./dev.db"
15+
}
16+
17+
model User {
18+
id Int @id @default(autoincrement())
19+
email String @unique
20+
name String?
21+
posts Post[]
22+
}
23+
24+
model Post {
25+
id Int @id @default(autoincrement())
26+
createdAt DateTime @default(now())
27+
updatedAt DateTime @updatedAt
28+
title String
29+
content String?
30+
published Boolean @default(false)
31+
viewCount Int @default(0)
32+
author User? @relation(fields: [authorId], references: [id])
33+
authorId Int?
34+
}

databases/turso/src/script.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require('dotenv').config()
2+
import { PrismaClient } from '@prisma/client'
3+
import { PrismaLibSQL } from '@prisma/adapter-libsql'
4+
import { createClient } from '@libsql/client'
5+
6+
7+
const libsql = createClient({
8+
url: `${process.env.TURSO_DATABASE_URL}`,
9+
authToken: `${process.env.TURSO_AUTH_TOKEN}`,
10+
})
11+
12+
const adapter = new PrismaLibSQL(libsql)
13+
const prisma = new PrismaClient({ adapter })
14+
15+
async function main() {
16+
// create user
17+
const user = await prisma.user.create({
18+
data: {
19+
20+
name: 'Hana',
21+
},
22+
})
23+
24+
console.log(user)
25+
26+
// find user
27+
const foundUser = await prisma.user.findUnique({
28+
where: {
29+
30+
}
31+
})
32+
console.log(foundUser)
33+
34+
// update user
35+
const updatedUser = await prisma.user.update({
36+
where: {
37+
38+
},
39+
data: {
40+
name: 'Peters',
41+
},
42+
})
43+
console.log(updatedUser)
44+
45+
// delete user
46+
const deleteUser = await prisma.user.delete({
47+
where: {
48+
49+
}
50+
})
51+
console.log("User deleted", deleteUser)
52+
53+
// create user with posts
54+
const userPosts = await prisma.user.create({
55+
data: {
56+
57+
name: 'David',
58+
posts: {
59+
create: [
60+
{ title: 'Hello world' },
61+
{ title: 'Introduction to Prisma with Mongo' },
62+
{ title: 'MongoDB and schemas' },
63+
],
64+
},
65+
},
66+
include: {
67+
posts: true,
68+
},
69+
})
70+
71+
console.dir(userPosts, { depth: Infinity })
72+
73+
// find posts
74+
const posts = await prisma.post.findMany({
75+
include: {
76+
author: true,
77+
},
78+
orderBy: {
79+
createdAt: 'desc',
80+
},
81+
})
82+
83+
console.dir(posts, { depth: Infinity })
84+
}
85+
86+
87+
88+
89+
main()
90+
.catch(e => {
91+
throw e
92+
})
93+
.finally(async () => {
94+
await prisma.$disconnect()
95+
})

databases/turso/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"sourceMap": true,
4+
"outDir": "dist",
5+
"strict": true,
6+
"lib": [
7+
"esnext",
8+
"dom"
9+
],
10+
"esModuleInterop": true
11+
}
12+
}

0 commit comments

Comments
 (0)