Build and Deploy a Backend With Fastify & Platformatic

Rate this content
Bookmark
The Platformatic workshop focuses on building and deploying a backend using PlatformaticDB and Fastify. The tool allows users to create OpenAPI and GraphQL schemas from their databases without writing code. To get started, ensure you have Node.js version 18.18.0 or higher and clone the provided repository. The workshop covers how to set up environment variables and avoid committing secrets to your Git repository. It also explains how to use GraphiQL for actions like getting movies by ID, counting movies, and running real-time subscriptions. The video emphasizes the importance of managing database migrations and generating types for autocompletion. Additionally, it demonstrates how to increment likes on a quote using SQL for efficient database operations. The session also touches on deploying the backend to Platformatic Cloud and configuring CORS for the UI.

From Author:

Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:

- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).

- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 


In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.


This workshop has been presented at JSNation 2023, check out the latest edition of this JavaScript Conference.

FAQ

In PlatformaticDB, generated types represent the schema of your database tables as interfaces, helping with autocompletion and reducing the need for manual schema management.

PlatformaticDB is a tool that allows you to create both Open API and Graphical schema from your database without writing any code. It's part of the Platformatic framework and can be customized using Node.js.

To check your Node.js version, enter 'node -v' in your terminal. Ensure it is version 18.18.0 or higher for compatibility with PlatformaticDB.

To start with PlatformaticDB, clone the repository, ensure Node.js is installed, and follow the setup instructions provided in the slides or the shared material online.

The setup process for PlatformaticDB involves running a generator to configure the database and environment, applying default migrations, and optionally creating plugins or generating types.

You can access the slides and materials for the Platformatic workshop by visiting the provided URL, platformatic.dev.dbungall.com, where all the material is publicly available.

PlatformaticDB uses environment variables to configure application settings securely. These variables are used to fill in configuration details without hard-coding them into the application's source code.

Migrations in PlatformaticDB are used to manage database schema changes. They help in versioning the database schema by tracking changes and applying them incrementally.

Matteo Collina
Matteo Collina
104 min
07 Jun, 2023

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to Platformatic Workshop

Short description:

Welcome to the platformatic workshop. We have a small crowd today, so feel free to ask questions. The slides and material are available at platformatic.dev.dbungall.com. PlatformaticDB is a tool that allows you to create open API and graphical schema from your database without coding. Make sure you are running node.js greater than 18.18.0. To get started, clone the repo or follow the slides. The workshop is incremental, and we will build an application to store and like movies. Run the command 'create platformatic' to get started.

Hi everybody. Welcome to this nice workshop about platformatic. And here you go. We have just a small crowd. So I kindly ask, if possible, if you can turn on the camera, otherwise I'm just talking to the wall. And I really don't want to talk to the wall for three hours or two or whatever it is. So talking to the wall is not nice. Thank you Jaroslav, Thank you Yusem but he's booting up and thank you Gianmarco.

Hello. Hi folks. It seems that we have a small crowd today, so happy days. And you can do things step by step, so that there is no point or no problem whatsoever in doing this stuff. And maybe a few more people are going to join in a few moments, but we'll see. Okay, so feel free to ask me questions, interrupt me anytime, hopefully if we get it done earlier than that. We'll try, okay. And that's it!

Okay? So first thing first, you can find these URLs, these slides at this URL. You will see it in here, platformatic.dev.dbungall.com. So you could take this thing and just copy it up. And I'm pasting there in the chat, too, so here you go. And you can check it out there. So everything, all the material is public, so you can just check it out.

So what is PlatformaticDB? Well, PlatformaticDB is the first tool that we shaped as part of platformatic. And it's a tool that enables you to create both open API and graphical schema from your database without having to write any code. And you can just use Node.js to customize it and so on and so forth. So, yeah, let's get into it. First thing's first, please do a safety check that you are running node.js greater than 18.18.0. So how do you do that? You do, you go, we can go back in our terminal and wait a second, yeah, close. And we can go back in our terminal and we can do node-diminusv and you should be able to get that value, something like that. Please let me know if you're stuck.

