sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

No documentation for db init and migration

Open FreifeldRoyi opened this issue 5 years ago • 11 comments

I'm finding myself struggling with finding answers about how to init my db and doing migrations. Eventually I managed to use the sequelize migrations with typescript in a type safe way, but there are more things sequelize-typescript related that I still haven't figured out and I feel it would be of great help to add them to the readme. All related to DB init and migrations:

  1. What type and best way to define the default Id type? flags (autoincrement, primarykey...)
  2. How to define the temporal timestamps types? (createdDate, updatedAt, deletionDate) + flags (most importantly, default values) ** I can probably think of more stuff but these 2 are the ones I'm currently facing

I feel that the init & migrations should be handled more gracefully with this lib. Generally speaking It would be great if the migration script could be generated from the annotations

FreifeldRoyi avatar Apr 22 '20 07:04 FreifeldRoyi

Yes! I really like using sequelize-typescript, but migrations is the one thing I cant figure out how to do in an easy way.

What I want, is to define a model like this /src/models/user.ts:

export class userModel extends Model {
    @AllowNull(false)
    @Column({
        type: DataType.TEXT,
    })
    name: string;
    
    @AllowNull(false)
    @Column({
        type: DataType.DATE,
    })
    birthDate: Date;
}

And then to make sequelize migrations like this: sequelize migration:generate --models-path ./src/models --name added_new_userModel

Anybody out there that thinks this can be done? Or has any idea about what needs to be done. If I have the time, I can contribute to this :smile:

hmol avatar Apr 22 '20 10:04 hmol

I'm in the same boat. Looking at sequelize-cli-typescript now, but I cannot get it to work yet.

kayvanbree avatar Apr 22 '20 11:04 kayvanbree

@hmol Do you rewrite your schemas in that migration file or does it generate for you, mine did generate a file but it just has some instructions on how to do the migration instead of actually detecting that would be so much to expect I guess.

@FreifeldRoyi Can you elaborate the safe way you mentioned please.

ertankara avatar May 30 '20 18:05 ertankara

@ertankara Unlike the official sequelize manual, I used the DataTypes and QueryInterface types from sequelize to create the functions:

import { DataTypes, QueryInterface } from 'sequelize';
export async function up(qi: QueryInterface) {
   try {
       const ret = await qi.createTable('table_name', {id: {type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4}})
       // .......
   } catch (e) {
       console.error(e);
       throw e;
   }
}

It's not that it's REALLY typesafe, but at least I can get better code completions. What I'd like to get is some automatic way to map between the decorators, already defined with this project, to create the db or generate the migration script. Or at least a documentation on how to map the decorated fields to the migrations script

FreifeldRoyi avatar Jun 05 '20 19:06 FreifeldRoyi

@ertankara I am using the following command for migrations:

tsc src/database/migrations/* --outdir dist/migrations && npx sequelize-cli db:migrate --url 'postgres://'$(dotenv -p DB_USERNAME)':'$(dotenv -p DB_PASSWORD)'@'$(dotenv -p DB_HOST)':'$(dotenv -p DB_PORT)'/'$(dotenv -p DATABASE)'' --migrations-path dist/migrations

As I am using dotenv-cli along with dotenv package for reading .env file, I can read the database credentials from .env and create the postgresql url easily. We do not need a configuration file if --url flag is used. What do you think about this solution?

Sample migration script (src/database/migrations/16085945884-test.ts) Thanks @freifeld

import { DataTypes, QueryInterface } from 'sequelize';

export async function up(qi: QueryInterface) {
try {
await qi.addColumn('PaymentMethods', 'number', {
allowNull: true,
type: DataTypes.DOUBLE
})
// .......
} catch (e) {
console.error(e);
throw e;
}
}

export async function down(qi: QueryInterface) {
try {
await qi.removeColumn('PaymentMethods', 'number')
// .......
} catch (e) {
console.error(e);
throw e;
}
}

piyusharora1989 avatar Jan 04 '21 16:01 piyusharora1989

Well, on behalf or our tribe, you're welcome @piyusharora1989, but it seems you meant to thank @FreifeldRoyi

freifeld avatar Jan 04 '21 16:01 freifeld

Yeah I meant to thank @FreifeldRoyi :)

piyusharora1989 avatar Jan 05 '21 04:01 piyusharora1989

@ertankara Unlike the official sequelize manual, I used the DataTypes and QueryInterface types from sequelize to create the functions:

import { DataTypes, QueryInterface } from 'sequelize';
export async function up(qi: QueryInterface) {
   try {
       const ret = await qi.createTable('table_name', {id: {type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4}})
       // .......
   } catch (e) {
       console.error(e);
       throw e;
   }
}

It's not that it's REALLY typesafe, but at least I can get better code completions. What I'd like to get is some automatic way to map between the decorators, already defined with this project, to create the db or generate the migration script. Or at least a documentation on how to map the decorated fields to the migrations script

Thanks a lot for sharing @freifeld . I'm wondering if you figured out a way to creat table such that we can use the Model that we defined in sequelize-typescript? For example,

import {TestModel} from ".../your/db/model/folder/TestModel";
queryInterface.createTable(TestModel)

In addition, I'm wondering how can you import modules? in the migration js files? since those files are generated start with module.exports?

zhangchi1 avatar May 20 '21 16:05 zhangchi1

@zhangchi1, Seems you (too) meant @FreifeldRoyi and not me :)

freifeld avatar Jun 17 '21 11:06 freifeld

Well, I don't get what the result of this discussion is. Have you found a way to use migrations?

tobiasmuecksch avatar Dec 12 '21 07:12 tobiasmuecksch

+1 following, useful link https://stackoverflow.com/questions/21105748/sequelize-js-how-to-use-migrations-and-sync

Ren22 avatar Jun 19 '22 13:06 Ren22