RedwoodJS and PostgreSQL
07.12.2022 - 12:28
RedwoodJS is a powerful JavaScript framework, and PostgreSQL is a fast modern RDBMs, this two can work hand-in-hand. We can connect a RedwoodJS application to a PostgreSQL database to achieve optimum performance in our application. It seems far-fetched on how the two can connect, but yes they can so in this article, we learn how to connect a RedwoodJS application to a PostgreSQL database.


RedwoodJS

RedwoodJS is a shiny new full-stack React framework started by Tom Preston (co-founder of GitHub). The framework boasts of many unique features:

  • Cells.
  • advanced Routing system.
  • Services.
  • CLI Generators.
  • Automatic page-based code-split components

RedwoodJS is a full-stack React app, this means that the front-end and the back-end are all handled in one place. The frontend is a React app that can be deployed to the cloud or CDNs. The back-end is a GraphQL API that can be hosted anywhere and can be called from external or internal service.

RedwoodJS uses Prisma for data modeling, database connection, queries, and migrations. We write our model in a schema file, and the model is replicated in the database. Also, we are provided with a Prisma instance so we can make queries to the database.

I know you must be thinking that we write SQL commands and queries here. But no, Prisma will do all the heavy lifting, we won't have to write any SQL commands. Prisma exposes methods/functions that we use to create, insert, edit and delete records from the database tables. The SQL commands are generated and run by Prisma, that's awesome :) isn't it?

So don't worry if you aren't too good with SQL, Prisma has got your back.

Before we can use RedwoodJS generators we need to have the following binaries installed in our system.

Requirements

  • Node.js: Since RedwoodJS is Node.js based, we need the Node.js binary installed in our machine. Go to Node.js downloads page to download the binary suitable for your machine and OS.
  • NPM: This is a Node.js package manager, you don't need to install it, it comes bundled with the Node.js binary.

Scaffolding a RedwoodJS project

A RedwoodJS project can be created by running the below command:


yarn create redwood-app redwoodprj
# OR
npx create-redwood-app redwoodprj

The above command will create a RedwoodJS project in a redwoodprj folder

The default database of a RedwoodJS application is SQLite. This means that the content of our GraphQL API endpoints is stored in an SQLite database. SQLite is a relational database management system contained in a C library.

To verify this go to the api/db folder in the redwoodprj folder. Open the schema.prisma file:


datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
  binaryTargets = "native"
}

// Define your own datamodels here and run `yarn redwood prisma migrate dev`
// to create migrations for them and apply to your dev DB.
// TODO: Please remove the following example:
model UserExample {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

See the datasource db object, this is where the database configurations are set and the configuration determines how RedwoodJS uses Prisma to connect to our database. The provider specifies the data source connector to use, here it is set to sqlite, this is because we are using an SQLite database.

The url points to the database connection URL of the database. Let's see the value of the env variable DATABASE_URL.


DATABASE_URL=file:./dev.db

It is a file path pointing to an SQLite database file. When you run your first migrations this dev.db file will be created in the db folder.

This datasource db object is where we can set our RedwoodJS to use different databases. Prisma majorly supports SQL databases like MySQL, PostgreSQL, MS SQL, SQL Server, SQLite, but recently Prisma is beginning to support NoSQL databases, MongoDB.

So this datasource object is where we can set the PostgreSQL database in a RedwoodJS app. Before we do that we have to download, install and start a PostgreSQL server in our machine.

We will learn how to do all that in the below sections.

Postgres DB setup

PostgreSQL is a relational database management system that extends the SQL standards with great upgrades, it was first released on 8 July 1996. PostgreSQL or commonly known as Postgres is an advanced version of SQL that provides more features to SQL. It provides functions to SQL like triggers, subqueries, and user-defined types and functions. Postgres is definitely more refined than other RDBMS.

To download the Postgres binary head over to its downloads page, there you can select and download the packages and installers for your OS.

You can peep into the source code here if you are feeling like a Rockstar. Also, you can find Postgres documentation here.

I listed each OS installers page below:

Now, install the downloaded package and start the server. During the installation of the PostgreSQL binary, make sure to write out the below details:

  • The port number, is usually 5432 but make sure to write out if it changes.
  • The database username.
  • The database host name (it is usually localhost).
  • The database name.



Bạn đang tìm hiểuHọc tiếng Đức & Du học Đức?
Đội ngũ chuyên gia giàu kinh nghiệm của chúng tôi sẵn lòng tư vấn và đưa ra phương án phù hợp nhất

CÔNG TY TNHH ĐINH NGUYỄN GIA

Công ty TNHH Đinh Nguyễn Gia là đơn vị tư vấn giáo dục uy tín có trụ sở tại TP. HCM. Sở hữu đội ngũ chuyên viên giàu kinh nghiệm cùng mạng lưới đối tác toàn cầu.

Địa chỉ

99 Hoàng Sa, Phường Võ Thị Sáu, Quận 3, TP. Hồ Chí Minh, Việt Nam.
Điện thoại: 090 6060 279
Email: info@dinhnguyengia.edu.vn
Website: dinhnguyengia.edu.vn