Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

refactor(nc-gui): getValidSearchQueryForColumn fn and move it from useSmartsheetStore to useFieldQuery so that we can use it anywhere without depending on smartsheetStore

Ramesh Mane2af36cb14a21
diff --git a/packages/nc-gui/components/n/RecordPicker/Records.vue b/packages/nc-gui/components/n/RecordPicker/Records.vue
index facb2ab65c..e2681710f8 100644
--- a/packages/nc-gui/components/n/RecordPicker/Records.vue
+++ b/packages/nc-gui/components/n/RecordPicker/Records.vue
@@ -1,13 +1,10 @@
<script setup lang="ts">
import {
type ColumnType,
- FormulaDataTypes,
type TableType,
- UITypes,
type ViewType,
isCreatedOrLastModifiedTimeCol,
isLinksOrLTAR,
- isNumericCol,
isSystemColumn,
} from 'nocodb-sdk'
import { searchLike } from '~/utils/searchUtils'
@@ -32,7 +29,7 @@ useProvideSmartsheetLtarHelpers(meta)
const { isMobileMode } = useGlobal()
-const { sqlUi, getValidSearchQueryForColumn } = useSmartsheetStoreOrThrow()
+const { getValidSearchQueryForColumn } = useFieldQuery()
const _fields = computedInject(FieldsInj, (_fields) => {
const conditionToCheck = (col: ColumnType) =>
@@ -58,23 +55,7 @@ const computedWhere = computed(() => {
const fieldQuery = columnsToSearch
.map((col) => {
- let searchQuery = where.value.trim()
-
- searchQuery = getValidSearchQueryForColumn(col, searchQuery, meta.value as TableType)
-
- if (!isValidValue(searchQuery)) return ''
-
- if (
- (col.uidt !== UITypes.Formula || getFormulaColDataType(col) !== FormulaDataTypes.NUMERIC) &&
- !isNumericCol(col) &&
- sqlUi.value &&
- ['text', 'string'].includes(sqlUi.value.getAbstractType(col)) &&
- col.dt !== 'bigint'
- ) {
- return `(${col.title},like,%${searchQuery}%)`
- }
-
- return `(${col.title},eq,${searchQuery})`
+ return getValidSearchQueryForColumn(col, where.value.trim(), meta.value as TableType, true)
})
.filter(Boolean)
.join('~or')
@@ -244,28 +225,34 @@ useScroll(scrollWrapper, {
behavior: 'smooth',
})
-watch(computedWhere, async () => {
- if (records?.value?.length) {
- const filteredRecords = searchLike(records.value, `%${where.value}%`)
- totalRows.value = filteredRecords.length
- const tempCachedRows = new Map()
- filteredRecords.forEach((row, index) => {
- tempCachedRows.set(index, {
- row,
- rowMeta: {
- rowIndex: index,
- },
- })
+// watch the where query and update the cached rows
+watch(where, () => {
+ if (!records?.value?.length) return
+
+ const filteredRecords = searchLike(records.value, `%${where.value}%`)
+ totalRows.value = filteredRecords.length
+ const tempCachedRows = new Map()
+ filteredRecords.forEach((row, index) => {
+ tempCachedRows.set(index, {
+ row,
+ rowMeta: {
+ rowIndex: index,
+ },
})
- cachedRows.value = tempCachedRows
- } else {
- await syncCount()
- const newItems = await loadData()
- newItems.forEach((item) => cachedRows.value.set(item.rowMeta.rowIndex!, item))
+ })
+ cachedRows.value = tempCachedRows
+})
- calculateSlices()
- await updateVisibleRows()
- }
+// watch the computed where query and update the cached rows
+watch(computedWhere, async () => {
+ if (records?.value?.length) return
+
+ await syncCount()
+ const newItems = await loadData()
+ newItems.forEach((item) => cachedRows.value.set(item.rowMeta.rowIndex!, item))
+
+ calculateSlices()
+ await updateVisibleRows()
})
onMounted(async () => {
diff --git a/packages/nc-gui/composables/useFieldQuery.ts b/packages/nc-gui/composables/useFieldQuery.ts
index c326f6f855..8fa8e7c699 100644
--- a/packages/nc-gui/composables/useFieldQuery.ts
+++ b/packages/nc-gui/composables/useFieldQuery.ts
@@ -1,4 +1,14 @@
+import { ColumnHelper, FormulaDataTypes, isNumericCol, isVirtualCol, UITypes, type ColumnType, type TableType } from 'nocodb-sdk'
+
export function useFieldQuery() {
+ const baseStore = useBase()
+
+ const { isMysql, isPg } = baseStore
+
+ const { sqlUis } = storeToRefs(baseStore)
+
+ const { metas } = useMetas()
+
// initial search object
const emptyFieldQueryObj = {
field: '',
@@ -23,5 +33,60 @@ export function useFieldQuery() {
search.value = searchMap.value[id]
}
- return { search, loadFieldQuery }
+ const getValidSearchQueryForColumn = (col: ColumnType, query?: string, tableMeta?: TableType, getWhereQuery = false) => {
+ if (!isValidValue(query)) return ''
+
+ let searchQuery = query
+
+ try {
+ /**
+ * This method can throw errors. so it's important to use a try-catch block when calling it.
+ */
+ searchQuery = ColumnHelper.serializeValue(searchQuery, {
+ col,
+ isMysql,
+ isPg,
+ meta: tableMeta,
+ metas: metas.value,
+ serializeSearchQuery: true,
+ })
+ } catch (_err: any) {
+ /**
+ * If it is a virtual column, then send query as it is
+ */
+ if (!isVirtualCol(col)) {
+ searchQuery = ''
+ /**
+ * We don't have to anything if serializeValue is not valid for current column
+ */
+ console.log('invalid search query for column', col.title, searchQuery)
+ } else if (col.uidt !== UITypes.Formula) {
+ searchQuery = query
+ }
+ }
+
+ if (isVirtualCol(col) && col.uidt !== UITypes.Formula && !isValidValue(searchQuery)) {
+ searchQuery = query
+ }
+
+ if (!isValidValue(searchQuery)) return ''
+
+ if (!getWhereQuery) return searchQuery ?? ''
+
+ const sqlUi = tableMeta?.source_id ? sqlUis.value[tableMeta.source_id] : Object.values(sqlUis.value)[0]
+
+ if (
+ (col.uidt !== UITypes.Formula || getFormulaColDataType(col) !== FormulaDataTypes.NUMERIC) &&
+ !isNumericCol(col) &&
+ sqlUi &&
+ ['text', 'string'].includes(sqlUi.getAbstractType(col)) &&
+ col.dt !== 'bigint'
+ ) {
+ return `(${col.title},like,%${searchQuery}%)`
+ }
+
+ return `(${col.title},eq,${searchQuery})`
+ }
+
+ return { search, loadFieldQuery, getValidSearchQueryForColumn }
}
diff --git a/packages/nc-gui/composables/useLTARStore.ts b/packages/nc-gui/composables/useLTARStore.ts
index f8e067b362..5963d9fc5e 100644
--- a/packages/nc-gui/composables/useLTARStore.ts
+++ b/packages/nc-gui/composables/useLTARStore.ts
@@ -114,7 +114,9 @@ const [useProvideLTARStore, useLTARStore] = useInjectionState(
const { sharedView } = useSharedView()
- const { getViewColumns, getValidSearchQueryForColumn } = useSmartsheetStoreOrThrow()
+ const { getViewColumns } = useSmartsheetStoreOrThrow()
+
+ const { getValidSearchQueryForColumn } = useFieldQuery()
const baseId = base.value?.id || (sharedView.value?.view as any)?.base_id
diff --git a/packages/nc-gui/composables/useSmartsheetStore.ts b/packages/nc-gui/composables/useSmartsheetStore.ts
index 9da83abe05..b210cc7cbd 100644
--- a/packages/nc-gui/composables/useSmartsheetStore.ts
+++ b/packages/nc-gui/composables/useSmartsheetStore.ts
@@ -45,7 +45,7 @@ const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
(meta.value as TableType)?.source_id ? sqlUis.value[(meta.value as TableType).source_id!] : Object.values(sqlUis.value)[0],
)
- const { search } = useFieldQuery()
+ const { search, getValidSearchQueryForColumn } = useFieldQuery()
const eventBus = useEventBus<SmartsheetStoreEvents>(EventBusEnum.SmartsheetStore)
@@ -110,47 +110,6 @@ const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
return search.value.query?.trim() && !isMobileMode.value && (isGrid.value || isGallery.value)
})
- const getValidSearchQueryForColumn = (col: ColumnType, query?: string, tableMeta?: TableType) => {
- if (!isValidValue(query)) return ''
-
- let searchQuery = query
-
- try {
- /**
- * This method can throw errors. so it's important to use a try-catch block when calling it.
- */
- searchQuery = ColumnHelper.serializeValue(searchQuery, {
- col,
- isMysql,
- isPg,
- meta: tableMeta,
- metas: metas.value,
- serializeSearchQuery: true,
- })
- } catch (_err: any) {
- /**
- * If it is a virtual column, then send query as it is
- */
- if (!isVirtualCol(col)) {
- searchQuery = ''
- /**
- * We don't have to anything if serializeValue is not valid for current column
- */
- console.log('invalid search query for column', col.title, searchQuery)
- } else if (col.uidt !== UITypes.Formula) {
- searchQuery = query
- }
- }
-
- if (isVirtualCol(col) && col.uidt !== UITypes.Formula && !isValidValue(searchQuery)) {
- searchQuery = query
- }
-
- if (!isValidValue(searchQuery)) return ''
-
- return searchQuery ?? ''
- }
-
const xWhere = computed(() => {
let where
@@ -168,24 +127,11 @@ const [useProvideSmartsheetStore, useSmartsheetStore] = useInjectionState(
let searchQuery = search.value.query.trim()
- searchQuery = getValidSearchQueryForColumn(col, searchQuery, meta.value as TableType)
-
- if (!isValidValue(searchQuery)) return where
-
- // concat the where clause if query is present
- if (
- (col.uidt !== UITypes.Formula || getFormulaColDataType(col) !== FormulaDataTypes.NUMERIC) &&
- !isNumericCol(col) &&
- sqlUi.value &&
- ['text', 'string'].includes(sqlUi.value.getAbstractType(col)) &&
- col.dt !== 'bigint'
- ) {
- where = `${where ? `${where}~and` : ''}(${col.title},like,%${searchQuery}%)`
- } else {
- where = `${where ? `${where}~and` : ''}(${col.title},eq,${searchQuery})`
- }
+ const colWhereQuery = getValidSearchQueryForColumn(col, searchQuery, meta.value as TableType, true)
+
+ if (!colWhereQuery) return where
- return where
+ return `${where ? `${where}~and` : ''}${colWhereQuery}`
})
const isActionPaneActive = ref(false)