Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/sequelize-declare-attributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"seamless-templates": patch
---

fix(templates): declare Sequelize model attributes instead of using public class fields

The `User` model in both API starters (`express` and `fastify`) declared its
attributes as `public id!: string`. Sequelize installs its attribute getters and
setters on the prototype, and a public class field is emitted as an own property
initialised to undefined, which shadows them: `user.id` reads undefined while
`user.get("id")` returns the row's value. Sequelize warns about this at model
init. `declare` emits no field at all, so the accessors survive.

Whether the field is emitted depends on `useDefineForClassFields`, which follows
`target`. Both starters compile at `target: ES2020`, where the field is erased
and the shadowing does not occur, so this is a guard rather than a repair of
behaviour anyone is seeing today. It matters because the guard is what keeps a
later `target` bump from silently breaking every model: at ES2022 the same code
returns undefined for every attribute, and the first symptom is a query built
with an undefined parameter on a handler that filters by `req.appUser.id`.
10 changes: 5 additions & 5 deletions templates/api/express/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface UserAttributes {
}

export class User extends Model<UserAttributes> implements UserAttributes {
public id!: string;
public email!: string | null;
public phone!: string | null;
public readonly created_at!: Date;
public readonly updated_at!: Date;
declare id: string;
declare email: string | null;
declare phone: string | null;
declare readonly created_at: Date;
declare readonly updated_at: Date;
}

const initializeUserModel = (sequelize: Sequelize) => {
Expand Down
10 changes: 5 additions & 5 deletions templates/api/fastify/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface UserAttributes {
}

export class User extends Model<UserAttributes> implements UserAttributes {
public id!: string;
public email!: string | null;
public phone!: string | null;
public readonly created_at!: Date;
public readonly updated_at!: Date;
declare id: string;
declare email: string | null;
declare phone: string | null;
declare readonly created_at: Date;
declare readonly updated_at: Date;
}

const initializeUserModel = (sequelize: Sequelize) => {
Expand Down
Loading