https://serverless-stack.com/ logo
#help
Title
# help
k

Koen A

01/05/2022, 12:41 PM
Hi, I am trying to run TypeORM within my SST lambda. When I run “npm run start” and trigger the lambda I get the following error:
Copy code
ConnectionNotFoundError2: Connection "default" was not found.
at ConnectionNotFoundError2.TypeORMError2 [as constructor] (/test-project/.sst/artifacts/8a2bd957/src/consumer.js:74327:28)
The ormconfig.json file is located in my project root as it should but it seems the transpiled JS file in the artifacts folder cannot find it. Are project root json files not included or is some manual build step needed?
m

Mischa Spiegelmock

01/05/2022, 12:42 PM
you probably need the bundling commandHooks
t

thdxr

01/05/2022, 1:02 PM
I'm not familiar with typeorm however we run your code in artifacts in the root directory of the project where sst.json is
k

Koen A

01/05/2022, 1:44 PM
Hm, my sst.json is also located in the root I can bypass the error by manually passing connection options and thus ignoring ormconfig.json But then it cannot find the typeORM entities in my .ts files In another dummy project without SST this works fine Thus, my guess is it has something to do with how sst builds the app (esbuild?)
t

thdxr

01/05/2022, 1:46 PM
@Adrián Mouly when you get a sec can you see if anything jumps out to you? I think you're using TypeORM iirc
a

Adrián Mouly

01/05/2022, 1:59 PM
Yes, I’m using typeorm, going to check my code, did the setup 1 year ago.
k

Koen A

01/05/2022, 8:01 PM
I discovered that typeorm uses the following base_path to look for the config file: _/project-root/node_modules/@serverless-stack/aws-lambda-ric/bin_ When I copy the ormconfig file and the .js entity files which I first compiled via tsc into that bin folder my lambda seems to find the files This is obviously not the solution but I don’t really understand what is going on
By adding “nodeModules: [‘typeorm’]” to the sst lambda bundle config it can find the ormconfig file in the root. But now it fails on the next step, loading the .ts entities:
Copy code
ERROR SyntaxError: Cannot use import statement outside a module 
 /project-root/src/db/entity/Order.ts:1
import {
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at /project-root/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:39
    at Array.map (<anonymous>)
    at importClassesFromDirectories (/project-root/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:10)
t

thdxr

01/06/2022, 3:50 PM
It looks like typeorm is trying to load the file at runtime
but it's trying to operate on a TS file which it's unlikely to understand
I'll need to find some time to try and get typeorm setup to see if I can help you
I'm not familiar enough with it to know what's going on. I'm using Kysely as my typed sql builder
k

Koen A

01/06/2022, 5:03 PM
Ok any more thoughts on this would be very helpful. Because the lambda code is executed from the artifacts folder I cannot reference .ts files in the ormconfig file as the .ts entities are loaded from the compiled JS context, which fails with the above error. So currently I manual compile the .ts entities and migrations with tsc and reference those in the ormconfig. But this has to be done for every db change so it is quite unworkable. Kysely looks interesting but would like to use a full orm for my project. Currently, I work with mysql in a aurora serverless db. Don’t know if Kysely or other orm’s supports the aurora data api.
t

thdxr

01/07/2022, 7:31 PM
Kysely supports data api (we wrote the integration) and typeorm is the only other one I know of