Last active
February 20, 2023 15:58
-
-
Save junkor-1011/27bee69f2178f2e6f63cc5b596638d72 to your computer and use it in GitHub Desktop.
prisma relations query test
This file contains hidden or 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
| DATABASE_URL="postgresql://postgres:password@localhost:5432/appdb?schema=app&connection_limit=1" |
This file contains hidden or 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
| .tmp | |
| tmp | |
| .temp | |
| temp | |
| pnpm-lock.yaml | |
| migrations | |
| ### Node ### | |
| # Logs | |
| logs | |
| *.log | |
| npm-debug.log* | |
| .pnpm-debug.log* | |
| # Diagnostic reports (https://nodejs.org/api/report.html) | |
| report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | |
| # Runtime data | |
| pids | |
| *.pid | |
| *.seed | |
| *.pid.lock | |
| # Dependency directories | |
| node_modules/ | |
| jspm_packages/ | |
| # TypeScript cache | |
| *.tsbuildinfo | |
| # Optional npm cache directory | |
| .npm | |
| # Optional REPL history | |
| .node_repl_history | |
| # Output of 'npm pack' | |
| *.tgz | |
| # dotenv environment variable files | |
| .env | |
| .env.development.local | |
| .env.test.local | |
| .env.production.local | |
| .env.local | |
| ### Vim ### | |
| # Swap | |
| [._]*.s[a-v][a-z] | |
| !*.svg # comment out if you don't need vector files | |
| [._]*.sw[a-p] | |
| [._]s[a-rt-v][a-z] | |
| [._]ss[a-gi-z] | |
| [._]sw[a-p] | |
| # Session | |
| Session.vim | |
| Sessionx.vim | |
| # Temporary | |
| .netrwhist | |
| # Auto-generated tag files | |
| tags | |
| # Persistent undo | |
| [._]*.un~ | |
| ### VisualStudioCode ### | |
| .vscode/* | |
| !.vscode/settings.json | |
| !.vscode/tasks.json | |
| !.vscode/launch.json | |
| !.vscode/extensions.json | |
| !.vscode/*.code-snippets | |
| # Local History for Visual Studio Code | |
| .history/ | |
| # Built Visual Studio Code Extensions | |
| *.vsix | |
| ### VisualStudioCode Patch ### | |
| # Ignore all local history of files | |
| .history | |
| .ionide |
This file contains hidden or 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({ | |
| log: ['query', 'warn', 'error'], | |
| }); | |
| await prisma.$transaction([ | |
| prisma.post.deleteMany(), | |
| prisma.user.deleteMany(), | |
| ]); | |
| await prisma.$disconnect(); |
This file contains hidden or 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
| version: '3.8' | |
| services: | |
| postgres: | |
| image: postgres:14.6-alpine | |
| container_name: postgres | |
| ports: | |
| - 5432:5432 | |
| volumes: | |
| - ./.tmp/data:/var/lib/postgresql/data | |
| environment: | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: password | |
| POSTGRES_INITDB_ARGS: '--encoding=UTF-8' | |
| hostname: postgres | |
| restart: 'no' | |
| user: root | |
| # networks: | |
| # - db_network | |
| # pgadmin4: | |
| # image: dpage/pgadmin4:6.15 | |
| # container_name: pgadmin4 | |
| # ports: | |
| # - 8080:80 | |
| # environment: | |
| # PGADMIN_DEFAULT_EMAIL: [email protected] | |
| # PGADMIN_DEFAULT_PASSWORD: p@ssword | |
| # hostname: pgadmin4 | |
| # depends_on: | |
| # - postgres | |
| # restart: 'no' | |
| # networks: | |
| # - db_network | |
| # | |
| # networks: | |
| # db_network: | |
| # driver: bridge |
erDiagram
Role {
ADMIN ADMIN
USER USER
}
users {
Int z_id PK
String user_id
Role role
String message "nullable"
DateTime created_at
DateTime updated_at
}
posts {
Int z_id PK
String post_id
Int author_id
String title
String body
DateTime created_at
DateTime updated_at
}
categories {
Int z_id PK
String category_name
}
users o|--|| Role : "enum:role"
posts o{--|| users : "author"
This file contains hidden or 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
| { | |
| "name": "experiment", | |
| "version": "1.0.0", | |
| "description": "", | |
| "type": "module", | |
| "packageManager": "[email protected]", | |
| "main": "index.js", | |
| "scripts": { | |
| "generate": "dotenv -e .env -e .env.default -- prisma generate", | |
| "studio": "dotenv -e .env -e .env.default -- prisma studio", | |
| "seed": "dotenv -e .env -e .env.default -- tsx seed.ts", | |
| "delete": "dotenv -e .env -e .env.default -- tsx delete.ts", | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "private": true, | |
| "devDependencies": { | |
| "@mermaid-js/mermaid-cli": "^9.3.0", | |
| "@types/node": "^18.14.0", | |
| "@types/uuid": "^9.0.0", | |
| "dotenv-cli": "^7.0.0", | |
| "prisma": "^4.10.1", | |
| "prisma-erd-generator": "^1.2.5", | |
| "tsx": "^3.12.3", | |
| "typescript": "^4.9.5" | |
| }, | |
| "dependencies": { | |
| "@prisma/client": "^4.10.1", | |
| "uuid": "^9.0.0" | |
| } | |
| } |
This file contains hidden or 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
| // 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" | |
| binaryTargets = ["native", "rhel-openssl-1.0.x", "debian-openssl-3.0.x"] | |
| } | |
| generator erd { | |
| provider = "prisma-erd-generator" | |
| output = "erd.md" | |
| includeRelationFromFields = true | |
| } | |
| datasource db { | |
| provider = "postgresql" | |
| url = env("DATABASE_URL") | |
| } | |
| model User { | |
| id Int @id @default(autoincrement()) @map("_id") | |
| userId String @unique @map("user_id") @db.Char(8) | |
| role Role @default(USER) | |
| message String? @db.VarChar(32) | |
| posts Post[] | |
| createdAt DateTime @default(now()) @map("created_at") | |
| updatedAt DateTime @default(now()) @map("updated_at") | |
| @@map("users") | |
| } | |
| model Post { | |
| id Int @id @default(autoincrement()) @map("_id") | |
| postId String @unique @default(uuid()) @map("post_id") @db.Uuid | |
| author User @relation(fields: [authorId], references: [id]) | |
| authorId Int @map("author_id") | |
| title String @unique @db.VarChar(30) | |
| body String @db.VarChar(300) | |
| categories Category[] | |
| createdAt DateTime @default(now()) @map("created_at") | |
| updatedAt DateTime @default(now()) @map("updated_at") | |
| @@map("posts") | |
| } | |
| model Category { | |
| id Int @id @default(autoincrement()) @map("_id") | |
| categoryName String @unique @map("category_name") @db.VarChar(32) | |
| posts Post[] | |
| @@map("categories") | |
| } | |
| enum Role { | |
| ADMIN | |
| USER | |
| } | |
This file contains hidden or 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 { type Prisma, PrismaClient } from '@prisma/client'; | |
| const prisma = new PrismaClient({ | |
| log: ['query', 'warn', 'error'], | |
| }); | |
| const usersData = [ | |
| { | |
| userId: 'user000x', | |
| }, | |
| { | |
| userId: 'user000z', | |
| message: 'hyper-message', | |
| role: 'ADMIN', | |
| }, | |
| ] satisfies Prisma.UserCreateManyInput[]; | |
| await prisma.$transaction([ | |
| prisma.user.createMany({ | |
| data: usersData, | |
| skipDuplicates: true, | |
| }), | |
| prisma.user.create({ | |
| data: { | |
| userId: 'user000w', | |
| posts: { | |
| create: [ | |
| { title: 'namahage', body: 'a long long ago story of the jedi.' }, | |
| { title: 'namahage2', body: 'super namahage' }, | |
| ] | |
| } | |
| } | |
| }) | |
| ]); | |
| await prisma.$disconnect(); |
This file contains hidden or 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
Show hidden characters
| { | |
| "compilerOptions": { | |
| "target": "esnext", | |
| "module": "esnext", | |
| "moduleResolution": "nodenext", | |
| "esModuleInterop": true, | |
| "forceConsistentCasingInFileNames": true, | |
| "strict": true, | |
| "noEmit": true, | |
| "skipLibCheck": true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment