Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue that didn't allow a user to update a document when deleting elements in an array attribute #495

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/helpers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ export function deepEqual<T>(obj1: T, obj2: T): boolean {

return true;
}

/**
* Creates a deep clone of the given object. This function uses the JSON methods for cloning,
* so it may not be suitable for objects with functions, symbols, or other non-JSON-safe data.
*
* @param obj the object to be cloned
* @returns a deep clone of the provided object
*/
export function deepClone<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,38 @@
import { collection, type Attributes } from '../../store';
import { Container } from '$lib/layout';
import AttributeItem from '../attributeItem.svelte';
import { difference, symmetricDifference } from '$lib/helpers/array';
import { symmetricDifference } from '$lib/helpers/array';
import { isRelationship, isRelationshipToMany } from '../attributes/store';
import { deepClone } from '$lib/helpers/object';

const databaseId = $page.params.database;
const collectionId = $page.params.collection;
const documentId = $page.params.document;
const editing = true;

const work = writable(
Object.keys($doc)
.filter((key) => {
return ![
'$id',
'$collection',
'$collectionId',
'$databaseId',
'$createdAt',
'$updatedAt'
].includes(key);
})
.reduce((obj, key) => {
obj[key] = $doc[key];
return obj;
}, {}) as Models.Document
);
function initWork() {
const prohibitedKeys = [
'$id',
'$collection',
'$collectionId',
'$databaseId',
'$createdAt',
'$updatedAt'
];

const filteredKeys = Object.keys($doc).filter((key) => {
return !prohibitedKeys.includes(key);
});

const result = filteredKeys.reduce((obj, key) => {
obj[key] = $doc[key];
return obj;
}, {});

return writable(deepClone(result as Models.Document));
}

const work = initWork();

async function updateData() {
try {
Expand Down Expand Up @@ -77,7 +84,7 @@
const docAttribute = $doc?.[attribute.key];

if (attribute.array) {
return !difference(Array.from(workAttribute), Array.from(docAttribute)).length;
return !symmetricDifference(Array.from(workAttribute), Array.from(docAttribute)).length;
}

if (isRelationship(attribute)) {
Expand Down