Skip to content

Commit

Permalink
feat(wolai): 支持排序和筛选
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Apr 26, 2024
1 parent 04240e1 commit aec0da8
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 6 deletions.
106 changes: 102 additions & 4 deletions packages/sdk-wolai/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { WoLaiConfig, WoLaiDoc, WoLaiTablePage, WoLaiTableRow, WoLaiTableRows } from './types'
import {
WoLaiConfig,
WoLaiDoc,
WolaiFilterAndSortParams,
WoLaiTablePage,
WoLaiTableRow,
WoLaiTableRows,
} from './types'
import { out, request, RequestOptions } from '@elog/shared'
import { DocCatalog, DocDetail, DocProperties } from '@elog/types'
import asyncPool from 'tiny-async-pool'
import { genCatalog, props } from './utils'
import { filterDocs, genCatalog, props, sortDocs } from './utils'
import * as buffer from 'buffer'
import { WolaiSortDirectionEnum, WolaiSortPresetEnum } from './const'

/**
* WoLaiClient
Expand All @@ -12,6 +20,7 @@ class WoLaiClient {
config: WoLaiConfig
docList: WoLaiTableRow[] = []
catalog: WoLaiTableRow[] = []
filterAndSortParams: WolaiFilterAndSortParams

constructor(config: WoLaiConfig) {
this.config = config
Expand All @@ -20,6 +29,88 @@ class WoLaiClient {
out.err('缺少参数', '缺少WoLai配置信息')
process.exit(-1)
}
this.filterAndSortParams = this.initFilterAndSortParamsParams()
}

/**
* 初始化过滤和排序参数
*/
initFilterAndSortParamsParams(): WolaiFilterAndSortParams {
let sort = this.config.sort as WolaiFilterAndSortParams['sort']
if (typeof this.config.sort === 'boolean') {
if (!this.config.sort) {
// 不排序
sort = undefined
} else {
// 默认排序
sort = { property: 'createdAt', direction: WolaiSortDirectionEnum.descending }
}
} else if (typeof this.config.sort === 'string') {
// 预设值
const sortPreset = this.config.sort as WolaiSortPresetEnum
switch (sortPreset) {
case WolaiSortPresetEnum.dateDesc:
sort = { property: 'date', direction: WolaiSortDirectionEnum.descending }
break
case WolaiSortPresetEnum.dateAsc:
sort = { property: 'date', direction: WolaiSortDirectionEnum.ascending }
break
case WolaiSortPresetEnum.sortDesc:
sort = { property: 'sort', direction: WolaiSortDirectionEnum.descending }
break
case WolaiSortPresetEnum.sortAsc:
sort = { property: 'sort', direction: WolaiSortDirectionEnum.ascending }
break
case WolaiSortPresetEnum.createTimeDesc:
sort = {
property: 'createdAt',
direction: WolaiSortDirectionEnum.descending,
}
break
case WolaiSortPresetEnum.createTimeAsc:
sort = {
property: 'createdAt',
direction: WolaiSortDirectionEnum.ascending,
}
break
case WolaiSortPresetEnum.updateTimeDesc:
sort = {
property: 'updatedAt',
direction: WolaiSortDirectionEnum.descending,
}
break
case WolaiSortPresetEnum.updateTimeAsc:
sort = {
property: 'updatedAt',
direction: WolaiSortDirectionEnum.ascending,
}
break
default:
sort = {
property: 'createdAt',
direction: WolaiSortDirectionEnum.descending,
}
}
}

let filter = this.config.filter as WolaiFilterAndSortParams['filter']
// 如果是boolean类型
if (typeof this.config.filter === 'boolean') {
// 如果设置为false
if (!this.config.filter) {
filter = undefined
} else {
// 如果设置为true
filter = {
property: 'status',
value: '已发布',
}
}
}
return {
filter,
sort,
}
}

/**
Expand Down Expand Up @@ -82,14 +173,21 @@ class WoLaiClient {
})
// 转换 props
const tableFields = tablePage.database_tables[databaseId].properties
const docs = rows.rows.map((row) => {
let docs = rows.rows.map((row) => {
const properties = props(row, tableFields)
return {
...row,
createdAt: row.created_time,
updatedAt: row.edited_time,
properties,
}
})
this.catalog.push(...docs)
const { filter, sort } = this.filterAndSortParams
docs = filterDocs(docs, filter)
// 排序
docs = sortDocs(docs, sort)
// 过滤条件
this.catalog = docs
this.docList = docs
return docs
}
Expand Down
21 changes: 21 additions & 0 deletions packages/sdk-wolai/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export enum WolaiSortPresetEnum {
/** 按自定义日期排序 */
dateDesc = 'dateDesc',
dateAsc = 'dateAsc',
/** 按创建时间排序 */
createTimeDesc = 'createTimeDesc',
createTimeAsc = 'createTimeAsc',
/** 按更新时间排序 */
updateTimeDesc = 'updateTimeDesc',
updateTimeAsc = 'updateTimeAsc',
/** 按sort字段排序 */
sortDesc = 'sortDesc',
sortAsc = 'sortAsc',
}

