Skip to content

Commit f4cc98b

Browse files
committed
JavaScript/Prisma: Add example project
The code has been scaffolded like this: npx try-prisma@latest --template javascript/script --install npm --name . --path . This commit adds the result unmodified.
1 parent 3e615e9 commit f4cc98b

File tree

6 files changed

+338
-0
lines changed

6 files changed

+338
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Simple Node.js Script Example
2+
3+
This example shows how to use [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) in a **simple Node.js script** to read and write data in a SQLite database. You can find the database file with some dummy data at [`./prisma/dev.db`](./prisma/dev.db).
4+
5+
## Getting started
6+
7+
### 1. Download example and install dependencies
8+
9+
Download this example:
10+
11+
```
12+
npx try-prisma@latest --template javascript/script
13+
```
14+
15+
Install npm dependencies:
16+
```
17+
cd script
18+
npm install
19+
```
20+
21+
<details><summary><strong>Alternative:</strong> Clone the entire repo</summary>
22+
23+
Clone this repository:
24+
25+
```
26+
git clone git@github.com:prisma/prisma-examples.git --depth=1
27+
```
28+
29+
Install npm dependencies:
30+
31+
```
32+
cd prisma-examples/javascript/script
33+
npm install
34+
```
35+
36+
</details>
37+
38+
### 2. Create the database
39+
40+
Run the following command to create your SQLite database file. This also creates the `User` and `Post` tables that are defined in [`prisma/schema.prisma`](./prisma/schema.prisma):
41+
42+
```
43+
npx prisma migrate dev --name init
44+
```
45+
46+
### 3. Run the script
47+
48+
Execute the script with this command:
49+
50+
```
51+
npm run dev
52+
```
53+
54+
## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB)
55+
56+
If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block.
57+
58+
Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls).
59+
60+
<details><summary>Expand for an overview of example configurations with different databases</summary>
61+
62+
### PostgreSQL
63+
64+
For PostgreSQL, the connection URL has the following structure:
65+
66+
```prisma
67+
datasource db {
68+
provider = "postgresql"
69+
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
70+
}
71+
```
72+
73+
Here is an example connection string with a local PostgreSQL database:
74+
75+
```prisma
76+
datasource db {
77+
provider = "postgresql"
78+
url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"
79+
}
80+
```
81+
82+
### MySQL
83+
84+
For MySQL, the connection URL has the following structure:
85+
86+
```prisma
87+
datasource db {
88+
provider = "mysql"
89+
url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"
90+
}
91+
```
92+
93+
Here is an example connection string with a local MySQL database:
94+
95+
```prisma
96+
datasource db {
97+
provider = "mysql"
98+
url = "mysql://janedoe:mypassword@localhost:3306/notesapi"
99+
}
100+
```
101+
102+
### Microsoft SQL Server
103+
104+
Here is an example connection string with a local Microsoft SQL Server database:
105+
106+
```prisma
107+
datasource db {
108+
provider = "sqlserver"
109+
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
110+
}
111+
```
112+
113+
### MongoDB
114+
115+
Here is an example connection string with a local MongoDB database:
116+
117+
```prisma
118+
datasource db {
119+
provider = "mongodb"
120+
url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
121+
}
122+
```
123+
124+
</details>
125+
126+
## Next steps
127+
128+
- Check out the [Prisma docs](https://www.prisma.io/docs)
129+
- Share your feedback in the [`#product-wishlist`](https://prisma.slack.com/messages/CKQTGR6T0/) channel on the [Prisma Slack](https://slack.prisma.io/)
130+
- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/)
131+
- Watch our biweekly "What's new in Prisma" livestreams on [Youtube](https://www.youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w)

by-language/javascript-prisma/package-lock.json

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "script",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"dev": "node ./script.js"
7+
},
8+
"dependencies": {
9+
"@prisma/client": "5.6.0"
10+
},
11+
"devDependencies": {
12+
"prisma": "5.6.0"
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
}
4+
5+
datasource db {
6+
provider = "sqlite"
7+
url = "file:./dev.db"
8+
}
9+
10+
model User {
11+
id Int @id @default(autoincrement())
12+
email String @unique
13+
name String?
14+
posts Post[]
15+
}
16+
17+
model Post {
18+
id Int @id @default(autoincrement())
19+
title String
20+
content String?
21+
published Boolean @default(false)
22+
author User? @relation(fields: [authorId], references: [id])
23+
authorId Int?
24+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const { PrismaClient } = require('@prisma/client')
2+
3+
const prisma = new PrismaClient()
4+
5+
// A `main` function so that we can use async/await
6+
async function main() {
7+
// Seed the database with users and posts
8+
const user1 = await prisma.user.create({
9+
data: {
10+
email: 'alice@prisma.io',
11+
name: 'Alice',
12+
posts: {
13+
create: {
14+
title: 'Watch the talks from Prisma Day 2019',
15+
content: 'https://www.prisma.io/blog/z11sg6ipb3i1/',
16+
published: true,
17+
},
18+
},
19+
},
20+
include: {
21+
posts: true,
22+
},
23+
})
24+
const user2 = await prisma.user.create({
25+
data: {
26+
email: 'bob@prisma.io',
27+
name: 'Bob',
28+
posts: {
29+
create: [
30+
{
31+
title: 'Subscribe to GraphQL Weekly for community news',
32+
content: 'https://graphqlweekly.com/',
33+
published: true,
34+
},
35+
{
36+
title: 'Follow Prisma on Twitter',
37+
content: 'https://twitter.com/prisma/',
38+
published: false,
39+
},
40+
],
41+
},
42+
},
43+
include: {
44+
posts: true,
45+
},
46+
})
47+
console.log(
48+
`Created users: ${user1.name} (${user1.posts.length} posts) and ${user2.name} (${user2.posts.length} posts) `,
49+
)
50+
// Retrieve all published posts
51+
const allPosts = await prisma.post.findMany({
52+
where: { published: true },
53+
})
54+
console.log(`Retrieved all published posts: `, allPosts)
55+
56+
// Create a new post (written by an already existing user with email alice@prisma.io)
57+
const newPost = await prisma.post.create({
58+
data: {
59+
title: 'Join the Prisma Slack community',
60+
content: 'http://slack.prisma.io',
61+
published: false,
62+
author: {
63+
connect: {
64+
email: 'alice@prisma.io', // Should have been created during initial seeding
65+
},
66+
},
67+
},
68+
})
69+
console.log(`Created a new post: `, newPost)
70+
71+
// Publish the new post
72+
const updatedPost = await prisma.post.update({
73+
where: {
74+
id: newPost.id,
75+
},
76+
data: {
77+
published: true,
78+
},
79+
})
80+
console.log(`Published the newly created post: `, updatedPost)
81+
82+
// Retrieve all posts by user with email alice@prisma.io
83+
const postsByUser = await prisma.user
84+
.findUnique({
85+
where: {
86+
email: 'alice@prisma.io',
87+
},
88+
})
89+
.posts()
90+
console.log(`Retrieved all posts from a specific user: `, postsByUser)
91+
}
92+
93+
main()
94+
.then(async () => {
95+
await prisma.$disconnect()
96+
})
97+
.catch(async (e) => {
98+
console.error(e)
99+
await prisma.$disconnect()
100+
process.exit(1)
101+
})

0 commit comments

Comments
 (0)