Repository: xataio/client-ts
Pull request #169 (2022-05-11 13:37:03)

Allow destructuring link object
Closes: #162

<!-- Add a nice description here -->

<!-- Don't forget the `Closed #{issue_number}` -->



Patch:
diff --git a/.changeset/gentle-snakes-relax.md b/.changeset/gentle-snakes-relax.md
new file mode 100644
index 00000000..06bf4a5c
--- /dev/null
+++ b/.changeset/gentle-snakes-relax.md
@@ -0,0 +1,5 @@
+---
+'@xata.io/client': patch
+---
+
+Allow sending updates with link object
diff --git a/client/src/schema/record.ts b/client/src/schema/record.ts
index 74580355..e7c3da59 100644
--- a/client/src/schema/record.ts
+++ b/client/src/schema/record.ts
@@ -79,6 +79,6 @@ export type EditableData<O extends BaseData> = {
   [K in keyof O]: O[K] extends XataRecord
     ? { id: string }
     : NonNullable<O[K]> extends XataRecord
-    ? { id: string } | undefined
+    ? { id: string } | null | undefined
     : O[K];
 };
diff --git a/client/src/schema/repository.ts b/client/src/schema/repository.ts
index b5aa438d..6af80b5c 100644
--- a/client/src/schema/repository.ts
+++ b/client/src/schema/repository.ts
@@ -584,6 +584,10 @@ const isBranchStrategyBuilder = (strategy: BranchStrategy): strategy is BranchSt
 
 const transformObjectLinks = (object: any) => {
   return Object.entries(object).reduce((acc, [key, value]) => {
+    // Ignore internal properties
+    if (key === 'xata') return acc;
+
+    // Transform links to identifier
     return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
   }, {});
 };


Test patch:
diff --git a/client/src/schema/index.test.ts b/client/src/schema/index.test.ts
index 8d9f50bc..2377eaa1 100644
--- a/client/src/schema/index.test.ts
+++ b/client/src/schema/index.test.ts
@@ -471,7 +471,7 @@ describe('Repository.update', () => {
         Array [
           "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/tables/users/data/rec_1234",
           Object {
-            "body": "{\\"id\\":\\"rec_1234\\",\\"xata\\":{\\"version\\":1},\\"name\\":\\"Ada\\"}",
+            "body": "{\\"id\\":\\"rec_1234\\",\\"name\\":\\"Ada\\"}",
             "headers": Object {
               "Authorization": "Bearer 1234",
               "Content-Type": "application/json",
diff --git a/test/integration.test.ts b/test/integration.test.ts
index 916fd7be..d1543abc 100644
--- a/test/integration.test.ts
+++ b/test/integration.test.ts
@@ -511,6 +511,17 @@ describe('integration tests', () => {
     expect(nestedRead?.id).toBeDefined();
     expect(nestedRead?.full_name).toEqual(user.full_name);
   });
+
+  test('Update link with linked object', async () => {
+    const owner = await client.db.users.create({ full_name: 'Example User' });
+    const owner2 = await client.db.users.create({ full_name: 'Example User 2' });
+
+    const team = await client.db.teams.create({ name: 'Example Team', owner });
+    const updated = await team.update({ owner: owner2 });
+
+    expect(team.owner?.id).toEqual(owner.id);
+    expect(updated.owner?.id).toEqual(owner2.id);
+  });
 });
 
 describe('record creation', () => {
@@ -621,7 +632,7 @@ describe('record update', () => {
   test('update multiple teams', async () => {
     const teams = await client.db.teams.create([{ name: 'Team cars' }, { name: 'Team planes' }]);
 
-    const updatedTeams = await client.db.teams.update(teams.map((team) => ({ id: team.id, name: 'Team boats' })));
+    const updatedTeams = await client.db.teams.update(teams.map((team) => ({ ...team, name: 'Team boats' })));
 
     expect(updatedTeams).toHaveLength(2);