Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Improve update mutations

Hakan Shehue88d007ac0f7
diff --git a/desktop/src/components/databases/tables/cells/table-view-boolean-cell.tsx b/desktop/src/components/databases/tables/cells/table-view-boolean-cell.tsx
index aa117c62..88108909 100644
--- a/desktop/src/components/databases/tables/cells/table-view-boolean-cell.tsx
+++ b/desktop/src/components/databases/tables/cells/table-view-boolean-cell.tsx
@@ -5,6 +5,7 @@ import { useMutation } from '@tanstack/react-query';
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
import { BooleanField, RecordNode } from '@/types/databases';
+import { sql } from 'kysely';
const getBooleanValue = (record: RecordNode, field: BooleanField): boolean => {
const attrs = record.attrs;
@@ -15,8 +16,8 @@ const getBooleanValue = (record: RecordNode, field: BooleanField): boolean => {
const fieldValue = attrs[field.id];
- if (typeof fieldValue === 'boolean') {
- return fieldValue;
+ if (typeof fieldValue === 'number') {
+ return fieldValue === 1;
}
return false;
@@ -34,22 +35,29 @@ export const TableViewBooleanCell = ({
const workspace = useWorkspace();
const { mutate, isPending } = useMutation({
mutationFn: async (newValue: boolean) => {
- const newAttrs = {
- ...record.attrs,
- [field.id]: newValue,
- };
- const query = workspace.schema
- .updateTable('nodes')
- .set({
- attrs: newAttrs ? JSON.stringify(newAttrs) : null,
- updated_at: new Date().toISOString(),
- updated_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- })
- .where('id', '=', record.id)
- .compile();
+ if (newValue) {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(coalesce(attrs, '{}'), '$.${sql.ref(field.id)}', 1),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id}
+ `.compile(workspace.schema);
- await workspace.mutate(query);
+ await workspace.mutate(query);
+ } else {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_remove(attrs, '$.${field.id}'),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id} AND attrs IS NOT NULL
+ `.compile(workspace.schema);
+
+ await workspace.mutate(query);
+ }
},
});
diff --git a/desktop/src/components/databases/tables/cells/table-view-email-cell.tsx b/desktop/src/components/databases/tables/cells/table-view-email-cell.tsx
index 108bf288..c097ea58 100644
--- a/desktop/src/components/databases/tables/cells/table-view-email-cell.tsx
+++ b/desktop/src/components/databases/tables/cells/table-view-email-cell.tsx
@@ -5,6 +5,7 @@ import { useMutation } from '@tanstack/react-query';
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
import { RecordNode, EmailField } from '@/types/databases';
+import { sql } from 'kysely';
const getEmailValue = (record: RecordNode, field: EmailField): string => {
const attrs = record.attrs;
@@ -34,22 +35,29 @@ export const TableViewEmailCell = ({
const workspace = useWorkspace();
const { mutate, isPending } = useMutation({
mutationFn: async (newValue: string) => {
- const newAttrs = {
- ...record.attrs,
- [field.id]: newValue,
- };
- const query = workspace.schema
- .updateTable('nodes')
- .set({
- attrs: newAttrs ? JSON.stringify(newAttrs) : null,
- updated_at: new Date().toISOString(),
- updated_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- })
- .where('id', '=', record.id)
- .compile();
+ if (newValue.length > 0) {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(coalesce(attrs, '{}'), '$.${field.id}', ${newValue}),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id}
+ `.compile(workspace.schema);
- await workspace.mutate(query);
+ await workspace.mutate(query);
+ } else {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_remove(attrs, '$.${field.id}'),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id} AND attrs IS NOT NULL
+ `.compile(workspace.schema);
+
+ await workspace.mutate(query);
+ }
},
});
diff --git a/desktop/src/components/databases/tables/cells/table-view-number-cell.tsx b/desktop/src/components/databases/tables/cells/table-view-number-cell.tsx
index 49ebcc27..c95dc3b0 100644
--- a/desktop/src/components/databases/tables/cells/table-view-number-cell.tsx
+++ b/desktop/src/components/databases/tables/cells/table-view-number-cell.tsx
@@ -7,6 +7,7 @@ import { useWorkspace } from '@/contexts/workspace';
import { RecordNode } from '@/types/databases';
import { NeuronId } from '@/lib/id';
import { NumberField } from '@/types/databases';
+import { sql } from 'kysely';
const getNumberValue = (
record: RecordNode,
@@ -38,23 +39,30 @@ export const TableViewNumberCell = ({
}: TableViewNumberCellProps) => {
const workspace = useWorkspace();
const { mutate, isPending } = useMutation({
- mutationFn: async (newValue: number) => {
- const newAttrs = {
- ...record.attrs,
- [field.id]: newValue,
- };
- const query = workspace.schema
- .updateTable('nodes')
- .set({
- attrs: newAttrs ? JSON.stringify(newAttrs) : null,
- updated_at: new Date().toISOString(),
- updated_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- })
- .where('id', '=', record.id)
- .compile();
+ mutationFn: async (newValue: number | null) => {
+ if (newValue !== null) {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(coalesce(attrs, '{}'), '$.${field.id}', ${newValue}),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id}
+ `.compile(workspace.schema);
- await workspace.mutate(query);
+ await workspace.mutate(query);
+ } else {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_remove(attrs, '$.${field.id}'),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id} AND attrs IS NOT NULL
+ `.compile(workspace.schema);
+
+ await workspace.mutate(query);
+ }
},
});
@@ -69,7 +77,7 @@ export const TableViewNumberCell = ({
}, [record.versionId]);
const saveIfChanged = (current: number | null, previous: number | null) => {
- if (current !== null && current !== previous) {
+ if (current !== previous) {
mutate(current);
}
};
@@ -84,6 +92,7 @@ export const TableViewNumberCell = ({
const number = Number(input);
if (Number.isNaN(number)) {
setInput('');
+ saveIfChanged(null, getNumberValue(record, field));
} else {
saveIfChanged(number, getNumberValue(record, field));
}
@@ -93,6 +102,7 @@ export const TableViewNumberCell = ({
const number = Number(input);
if (Number.isNaN(number)) {
setInput('');
+ saveIfChanged(null, getNumberValue(record, field));
} else {
saveIfChanged(number, getNumberValue(record, field));
}
diff --git a/desktop/src/components/databases/tables/cells/table-view-select-cell.tsx b/desktop/src/components/databases/tables/cells/table-view-select-cell.tsx
index edbb5eba..e9ed5af2 100644
--- a/desktop/src/components/databases/tables/cells/table-view-select-cell.tsx
+++ b/desktop/src/components/databases/tables/cells/table-view-select-cell.tsx
@@ -10,6 +10,7 @@ import { SelectFieldOptions } from '@/components/databases/fields/select-field-o
import { useMutation } from '@tanstack/react-query';
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
+import { sql } from 'kysely';
const getSelectValue = (
record: RecordNode,
@@ -42,22 +43,29 @@ export const TableViewSelectCell = ({
const workspace = useWorkspace();
const { mutate, isPending } = useMutation({
mutationFn: async (newValue: string | null) => {
- const newAttrs = {
- ...record.attrs,
- [field.id]: newValue,
- };
- const query = workspace.schema
- .updateTable('nodes')
- .set({
- attrs: newAttrs ? JSON.stringify(newAttrs) : null,
- updated_at: new Date().toISOString(),
- updated_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- })
- .where('id', '=', record.id)
- .compile();
+ if (newValue !== null && newValue.length > 0) {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(coalesce(attrs, '{}'), '$.${field.id}', ${newValue}),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id}
+ `.compile(workspace.schema);
- await workspace.mutate(query);
+ await workspace.mutate(query);
+ } else {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_remove(attrs, '$.${field.id}'),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id} AND attrs IS NOT NULL
+ `.compile(workspace.schema);
+
+ await workspace.mutate(query);
+ }
},
});
diff --git a/desktop/src/components/databases/tables/cells/table-view-text-cell.tsx b/desktop/src/components/databases/tables/cells/table-view-text-cell.tsx
index 9a8bf0dc..2a426662 100644
--- a/desktop/src/components/databases/tables/cells/table-view-text-cell.tsx
+++ b/desktop/src/components/databases/tables/cells/table-view-text-cell.tsx
@@ -5,6 +5,7 @@ import { useMutation } from '@tanstack/react-query';
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
import { RecordNode, TextField } from '@/types/databases';
+import { sql } from 'kysely';
const getTextValue = (record: RecordNode, field: TextField): string => {
const attrs = record.attrs;
@@ -34,22 +35,29 @@ export const TableViewTextCell = ({
const workspace = useWorkspace();
const { mutate, isPending } = useMutation({
mutationFn: async (newValue: string) => {
- const newAttrs = {
- ...record.attrs,
- [field.id]: newValue,
- };
- const query = workspace.schema
- .updateTable('nodes')
- .set({
- attrs: newAttrs ? JSON.stringify(newAttrs) : null,
- updated_at: new Date().toISOString(),
- updated_by: workspace.userId,
- version_id: NeuronId.generate(NeuronId.Type.Version),
- })
- .where('id', '=', record.id)
- .compile();
+ if (newValue.length > 0) {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(coalesce(attrs, '{}'), '$.${field.id}', ${newValue}),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id}
+ `.compile(workspace.schema);
- await workspace.mutate(query);
+ await workspace.mutate(query);
+ } else {
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_remove(attrs, '$.${field.id}'),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${record.id} AND attrs IS NOT NULL
+ `.compile(workspace.schema);
+
+ await workspace.mutate(query);
+ }
},
});