Skip to content

Commit 2a0628a

Browse files
Merge pull request #495 from safwanyp/fix-5476-user-unable-to-update-document-after-array-value-deletion
fixed issue that didn't allow a user to update a document when deleting elements in an array attribute
2 parents f2b170c + 5ad0349 commit 2a0628a

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

  • src
    • lib/helpers
    • routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/data

src/lib/helpers/object.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ export function deepEqual<T>(obj1: T, obj2: T): boolean {
3636

3737
return true;
3838
}
39+
40+
/**
41+
* Creates a deep clone of the given object. This function uses the JSON methods for cloning,
42+
* so it may not be suitable for objects with functions, symbols, or other non-JSON-safe data.
43+
*
44+
* @param obj the object to be cloned
45+
* @returns a deep clone of the provided object
46+
*/
47+
export function deepClone<T>(obj: T): T {
48+
return JSON.parse(JSON.stringify(obj));
49+
}

src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/data/+page.svelte

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,38 @@
1313
import { collection, type Attributes } from '../../store';
1414
import { Container } from '$lib/layout';
1515
import AttributeItem from '../attributeItem.svelte';
16-
import { difference, symmetricDifference } from '$lib/helpers/array';
16+
import { symmetricDifference } from '$lib/helpers/array';
1717
import { isRelationship, isRelationshipToMany } from '../attributes/store';
18+
import { deepClone } from '$lib/helpers/object';
1819
1920
const databaseId = $page.params.database;
2021
const collectionId = $page.params.collection;
2122
const documentId = $page.params.document;
2223
const editing = true;
2324
24-
const work = writable(
25-
Object.keys($doc)
26-
.filter((key) => {
27-
return ![
28-
'$id',
29-
'$collection',
30-
'$collectionId',
31-
'$databaseId',
32-
'$createdAt',
33-
'$updatedAt'
34-
].includes(key);
35-
})
36-
.reduce((obj, key) => {
37-
obj[key] = $doc[key];
38-
return obj;
39-
}, {}) as Models.Document
40-
);
25+
function initWork() {
26+
const prohibitedKeys = [
27+
'$id',
28+
'$collection',
29+
'$collectionId',
30+
'$databaseId',
31+
'$createdAt',
32+
'$updatedAt'
33+
];
34+
35+
const filteredKeys = Object.keys($doc).filter((key) => {
36+
return !prohibitedKeys.includes(key);
37+
});
38+
39+
const result = filteredKeys.reduce((obj, key) => {
40+
obj[key] = $doc[key];
41+
return obj;
42+
}, {});
43+
44+
return writable(deepClone(result as Models.Document));
45+
}
46+
47+
const work = initWork();
4148
4249
async function updateData() {
4350
try {
@@ -77,7 +84,7 @@
7784
const docAttribute = $doc?.[attribute.key];
7885
7986
if (attribute.array) {
80-
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
87+
return !symmetricDifference(Array.from(workAttribute), Array.from(docAttribute)).length;
8188
}
8289
8390
if (isRelationship(attribute)) {

0 commit comments

Comments
 (0)