So what do we do to get started, you can go and clone this repo. Now if you're following the slide you can just click this nice button here and you can go back in your route and just pass this the topic and just run the command. Run the command. So these these things contain both the steps. It contains both the steps and the slides. So if we go and open this up you can find that we have these slides which are these ones that we just talked and those are also the value steps for setting up our application. Does it make sense? Any problem with using Visual Studio code? I do have many. I'm using Vim. I've never exited so sorry. Still the same instance. The workshop is incremental. So every step that you can find will see that you have 0-1, 0-2, 0-3, so on and so forth. Each one of those steps is actually straight forward and based upon the previous one. So you can jump through steps if you want to. Very important. If you don't need to do anything you can just do some stuff. What we're going to build will be a small application that will enable you to do some sort of sorts of APIs. Sorry an application that will enable you to store your movies and then like them. Very simple. First thing we copy these bits here and we run create platformatic. So how do we do that? Well we can run this code, we can cd and we can run create platformatic.

2. Platformatic Generator and Generated Files

Short description:

The generator creates a platformatic db.json config file with autocompletion. It also generates a plugin file and types for the database representation. These types provide autocompletion and are loaded via a global.tls file. Running the command 'Cd movie quotes apps mpm start' generates an open API documentation and graphical interface. We can interact with the API using various HTTP methods and query the database using GraphiQL. Environment variables are stored in the .env.sample file, which should be committed instead of the .env file.

Note that this says npx create platformatic latest at latest what you could do also is, do npm create space platformatic at latest. The two things are identical.

Okay and then it prompts some questions and here are some of the answers that we are going to do. We are picking DB, we are creating in this folder. We want to create a default migration, create a plugin maybe. Yes, and don't you know TypeScript port and we want to run npm install. Then we want to apply the default migrations and we also generate the types. Come on. And we skip the GitHub actions, we are not going to create a repo for this. Okay.

Were you able to run the generator? Yes, I was able. I just wanted to ask what are the migrations, what are these generated types if you're not using TypeScript? Oh, yes. I'll show that in a moment. Okay. Let me show you. So first of all, what files it generated is for us. So it generated us a platformatic db.json. This is our config file. Note that our config file is, sorry, it's, you see that it has full autocompletion. So you will see, you will be able to autocomplete the values with Visual Studio Code 2 using the same language server. It uses environment variables to fill things in, so it's automatically completed. What are the types? So you see here that it has a plugin file, which is essentially these, which is a Fastify plugin. We'll talk about that in a moment. The types can then be used for the representation of our database, so in fact it includes all the properties. So for example you can see that you have a movie here has an idea in the title, which is the content of the default migration. So it automatically creates for each table, it automatically creates an interface for each one of our table so that you can avoid some time. Does it make sense? Yeah, yes, thank you. And all of those are loaded via this global.tls file and which then is loaded here as part of the plugin. So you could then use those types in this JavaScript file and we'll talk about that in a moment. Essentially they're there to provide autocompletion. Do you like autocompletion? Who doesn't like autocompletion?

So let's start the thing. What we can do? Cd movie quotes apps mpm start. What has this generated us? It has generated us an open API documentation and graphical. We can open up those two links. Taking a look at the first one, you can see that it has get, post, put, and all the things that we would expect it to be. These are essentially just normal stuff, the normal things that you can imagine. We can run a SlashRequest and you see it's an empty array. We could potentially put post. Where is it? Post. Post in there a movie, like for example Harry Potter. And it has stored it with ID 1. And then if we go back to the top and run the query again, you will see that we got Harry Potter. Then we could actually do the same using GraphiQL. So for example, you could do a query like movies and title, and it works the same. Hey, we are at this URL right now. And you can follow the slides earlier than that. Note that we have here, we have the environment variables, OK? Where is platformatic fill in those environment variables from? We have two files here. One is.env, one is.env.sample. Do not commit your.env file but commit your.env.sample file. Why you need to commit that one? Because this is typically personal information.

QnA

Watch more workshops on topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
ESM Loaders enhance module loading in Node.js by resolving URLs and reading files from the disk. Module loaders can override modules and change how they are found. Enhancing the loading phase involves loading directly from HTTP and loading TypeScript code without building it. The loader in the module URL handles URL resolution and uses fetch to fetch the source code. Loaders can be chained together to load from different sources, transform source code, and resolve URLs differently. The future of module loading enhancements is promising and simple to use.
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
This talk covers various techniques for getting diagnostics information out of Node.js, including debugging with environment variables, handling warnings and deprecations, tracing uncaught exceptions and process exit, using the v8 inspector and dev tools, and generating diagnostic reports. The speaker also mentions areas for improvement in Node.js diagnostics and provides resources for learning and contributing. Additionally, the responsibilities of the Technical Steering Committee in the TS community are discussed.