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

Projects
None yet
5 participants
@prashaantt
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor Author

prashaantt commented Nov 3, 2016

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

prashaantt added some commits Nov 3, 2016

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

This comment has been minimized.

@elhigu

elhigu Nov 29, 2016

Collaborator

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.

This comment has been minimized.

@prashaantt

prashaantt Nov 29, 2016

Author Contributor

Sure, you mean this — ?

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

This comment has been minimized.

@elhigu

elhigu Nov 30, 2016

Collaborator

yep definitely better

This comment has been minimized.

@prashaantt

prashaantt Nov 30, 2016

Author Contributor

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 tgriesser:master Dec 1, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@elhigu

This comment has been minimized.

Copy link
Collaborator

elhigu commented Dec 1, 2016

thanks!

@prashaantt prashaantt deleted the prashaantt:typescript branch Dec 2, 2016

@barinbritva

This comment has been minimized.

Copy link

barinbritva commented Jan 20, 2017

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

This comment has been minimized.

Copy link
Contributor Author

prashaantt commented Jan 20, 2017

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

This comment has been minimized.

Copy link

barinbritva commented Jan 20, 2017

@prashaantt, thanks!

@elhigu

This comment has been minimized.

Copy link
Collaborator

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

This comment has been minimized.

Copy link

leonardfactory commented Feb 7, 2017

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 added a commit to elhigu/knex that referenced this pull request Feb 15, 2017

Support knexfile, migration and seeds in TypeScript (tgriesser#1769)
* 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

This comment has been minimized.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.