mirror of
https://github.com/colanode/colanode.git
synced 2025-12-29 00:25:03 +01:00
Improve update mutations
This commit is contained in:
@@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user