sequelize-typescript
sequelize-typescript copied to clipboard
No documentation for db init and migration
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:
- What type and best way to define the default Id type? flags (autoincrement, primarykey...)
- 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
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:
I'm in the same boat. Looking at sequelize-cli-typescript now, but I cannot get it to work yet.
@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 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
@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;
}
}
Well, on behalf or our tribe, you're welcome @piyusharora1989, but it seems you meant to thank @FreifeldRoyi
Yeah I meant to thank @FreifeldRoyi :)
@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, Seems you (too) meant @FreifeldRoyi and not me :)
Well, I don't get what the result of this discussion is. Have you found a way to use migrations?
+1 following, useful link https://stackoverflow.com/questions/21105748/sequelize-js-how-to-use-migrations-and-sync