Repository navigation
Description
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 deployAnd 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!
Activity
h-takenori commentedon Oct 16, 2019
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
coyoteecd commentedon 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.
fix: load typeorm-aurora-data-api-driver correctly when using webpack (…
Temporary publish fix for typeorm#4788 so we can use it in our project
Temporary publish fix for typeorm#4788 so we can use it in our project
Temporary publish fix for typeorm#4788 so we can use it in our project
Can-Sahin commentedon Jan 21, 2020
I currently moved
typeorm-aurora-data-api-driverto webpack externals. It works like this as a quick workaround. Waiting for the fix...fix: load typeorm-aurora-data-api-driver correctly when using webpack (…