Skip to content

Commit f749dbb

Browse files
committed
Initial commit
0 parents  commit f749dbb

12 files changed

+10017
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
yarn-error.log
3+
dist
4+
build
5+
.DS_Store
6+
examples
7+
tmp
8+
pnpm-debug.log
9+
sandbox
10+
*.tsbuildinfo
11+
# Local Netlify folder
12+
.netlify

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Prisma Deployment Example - Netlify

functions/createUser.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { PrismaClient, PrismaClientRequestError } = require('@prisma/client')
2+
const prisma = new PrismaClient()
3+
4+
exports.handler = async (event, context, callback) => {
5+
try {
6+
const data = JSON.parse(event.body)
7+
const createdUser = await prisma.user.create({ data })
8+
9+
return {
10+
statusCode: 200,
11+
headers: { 'Content-Type': 'application/json' },
12+
body: JSON.stringify(createdUser)
13+
}
14+
} catch (e) {
15+
if (e instanceof PrismaClientRequestError) {
16+
if (e.code === 'P2002') {
17+
return {
18+
statusCode: 409,
19+
headers: { 'Content-Type': 'application/json' },
20+
body: JSON.stringify({
21+
error: 'A user with this email already exists'
22+
})
23+
}
24+
}
25+
}
26+
27+
console.error(e)
28+
return { statusCode: 500 }
29+
}
30+
}

functions/getPosts.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { PrismaClient } = require('@prisma/client')
2+
3+
const prisma = new PrismaClient()
4+
5+
exports.handler = async (event, context, callback) => {
6+
try {
7+
const posts = await prisma.post.findMany({
8+
include: { author_id: true }
9+
})
10+
return {
11+
statusCode: 200,
12+
headers: { 'Content-Type': 'application/json' },
13+
body: JSON.stringify(posts)
14+
}
15+
} catch (error) {
16+
console.error(error)
17+
return {
18+
statusCode: 500,
19+
headers: { 'Content-Type': 'application/json' },
20+
body: JSON.stringify(error)
21+
}
22+
}
23+
}

functions/getUsers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { PrismaClient } = require('@prisma/client')
2+
const prisma = new PrismaClient()
3+
4+
exports.handler = async (event, context, callback) => {
5+
try {
6+
const users = await prisma.user.findMany({
7+
include: { profile: true }
8+
})
9+
return {
10+
statusCode: 200,
11+
headers: { 'Content-Type': 'application/json' },
12+
body: JSON.stringify(users)
13+
}
14+
} catch (error) {
15+
console.error(error)
16+
return {
17+
statusCode: 500,
18+
headers: { 'Content-Type': 'application/json' },
19+
body: JSON.stringify(error)
20+
}
21+
}
22+
}

functions/seed.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const { PrismaClient } = require('@prisma/client')
2+
const prisma = new PrismaClient()
3+
4+
exports.handler = async (event, context, callback) => {
5+
try {
6+
await Promise.all([prisma.profile.deleteMany(), prisma.post.deleteMany()])
7+
await prisma.user.deleteMany()
8+
9+
const createdUser = await prisma.user.create({
10+
data: seedUser
11+
})
12+
13+
const createdUser2 = await prisma.user.create({
14+
data: seedUser2
15+
})
16+
17+
return {
18+
statusCode: 201,
19+
headers: { 'Content-Type': 'application/json' },
20+
body: JSON.stringify([createdUser, createdUser2])
21+
}
22+
} catch (error) {
23+
console.error(error)
24+
return { statusCode: 500 }
25+
}
26+
}
27+
28+
const seedUser = {
29+
30+
name: 'Jane',
31+
profile: {
32+
create: [
33+
{
34+
bio: 'Technical Writer'
35+
},
36+
{
37+
bio: 'Health Enthusiast'
38+
},
39+
{
40+
bio: 'Self Quantifier'
41+
}
42+
]
43+
},
44+
post: {
45+
create: [
46+
{
47+
title:
48+
'Comparing Database Types: How Database Types Evolved to Meet Different Needs',
49+
content:
50+
'https://www.prisma.io/blog/comparison-of-database-models-1iz9u29nwn37/'
51+
},
52+
{
53+
title: 'Analysing Sleep Patterns: The Quantified Self',
54+
content: 'https://quantifiedself.com/get-started/'
55+
}
56+
]
57+
}
58+
}
59+
60+
const seedUser2 = {
61+
62+
name: 'Toru Takemitsu',
63+
profile: {
64+
create: [
65+
{
66+
bio: 'Composer'
67+
},
68+
{
69+
bio: 'Musician'
70+
},
71+
{
72+
bio: 'Writer'
73+
}
74+
]
75+
},
76+
post: {
77+
create: [
78+
{
79+
title: 'Requiem for String Orchestra',
80+
content: ''
81+
},
82+
{
83+
title: 'Music of Tree',
84+
content: ''
85+
},
86+
{
87+
title: 'Waves for clarinet, horn, two trombones and bass drum ',
88+
content: ''
89+
}
90+
]
91+
}
92+
}

functions/status.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
exports.handler = async (event, context, callback) => {
2+
return {
3+
statusCode: 200,
4+
headers: { 'Content-Type': 'application/json' },
5+
body: JSON.stringify({ up: true })
6+
}
7+
}

netlify.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[build]
2+
functions = "functions/"
3+
4+
publish = "public/"

0 commit comments

Comments
 (0)