By Hitesh Jethva, Alibaba Cloud Community Blog author.
Prisma is a data layer that replaces traditional (object-relational mapping) ORMs and simplifies database workflows. You can access your data and connect to your database with the GraphQL API using Prisma service.
In this tutorial, we will learn how to install and configure Prisma on an Alibaba Cloud Elastic Compute Service (ECS) Ubuntu 18.04 server.
Create a new ECS instance and connect to your instance as the root user.
Once you are logged into your Ubuntu 18.04 instance, run the following command to update your base system with the latest available packages.
apt-get update -y
Before starting, you will need to install a few prerequisite packages which let apt use packages over HTTPS.
apt-get install apt-transport-https ca-certificates curl software-properties-common -y
Next, add the GPG key for the official Docker repository to your system with the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
Next, add the Docker repository to your system with the following command:
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
Next, update the repository and install Docker CE with the following command:
apt-get update -y
apt-get install docker-ce -y
Once the Docker has been installed, check the status of Docker with the following command:
systemctl status docker
Next, you will need to install Node.js to your system. By default, the latest version of Node.js is not available in the Ubuntu 18.04 server. So, you will need to add PPA for that.
First, download the node setup script with the following command:
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
Next, run the script with the following command:
bash nodesource_setup.sh
This will setup repository for Node.js.
Next, update the repository and install Node.js with the following command:
apt-get update -y
apt-get install nodejs -y
Once the Node.js has been installed, you can proceed to install Docker Compose.
Docker Compose allows you to manage and run multi-container applications. So, you will need to install it to set up the infrastructure required for the Prisma service.
By default, Docker Compose is not available in the Ubuntu 18.04 default repository. So, you will need to install it from the Docker's GitHub repository.
You can install it by just running the following command:
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Next, give proper permissions with the following command:
chmod +x /usr/local/bin/docker-compose
Next, verify the version of Docker Compose with the following command:
docker-compose --version
You should see the following command:
docker-compose version 1.21.2, build a133471
Once you have done, you can proceed to the next step.
The Prisma CLI is a main tool to deploy and manage your Prisma services. So, you will need to setup required infrastructure to start the Prisma services. You can setup it by creating docker-compose.yml file. This file will automatically start Prisma, an associated database, and configure the necessary details.
nano docker-compose.yml
You can replace the passwords for managementAPIsecret and MYSQL_ROOT_PASSWORD as per your need as shown below:
version: "3"
services:
prisma:
image: prismagraphql/prisma:1.20
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
managementApiSecret: my-secret
databases:
default:
connector: mysql
host: mysql
port: 3306
user: root
password: admin@123
migrations: true
mysql:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: admin@123
volumes:
- mysql:/var/lib/mysql
volumes:
mysql:
Save and close the file, when you are finished. Then, start the Docker containers with the following command:
docker-compose up -d
This command will download the Docker images for both prisma and mysql.
You can see the Docker containers with the following command:
docker ps
Once you have completed the above steps, you can proceed to the next section.
Your Prisma server and database is now configured, it's time to work locally to deploy the Prisma service.
First, create a directory to store all the Prisma files:
mkdir prisma
Next, change the directory to prisma and install Prisma with the following command:
cd prizma
npm install -g prisma
Next, you will need to create the file structure for a new Prisma database API. You can use prisma init command to generate the files necessary to build your application with Prisma.
You can do it with the following command:
prisma init hello-world
After running the above command, you will be asked to select server. Select, Use other server and press Enter:
? Set up a new Prisma server or deploy to an existing server? Use other server
Next, provide the endpoint of your server:
? Enter the endpoint of your Prisma server http://192.168.0.104:4466
Next, provide password that you indicated earlier in the configuration file and press Enter:
? Enter the management API secret [hidden]
Next, choose a name of your service and stage, then press Enter:
? Choose a name for your service hello-world
? Choose a name for your stage dev
Next, select the programming language and press Enter:
? Select the programming language for the generated Prisma client Prisma TypeScript Client
Next, change the hello-world directory and sync these changes to your server with the following command:
cd hello-world
prisma deploy
Your Prisma service is now running. You can connect to two different interface.
You can connect the management interface at http://192.168.0.104:4466management
. That can be used to deploy and manage Prisma services. You can connect the GraphQL API for your Prisma service at http://192.168.0.104:4466/hello-world/dev
.
Once you have done, you can proceed to the next step.
Now, open your web browser and type the URL http://192.168.0.104:4466/hello-world/dev
. You will be redirected to the following page:
You can create, delete, modify and update data in the backend using GraphQL. You can now send a mutation to create a new user and explore the functionality.
To do so, enter the following query in the left side of the page:
mutation {
createUser(data: { name: "Hitesh" }) {
id
name
}
}
Next, press the play button, you should see the results on the right side of the page as shown below:
38 posts | 4 followers
FollowHiteshjethva - March 2, 2020
Hiteshjethva - January 8, 2020
Hiteshjethva - March 2, 2020
Hiteshjethva - October 31, 2019
Hiteshjethva - January 8, 2020
Hiteshjethva - October 31, 2019
38 posts | 4 followers
FollowAlibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.
Learn MoreElastic and secure virtual cloud servers to cater all your cloud hosting needs.
Learn MoreAn elastic and horizontally scalable high-performance computing service providing the same computing performance as traditional physical servers including physical isolation.
Learn MoreBuild superapps and corresponding ecosystems on a full-stack platform
Learn MoreMore Posts by Hiteshjethva