Last active
September 24, 2022 21:07
-
-
Save paulwongx/8be22a5789a059fc2a9d9431effc790a to your computer and use it in GitHub Desktop.
Example prisma schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrismaClient } from "@prisma/client" | |
const prisma = new PrismaClient() | |
// const prisma = new PrismaClient({ log: ["query"]}) | |
// npx prisma generate | |
// to generate the types, if not, close and re-open file. If not, ctrl+click create to reload typescript | |
// npx prisma migrate dev --name test2 | |
async function main() { | |
// .delete() | |
await prisma.user.deleteMany(); | |
await prisma.user.create({ | |
data: { | |
name: "Kyle", | |
email: "kyle@test.com", | |
age: 27, | |
userPreference: { | |
create: { | |
emailUpdates: true | |
} | |
} | |
}, | |
include: { | |
userPreference: true, | |
}, | |
// select: { | |
// name: true, | |
// userPreference: { select: { id: true }}, | |
// } | |
}); | |
await users = await prisma.user.createMany({ | |
data: [{ | |
name: "Kyle", | |
}, | |
{ | |
name: "Billy", | |
}] | |
}); | |
// .findUnique() | |
// .findFirst() | |
// .findMany() | |
const unqiue = await prisma.user.findUnique({ | |
where: { | |
email: "kyle@test.com", | |
// age_name: { | |
// age: 27, | |
// name: "kyle" | |
// } | |
} | |
}); | |
const manyUnique = await prisma.user.findMany({ | |
where: { | |
name: "Sally" | |
// name: { equals: "Sally" }, | |
// name: { not: "Sally" }, | |
// name: { in: ["Sally", "Kyle"] }, // includes | |
// name: { notIn: ["Sally", "Kyle"] }, // does not includes | |
// age: { lt: 20 } // lt gt lte gte | |
// email: { contains: "@test.com" }, | |
// email: { endsWith: "@test.com" }, | |
// email: { startsWith: "kyle" }, | |
// AND: [ { email: { startsWith: "sally" }}, { email: { endsWith: "@test.com" }} ], | |
// OR: [ { email: { startsWith: "sally" }}, { age: { gt: 20 }} ], | |
// NOT: { email: { startsWith: "sally" }}, | |
// userPreference: { | |
// emailUpdates: true, | |
// } | |
// writtenPosts: { | |
// every: { title: "Test" }, | |
// none: { title: "Test" }, | |
// some: { title: "Test" }, | |
// } | |
}, | |
// take: 2, // for pagination | |
// skip: 1, // skips the first result | |
// distinct: ["name", "age"] | |
orderBy: { | |
age: "asc", // asc or desc | |
} | |
}) | |
const post = await prisma.post.findMany({ | |
where: { | |
author: { | |
is: { age: 27 }, | |
// isNot: { age: 27 }, | |
} | |
} | |
}) | |
// .updateMany() | |
const updatedUser = await prisma.user.update({ | |
where: { | |
email: "sally@test.com" | |
}, | |
data: { | |
email: "sally@test1.com" | |
age: { | |
increment: 1, | |
// decrement: 1, | |
// multiply: 10, | |
// divide: 10, | |
} | |
// userPreference: { | |
// connect: { | |
// id: "sda897-sda98-asd908-asd98asd" | |
// }, | |
// disconnect: true | |
// } | |
} | |
}) | |
console.log(user); | |
} | |
main() | |
.catch(e => { | |
console.error(e.message) | |
}) | |
.finally(async () => { | |
await prisma.$disconnect() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npx prisma migrate dev | |
generator client { | |
provider = "prisma-client-js" | |
} | |
datasource db { | |
provider = "postgresql" | |
url = env("DATABASE_URL") | |
} | |
model User { | |
id Int @id @default(uuid()) | |
age Int | |
name String | |
email String @unqiue | |
isRegistered Boolean | |
role Role @default(BASIC) | |
metadata Json | |
writtenPosts Post[] @relation("WrittenPosts") | |
favoritePosts Post[] @relation("FavoritePosts") | |
userPreference UserPreference? @relation(fields: [userPreferenceId], references: [id]) | |
userPreferenceId String? @unqiue | |
// @@unqiue([age, name]) | |
// @@index([email, name]) | |
} | |
model UserPreferences { | |
id String @id @default(uuid()) | |
emailUpdated Boolean | |
user User? | |
} | |
model Post { | |
id String @id @default(uuid()) | |
title String | |
averageRating Float | |
createdAt DateTime @default(now()) | |
updatedAt DateTime @updatedAt | |
author User @relation("WrittenPosts", fields: [authorId], references: [id]) | |
authorId String | |
favoritedBy User? @relation("FavoritePosts", fields: [favoriteId], references: [id]) | |
favoriteById String? | |
categories Category[] | |
// @@id([title, authorId]) | |
} | |
model Category { | |
id String @id @default(uuid()) | |
name String @unique | |
posts Post[] | |
} | |
enum Role { | |
BASIC | |
EDITOR | |
ADMIN | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment