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": "./",