Skip to content

Commit

Permalink
refactor: data service
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Dec 12, 2024
1 parent b8ad341 commit 2a3ca1d
Show file tree
Hide file tree
Showing 83 changed files with 391 additions and 664 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import { TriangleAlertIcon } from "lucide-svelte"
import * as Tooltip from "$lib/components/ui/tooltip"
import type { TableDo } from "@undb/table"
import { getDataService, getIsLocal } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
export let tableId: string | undefined
export let table: TableDo | undefined
Expand All @@ -22,8 +22,7 @@
$: isValid = isValidWidget(widget) && !!tableId
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const getAggregate = createQuery({
queryKey: ["aggregate", widget.id],
Expand All @@ -43,7 +42,6 @@
ignoreView,
})
}
const dataService = await getDataService(isLocal, isPlayground)
return dataService.records.getAggregates({
tableId: tableId!,
viewId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import autoAnimate from "@formkit/auto-animate"
import type { Readable } from "svelte/store"
import { LL } from "@undb/i18n/client"
import { getDataService, getIsLocal } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
const table = getTable()
export let viewId: Readable<string | undefined>
Expand All @@ -44,17 +42,15 @@
export let filter: IViewFilterGroup | undefined = undefined
export let onSuccess: (data: IBulkUpdateRecordsCommandOutput) => void = () => {}
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
let selectedFieldIds: string[] = []
$: selectedFields = selectedFieldIds.map((id) => $table.schema.getFieldById(new FieldIdVo(id)).unwrap())
const client = useQueryClient()
const dataService = _getDataService()
const updateRecordMutation = createMutation({
mutationFn: async (command: IBulkUpdateRecordsCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.records.updateRecords(command)
},
onSuccess: async (data) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import { useQueryClient } from "@tanstack/svelte-query"
import { cn } from "$lib/utils"
import { calendarStore } from "$lib/store/calendar.store"
import { getDataService, getIsLocal } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
import { type IUpdateRecordCommand } from "@undb/commands"
export let view: CalendarView
Expand All @@ -21,12 +20,10 @@
let field = fieldId ? $t.schema.getFieldById(new FieldIdVo(fieldId)).into(undefined) : undefined
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const updateRecord = createMutation({
mutationFn: async (command: IUpdateRecordCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.records.updateRecord(command)
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import { defaults, superForm } from "sveltekit-superforms"
import { zodClient } from "sveltekit-superforms/adapters"
import CreateFieldButton from "../create-field/create-field-button.svelte"
import { trpc } from "$lib/trpc/client"
import { createMutation } from "@tanstack/svelte-query"
import { toast } from "svelte-sonner"
import { invalidate } from "$app/navigation"
import { hasPermission } from "$lib/store/space-member.store"
import { CircleCheckBigIcon } from "lucide-svelte"
import { LL } from "@undb/i18n/client"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
import { type IUpdateViewCommand } from "@undb/commands"
export let readonly = false
Expand Down Expand Up @@ -52,12 +50,10 @@
const { enhance, form: formData } = form
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const updateViewMutation = createMutation({
mutationFn: async (command: IUpdateViewCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.table.view.updateView(command)
},
mutationKey: ["updateView"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
import { LL } from "@undb/i18n/client"
import { calendarTimeScales, type CalendarTimeScale, type CalendarView } from "@undb/table"
import { createMutation } from "@tanstack/svelte-query"
import { trpc } from "$lib/trpc/client"
import { toast } from "svelte-sonner"
import { getTable } from "$lib/store/table.store"
import { invalidate } from "$app/navigation"
import { type ICalendarViewDTO } from "@undb/table"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
import { type IUpdateViewCommand } from "@undb/commands"
export let view: CalendarView
Expand All @@ -41,12 +38,10 @@
})
}
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const updateViewMutation = createMutation({
mutationFn: async (command: IUpdateViewCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.table.view.updateView(command)
},
mutationKey: ["updateView"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import { CREATE_RECORD_MODAL, openModal } from "$lib/store/modal.store"
import { tick } from "svelte"
import { hasPermission } from "$lib/store/space-member.store"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
import { type IUpdateRecordCommand } from "@undb/commands"
export let viewId: Readable<string | undefined>
Expand Down Expand Up @@ -106,8 +105,7 @@
const t = getTable()
const q = queryParam("q")
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const getRecords = createQuery(
derived([t, viewId, q, date], ([$table, $viewId, $q, $date]) => {
Expand All @@ -116,7 +114,6 @@
queryKey: ["records", $table?.id.value, $viewId, $q, $date.toISOString()],
enabled: view?.type === "calendar" && !disableRecordQuery,
queryFn: async () => {
const dataService = await getDataService(isLocal, isPlayground)
const value = format($date, "yyyy-MM-dd")
if (shareId) {
return trpc.shareData.records.query({
Expand Down Expand Up @@ -258,10 +255,7 @@
let overMinutes: number | undefined = undefined
const updateRecord = createMutation({
mutationFn: async (command: IUpdateRecordCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.records.updateRecord(command)
},
mutationFn: dataService.records.updateRecord,
})
const client = useQueryClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
import { isToday } from "date-fns/isToday"
import { isWeekend } from "date-fns/isWeekend"
import { createMutation, useQueryClient } from "@tanstack/svelte-query"
import { trpc } from "$lib/trpc/client"
import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
import { format } from "date-fns/format"
import { cn } from "$lib/utils"
import { CREATE_RECORD_MODAL, openModal } from "$lib/store/modal.store"
import { defaultRecordValues } from "$lib/store/records.store"
import { type Readable } from "svelte/store"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { type IUpdateRecordCommand } from "@undb/commands"
import { _getDataService } from "$lib/store/data-service.store"
export let field: DateField | DateRangeField
export let date: Date
Expand All @@ -36,8 +33,7 @@
const isSelected = calendarStore.isSelected
const getIsSameMonth = calendarStore.getIsSameMonth
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
$: color = $viewId ? $table.views.getViewById($viewId)?.color.into(undefined) : undefined
Expand Down Expand Up @@ -90,10 +86,7 @@
}
const updateRecord = createMutation({
mutationFn: async (command: IUpdateRecordCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.records.updateRecord(command)
},
mutationFn: dataService.records.updateRecord,
})
const client = useQueryClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import { CalendarView } from "@undb/table"
import { type MaybeConditionGroup, type IViewFilterOptionSchema } from "@undb/table"
import { LL } from "@undb/i18n/client"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
export let viewId: Readable<string | undefined>
export let view: CalendarView
Expand All @@ -31,8 +30,8 @@
export let readonly = false
const t = getTable()
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
let defaultField = $t.schema.getDefaultDisplayField().into(undefined)
Expand Down Expand Up @@ -102,7 +101,6 @@
return {
queryKey: ["records", $table?.id.value, $viewId, $scope, dateString, $search],
queryFn: async ({ pageParam = 1 }) => {
const dataService = await getDataService(isLocal, isPlayground)
if (shareId) {
return trpc.shareData.records.query({
shareId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import { createQuery } from "@tanstack/svelte-query"
import { queryParam } from "sveltekit-search-params"
import CalendarDateRemoveButton from "./calendar-date-remove-button.svelte"
import CalendarViewMiniMonth from "./calendar-view-mini-month.svelte"
import CalendarViewMonthRecords from "./calendar-view-month-records.svelte"
import CalendarViewMonthDate from "./calendar-view-month-date.svelte"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
export let viewId: Readable<string | undefined>
export let view: CalendarView
Expand All @@ -28,8 +26,7 @@
const t = getTable()
const q = queryParam("q")
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const getRecords = createQuery(
derived([t, viewId, q, startTimestamp, endTimestamp], ([$table, $viewId, $q, $startTimestamp, $endTimestamp]) => {
Expand All @@ -45,7 +42,6 @@
],
enabled: view?.type === "calendar" && !!$startTimestamp && !!$endTimestamp && !disableRecordQuery,
queryFn: async () => {
const dataService = await getDataService(isLocal, isPlayground)
if (shareId) {
return trpc.shareData.records.query({
shareId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import { CircleCheckBigIcon } from "lucide-svelte"
import * as Form from "$lib/components/ui/form"
import { LL } from "@undb/i18n/client"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { type IUpdateViewCommand } from "@undb/commands"
import { _getDataService } from "$lib/store/data-service.store"
export let readonly = false
Expand Down Expand Up @@ -64,14 +62,10 @@
const { enhance, form: formData, validateForm } = form
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const updateViewMutation = createMutation({
mutationFn: async (command: IUpdateViewCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.table.view.updateView(command)
},
mutationFn: dataService.table.view.updateView,
mutationKey: ["updateView"],
async onSuccess(data, variables, context) {
toast.success($LL.table.view.updated())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
import { LoaderCircleIcon } from "lucide-svelte"
import { getNextName } from "@undb/utils"
import { LL } from "@undb/i18n/client"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { _getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const mutation = createMutation({
mutationFn: async (command: ICreateBaseCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.base.createBase(command)
},
mutationFn: dataService.base.createBase,
async onSuccess(data) {
form.reset()
if (isPlayground) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,21 @@
import FieldTypePicker from "../field-picker/field-type-picker.svelte"
import { LL } from "@undb/i18n/client"
import { BetweenVerticalStartIcon, LoaderCircleIcon } from "lucide-svelte"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { type ICreateFieldCommand } from "@undb/commands"
import { _getDataService } from "$lib/store/data-service.store"
const table = getTable()
export let onSuccess: () => void = () => {}
let name = ""
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const client = useQueryClient()
const createFieldMutation = createMutation(
derived([table], ([$table]) => ({
mutationKey: ["table", $table.id.value, "createField"],
mutationFn: async (command: ICreateFieldCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.table.field.createField(command)
},
mutationFn: dataService.table.field.createField,
async onSuccess() {
toast.success($LL.table.field.created())
reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import type { ICreateRecordCommand, ICreateRecordCommandOutput } from "@undb/commands"
import { onMount } from "svelte"
import { LL } from "@undb/i18n/client"
import { getIsLocal, getDataService } from "$lib/store/data-service.store"
import { getIsPlayground } from "$lib/store/playground.svelte"
import { _getDataService } from "$lib/store/data-service.store"
// beforeNavigate(({ cancel }) => {
Expand All @@ -42,15 +41,11 @@
const client = useQueryClient()
const mediaQuery = useMediaQuery("(max-width: 768px)")
const isLocal = getIsLocal()
const isPlayground = getIsPlayground()
const dataService = _getDataService()
const createRecordMutation = createMutation(
derived([table], ([$table]) => ({
mutationFn: async (command: ICreateRecordCommand) => {
const dataService = await getDataService(isLocal, isPlayground)
return dataService.records.createRecord(command)
},
mutationFn: dataService.records.createRecord,
mutationKey: [$table.id.value, "createRecord"],
onSuccess: (data: ICreateRecordCommandOutput) => {
client.invalidateQueries({
Expand Down
Loading

0 comments on commit 2a3ca1d

Please sign in to comment.