forked from prisma/prisma-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mysql and postgres examples for typescript
- Loading branch information
1 parent
6e6eea2
commit 332873e
Showing
25 changed files
with
4,860 additions
and
1 deletion.
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
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
19 changes: 19 additions & 0 deletions
19
.github/prisma-configs/typescript/mysql-and-postgres/prisma.yml
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,19 @@ | ||
# Specifies the HTTP endpoint of your Prisma API. | ||
endpoint: http://localhost:4466 | ||
|
||
# Defines your models, each model is mapped to the database as a collection. | ||
datamodel: datamodel.prisma | ||
|
||
# Specifies the language and directory for the generated Prisma client. | ||
generate: | ||
- generator: typescript-client | ||
output: ../src/generated/prisma-client/ | ||
|
||
# Ensures Prisma client is re-generated after a datamodel change. | ||
hooks: | ||
post-deploy: | ||
- prisma generate | ||
|
||
# Seeds initial data into the database by running a script. | ||
seed: | ||
import: seed.graphql |
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,44 @@ | ||
# MySQL with Docker Example | ||
|
||
This example shows how to **set up Prisma using Docker and MySQL** locally on your machine. It then uses the Prisma client in a simple TypeScript script to read and write data in the database. | ||
|
||
> This example uses a new and empty database. **Learn how to connect Prisma to your existing database [here](https://www.prisma.io/docs/-a003/)**. | ||
__INLINE(../_setup-1.md)__ | ||
cd prisma-examples/typescript/docker-mysql | ||
npm install | ||
``` | ||
### 2. Launch Prisma with Docker | ||
This example is based on Docker. If you don't have Docker installed, you can get it from [here](https://store.docker.com/search?type=edition&offering=community). Use the Docker Compose CLI to launch the Docker containers specified in [docker-compose.yml](./docker-compose.yml): | ||
``` | ||
docker-compose up -d | ||
``` | ||
### 3. Install the Prisma CLI | ||
To run the example, you need the Prisma CLI. Please install it via NPM or [using another method](https://www.prisma.io/docs/prisma-cli-and-configuration/using-the-prisma-cli-alx4/#installation): | ||
``` | ||
npm install -g prisma | ||
``` | ||
### 4. Set up database & deploy Prisma datamodel | ||
To deploy the datamodel for this example, run the following command: | ||
``` | ||
prisma deploy | ||
``` | ||
### 5. Run the script | ||
Execute the script with this command: | ||
``` | ||
npm run start | ||
``` | ||
__INLINE(../_next-steps.md)__ |
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,44 @@ | ||
# PostgreSQL with Docker Example | ||
|
||
This example shows how to **set up Prisma using Docker and PostgreSQL** locally on your machine. It then uses the Prisma client in a simple TypeScript script to read and write data in the database. | ||
|
||
> This example uses a new and empty database. **Learn how to connect Prisma to your existing database [here](https://www.prisma.io/docs/-a003/)**. | ||
__INLINE(../_setup-1.md)__ | ||
cd prisma-examples/typescript/docker-postgres | ||
npm install | ||
``` | ||
### 2. Launch Prisma with Docker | ||
This example is based on Docker. If you don't have Docker installed, you can get it from [here](https://store.docker.com/search?type=edition&offering=community). Use the Docker Compose CLI to launch the Docker containers specified in [docker-compose.yml](./docker-compose.yml): | ||
``` | ||
docker-compose up -d | ||
``` | ||
### 3. Install the Prisma CLI | ||
To run the example, you need the Prisma CLI. Please install it via NPM or [using another method](https://www.prisma.io/docs/prisma-cli-and-configuration/using-the-prisma-cli-alx4/#installation): | ||
``` | ||
npm install -g prisma | ||
``` | ||
### 4. Set up database & deploy Prisma datamodel | ||
To deploy the datamodel for this example, run the following command: | ||
``` | ||
prisma deploy | ||
``` | ||
### 5. Run the script | ||
Execute the script with this command: | ||
``` | ||
npm run start | ||
``` | ||
__INLINE(../_next-steps.md)__ |
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,60 @@ | ||
# MySQL with Docker Example | ||
|
||
This example shows how to **set up Prisma using Docker and MySQL** locally on your machine. It then uses the Prisma client in a simple TypeScript script to read and write data in the database. | ||
|
||
> This example uses a new and empty database. **Learn how to connect Prisma to your existing database [here](https://www.prisma.io/docs/-a003/)**. | ||
## How to use | ||
|
||
### 1. Download example & install dependencies | ||
|
||
Clone the repository: | ||
|
||
``` | ||
git clone [email protected]:prisma/prisma-examples.git | ||
``` | ||
|
||
Install Node dependencies: | ||
|
||
``` | ||
cd prisma-examples/typescript/docker-mysql | ||
npm install | ||
``` | ||
|
||
### 2. Launch Prisma with Docker | ||
|
||
This example is based on Docker. If you don't have Docker installed, you can get it from [here](https://store.docker.com/search?type=edition&offering=community). Use the Docker Compose CLI to launch the Docker containers specified in [docker-compose.yml](./docker-compose.yml): | ||
|
||
``` | ||
docker-compose up -d | ||
``` | ||
|
||
### 3. Install the Prisma CLI | ||
|
||
To run the example, you need the Prisma CLI. Please install it via NPM or [using another method](https://www.prisma.io/docs/prisma-cli-and-configuration/using-the-prisma-cli-alx4/#installation): | ||
|
||
``` | ||
npm install -g prisma | ||
``` | ||
|
||
### 4. Set up database & deploy Prisma datamodel | ||
|
||
To deploy the datamodel for this example, run the following command: | ||
|
||
``` | ||
prisma deploy | ||
``` | ||
|
||
### 5. Run the script | ||
|
||
Execute the script with this command: | ||
|
||
``` | ||
npm run start | ||
``` | ||
|
||
## Next steps | ||
|
||
- [Use Prisma with an existing database](https://www.prisma.io/docs/-t003/) | ||
- [Explore the Prisma client API](https://www.prisma.io/client/client-typescript) | ||
- [Learn more about the GraphQL schema](https://www.prisma.io/blog/graphql-server-basics-the-schema-ac5e2950214e/) |
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,30 @@ | ||
version: '3' | ||
services: | ||
prisma: | ||
image: prismagraphql/prisma:1.24 | ||
restart: always | ||
ports: | ||
- "4466:4466" | ||
environment: | ||
PRISMA_CONFIG: | | ||
port: 4466 | ||
# uncomment the next line and provide the env var PRISMA_MANAGEMENT_API_SECRET=my-secret to activate cluster security | ||
# managementApiSecret: my-secret | ||
databases: | ||
default: | ||
connector: mysql | ||
host: mysql | ||
user: root | ||
password: prisma | ||
rawAccess: true | ||
port: 3306 | ||
migrations: true | ||
mysql: | ||
image: mysql:5.7 | ||
restart: always | ||
environment: | ||
MYSQL_ROOT_PASSWORD: prisma | ||
volumes: | ||
- mysql:/var/lib/mysql | ||
volumes: | ||
mysql: |
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,15 @@ | ||
{ | ||
"name": "docker-mysql", | ||
"license": "MIT", | ||
"dependencies": { | ||
"prisma-client-lib": "1.24.0" | ||
}, | ||
"scripts": { | ||
"start": "ts-node src/script.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/graphql": "^14.0.5", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.2.2" | ||
} | ||
} |
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 @@ | ||
type User { | ||
id: ID! @unique | ||
email: String! @unique | ||
name: String | ||
posts: [Post!]! | ||
comments: [Comment!]! | ||
} | ||
|
||
type Post { | ||
id: ID! @unique | ||
createdAt: DateTime! | ||
updatedAt: DateTime! | ||
title: String! | ||
content: String | ||
published: Boolean! @default(value: "false") | ||
author: User! | ||
comments: [Comment!]! | ||
} | ||
|
||
type Comment { | ||
id: ID! @unique | ||
text: String! | ||
writtenBy: User! | ||
} |
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,19 @@ | ||
# Specifies the HTTP endpoint of your Prisma API. | ||
endpoint: http://localhost:4466 | ||
|
||
# Defines your models, each model is mapped to the database as a collection. | ||
datamodel: datamodel.prisma | ||
|
||
# Specifies the language and directory for the generated Prisma client. | ||
generate: | ||
- generator: typescript-client | ||
output: ../src/generated/prisma-client/ | ||
|
||
# Ensures Prisma client is re-generated after a datamodel change. | ||
hooks: | ||
post-deploy: | ||
- prisma generate | ||
|
||
# Seeds initial data into the database by running a script. | ||
seed: | ||
import: seed.graphql |
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,42 @@ | ||
mutation { | ||
user1: createUser(data: { | ||
email: "[email protected]" | ||
name: "Alice" | ||
posts: { | ||
create: { | ||
title: "Join us for GraphQL Conf 2019 in Berlin" | ||
content: "https://www.graphqlconf.org/" | ||
published: true | ||
} | ||
} | ||
}) { | ||
id | ||
} | ||
|
||
user2: createUser(data: { | ||
email: "[email protected]" | ||
name: "Bob" | ||
posts: { | ||
create: [{ | ||
title: "Subscribe to GraphQL Weekly for community news" | ||
content: "https://graphqlweekly.com/" | ||
published: true | ||
comments: { | ||
create: [{ | ||
text: "Can recommend 💯", | ||
writtenBy: { | ||
connect: { | ||
email: "[email protected]" | ||
} | ||
} | ||
}] | ||
} | ||
} { | ||
title: "Follow Prisma on Twitter" | ||
content: "https://twitter.com/prisma" | ||
}] | ||
} | ||
}) { | ||
id | ||
} | ||
} |
Oops, something went wrong.