Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

sequelize.js - Using the Serverless Framework with AWS, Sequelize's auto-generated table aliases differ depending on environment (offline vs. Lambda)

I'm using the Serverless Framework with the serverless-offline plugin. I've been developing an AWS Lambda function offline and so far haven't had many huge problems.

I need to do a more complicated SQL query, and so I opted to use the literal method to write some pure SQL. I checked the log and saw that Sequelize (with sequelize-typescript) was assigning aliases to the table names so that they matched the model names (or in the case of table relationships, the aliases matched the key that the relationship was assigned to. So I wrote my SQL accordingly. I ended up with the following.

const customer = await this.findOne({
    include: [Coupons, CustomersInfo],
    where: {
        email_address: {
            [Op.eq]: sql.literal(`binary '${email}'`)
        },
        authorization_level: {
            [Op.ne]: 6
        },
        [Op.and]: [
            sql.literal(`
            CASE WHEN '${coupon_code}' is null || '${coupon_code}' = ''
            THEN (coupon.coupon_flag !=2 || coupon.coupon_flag is null)
            ELSE Customers.referral = '${coupon_code}'
            END
        `)
        ]
    },
});

So again, to clarify, in the logs I could see that the customers table was being aliased to "Customers" and the coupon table was being aliased to "coupon".

I did a bunch of local development offline using the serverless-offline plugin, just put it up on lambda and... it doesn't work.

It doesn't work because for some reason on Lambda the same customers table is getting aliased as "l". If I edit my hard coded query to reference the customers table as "l", then it works fine on Lambda... but it stops working offline because offline it is getting aliased as "Customers".

Is there any way to force Sequelize to alias the table as a certain name? Or something I can do to normalize the names between the two environments?

question from:https://stackoverflow.com/questions/65947908/using-the-serverless-framework-with-aws-sequelizes-auto-generated-table-aliase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I figured this out while I was typing up the question, so I'll go ahead and write out the answer.

The problem was that my code was getting minimized when it was deployed as a Lambda function. Here is the relevant documentation about minification and sequelize-typescript. Once minimized, the derived table alias was becoming "l" (and in a subsequent attempt "b"). In order to force the table alias to be a specific name even after minimization, you need to define modelName when making your model class. Example below.

@Table({
  tableName: "customers",
  modelName: "xyz",
})
export class Customers extends Model {
  // The rest of your column definitions here...
}

The xyz will become the name that the table is aliased to in the raw SQL that is generated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...