DEV Community

Aidan
Aidan

Posted on

5 2 2 2 2

Migration - Module query with TypeORM version 0.3.x

Hi everyone!!

I like to use the library TypeORM •ᴗ••ᴗ••ᴗ••ᴗ•
As usual, When stating new project, i typically copy the database modules & config script migration from the old source. However, with TypeORM version 0.3.x, it doesn't seem to work. ¯_(ツ)/¯ ¯_(ツ)/¯ ¯_(ツ)_/¯
I have researched and felt confused while applying TypeORM to the project.

So, I want to share it with everyone.

Prepare:

New app with nestjs
Install yarn add mysql2 typeorm @nestjs/typeorm


Repository
https://bitbucket.org/main-28/typeorm-migration-0.3/src/main/


Setup database module

.
├── src/
│   ├── database/
│   │   ├── migrations # This folder contains migrate file
│   │   ├── data-source-migration.ts # This file config run migration create, run, ...
│   │   └── database.module.ts # This file config query to database
│   └── app.module.ts
└── package.json
Enter fullscreen mode Exit fullscreen mode

Config package json file

"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli",
"migration:create": "yarn typeorm migration:create ./src/database/migrations/${name}",
"migration:create:window": "yarn typeorm migration:create ./src/database/migrations/%name%",
"migration:run": "yarn typeorm -- --dataSource=./src/database/data-source-migration.ts migration:run",
"migration:revert": "yarn typeorm -- --dataSource=./src/database/data-source-migration.ts migration:revert",
Enter fullscreen mode Exit fullscreen mode

File name format
This format would help you and the team quickly understand what someone has edited.
Please provide feedback on this format for me.

- Create: create-{table-name}-table
- Modify > 1: modify-{modify-name}-{table-name}-table
- Modify = 1: add-{field-name}-{table-name}-table
- Modify = 1: update-{field-name}-{table-name}-table
- Modify = 1: delete-{field-name}-{table-name}-table
Enter fullscreen mode Exit fullscreen mode

Short script

Create table post name=create-post-table yarn migration:create
Run migration yarn migration:run

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
lucdticdvn profile image
lucdticd-vn

I feel that this world is wonderful when there are people like you. I am so happy to read these heartfelt shares. Thank you.

Collapse
 
trantn profile image
Aidan

thank you @lucdticdvn

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs