@personal-companion/prisma-service v1.0.0
prisma-service
Prisma service for personal companion
GraphQL Server Example with NestJS (SDL-first)
This example shows how to implement an GraphQL server (SDL-first) with TypeScript with the following stack:
- NestJS: Web framework for building scalable server-side applications
- graphql-tools: A tool for combining resolvers and type-definitions into an executable schema
- Prisma Client: Databases access (ORM)
- Prisma Migrate: Database migrations
Getting started
1. Install dependencies
Clone repository:
git clone https://github.com/personal-companion/prisma-service.git
Install npm dependencies:
cd prisma-service
npm install
2. Create and seed the database
npx prisma migrate dev --name init
When npx prisma migrate dev
is executed against a newly created database, seeding is also triggered. The seed file in prisma/seed.ts
will be executed and your database will be populated with the sample data.
2. Start the GraphQL server
Launch your GraphQL server with this command:
npm run dev
Navigate to http://localhost:710/graphql in your browser to explore the API of your GraphQL server in a GraphQL Playground.
Using the GraphQL API
The schema that specifies the API operations of your GraphQL server is defined in ./schema.graphql
. Below are a number of operations that you can send to the API using the GraphQL Playground.
1. Migrate your database using Prisma Migrate
The first step is to add a new table, e.g. called Profile
, to the database. You can do this by adding a new model to your Prisma schema file file and then running a migration afterwards:
Once you've updated your data model, you can execute the changes against your database with the following command:
npx prisma migrate dev --name [DESCRIPTION]
This adds another migration to the prisma/migrations
directory.
2. Update your application code
You can now use your PrismaClient
instance to perform operations against the new `Profile table.
Those operations can be used to implement queries and mutations in the GraphQL API
Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB)
If you want to try this example with another database than SQLite, you can adjust the the database connection in prisma/schema.prisma
by reconfiguring the datasource
block.
Learn more about the different connection configurations in the docs.
PostgreSQL
For PostgreSQL, the connection URL has the following structure:
datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}
Here is an example connection string with a local PostgreSQL database:
datasource db {
provider = "postgresql"
url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"
}
MySQL
For MySQL, the connection URL has the following structure:
datasource db {
provider = "mysql"
url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"
}
Here is an example connection string with a local MySQL database:
datasource db {
provider = "mysql"
url = "mysql://janedoe:mypassword@localhost:3306/notesapi"
}
Microsoft SQL Server
Here is an example connection string with a local Microsoft SQL Server database:
datasource db {
provider = "sqlserver"
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}
MongoDB
Here is an example connection string with a local MongoDB database:
datasource db {
provider = "mongodb"
url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
}
Next steps
- Check out the Prisma docs
- Share your feedback in the
#product-wishlist
channel on the Prisma Slack - Create issues and ask questions on GitHub
- Watch our biweekly "What's new in Prisma" livestreams on Youtube
8 months ago