Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript support #328

Open
sebastianwahn opened this issue Jul 21, 2016 · 29 comments
Open

Add TypeScript support #328

sebastianwahn opened this issue Jul 21, 2016 · 29 comments
Labels

Comments

@sebastianwahn
Copy link

sebastianwahn commented Jul 21, 2016

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.

@Vayrex
Copy link

Vayrex commented Aug 9, 2016

Really good proposition +1

@felixfbecker
Copy link

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.

@kenhowardpdx
Copy link

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

@sebastianwahn
Copy link
Author

sebastianwahn commented Dec 19, 2016

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;
    }

    ...
}

@felixfbecker
Copy link

What does this have to do with classes?

@sebastianwahn
Copy link
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).

@felixfbecker
Copy link

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

@Mikhus
Copy link

Mikhus commented Jun 3, 2017

+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...

@kenhowardpdx
Copy link

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

@hronro
Copy link

hronro commented Jul 30, 2017

+1

3 similar comments
@limichange
Copy link

+1

@GeeWee
Copy link

GeeWee commented Aug 16, 2017

+1

@MarcoPasqualini
Copy link

+1

@felixfbecker
Copy link

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

@virgenherrera
Copy link

thks @felixfbecker i'do that.

@georgeedwards
Copy link

@felixfbecker I have not used it in a project (but am planning to on mynext project), but have you seen the [sequelize-typescript)[https://github.com/RobinBuschmann/sequelize-typescript] project - it looks like a really interesting way of making Sequelize a bit more typescripty...

I am not sure if that makes the generated application prescribe a package that people might not be familiar with / want.

@sushantdhiman sushantdhiman mentioned this issue Sep 13, 2017
21 tasks
@ncphillips
Copy link

ncphillips commented Sep 27, 2017

Another option would be to treat the models directory as a package, and instead of generating different code for typescript, just generating the declaration files.

This might look something like:

Directory Structure

models/
  index.js
  index.d.ts
  package.json
  user.js
  user.d.ts

package.json

{
  "name": "create-observable-thunk",
  "main": "./index.js",
  "typings": "./index.d.ts",
}

types.d.ts

import * as Sequelize from "sequelize"
export const Sequelize: Sequelize
export const sequelize: Sequelize.Sequelize
export default namespace db {

}

user.d.ts

import * as Sequelize from "sequelize"

interface Attributes {
  // ...
}

export default namespace db {
  Cat: Sequelize.Model
}

Note I haven't made sure these typings made sense.

@douglas-treadwell
Copy link

douglas-treadwell commented Dec 8, 2017

Until sequelize-cli has a TypeScript support option, the people on this thread may want to check out https://github.com/douglas-treadwell/sequelize-cli-typescript. (Note that it is already available on NPM.). I forked sequelize-cli and made a version that outputs TypeScripts (including migrations). See the README section "Differences...", but otherwise it works like regular sequelize-cli and should be a drop-in replacement.

Also, by looking at the changes in my fork the maintainers of sequelize-cli or other contributors may be able to notice what sort of changes would be needed to support TypeScript in the mainline project.

@abdoolly
Copy link

abdoolly commented Apr 9, 2018

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

@dantaub
Copy link

dantaub commented Jul 24, 2018

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

codetriage-readme-bot pushed a commit to codetriage-readme-bot/cli that referenced this issue Jun 5, 2019
append solution URL path consistently
@mankinchi
Copy link

Any updates on this?

@mankinchi
Copy link

@mrdulin I ended up using this

@intellix
Copy link

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

@AnthonyLzq
Copy link

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

@CyanoFresh
Copy link

would like to know too

@mjovanc
Copy link

mjovanc commented Oct 5, 2022

Any news? This seems like a very good proposition.

@virgenherrera
Copy link

virgenherrera commented Oct 5, 2022

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

@mjovanc
Copy link

mjovanc commented Oct 5, 2022

@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.

@WikiRik
Copy link
Member

WikiRik commented Oct 5, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests