Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions by-language/javascript-prisma/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
40 changes: 40 additions & 0 deletions by-language/javascript-prisma/README.md
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
```
Comment on lines +22 to +29
Copy link
Member Author

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 like postgresql://crate@localhost:5432/mydb?schema=public.

$ npx prisma migrate dev --name init
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydb", schema "public" at "localhost:5432"

Error: Schema engine error:
ERROR: line 1:8: no viable alternative at input 'CREATE SCHEMA'

Copy link
Member Author

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 the CREATE 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


### 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
67 changes: 67 additions & 0 deletions by-language/javascript-prisma/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions by-language/javascript-prisma/package.json
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"
}
}
24 changes: 24 additions & 0 deletions by-language/javascript-prisma/prisma/schema.prisma
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?
}
101 changes: 101 additions & 0 deletions by-language/javascript-prisma/script.js
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)
})