From 8f95cc7b807aede1038eb6b249398511979e1e7e Mon Sep 17 00:00:00 2001 From: EYHN Date: Tue, 29 Oct 2024 09:18:14 +0000 Subject: [PATCH] fix(infra): orm create override optional (#8627) --- packages/common/infra/src/orm/core/__tests__/hook.spec.ts | 2 +- packages/common/infra/src/orm/core/__tests__/yjs.spec.ts | 2 +- packages/common/infra/src/orm/core/adapters/yjs/table.ts | 4 ++++ packages/common/infra/src/orm/core/table.ts | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/common/infra/src/orm/core/__tests__/hook.spec.ts b/packages/common/infra/src/orm/core/__tests__/hook.spec.ts index 35c1378aa598..9fd3e65c53b9 100644 --- a/packages/common/infra/src/orm/core/__tests__/hook.spec.ts +++ b/packages/common/infra/src/orm/core/__tests__/hook.spec.ts @@ -125,6 +125,6 @@ describe('ORM hook mixin', () => { // @ts-expect-error private const rawTag = client.tags.adapter.data.get(tag.id); expect(rawTag.color).toBe('red'); - expect(rawTag.colors).toBe(null); + expect(rawTag.colors).toBe(undefined); }); }); diff --git a/packages/common/infra/src/orm/core/__tests__/yjs.spec.ts b/packages/common/infra/src/orm/core/__tests__/yjs.spec.ts index 77cd42ab26d1..845840e72a66 100644 --- a/packages/common/infra/src/orm/core/__tests__/yjs.spec.ts +++ b/packages/common/infra/src/orm/core/__tests__/yjs.spec.ts @@ -368,7 +368,7 @@ describe('ORM entity CRUD', () => { name: 'test', }); - expect(user.email).toBe(null); + expect(user.email).toBe(undefined); } { diff --git a/packages/common/infra/src/orm/core/adapters/yjs/table.ts b/packages/common/infra/src/orm/core/adapters/yjs/table.ts index e01f3f4cb334..1564fb8fe748 100644 --- a/packages/common/infra/src/orm/core/adapters/yjs/table.ts +++ b/packages/common/infra/src/orm/core/adapters/yjs/table.ts @@ -64,6 +64,10 @@ export class YjsTableAdapter implements TableAdapter { this.doc.transact(() => { for (const key in data) { + if (data[key] === undefined) { + // skip undefined fields, avoid unexpected override + continue; + } record.set(key, data[key]); } diff --git a/packages/common/infra/src/orm/core/table.ts b/packages/common/infra/src/orm/core/table.ts index bad752d2d3d0..02a3b931190d 100644 --- a/packages/common/infra/src/orm/core/table.ts +++ b/packages/common/infra/src/orm/core/table.ts @@ -160,11 +160,11 @@ export class Table { if (inputVal === undefined) { if (schema.optional) { - acc[key] = null; + acc[key] = undefined; } if (schema.default) { - acc[key] = schema.default() ?? null; + acc[key] = schema.default() ?? undefined; } }