export enum WolaiSortDirectionEnum {
/** 降序 */
descending = 'descending',
/** 升序 */
ascending = 'ascending',
}
21 changes: 21 additions & 0 deletions packages/sdk-wolai/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DocProperties } from '@elog/types'
import { WolaiSortPresetEnum } from './const'
export interface WoLaiConfig {
token: string
/** 文档ID */
Expand All @@ -9,8 +10,26 @@ export interface WoLaiConfig {
baseUrl?: string
/** 并发限制 */
limit?: number
filter?: WolaiFilter
sort?: WolaiSort
}

export interface WolaiFilterItem {
property: string
value: string
}

export interface WolaiSortItem {
property: string
direction: string
}

export type WolaiFilter = boolean | WolaiFilterItem | WolaiFilterItem[]
export type WolaiSort = boolean | WolaiSortPresetEnum | WolaiSortItem
export interface WolaiFilterAndSortParams {
filter?: WolaiFilterItem | WolaiFilterItem[]
sort?: WolaiSortItem
}
/**
* 文档块值
*/
Expand Down Expand Up @@ -123,6 +142,8 @@ export interface WoLaiTableRow {

export interface WoLaiDoc extends WoLaiTableRow {
_index?: number
createdAt: number
updatedAt: number
properties: DocProperties
}

Expand Down
81 changes: 79 additions & 2 deletions packages/sdk-wolai/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { WoLaiDatabaseTableProperty, WoLaiTableRow } from './types'
import {
WoLaiDatabaseTableProperty,
WoLaiDoc,
WolaiFilterItem,
WolaiSortItem,
WoLaiTableRow,
} from './types'
import { DocProperties, DocCatalog, DocDetail } from '@elog/types'
import { timeFormat, out } from '@elog/shared'
import { timeFormat, out, isTime, getTimes } from '@elog/shared'
import { WolaiSortDirectionEnum } from './const'

/**
* 获取元数据Val
Expand Down Expand Up @@ -139,3 +146,73 @@ export function genCatalog(doc: DocDetail, property: string): DocCatalog[] | und
return undefined
}
}

/**
* 文档排序
* @param docs
* @param sorts
*/
export function sortDocs(docs: WoLaiDoc[], sorts?: WolaiSortItem) {
return docs.sort((a, b) => {
if (sorts) {
let aSortValue = a.properties[sorts.property]
let bSortValue = b.properties[sorts.property]
const sortDirection = sorts.direction
// 如果不存在则不排序
if (!aSortValue || !bSortValue) {
return 0
}
// 判断是不是数字
if (Number.isNaN(Number(aSortValue)) || Number.isNaN(Number(bSortValue))) {
// 如果判断字符串是不是时间
if (isTime(aSortValue) && isTime(bSortValue)) {
// 将2023/05/08 00:00转成时间戳
aSortValue = getTimes(aSortValue)
bSortValue = getTimes(bSortValue)
} else {
// 都不是则排后面
return -1
}
} else {
aSortValue = Number(aSortValue)
bSortValue = Number(bSortValue)
}

if (sortDirection === WolaiSortDirectionEnum.ascending) {
// 正序排序
return aSortValue - bSortValue
} else if (sortDirection === WolaiSortDirectionEnum.descending) {
// 倒序排序
return bSortValue - aSortValue
} else {
// 属性错误
return 0
}
} else {
// 不排序
return 0
}
})
}

/**
* 文档过滤
* @param docs
* @param filter
*/
export function filterDocs(docs: WoLaiDoc[], filter?: WolaiFilterItem | WolaiFilterItem[]) {
return docs.filter((page) => {
const pageProperties = page.properties
// 过滤
if (filter && Array.isArray(filter)) {
return filter.every((f) => {
return pageProperties[f.property] === f.value
})
// 如果是对象
} else if (typeof filter === 'object') {
return pageProperties[filter.property] === filter.value
}
// 不过滤
return true
})
}

0 comments on commit aec0da8

Please sign in to comment.