docs/1.30/get-started/02-change-data-model-JAVASCRIPT-c001.mdx
import Warning from 'components/Markdown/Warning' import Code from 'components/Markdown/Code' import Info from 'components/Markdown/Info'
export const meta = { title: 'Change Datamodel', position: 2, nextText: 'Fantastic! 🎉 You are now able to migrate your database using Prisma. Next you will learn how to build an API server based on Prisma client.', technology: 'node', technologyOrder: 1, gettingStartedOrder: 1, articleGroup: 'Change Datamodel' }
On this page, you will learn how to:
This page is only relevant if you started with a new database or a Demo server. If you configured Prisma with an existing database, you need to run migrations directly against your database.
</Warning>Update the datamodel in datamodel.prisma as follows:
<Code languages={["SQL", "MongoDB"]}>
type User {
id: ID! @unique
email: String @unique
name: String!
posts: [Post!]!
}
type Post {
id: ID! @unique
title: String!
published: Boolean! @default(value: "false")
author: User
}
type User {
id: ID! @id
email: String @unique
name: String!
posts: [Post!]! @relation(link: INLINE)
}
type Post {
id: ID! @id
title: String!
published: Boolean! @default(value: false)
author: User
}
Here's what changed:
email field to the User type.Post type to the datamodel.User and Post (via the posts and author fields).To apply the changes you just made to your datamodel, you need to redeploy the Prisma datamodel:
prisma deploy
Because the Prisma client is based on your datamodel, it needs to be regenerated every time the datamodel is updated:
prisma generate
The Prisma client library in the /generated/prisma-client directory is now being updated and its API has been adjusted to use the new datamodel.
You can ensure that your Prisma client is automatically being updated after every deploy by adding the following lines to your prisma.yml:
hooks:
post-deploy:
- prisma generate
The Prisma client API allows to write nested objects in a single transaction without having to manually control when the transaction starts or ends.
<Warning>The MongoDB database connector currently does not support ACID transactions. Learn more in this GitHub issue.
</Warning>Update your index.js to look as follows:
const { prisma } = require('./generated/prisma-client')
// A `main` function so that we can use async/await
async function main() {
// Create a new user with a new post
const newUser = await prisma
.createUser({
name: "Bob",
email: "[email protected]",
posts: {
create: [{
title: "Join us for GraphQL Conf in 2019",
}, {
title: "Subscribe to GraphQL Weekly for GraphQL news",
}]
},
})
console.log(`Created new user: ${newUser.name} (ID: ${newUser.id})`)
// Read all users from the database and print them to the console
const allUsers = await prisma.users()
console.log(allUsers)
const allPosts = await prisma.posts()
console.log(allPosts)
}
main().catch(e => console.error(e))
Run the script with the following command:
node index.js
With the Prisma client API, you can navigate relations in your data graph using chained method calls (also called fluent API). Here is how you can query the posts written by a certain User:
const { prisma } = require('./generated/prisma-client')
// A `main` function so that we can use async/await
async function main() {
// Read the previously created user from the database and print their posts to the console
const postsByUser = await prisma
.user({ email: "[email protected]" })
.posts()
console.log(`All posts by that user: ${JSON.stringify(postsByUser)}`)
}
main().catch(e => console.error(e))