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

Serverless framework + webpack #4788

Closed
Schavras opened this issue Sep 20, 2019 · 3 comments · Fixed by #5302
Closed

Serverless framework + webpack #4788

Schavras opened this issue Sep 20, 2019 · 3 comments · Fixed by #5302

Comments

@Schavras
Copy link

Issue type:

[x] question

Database system/driver:

[x] mysql / mariadb (+ aurora data api)

TypeORM version:

[x] latest

Steps to reproduce or a small repository showing the problem:

sls create --template aws-nodejs-typescript --name typeorm-webpack
npm i --save typeorm
npm i --save typeorm-aurora-data-api-driver  (or probably any other driver)

Add this on handler.ts

await createConnection({
	type: 'aurora-data-api',
	database: 'xxxx',
	secretArn: 'arn:aws:secretsmanager:xxxx',
	resourceArn: 'arn:aws:rds:xxxxxx',
	region: 'xx-xxx-x',
});
sls deploy

And then invoke the lamda from the url.

The issue

Greeting and thanks for this amazing library.

I have a problem while i try to make typeorm work with the serverless framework, and especially the template with typescript+webpack +aws. I tried this with the aurora-data-api, but I have a similar problem with MySQL.

I think it's a 2-part problem.

The first problem is the dynamic load of typeorm. Webpack is compiling code and libraries on a single file, while perfoming tree-shaking (correct me if I'm wrong).

So when you invoke the function on aws lambda, you would get

{
    "errorType": "Error",
    "errorMessage": "Cannot find module '/var/task/node_modules/typeorm-aurora-data-api-driver'",
    "code": "MODULE_NOT_FOUND",
    "stack": [
        "Error: Cannot find module '/var/task/node_modules/typeorm-aurora-data-api-driver'",
        "    at webpackEmptyContext (/var/task/src/api/main.js:25552:10)",
        "    at Function.PlatformTools.load (/var/task/src/api/main.js:990:47)",
        "    at AuroraDataApiDriver.loadDependencies (/var/task/src/api/main.js:81265:60)",
        "    at new AuroraDataApiDriver (/var/task/src/api/main.js:80838:14)",
        "    at DriverFactory.create (/var/task/src/api/main.js:79471:24)",
        "    at new Connection (/var/task/src/api/main.js:21630:59)",
        "    at ConnectionManager.create (/var/task/src/api/main.js:21563:26)",
        "    at /var/task/src/api/main.js:516:66",
        "    at step (/var/task/src/api/main.js:211:23)",
        "    at Object.next (/var/task/src/api/main.js:192:53)"
    ]
}

My workaround for that was to add on node_modules/typeorm/platform/PlatformTools.js file, on the load function the following code:

 case "typeorm-aurora-data-api-driver":
     return require(/* webpackMode: "eager" */ "typeorm-aurora-data-api-driver");

This way webpack is forced to import the module, which leads as to the next error.

When you invoke again the lambda, you get this error:

 TypeError: this.DataApiDriver is not a constructor
at new AuroraDataApiDriver (/var/task/src/api/main.js:113741:23)
at DriverFactory.create (/var/task/src/api/main.js:112373:24)
at new Connection (/var/task/src/api/main.js:28271:59)
at ConnectionManager.create (/var/task/src/api/main.js:28204:26)
at /var/task/src/api/main.js:747:66
at step (/var/task/src/api/main.js:442:23)
at Object.next (/var/task/src/api/main.js:423:53)
at /var/task/src/api/main.js:416:71
at new Promise (<anonymous>)
at Module.__awaiter (/var/task/src/api/main.js:412:12)

After digging around, i saw that the way you have compiled the library, the module is loaded like
{default: DataApiDriver}

So my second "workaround" was change the loadDependencies method of node_modules/typeorm/driver/aurora-data-api/AuroraDataApiDriver.js to:

this.DataApiDriver = PlatformTools_1.PlatformTools.load("typeorm-aurora-data-api-driver").default;

So with those 2 changes, i was able to use the 2 libraries(typeorm + typeorm-aurora-data-api-driver). Obviously, changing directly the library files is a big no-no, but also because those changes are specific for aws+serverless framework+ webpack, i guess I can't make a PR.

I think my backup plan is to fork this and try to stay up to date, but i would like to discuss it if there is a cleaner solution.

Thanks in advance!

@h-takenori
Copy link

About the second error.
I edited 'AuroraDataApiDriver.ts' and succeeded with both two patterns: using aws+serverless framework+ webpack and without using webpack.

src/driver/aurora-data-api/AuroraDataApiDriver.ts

# before
this.DataApiDriver = PlatformTools.load("typeorm-aurora-data-api-driver");

# after 
const driver = PlatformTools.load("typeorm-aurora-data-api-driver");
this.DataApiDriver = driver.default || driver;

@coyoteecd
Copy link
Contributor

coyoteecd commented Jan 8, 2020

I believe this is related to this: webpack/webpack#4742
The author of typeorm-aurora-data-api-driver uses rollup when publishing the package (explicitly mentioned here). PlatformTools.load() just does a simple require, which loads the module as:
Module {default: , __esModule: true, Symbol(Symbol.toStringTag): "Module"}

The fix provided by @h-takenori worked for us as well; I will submit a pull request so it gets included in the main package.

coyoteecd added a commit to coyoteecd/typeorm that referenced this issue Jan 8, 2020
…typeorm#4788)

TypeORM uses require() within PlatformTools to load driver packages.
The typeorm-aurora-data-api-driver is exported with export default and packaged with rollup, which, as per webpack/webpack#4742,
causes the require'd module to be available via require('typeorm-aurora-data-api-driver').default property.
coyoteecd added a commit to coyoteecd/typeorm that referenced this issue Jan 8, 2020
coyoteecd added a commit to coyoteecd/typeorm that referenced this issue Jan 8, 2020
coyoteecd added a commit to coyoteecd/typeorm that referenced this issue Jan 8, 2020
@Can-Sahin
Copy link

I currently moved typeorm-aurora-data-api-driver to webpack externals. It works like this as a quick workaround. Waiting for the fix...

pleerock pushed a commit that referenced this issue Jan 22, 2020
…#4788) (#5302)

TypeORM uses require() within PlatformTools to load driver packages.
The typeorm-aurora-data-api-driver is exported with export default and packaged with rollup, which, as per webpack/webpack#4742,
causes the require'd module to be available via require('typeorm-aurora-data-api-driver').default property.
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 a pull request may close this issue.

4 participants