does anyone have any resources or guide i can look...
# sst
j
does anyone have any resources or guide i can look at in regards to using SST with nestjs? im trying to create a very simple lambda handler that fetches a service from the nestjs DI container and calls a simple method and i get the following error:
Copy code
john@pop-os:~/Development/edvisor-io/pricing-lambda$ yarn sst build
yarn run v1.22.15
$ /home/john/Development/edvisor-io/pricing-lambda/node_modules/.bin/sst build
Using stage: john
Preparing your SST app
Synthesizing CDK

Error: There was a problem transpiling the Lambda handler: ✘ [ERROR] Could not resolve "@nestjs/websockets/socket-module"

    node_modules/@nestjs/core/nest-application.js:18:115:
      18 │ ...ocket-module', () => require('@nestjs/websockets/socket-module'));
         ╵                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@nestjs/websockets/socket-module" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
.. along with other errors regarding other modules (ie.
class-transformer
,
class-validator
, etc..)
t
I'm not sure how nestjs works but I'd consider the following 1. Do you really have to use it? Typically these frameworks aren't architected specifically for lambda and can be hard to work with in a serverless context 2. Nest's websocket model won't work with Lambda - basically an example of #1 3. Are these dependencies added to your project?
j
thanks for the response @thdxr! ill try to answer your questions below: 1. i use nestjs just to organize my code into modules and leverage nestjs's DI containers. not 100% necessary to use it but i would like to just for code organization 2. my nestjs app doesnt use websockets but is just an optional feature in the nestjs framework (although im not using it explicitly) 3. these deps are not added to the project explicitly. when running the nestjs app standalone i dont need to have it explicitly added to the project.. do i need to add them explicitly in sst?
t
It's odd because in the error I can see nestjs dynamically loading it (which is correct if it's optional) and it shouldn't be hitting that if you're not using it. That said, do you have esm turned on right now? In would probably be in
stacks/index.ts
- if so you can try turning it off or update this to include all the packages it's complaining about
j
thanks @thdxr! let me experiment a bit and ill get back to you if those suggestions work 🙂
allllright i think i figured most of it out. leaving a paper trail here in case it helps someone in the future specifically, regarding my original post, i came across this github issue that helped me resolve it: https://github.com/serverless-stack/serverless-stack/issues/1360 i also use
sequelize-typescript
to define my sequelize models. this relies on some typescript inference magic to define things. it mostly works except i need to explicitly define the column data-types because otherwise it seems to fail... for example, this does not work (but does outside of the SST context):
Copy code
@Table({
  tableName: 'user',
  modelName: 'User',
})
export class User extends Model {
  @PrimaryKey
  @Column({
    field: 'user_id',
  })
  userId: number
}
however, this does work:
Copy code
@Table({
  tableName: 'user',
  modelName: 'User',
})
export class User extends Model {
  @PrimaryKey
  @Column({
    field: 'user_id',
    type: DataType.INTEGER //must be explicit
  })
  userId: number
}
i originally thought esbuild was messing something up with the type inference, so i explicitly turned minification off and also tried the
keepNames: true
config... both didnt seem to fix the above issue 🤷
t
I'm also pretty sure decorator support needs additional esbuild config
j
yup, i already added it as a plugin to esbuild. still, doesnt seem to work completely with
sequelize-typescript
😞
t
not sure if you've checked it out but if this is a new project we recommend using
kysely
+
kysely-data-api
as setup here: https://github.com/serverless-stack/ideal-stack-preview/blob/master/backend/core/article.ts
j
thanks! unfortunately im not in a position to be changing our db ORM/connector at the moment
h
@John Kor were you able to proceed with Nestjs + SST ?
j
@Hitesh Balwani yes i was another discovery i found, which is a bit unintuitive, is you must add decorator support to the root `tsconfig.json`:
Copy code
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
i only had it in the
./backend/tsconfig.json
file but not in the
./tsconfig.json
file