-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example project using Prisma #188
Draft
amotl
wants to merge
3
commits into
main
Choose a base branch
from
amo/prisma
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Using CrateDB with Prisma | ||
|
||
|
||
## About | ||
|
||
[Prisma] is a next-generation Node.js and TypeScript ORM. | ||
|
||
|
||
## Details | ||
|
||
This example shows how to use [Prisma Client] in a **simple Node.js script**, to | ||
read and write data in a CrateDB database. | ||
|
||
The folder has been scaffolded using this command: | ||
```shell | ||
npx try-prisma@latest --template javascript/script --install npm --name . --path . | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
### Create the database | ||
|
||
Run the following command to submit the SQL DDL to the database. This will create | ||
database tables for the `User` and `Post` entities that are defined in | ||
[`prisma/schema.prisma`](./prisma/schema.prisma). | ||
```shell | ||
npx prisma migrate dev --name init | ||
``` | ||
|
||
### Execute the script | ||
```shell | ||
npm run dev | ||
``` | ||
|
||
|
||
|
||
[Prisma]: https://www.prisma.io/ | ||
[Prisma Client]: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client | ||
[Simple Node.js Script Example]: https://github.com/prisma/prisma-examples/tree/latest/javascript/script |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "script", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "node ./script.js" | ||
}, | ||
"dependencies": { | ||
"@prisma/client": "5.6.0" | ||
}, | ||
"devDependencies": { | ||
"prisma": "5.6.0" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = "postgresql://crate@localhost:5432/?schema=doc" | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
email String @unique | ||
name String? | ||
posts Post[] | ||
} | ||
|
||
model Post { | ||
id Int @id @default(autoincrement()) | ||
title String | ||
content String? | ||
published Boolean @default(false) | ||
author User? @relation(fields: [authorId], references: [id]) | ||
authorId Int? | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const { PrismaClient } = require('@prisma/client') | ||
|
||
const prisma = new PrismaClient() | ||
|
||
// A `main` function so that we can use async/await | ||
async function main() { | ||
// Seed the database with users and posts | ||
const user1 = await prisma.user.create({ | ||
data: { | ||
email: '[email protected]', | ||
name: 'Alice', | ||
posts: { | ||
create: { | ||
title: 'Watch the talks from Prisma Day 2019', | ||
content: 'https://www.prisma.io/blog/z11sg6ipb3i1/', | ||
published: true, | ||
}, | ||
}, | ||
}, | ||
include: { | ||
posts: true, | ||
}, | ||
}) | ||
const user2 = await prisma.user.create({ | ||
data: { | ||
email: '[email protected]', | ||
name: 'Bob', | ||
posts: { | ||
create: [ | ||
{ | ||
title: 'Subscribe to GraphQL Weekly for community news', | ||
content: 'https://graphqlweekly.com/', | ||
published: true, | ||
}, | ||
{ | ||
title: 'Follow Prisma on Twitter', | ||
content: 'https://twitter.com/prisma/', | ||
published: false, | ||
}, | ||
], | ||
}, | ||
}, | ||
include: { | ||
posts: true, | ||
}, | ||
}) | ||
console.log( | ||
`Created users: ${user1.name} (${user1.posts.length} posts) and ${user2.name} (${user2.posts.length} posts) `, | ||
) | ||
// Retrieve all published posts | ||
const allPosts = await prisma.post.findMany({ | ||
where: { published: true }, | ||
}) | ||
console.log(`Retrieved all published posts: `, allPosts) | ||
|
||
// Create a new post (written by an already existing user with email [email protected]) | ||
const newPost = await prisma.post.create({ | ||
data: { | ||
title: 'Join the Prisma Slack community', | ||
content: 'http://slack.prisma.io', | ||
published: false, | ||
author: { | ||
connect: { | ||
email: '[email protected]', // Should have been created during initial seeding | ||
}, | ||
}, | ||
}, | ||
}) | ||
console.log(`Created a new post: `, newPost) | ||
|
||
// Publish the new post | ||
const updatedPost = await prisma.post.update({ | ||
where: { | ||
id: newPost.id, | ||
}, | ||
data: { | ||
published: true, | ||
}, | ||
}) | ||
console.log(`Published the newly created post: `, updatedPost) | ||
|
||
// Retrieve all posts by user with email [email protected] | ||
const postsByUser = await prisma.user | ||
.findUnique({ | ||
where: { | ||
email: '[email protected]', | ||
}, | ||
}) | ||
.posts() | ||
console.log(`Retrieved all posts from a specific user: `, postsByUser) | ||
} | ||
|
||
main() | ||
.then(async () => { | ||
await prisma.$disconnect() | ||
}) | ||
.catch(async (e) => { | ||
console.error(e) | ||
await prisma.$disconnect() | ||
process.exit(1) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prisma wants to run a
CREATE SCHEMA
command when using a connection string likepostgresql://crate@localhost:5432/mydb?schema=public
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using
postgresql://crate@localhost:5432/?schema=doc
as database connection string, it trips on theCREATE DATABASE
statement.Error: P3014 Prisma Migrate could not create the shadow database. Please make sure the database user has permission to create databases. Read more about the shadow database (and workarounds) at https://pris.ly/d/migrate-shadow Original error: ERROR: line 1:8: no viable alternative at input 'CREATE DATABASE' 0: schema_core::state::DevDiagnostic at schema-engine/core/src/state.rs:270
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crate/crate#14601