Skip to content

Commit

Permalink
Merge pull request #2163 from undb-io/release/v1.0.0-128
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin authored Nov 24, 2024
2 parents 59ebe02 + 932fded commit 0ab2036
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 22 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## v1.0.0-128


### 🩹 Fixes

- Fix json default format ([036aeca](https://github.com/undb-io/undb/commit/036aeca))

### ❤️ Contributors

- Nichenqin ([@nichenqin](http://github.com/nichenqin))

## v1.0.0-127


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export let onValueChange: (value: Json) => void
let content: Content = {
text: "{}",
text: JSON.stringify(value ?? {}, null, 2),
json: value ?? {},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
export let isSelected: boolean
export let field: JsonField
export let recordId: string
export let onValueChange: (value: JsonValue) => void
export let onValueChange: (value: JsonValue | undefined) => void
const updateCell = createMutation({
mutationKey: ["record", tableId, field.id.value, recordId],
Expand All @@ -25,7 +25,7 @@
})
let content: Content = {
text: undefined,
text: JSON.stringify(value ?? {}, null, 2),
json: value ?? {},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@
)}
onValueChange={(v) => {
value = v
recordsStore.setRecordValue(recordId, field.id.value, v)
recordsStore.setRecordValue(recordId, field, v)
}}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import {
FieldIdVo,
KanbanView,
RecordDO,
SelectFieldValue,
Records,
SelectEqual,
SelectField,
Expand Down Expand Up @@ -122,10 +124,12 @@
let laneElement: HTMLElement
const spec = Some(new SelectEqual(option?.id ?? null, new FieldIdVo(fieldId)))
const updateRecord = createMutation({
mutationFn: trpc.record.update.mutate,
onSuccess: (data, variables, context) => {
recordsStore?.invalidateRecord($table, variables.id)
onError: (error, variables, context) => {
toast.error(error.message)
},
})
Expand Down Expand Up @@ -170,6 +174,11 @@
},
})
}
let records = derived(recordsStore, ($recordsStore) =>
[...$recordsStore.records.values()].filter((record) =>
spec.isSome() ? spec.unwrap().isSatisfiedBy(record) : true,
),
)
onMount(() => {
if (!shareId && !readonly && laneElement) {
Expand All @@ -192,8 +201,14 @@
onEnd: (evt) => {
const recordId = evt.item.dataset.recordId
if (!recordId) return
const fromOptionId = evt.from.dataset.optionId ?? null
const optionId = evt.to.dataset.optionId ?? null
recordsStore.setRecordValue(recordId, field, optionId)
if (fromOptionId !== optionId) {
evt.item.remove()
}
$updateRecord.mutate({
tableId,
id: recordId,
Expand All @@ -206,19 +221,19 @@
}
})
let lastSetPage = 0
$: {
// @ts-ignore
const records = ($query.data?.pages.flatMap((r: any) => r.records) as IRecordsDTO) ?? []
const records = ($query.data?.pages.slice(lastSetPage).flatMap((r: any) => r.records) as IRecordsDTO) ?? []
// @ts-ignore
lastSetPage = $query.data?.pages.length ?? 0
recordsStore.upsertRecords(Records.fromJSON($table, records))
}
onDestroy(() => {
recordsStore.clearRecords()
})
let storeGetRecords = recordsStore.getRecords
$: recordDos = $storeGetRecords(Some(new SelectEqual(option?.id ?? null, new FieldIdVo(fieldId))))
$: fields = $table.getOrderedVisibleFields($viewId) ?? []
let updateOptionDialogOpen = false
Expand Down Expand Up @@ -330,7 +345,7 @@
>
{#if $hasPermission("record:create")}
{#if $query.isFetchedAfterMount}
{#if recordDos.length > 0}
{#if $records.length > 0}
<Button on:click={onCreateRecord} variant="outline" size="sm" class="w-full">
<PlusIcon class="text-muted-foreground mr-2 h-4 w-4 font-semibold" />
</Button>
Expand Down Expand Up @@ -359,7 +374,7 @@
{:else if $query.isError}
<p>error: {$query.error.message}</p>
{:else}
{#each recordDos as record (record.id.value)}
{#each $records as record (record.id.value)}
<KanbanCard {readonly} {record} {fields} {color} {r} />
{/each}
{#if $query.hasNextPage && $query.isFetchedAfterMount}
Expand Down
25 changes: 22 additions & 3 deletions apps/frontend/src/lib/store/records.store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { trpc } from "$lib/trpc/client"
import type { Option } from "@undb/domain"
import { RecordComositeSpecification, RecordDO, TableDo, type IRecordValues, type Records } from "@undb/table"
import {
Field,
FieldValueFactory,
RecordComositeSpecification,
RecordDO,
TableDo,
type IRecordValues,
type Records,
} from "@undb/table"
import { getContext, setContext } from "svelte"
import { derived, writable } from "svelte/store"
import { queryParam, ssp } from "sveltekit-search-params"
Expand All @@ -23,6 +31,7 @@ export const createRecordsStore = () => {
const id = record.id.value
if (store.records.has(id)) {
store.records.set(id, record)
store.records = store.records
data.update((data) => data.map((d) => (d.id === id ? record.flatten() : d)))
} else {
store.records.set(id, record)
Expand All @@ -41,12 +50,14 @@ export const createRecordsStore = () => {
return store.update((store) => {
if (store.records.has(record.id.value)) {
store.records.set(record.id.value, record)
store.records = store.records
data.update((data) => {
return data.map((d) => (d.id === record.id.value ? record.flatten() : d))
})
return store
} else {
store.records.set(record.id.value, record)
store.records = store.records
store.ids.push(record.id.value)
data.update((data) => {
data.push(record.flatten())
Expand All @@ -57,12 +68,20 @@ export const createRecordsStore = () => {
})
}

const setRecordValue = (id: string, key: string, value: any) => {
const setRecordValue = (id: string, field: Field, value: any) => {
const v = FieldValueFactory.fromJSON(field, value).into(undefined)
if (!v) return

return store.update((store) => {
if (store.records.has(id)) {
data.update((data) => {
return data.map((d) => (d.id === id ? { ...d, [key]: value } : d))
const updated = data.map((d) => (d.id === id ? { ...d, [field.id.value]: v.value } : d))
return updated
})
const record = store.records.get(id)!
record.values.setValue(field.id, v)
store.records.set(record.id.value, record)
store.records = store.records
}
return store
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "undb",
"version": "1.0.0-127",
"version": "1.0.0-128",
"private": true,
"scripts": {
"build": "NODE_ENV=production bun --bun turbo build",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Some } from "@undb/domain"
import { z } from "@undb/zod"
import type { FormFieldVO } from "../../../../forms/form/form-field.vo"
import { FieldConstraintVO, baseFieldConstraint } from "../../field-constraint.vo"
import { jsonSchemaValue } from "./json-field-value.vo"

export const jsonFieldConstraint = z
.object({
Expand All @@ -20,8 +21,7 @@ export class JsonFieldConstraint extends FieldConstraintVO<IJsonFieldConstraint>
})
}
override get schema() {
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()])
let base: z.ZodTypeAny = z.lazy(() => z.union([literalSchema, z.array(base), z.record(base)]))
let base: z.ZodTypeAny = jsonSchemaValue

if (!this.props.required) {
base = base.optional().nullable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { FieldValueObject } from "../../field-value"

export type { JsonValue } from "type-fest"

const baseSchema = z.union([z.string(), z.number(), z.boolean(), z.null()])
const jsonSchemaValue: z.ZodTypeAny = z.lazy(() =>
z.union([baseSchema, z.array(jsonSchemaValue), z.record(jsonSchemaValue)]),
)
const baseSchema = z.union([
z.string(),
z.number(),
z.boolean(),
z.null(),
z.array(z.any()),
z.record(z.any(), z.any()),
])
export const jsonSchemaValue = baseSchema

export const mutateJsonFieldValueSchema = jsonSchemaValue

Expand Down

0 comments on commit 0ab2036

Please sign in to comment.