Skip to content

Commit

Permalink
feat: add isPublished filter
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 28, 2024
1 parent 9207ab0 commit 4f7ec85
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
6 changes: 6 additions & 0 deletions libs/zhi-siyuan-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# zhi-siyuan-api

## 2.18.0

### Minor Changes

- feat: add count filter

## 2.17.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion libs/zhi-siyuan-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-siyuan-api",
"version": "2.17.0",
"version": "2.18.0",
"type": "module",
"description": "a siyuan-note api including both kernel and client",
"main": "./dist/index.js",
Expand Down
67 changes: 43 additions & 24 deletions libs/zhi-siyuan-api/src/lib/kernel/siyuanKernelApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
* questions.
*/

import SiyuanConfig from "../config/siyuanConfig";
import ISiyuanKernelApi, { type SiyuanData } from "./ISiyuanKernelApi";
import { JsonUtil, StrUtil } from "zhi-common";
import { createSiyuanAppLogger } from "../utils";
import SiyuanConfig from "../config/siyuanConfig"
import ISiyuanKernelApi, { type SiyuanData } from "./ISiyuanKernelApi"
import { JsonUtil, StrUtil } from "zhi-common"
import { createSiyuanAppLogger } from "../utils"

/**
* 思源笔记服务端API v2.8.2
Expand Down Expand Up @@ -66,14 +66,27 @@ class SiyuanKernelApi implements ISiyuanKernelApi {
/**
* 分页获取根文档
*
* ```sql
* SELECT COUNT(DISTINCT b1.root_id) as count
* FROM blocks b1, attributes a
* WHERE b1.root_id = a.root_id
* AND (b1.root_id ='' OR (b1.content LIKE '%%') OR (b1.tag LIKE '%%'))
* AND a.name LIKE '%%';
* ```
*
* @param keyword - 关键字
* @param isPublished - 是否发布
*/
public async getRootBlocksCount(keyword: string): Promise<number> {
public async getRootBlocksCount(keyword: string, isPublished?: boolean): Promise<number> {
let isPublishedFilter = "%%"
if (isPublished) {
isPublishedFilter = "custom-%-yaml"
}
const stmt = `SELECT COUNT(DISTINCT b1.root_id) as count
FROM blocks b1
WHERE 1 = 1
AND (b1.root_id ='${keyword}' OR (b1.content LIKE '%${keyword}%') OR (b1.tag LIKE '%${keyword}%')
)`
FROM blocks b1, attributes a
WHERE b1.root_id = a.root_id
AND (b1.root_id ='${keyword}' OR (b1.content LIKE '%${keyword}%') OR (b1.tag LIKE '%${keyword}%'))
AND a.name LIKE '${isPublishedFilter}'`
const data = (await this.sql(stmt)) as any[]
this.logger.debug("getRootBlocksCount data=>", data[0].count)
return data[0].count
Expand Down Expand Up @@ -140,16 +153,25 @@ class SiyuanKernelApi implements ISiyuanKernelApi {
*
* ```sql
* SELECT COUNT(DISTINCT b1.root_id) AS count
* FROM blocks b1
* WHERE b1.path LIKE '%/20220927094918-1d85uyp%';
* FROM blocks b1, attributes a
* WHERE b1.root_id = a.root_id
* AND b1.root_id='20230815225853-a2ybito' OR b1.path LIKE '%/20230815225853-a2ybito%'
* AND a.name LIKE '%%'
* ```
*
* @param docId 文档ID
* @param isPublished 是否已发布
*/
public async getSubdocCount(docId: string): Promise<number> {
public async getSubdocCount(docId: string, isPublished?: boolean): Promise<number> {
let isPublishedFilter = "%%"
if (isPublished) {
isPublishedFilter = "custom-%-yaml"
}
const stmt = `SELECT COUNT(DISTINCT b1.root_id) AS count
FROM blocks b1
WHERE b1.root_id='${docId}' OR b1.path LIKE '%/${docId}%'`
FROM blocks b1, attributes a
WHERE b1.root_id = a.root_id
AND b1.root_id='${docId}' OR b1.path LIKE '%/${docId}%'
AND a.name LIKE '${isPublishedFilter}'`
const data = (await this.sql(stmt)) as any[]
return data[0].count
}
Expand All @@ -175,7 +197,13 @@ class SiyuanKernelApi implements ISiyuanKernelApi {
* @param keyword 关键字
* @param isPublished 是否已发布
*/
public async getSubdocs(docId: string, page: number, pagesize: number, keyword: string, isPublished?: boolean): Promise<any> {
public async getSubdocs(
docId: string,
page: number,
pagesize: number,
keyword: string,
isPublished?: boolean
): Promise<any> {
let isPublishedFilter = "%%"
if (isPublished) {
isPublishedFilter = "custom-%-yaml"
Expand All @@ -194,15 +222,6 @@ class SiyuanKernelApi implements ISiyuanKernelApi {
LIMIT ${page * pagesize}, ${pagesize}
)
ORDER BY b2.content, b2.updated DESC, b2.created DESC, b2.id`
// const stmt = `SELECT DISTINCT b2.root_id,b2.content,b2.path FROM blocks b2
// WHERE b2.id IN (
// SELECT DISTINCT b1.root_id
// FROM blocks b1
// WHERE b1.root_id='${docId}' OR b1.path like '%/${docId}%'
// AND ((b1.content LIKE '%${keyword}%') OR (b1.tag LIKE '%${keyword}%'))
// ORDER BY b1.updated DESC,b1.created DESC LIMIT ${page * pagesize},${pagesize}
// )
// ORDER BY b2.content,b2.updated DESC,b2.created DESC,id`

this.logger.debug("siyuanApi getSubdocs sql=>", stmt)
return await this.sql(stmt)
Expand Down

0 comments on commit 4f7ec85

Please sign in to comment.