Skip to content

Commit

Permalink
add mysql and postgres examples for node
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Jan 17, 2019
1 parent 740434f commit e00825f
Show file tree
Hide file tree
Showing 24 changed files with 5,068 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/prisma-configs/configure.sh
Expand Up @@ -18,6 +18,8 @@ cp ./node/prisma.yml ../../node/graphql-subscriptions/prisma
cp ./node/prisma.yml ../../node/rest-express/prisma
cp ./node/prisma.yml ../../node/script/prisma
cp ./node/mongo/prisma.yml ../../node/docker-mongodb/prisma
cp ./node/mysql-and-postgres/prisma.yml ../../node/docker-mysql/prisma
cp ./node/mysql-and-postgres/prisma.yml ../../node/docker-postgres/prisma

# TypeScript (prisma.yml)
cp ./typescript/prisma.yml ../../typescript/cli-app/prisma
Expand Down
19 changes: 19 additions & 0 deletions .github/prisma-configs/node/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: javascript-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/node/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 Node 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/node/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/node/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 Node 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/node/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 node/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 Node 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/node/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/-a003/)
- [Explore the Prisma client API](https://www.prisma.io/client/client-javascript)
- [Learn more about the GraphQL schema](https://www.prisma.io/blog/graphql-server-basics-the-schema-ac5e2950214e/)
30 changes: 30 additions & 0 deletions node/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:
10 changes: 10 additions & 0 deletions node/docker-mysql/package.json
@@ -0,0 +1,10 @@
{
"name": "docker-mysql",
"license": "MIT",
"dependencies": {
"prisma-client-lib": "1.24.0"
},
"scripts": {
"start": "node src/script.js"
}
}
24 changes: 24 additions & 0 deletions node/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 node/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: javascript-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 node/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 e00825f

Please sign in to comment.