Skip to content

Commit

Permalink
add mysql and postgres examples for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Jan 17, 2019
1 parent 6e6eea2 commit 332873e
Show file tree
Hide file tree
Showing 25 changed files with 4,860 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Expand Up @@ -38,6 +38,8 @@ jobs:
- run: cd typescript/rest-express && yarn && prisma generate && yarn tsc
- run: cd typescript/script && yarn && prisma generate && yarn tsc
- run: cd typescript/docker-mongodb && yarn && prisma generate
- run: cd typescript/docker-mysql && yarn && prisma generate
- run: cd typescript/docker-postgres && yarn && prisma generate

flow:
docker:
Expand Down
5 changes: 4 additions & 1 deletion .github/prisma-configs/configure.sh
Expand Up @@ -28,6 +28,8 @@ cp ./typescript/prisma.yml ../../typescript/graphql-auth/prisma
cp ./typescript/prisma.yml ../../typescript/graphql-subscriptions/prisma
cp ./typescript/prisma.yml ../../typescript/script/prisma
cp ./typescript/mongo/prisma.yml ../../typescript/docker-mongodb/prisma
cp ./typescript/mysql-and-postgres/prisma.yml ../../typescript/docker-mysql/prisma
cp ./typescript/mysql-and-postgres/prisma.yml ../../typescript/docker-postgres/prisma

# TypeScript (tsconfig.json)
cp ./typescript/tsconfig.json ../../typescript/cli-app
Expand All @@ -39,6 +41,8 @@ cp ./typescript/tsconfig-graphql.json ../../typescript/graphql-subscriptions
mv ../../typescript/graphql-subscriptions/tsconfig-graphql.json ../../typescript/graphql-subscriptions/tsconfig.json
cp ./typescript/tsconfig.json ../../typescript/script
cp ./typescript/tsconfig.json ../../typescript/docker-mongodb
cp ./typescript/tsconfig.json ../../typescript/docker-mysql
cp ./typescript/tsconfig.json ../../typescript/docker-postgres

# TypeScript (graphqlgen)
cp ./typescript/graphqlgen.yml ../../typescript/graphql
Expand All @@ -52,7 +56,6 @@ cp ./flow/prisma.yml ../../flow/script/prisma
# Flow (.flowconfig)
cp ./flow/.flowconfig ../../flow/graphql
cp ./flow/.flowconfig ../../flow/script
\

# Go
cp ./go/prisma.yml ../../go/cli-app/prisma
Expand Down
19 changes: 19 additions & 0 deletions .github/prisma-configs/typescript/mysql-and-postgres/prisma.yml
@@ -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
44 changes: 44 additions & 0 deletions .github/readmes/typescript/docker-mysql/README.md
@@ -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)__
44 changes: 44 additions & 0 deletions .github/readmes/typescript/docker-postgres/README.md
@@ -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)__
60 changes: 60 additions & 0 deletions typescript/docker-mysql/README.md
@@ -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 git@github.com: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/)
30 changes: 30 additions & 0 deletions typescript/docker-mysql/docker-compose.yml
@@ -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:
15 changes: 15 additions & 0 deletions typescript/docker-mysql/package.json
@@ -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"
}
}
24 changes: 24 additions & 0 deletions typescript/docker-mysql/prisma/datamodel.prisma
@@ -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!
}
19 changes: 19 additions & 0 deletions typescript/docker-mysql/prisma/prisma.yml
@@ -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
42 changes: 42 additions & 0 deletions typescript/docker-mysql/prisma/seed.graphql
@@ -0,0 +1,42 @@
mutation {
user1: createUser(data: {
email: "alice@prisma.io"
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: "bob@prisma.io"
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: "alice@prisma.io"
}
}
}]
}
} {
title: "Follow Prisma on Twitter"
content: "https://twitter.com/prisma"
}]
}
}) {
id
}
}

0 comments on commit 332873e

Please sign in to comment.