Skip to content

Add TypeScript support #328

Open
Open

Description

@sebastianwahn

Since coffeescript is supported, i think it would be cool to have a typescript support also.

Expectation:
run node_modules/sequelize-cli/bin/sequelize model:create --name Collection --attributes id:integer,name:string --typescript

Will produce a model which ends in '.ts' instead of '.js'.

Justification:
In projects where typescript is used, javascript files are sometimes ignored (because they are produced through typescript). Therefore, the model will be ignored by default. Afterwards, a developer must edit the model/migration.

Result:
Tiny bit of work removed when developing with typescript.

Activity

  1. Vayrex commented on Aug 9, 2016

    @Vayrex

    Really good proposition +1

  2. felixfbecker commented on Aug 28, 2016

    @felixfbecker

    The problem I see here is that the pattern I found to be the best for TypeScript is totally different than the approach sequelize-cli uses. The pattern of the exported closure and a static associate() method is really hard to type (how do you type the models parameter?). Using late imports works much better.

  3. kenhowardpdx commented on Dec 16, 2016

    @kenhowardpdx

    Does "Add TypeScript support" also include generating migrations? If so the only change there is to save the file as .ts instead of .js.

  4. sebastianwahn commented on Dec 19, 2016

    @sebastianwahn
    Author

    Well, putting just .ts instead of .js as suffix would do the job in terms of a TypeScript file is created. Additionally, i would expect, that the class syntax of TypeScript would be supported and the typings for properties.

    class Test {
        
        private someNumber: number;
        private someString: string;
    
        public function setSomeNumber(myNumber: number): void {
            this.someNumber = myNumber;
        }
        public function getSomeNumber(): number {
            return this.someNumber;
        }
    
        ...
    }
    
  5. felixfbecker commented on Dec 19, 2016

    @felixfbecker

    What does this have to do with classes?

  6. sebastianwahn commented on Dec 19, 2016

    @sebastianwahn
    Author

    Sorry, you are right. Thought about another thing.
    Regarding the TypeScript, it would be sufficient to have the .ts ending and some TypeScript syntax (lambda functions, ...).

    Another feature would be to have the classes above instead of the current syntax:
    I can define the above class, it will be derived from a sequlize-parent-model (or something).
    Afterwards this class is scanned for its properties etc.
    This thing would go into the direction of EntityFramework (C#) and Hibernate (Java).

  7. felixfbecker commented on Dec 19, 2016

    @felixfbecker

    You are thinking about models, not migration files. A migration file just exports two functions, up and down.

  8. Mikhus commented on Jun 3, 2017

    @Mikhus

    +1 to this. Actually it should look not simply as file saved with .ts extension, but contain a slightly different contents in it, for example:

    import { QueryInterface } from 'sequelize';
    import { DataType } from 'sequelize-typescript';
    
    export async function up(query: QueryInterface) {
        /*
          Add altering commands here.
    
          Example:
          await query.createTable('users', { id: DataType.INTEGER });
          console.log('Table users created!');
        */
    }
    
    export async function down(query: QueryInterface) {
        /*
          Add reverting commands here.
    
          Example:
          await query.dropTable('users');
          console.log('Table users dropped!');
        */
    }

    So:

    1. it should save with different extension
    2. it should put different contents inside....

    Maybe it's even better to provide those possibilities in more generic way, like add an ability to provide user-defined template for those files. An I will be able to construct myself which dialect of javascript I use...

  9. kenhowardpdx commented on Jun 5, 2017

    @kenhowardpdx

    @Mikhus YES! Native module support is required. The nice thing about your sample code is that you only introduce TypeScript to utilize QueryInterface. 👍

  10. hronro commented on Jul 30, 2017

    @hronro

    +1

  11. limichange commented on Aug 7, 2017

    @limichange

    +1

  12. GeeWee commented on Aug 16, 2017

    @GeeWee

    +1

  13. MarcoPasqualini commented on Aug 21, 2017

    @MarcoPasqualini

    +1

  14. felixfbecker commented on Aug 21, 2017

    @felixfbecker

    Please don't +1 issues. It clutters the thread without adding value to the discussion and spams maintainers with notifications. Use GitHub reactions to upvote features.
    image

  15. 4 remaining items

  16. abdoolly commented on Apr 9, 2018

    @abdoolly

    checkout this package which may help you to make typescript models and ease out models initialization
    https://www.npmjs.com/package/ts-sequelize-models

  17. dantaub commented on Jul 24, 2018

    @dantaub

    I would request that instead of --typescript we would have --generatedExtension ts

    If coupled with registering transformations based on extension, we could support output in coffeescript, livescript, es6, etc

  18. added a commit that references this issue on Jun 5, 2019
  19. mankinchi commented on Jan 17, 2020

    @mankinchi

    Any updates on this?

  20. mankinchi commented on Jan 23, 2020

    @mankinchi

    @mrdulin I ended up using this

  21. intellix commented on Apr 25, 2020

    @intellix

    I assume the opinion has changed here since Sequelize 5 exports TypeScript types and could be of some value here. Would love type hints when writing my migrations as it's currently trial and error as the documentation often just says: "object" for options

  22. AnthonyLzq commented on Jul 23, 2022

    @AnthonyLzq

    It seems that there has been no progress on this requirement, so does anyone knows a reliable way to perform migrations with TS?

  23. CyanoFresh commented on Aug 3, 2022

    @CyanoFresh

    would like to know too

  24. mjovanc commented on Oct 5, 2022

    @mjovanc

    Any news? This seems like a very good proposition.

  25. virgenherrera commented on Oct 5, 2022

    @virgenherrera

    I'd better opt to migrate to Typeorm, since this library looks like this library is continuously ignoring Typescript support.

  26. mjovanc commented on Oct 5, 2022

    @mjovanc

    @virgenherrera It doesn't make any sense to ignore it (if they seriously are). To work with databases and not to use TypeScript (which enforces types) seems rather ridiculous IMO.

  27. WikiRik commented on Oct 5, 2022

    @WikiRik
    Member

    We're hard at work at improving TypeScript support for the main repo and we'll work on CLI after we've done most of our planned changes. v7 will have much better TypeScript support but we're open to PRs of the community in the meantime

  28. IArnaut2 commented on Feb 26, 2025

    @IArnaut2

    We're hard at work at improving TypeScript support for the main repo and we'll work on CLI after we've done most of our planned changes. v7 will have much better TypeScript support but we're open to PRs of the community in the meantime

    Any updates? It's 2025.

  29. virgenherrera commented on Feb 27, 2025

    @virgenherrera

    We're hard at work at improving TypeScript support for the main repo and we'll work on CLI after we've done most of our planned changes. v7 will have much better TypeScript support but we're open to PRs of the community in the meantime

    Any updates? It's 2025.

    Since the last release of this ORM was on Nov 3, 2023, I think that no further updates will come to this project

  30. nbkhope commented on Apr 27, 2026

    @nbkhope

    Any updates? It's 2025.

    Now it's 2026 😉

    Have you tried running sequelize cli like this?

    tsx ./node_modules/sequelize-cli/lib/sequelize db:migrate --config config/config.ts
    

    That way, instead of using node, it uses tsx to run the migrations written with the .ts extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions