This repository serves as a Node.js TypeScript project that utilizes Drizzle for managing database tables in a Neon distributed Postgres database. The main purpose of this repository is to define database tables, generate migrations, apply migrations, and generate and insert test data. The following instructions will guide you through the workflow for using this repository effectively.
Before proceeding with the setup and workflow, ensure that you have the following prerequisites:
- Node.js (v18+ recommended)
- NPM (v9+ recommended)
- Visual Studio Code
- Prettier Extension for Visual Studio Code
- Access to a neon database and ability to get connection information.
- Install Visual Studio Code, if not already installed.
- Install the prettier extension for vscode, if not already installed.
- Open settings (File > Preferences > Settings or Ctrl + ,).
- Search for "Editor: Default Formatter" and select "Prettier - Code formatter" from the dropdown.
- Search for "editor.formatOnSave" and enable it.
- Save your settings.
- Run
npm run setup
from the project root directory.
- Go to your project dashboard on Neon and select the production branch in the "Connection Details" section. Click on "Direct connection" in the "Connection Details" section and copy the connection string.
- Go to your repository on Github and navigate to Settings > Secrets.
- Click on "New repository secret" and create a secret named
DATABASE_URL_PROD
with the value of the connection string you copied from Neon, adding?sslmode=require
to the end of the string. - Go to your project dashboard on Neon and select the dev branch in the "Connection Details" section. Click on "Direct connection" in the "Connection Details" section and copy the connection string.
- Go to your repository on Github and navigate to Settings > Secrets.
- Click on "New repository secret" and create a secret named
DATABASE_URL_DEV
with the value of the connection string you copied from Neon, adding?sslmode=require
to the end of the string.
Create a new branch on the Neon with 'dev' as it's parent branch. This will provide you with a separate environment for making and testing changes to the database schema.
To establish a connection between your local environment and Neon database, follow these steps:
a. On Neon navigate to your project dashboard and select the branch you created in step 1.
b. Click on "Direct connection" in the "Connection Details" section and copy the connection string.
c. Create a .env
file in the root directory of the project add the following lines to it:
DATABASE_URL=<connection string>?sslmode=require
d. Replace <connection string>
with the connection string you copied from Neon.
In order to run the required scripts and set up the project, you need to download all the necessary dependencies and set up husky hooks. Run the following command in your terminal:
npm run setup
Navigate to the ./schema
directory and make the necessary changes to the table schema files. These schema files define the structure of the database tables.
To automatically generate migration files for your changes, run the following command:
npm run DB:createNewMigration
This command will check your schema and refreshTestData files to ensure they are valid, and then generate migration files based on the changes you made.
Once the migration files are generated, review them to ensure they reflect the intended changes accurately. Confirm that the migration files align with your desired table schema modifications.
To apply the generated migration files to the Neon database, execute the following command:
npm run DB:applyMigration
This command will apply the migrations and update the database schema accordingly.
After applying the migrations, it is recommended to insert test data into the newly changed tables. To accomplish this, run the following command:
npm run DB:refreshTestData
This command will update the tables with the relevant test data.
At this point, you can test your changes and iterate the process if needed. If further modifications to the table schema are required, repeat steps 4 to 8 accordingly.