For people using typeorm, how are you setting it u...
# sst
j
For people using typeorm, how are you setting it up and how does the connection work? I’m just getting
TypeError: Cannot read property 'filename' of undefined
whenever I try to make a connection
a
where does this error originate? do you have a stack trace? did you exclude from your bundle dynamically imported dependencies by typeorm (e.g. pg) and added them in a layer?
d
this is what my connection looks like (I'm using the Aurora data API to connect):
Copy code
const connection = await createConnection({
      type: "aurora-data-api",
      database: "mydatabase",
      secretArn: process.env.AURORA_SECRET_ARN,
      resourceArn: process.env.AURORA_CLUSTER_ARN,
      entities: [
        User,
        Orders
      ],
    });
f
@jamlen did u get this to work? ^^ a couple of folks got a similar error message when using
AppSyncApi
on Windows. Do u happen to have the same setup by any chance?
s
If you are still early games into implementing an ORM I would suggest evaluating kysely too.
j
@Frank no I didn’t get it working 😞 I did create a new empty SST project and have the RDS working fine but when I try to put typeorm in I get that message. I’m thinking that the setup of my stack/connection is what is at fault. @Dan Suceava where do you make your connection? do you use
sychronize: true
?
This is the error and stack:
Copy code
{
  errorType: 'Runtime.UnhandledPromiseRejection',
  errorMessage: "TypeError: Cannot read property 'filename' of undefined",
  reason: "TypeError: Cannot read property 'filename' of undefined",
  promise: {},
  stack: [
    "Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'filename' of undefined",
    '    at process.<anonymous> (file:///Users/jamlen/dev/src/github.com/tfgm/orm-test/node_modules/@serverless-stack/aws-lambda-ric/lib/index.js:34:23)',
    '    at process.emit (events.js:400:28)',
    '    at processPromiseRejections (internal/process/promises.js:245:33)',
    '    at processTicksAndRejections (internal/process/task_queues.js:96:32)'
  ]
}
d
@jamlen I do not use
synchronize: true
although I did try it once and seemed to work. I make the connection in the Lambda handler, this is what my core handler looks like (this is from an early test):
Copy code
import { APIGatewayProxyEventV2, APIGatewayProxyHandlerV2 } from "aws-lambda";

import { getDatabase } from "../domain/database/database";

export const handler: APIGatewayProxyHandlerV2 = async (event: APIGatewayProxyEventV2) => {
  const message = event.body;

  // connect to the database to perform the migration
  let response = "";
  try {
    const db = await getDatabase({ synchronize: true });
    response = "Database connected";
  } catch (error: any) {
    console.error(error);
    response = error.message;
  }

  return {
    statusCode: 200,
    body: response,
  };
};
the
getDatabase()
call makes the connection as I posted above
are you getting this error when you deploy or are you running live debug? I am seeing this same error in live debug, and there's another thread on it from someone else here: https://serverless-stack.slack.com/archives/C01JG3B20RY/p1647509817438479
j
@Dan Suceava yes I am seeing in when live debugging. I am also on a mac and have no AppSyncApi, just APIGateways.
f
@Dan Suceava can you try adding this at the beginning of the
main
function inside ur
stacks/index.ts
Copy code
app.setDefaultFunctionProps({
  bundle: {
    nodeModules: ["typeorm"],
  },
});
The error comes from this app-root-path library that TypeORM uses. And it doesn’t seem to be compatible with
esbuild
.
Give it a try and let me know if it works for you.
j
Thanks @Frank that got me past that… my next issue seems to be that typeorm doesn’t see the driver. @Dan Suceava have you had any issues with the
typeorm-aurora-data-api-driver
? I keep getting
Copy code
MissingDriverError: Wrong driver: "aurora-data-api-pg" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs".
even though I have
Copy code
"dependencies": {
      "data-api-client": "^1.2.0",
      "reflect-metadata": "^0.1.13",
      "typeorm": "0.3.0",
      "typeorm-aurora-data-api-driver": "^2.3.5"
   }
d
@jamlen no, never had any issues with it, here's what I have:
Copy code
"dependencies": {
    "@aws-sdk/client-sfn": "^3.53.0",
    "@aws-sdk/client-sns": "^3.50.0",
    "@middy/core": "^2.5.7",
    "@middy/http-error-handler": "^2.5.7",
    "axios": "^0.25.0",
    "http-errors": "^2.0.0",
    "jsonwebtoken": "^8.5.1",
    "md5": "^2.3.0",
    "middy-middleware-jwt-auth": "^5.0.0",
    "mysql2": "^2.3.3",
    "reflect-metadata": "^0.1.13",
    "typeorm": "^0.2.41",
    "typeorm-aurora-data-api-driver": "^2.3.5"
  },
Looks like tyeporm
0.3.0
was a big release, haven't tried it yet
j
@Dan Suceava I’m still struggling with TypeORM.
I keep getting stuck on
Copy code
{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "ColumnTypeUndefinedError: Column type for Customer#firstName is not defined and cannot be guessed. Make sure you have turned on an \"emitDecoratorMetadata\": true option in tsconfig.json. Also make sure you have imported \"reflect-metadata\" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.",
}
I’m only doing a really simple example right now and I have got the tsconfig settings.
I’ve pushed it up to a repo, if you could take a look at it I’d appreciate it. I’m sure that I’m missing something simple and I really want to use typeorm but its super frustrating me now! https://github.com/jamlen/typeorm-sst-demo
d
@jamlen have you tried defining the column type for
firstName
&
lastName
in the entity file:
Copy code
@Column({ type: 'nvarchar', length: 100 })
firstName: string;