Skip to content

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

Improve some update queries

Hakan Shehu59e5d1e31c36
diff --git a/desktop/src/components/databases/fields/select-option-settings-popover.tsx b/desktop/src/components/databases/fields/select-option-settings-popover.tsx
index 9b34c367..9b6e810b 100644
--- a/desktop/src/components/databases/fields/select-option-settings-popover.tsx
+++ b/desktop/src/components/databases/fields/select-option-settings-popover.tsx
@@ -16,6 +16,7 @@ import { SelectOptionDeleteDialog } from '@/components/databases/fields/select-o
import { useWorkspace } from '@/contexts/workspace';
import { NeuronId } from '@/lib/id';
import { useMutation } from '@tanstack/react-query';
+import { sql } from 'kysely';
interface SelectOptionSettingsPopoverProps {
option: SelectOption;
@@ -33,20 +34,18 @@ export const SelectOptionSettingsPopover = ({
const { mutate, isPending } = useMutation({
mutationFn: async (input: UpdateSelectOptionInput) => {
- const newAttrs = {
- name: input.name,
- color: input.color,
- };
- 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', '=', option.id)
- .compile();
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(
+ coalesce(attrs, '{}'),
+ '$.name', ${input.name},
+ '$.color', ${input.color}
+ ),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${option.id}
+ `.compile(workspace.schema);
await workspace.mutate(query);
},
diff --git a/desktop/src/components/databases/tables/table-view-name-cell.tsx b/desktop/src/components/databases/tables/table-view-name-cell.tsx
index 895e022c..2afd2577 100644
--- a/desktop/src/components/databases/tables/table-view-name-cell.tsx
+++ b/desktop/src/components/databases/tables/table-view-name-cell.tsx
@@ -6,6 +6,7 @@ import { useMutation } from '@tanstack/react-query';
import { NeuronId } from '@/lib/id';
import { Spinner } from '@/components/ui/spinner';
import { RecordNode } from '@/types/databases';
+import { sql } from 'kysely';
interface NameEditorProps {
initialValue: string;
@@ -14,7 +15,7 @@ interface NameEditorProps {
}
const NameEditor = ({ initialValue, onSave, onCancel }: NameEditorProps) => {
- const [value, setValue] = React.useState(initialValue);
+ const [value, setValue] = React.useState(initialValue ?? '');
const inputRef = React.useRef<HTMLInputElement>(null);
React.useEffect(() => {
@@ -58,20 +59,14 @@ export const TableViewNameCell = ({ record }: TableViewNameCellProps) => {
const { mutate, isPending } = useMutation({
mutationFn: async (newName: string) => {
- const newAttrs = {
- ...record.attrs,
- name: newName,
- };
- 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();
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(coalesce(attrs, '{}'), '$.name', ${newName}),
+ 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);
},
diff --git a/desktop/src/components/workspaces/container-header.tsx b/desktop/src/components/workspaces/container-header.tsx
index 5443cc1e..572ee15a 100644
--- a/desktop/src/components/workspaces/container-header.tsx
+++ b/desktop/src/components/workspaces/container-header.tsx
@@ -75,20 +75,14 @@ export const BreadcrumbNodeEditor = ({ node }: BreadcrumbNodeEditorProps) => {
const handleNameChange = React.useMemo(
() =>
debounce(async (newName: string) => {
- const newAttrs = {
- ...node.attrs,
- name: newName,
- };
- 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', '=', node.id)
- .compile();
+ const query = sql`
+ UPDATE nodes
+ SET attrs = json_set(attrs, '$.name', ${newName}),
+ updated_at = ${new Date().toISOString()},
+ updated_by = ${workspace.userId},
+ version_id = ${NeuronId.generate(NeuronId.Type.Version)}
+ WHERE id = ${node.id}
+ `.compile(workspace.schema);
await workspace.mutate(query);
}, 500),