From bb7e3fc46eba821abc684415fac061d33dbb0ae2 Mon Sep 17 00:00:00 2001 From: Daniel Padrino Date: Mon, 13 Jul 2026 16:59:17 -0300 Subject: [PATCH] fix(db): disable useDefineForClassFields so entity updates stay updates The es2022 target bump (e18f05a9) flipped useDefineForClassFields to true, so entities from repo.create() carry every declared column as an own undefined property. The Object.assign(dbEntity, freshEntity) update pattern then wipes the loaded row's id and save() inserts a duplicate row instead of updating. Restores the pre-bump Set semantics and pins them with a regression spec. Root cause of #205. --- src/shared/db/__tests__/entity.spec.ts | 29 ++++++++++++++++++++++++++ tsconfig.json | 1 + 2 files changed, 30 insertions(+) create mode 100644 src/shared/db/__tests__/entity.spec.ts diff --git a/src/shared/db/__tests__/entity.spec.ts b/src/shared/db/__tests__/entity.spec.ts new file mode 100644 index 0000000000..97651c1b7a --- /dev/null +++ b/src/shared/db/__tests__/entity.spec.ts @@ -0,0 +1,29 @@ +import { TransactionOnchainEntity } from 'src/subdomains/lightning/entities/transaction-onchain.entity'; + +// Pins "useDefineForClassFields": false in tsconfig.json. With define semantics (the default +// for target >= es2022), fresh entities carry own undefined class fields, so the +// Object.assign(dbEntity, freshEntity) update pattern wipes the loaded row's id and +// save() inserts a duplicate row instead of updating (issue #205). +describe('entity class-field semantics', () => { + it('fresh entity carries no own id property', () => { + const fresh = new TransactionOnchainEntity(); + + expect(Object.getOwnPropertyNames(fresh)).not.toContain('id'); + }); + + it('Object.assign of a fresh entity onto a loaded row preserves the row id', () => { + const dbRow = new TransactionOnchainEntity(); + dbRow.id = 42; + dbRow.transaction = 'txid'; + dbRow.amount = 1; + + const fresh = new TransactionOnchainEntity(); + fresh.transaction = 'txid'; + fresh.amount = 2; + + Object.assign(dbRow, fresh); + + expect(dbRow.id).toBe(42); + expect(dbRow.amount).toBe(2); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 4fcd093082..ba136405e2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2022", + "useDefineForClassFields": false, "sourceMap": true, "outDir": "./dist", "baseUrl": "./",