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

Support knexfile, migration and seeds in TypeScript #1769

Merged
merged 6 commits into from
Dec 1, 2016

Conversation

prashaantt
Copy link
Contributor

@prashaantt prashaantt commented Nov 3, 2016

This will allow using knex in a TypeScript project. These commands will now work:

$ knex init -x ts
$ knex migrate:make migration_name
$ knex seed:make seed_name

knex will now generate and automatically pick up knexfile.ts, migration_name.ts, seed_name.ts, assuming ts-node is available to execute from within the project.

If you like this, I'll also update the docs to indicate that this option is now available.

@prashaantt
Copy link
Contributor Author

I just saw this has already been requested in #1564.

// Deletes ALL existing entries
return knex("table_name").del()
.then(function () {
return Promise.all([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise.all should not be promoted here, since people are using it wrong in places where order of queries matters. It should be fixed in every other stub as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, you mean this — ?

.then(function () {
    return knex("table_name").insert([
        { id: 1, colName: "rowValue1" },
        { id: 2, colName: "rowValue2" },
        { id: 3, colName: "rowValue3" }
    ]);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep definitely better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this branch for the TypeScript stub, have a patch ready for the other stubs once you find it good to merge.

@elhigu elhigu merged commit ba5ae37 into knex:master Dec 1, 2016
@elhigu
Copy link
Member

elhigu commented Dec 1, 2016

thanks!

@barinbritva
Copy link

Can anybody help me run Knex with Typescript?
I created a new project for experement.
My package.json file:

{
  "dependencies": {
    "knex": "github:tgriesser/knex#49aa6f73142a7710f33acc095d4b9cdfe6919f46"
  },
  "devDependencies": {
    "@types/knex": "0.0.39",
    "@types/node": "^7.0.0",
    "ts-node": "^2.0.0",
    "typescript": "^2.1.5"
  }
}

My tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "sourceMap": true,
        "typeRoots": [
            "node_modules/@types"
        ]
    }
}

I run node_modules/.bin/knex init -x ts, and then node_modules/.bin/knex seed:make test-seed. Console output:

Requiring external module ts-node/register
Using environment: development
C:\Users\barin\project\knex-test\node_modules\knex\lib\index.js:2
import Raw from './raw';
^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\barin\project\knex-test\node_modules\knex\knex.js:8:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)

It does not understand ES6 syntax. But after install Knex by commit hash I processed npm install and npm run build in node_modules/knex folder.

@prashaantt
Copy link
Contributor Author

The issue here is that the source isn't getting transpiled to commonjs modules because .babelrc is not getting included in the node_modules/knex. Copying .babelrc manually and then npm install inside it works for me.

@elhigu When can we expect the next release?

@barinbritva
Copy link

@prashaantt, thanks!

@elhigu
Copy link
Member

elhigu commented Jan 21, 2017

@prashaantt I haven't thought about it, but I could roll out next version when I got some spare time... can't say exactly when.

@leonardfactory
Copy link

Hi @elhigu! Have you got any news about release?
Sorry to bother you, I appreciate and respect your work and don't want in any way to put pressure. Just asking. Thank you very much!

elhigu pushed a commit to elhigu/knex that referenced this pull request Feb 15, 2017
* Support knexfile, migration and seeds in TypeScript

* Look for .ts seed files

* Fix linter warning for max-len

* Enforce migration scripts to return a promise

* Remove Promise.all example from stub
@Seikho
Copy link

Seikho commented Feb 18, 2017

This is a breaking change for existing TypeScript users.

This has broken my existing project migrations by now using TypeScript files by default where I have been compiling to JavaScript.

Using ts-node is not typical run-time behaviour. Can this behaviour be opt-in?

mmozcelik pushed a commit to mailshake/knex that referenced this pull request Sep 8, 2023
* Support knexfile, migration and seeds in TypeScript

* Look for .ts seed files

* Fix linter warning for max-len

* Enforce migration scripts to return a promise

* Remove Promise.all example from stub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants