@rxdrag/website-lib-core 0.0.83 → 0.0.86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rxdrag/website-lib-core",
3
- "version": "0.0.83",
3
+ "version": "0.0.86",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.ts"
@@ -25,8 +25,8 @@
25
25
  "eslint": "^7.32.0",
26
26
  "typescript": "^5",
27
27
  "@rxdrag/slate-preview": "1.2.61",
28
- "@rxdrag/eslint-config-custom": "0.2.12",
29
- "@rxdrag/tsconfig": "0.2.0"
28
+ "@rxdrag/tsconfig": "0.2.0",
29
+ "@rxdrag/eslint-config-custom": "0.2.12"
30
30
  },
31
31
  "dependencies": {
32
32
  "@iconify/utils": "^3.0.2",
@@ -48,6 +48,19 @@ export class Entify implements IEntify {
48
48
 
49
49
  private constructor(protected envVariables: EnvVariables, protected imageSizes: ImageSizes) { }
50
50
 
51
+ private flattenTree<T>(nodes: T[] | undefined): T[] {
52
+ if (!nodes) return [];
53
+ let result: T[] = [];
54
+ for (const node of nodes) {
55
+ result.push(node);
56
+ const children = (node as { children?: T[] }).children;
57
+ if (children && children.length > 0) {
58
+ result = result.concat(this.flattenTree(children));
59
+ }
60
+ }
61
+ return result;
62
+ }
63
+
51
64
  public static getInstance(envVariables: EnvVariables, imageSizes: ImageSizes): Entify {
52
65
  if (!Entify.instance) {
53
66
  Entify.instance = new Entify(envVariables, imageSizes);
@@ -238,7 +251,7 @@ export class Entify implements IEntify {
238
251
  }
239
252
 
240
253
  public async getPostCategories() {
241
- return (await queryPostCategories(this.envVariables))?.items;
254
+ return await queryPostCategories(this.envVariables);
242
255
  }
243
256
 
244
257
  public async getPostCategoryBySlug(slug: string) {
@@ -277,7 +290,8 @@ export class Entify implements IEntify {
277
290
 
278
291
  public async getCategoredProductListPaths() {
279
292
  // 获取所有产品分类
280
- const categories = await this.getProductCategories();
293
+ const categoryTree = await this.getProductCategories();
294
+ const categories = this.flattenTree(categoryTree);
281
295
 
282
296
  // 为每个分类生成路径
283
297
  const paths = [];
@@ -335,14 +349,14 @@ export class Entify implements IEntify {
335
349
  }
336
350
 
337
351
  /**
338
- * TODO:未调试
339
352
  * 获取所有文章分类的分页路径
340
353
  * 用于生成静态页面
341
354
  * @returns 所有文章分类的分页路径
342
355
  */
343
356
  public async getCategoredPostListPaths() {
344
357
  // 获取所有文章分类
345
- const categories = await this.getPostCategories();
358
+ const categoryTree = await this.getPostCategories();
359
+ const categories = this.flattenTree(categoryTree);
346
360
 
347
361
  // 为每个分类生成路径
348
362
  const paths = [];
@@ -433,7 +447,7 @@ export class Entify implements IEntify {
433
447
  }
434
448
 
435
449
  public async getProductCategories() {
436
- return (await queryProductCategories(this.envVariables))?.items;
450
+ return await queryProductCategories(this.envVariables);
437
451
  }
438
452
 
439
453
  public async getProductCategoryBySlug(slug: string) {
@@ -0,0 +1,20 @@
1
+ export interface CategoryNode {
2
+ id?: string | null;
3
+ children?: CategoryNode[];
4
+ }
5
+
6
+ export function collectCategoryIds(category: CategoryNode): string[] {
7
+ const ids: string[] = [];
8
+ if (category.id) ids.push(category.id);
9
+
10
+ const traverse = (node: CategoryNode) => {
11
+ if (node.children) {
12
+ node.children.forEach((child) => {
13
+ if (child.id) ids.push(child.id);
14
+ traverse(child);
15
+ });
16
+ }
17
+ };
18
+ traverse(category);
19
+ return ids;
20
+ }
@@ -27,4 +27,6 @@ export * from "./queryWebSiteSettings";
27
27
  export * from "./upsertEntity";
28
28
  export * from "./fulltextSearch";
29
29
  export * from "./sendEmail";
30
- export * from "./createUploadCredentials";
30
+ export * from "./createUploadCredentials";
31
+ export * from "./listToTree";
32
+ export * from "./collectCategoryIds";
@@ -0,0 +1,23 @@
1
+ export function listToTree<
2
+ T extends { id?: string | null; parent?: { id?: string | null } | null }
3
+ >(items: T[]): (T & { children: T[] })[] {
4
+ const nodeMap = new Map<string, T & { children: T[] }>();
5
+
6
+ items.forEach((item) => {
7
+ if (item.id) {
8
+ nodeMap.set(item.id, { ...item, children: [] });
9
+ }
10
+ });
11
+
12
+ const tree: (T & { children: T[] })[] = [];
13
+
14
+ nodeMap.forEach((node) => {
15
+ if (node.parent?.id && nodeMap.has(node.parent.id)) {
16
+ nodeMap.get(node.parent.id)!.children.push(node);
17
+ } else {
18
+ tree.push(node);
19
+ }
20
+ });
21
+
22
+ return tree;
23
+ }
@@ -14,6 +14,7 @@ import { queryEntityList } from "./queryEntityList";
14
14
  import { ListResult } from "@rxdrag/entify-lib";
15
15
  import { EnvVariables } from "../types";
16
16
  import { TPostCategory } from "../view-model";
17
+ import { listToTree } from "./listToTree";
17
18
 
18
19
  export async function queryPostCategories(envVariables: EnvVariables) {
19
20
  const result = await queryEntityList<
@@ -39,17 +40,28 @@ export async function queryPostCategories(envVariables: EnvVariables) {
39
40
  },
40
41
  orderBy: [{ [PostCategoryFields.seqValue]: "asc" }],
41
42
  }
42
- ).posts(
43
- new PostQueryOptions([PostFields.id, PostFields.title, PostFields.slug], {
44
- where: {
45
- [PostFields.status]: {
46
- _eq: PublishableStatus.published,
47
- },
48
- },
49
- orderBy: [{ [ProductFields.seqValue]: "asc" }],
50
- })
51
- ),
43
+ )
44
+ .parent()
45
+ .posts(
46
+ new PostQueryOptions(
47
+ [PostFields.id, PostFields.title, PostFields.slug],
48
+ {
49
+ where: {
50
+ [PostFields.status]: {
51
+ _eq: PublishableStatus.published,
52
+ },
53
+ },
54
+ orderBy: [{ [ProductFields.seqValue]: "asc" }],
55
+ }
56
+ )
57
+ ),
52
58
  envVariables
53
59
  );
54
- return result as ListResult<TPostCategory> | undefined;
60
+ const typedResult = result as ListResult<TPostCategory> | undefined;
61
+
62
+ if (!typedResult) {
63
+ return undefined;
64
+ }
65
+
66
+ return listToTree(typedResult.items || []);
55
67
  }
@@ -14,11 +14,18 @@ import {
14
14
  ImageSize,
15
15
  PostCategoryFields,
16
16
  MediaFields,
17
+ PostCategory,
18
+ PostCategoryBoolExp,
19
+ PostCategoryOrderBy,
20
+ PostCategoryDistinctExp,
21
+ PostCategoryQueryOptions,
17
22
  } from "@rxdrag/rxcms-models";
18
23
  import { queryEntityList } from "./queryEntityList";
19
24
  import { ListResult } from "@rxdrag/entify-lib";
20
25
  import { EnvVariables } from "../types";
21
26
  import { TPost } from "../view-model";
27
+ import { queryOneEntity } from "./queryOneEntity";
28
+ import { CategoryNode, collectCategoryIds } from "./collectCategoryIds";
22
29
 
23
30
  export interface ListConditions {
24
31
  category?: string; //category slug
@@ -37,13 +44,45 @@ export async function queryPosts(
37
44
 
38
45
  let where = {};
39
46
  if (categorySlug) {
40
- where = {
41
- [PostAssciations.category]: {
42
- slug: {
43
- _eq: categorySlug,
47
+ // 查询分类及其子分类
48
+ const category = await queryOneEntity<
49
+ PostCategory,
50
+ PostCategoryBoolExp,
51
+ PostCategoryOrderBy,
52
+ PostCategoryDistinctExp
53
+ >(
54
+ new PostCategoryQueryOptions(
55
+ [PostCategoryFields.id, PostCategoryFields.slug],
56
+ {
57
+ where: {
58
+ slug: {
59
+ _eq: categorySlug,
60
+ },
61
+ },
62
+ }
63
+ )
64
+ // 支持三级结构
65
+ .children(
66
+ new PostCategoryQueryOptions([
67
+ PostCategoryFields.id,
68
+ PostCategoryFields.slug,
69
+ ]).children([])
70
+ ),
71
+ envVariables
72
+ );
73
+
74
+ if (category) {
75
+ const categoryIds = collectCategoryIds(
76
+ category as unknown as CategoryNode
77
+ );
78
+ where = {
79
+ [PostAssciations.category]: {
80
+ id: {
81
+ _in: categoryIds,
82
+ },
44
83
  },
45
- },
46
- };
84
+ };
85
+ }
47
86
  }
48
87
 
49
88
  // 默认查询字段
@@ -3,6 +3,7 @@ import { queryEntityList } from "./queryEntityList";
3
3
  import { ListResult } from "@rxdrag/entify-lib";
4
4
  import { EnvVariables } from "../types";
5
5
  import { TProductCategory } from "../view-model";
6
+ import { listToTree } from "./listToTree";
6
7
 
7
8
  export async function queryProductCategories(envVariables: EnvVariables) {
8
9
 
@@ -27,6 +28,7 @@ export async function queryProductCategories(envVariables: EnvVariables) {
27
28
  ]
28
29
  }
29
30
  )
31
+ .parent()
30
32
  .products(
31
33
  new ProductQueryOptions(
32
34
  [ProductFields.id, ProductFields.title, ProductFields.slug],
@@ -47,5 +49,11 @@ export async function queryProductCategories(envVariables: EnvVariables) {
47
49
  ),
48
50
  envVariables
49
51
  );
50
- return result as ListResult<TProductCategory> | undefined;
52
+ const typedResult = result as ListResult<TProductCategory> | undefined;
53
+
54
+ if (!typedResult) {
55
+ return undefined;
56
+ }
57
+
58
+ return listToTree(typedResult.items || []);
51
59
  }
@@ -7,6 +7,12 @@ import {
7
7
  ProductQueryOptions,
8
8
  PublishableStatus,
9
9
  ProductAssciations,
10
+ ProductCategory,
11
+ ProductCategoryBoolExp,
12
+ ProductCategoryOrderBy,
13
+ ProductCategoryDistinctExp,
14
+ ProductCategoryFields,
15
+ ProductCategoryQueryOptions,
10
16
  } from "@rxdrag/rxcms-models";
11
17
  import { ListConditions } from "./queryPosts";
12
18
  import { queryEntityList } from "./queryEntityList";
@@ -15,6 +21,8 @@ import { ListResult } from "@rxdrag/entify-lib";
15
21
  import { EnvVariables } from "../types";
16
22
  import { TProduct } from "../view-model";
17
23
  import { ImageSize } from "@rxdrag/rxcms-models";
24
+ import { queryOneEntity } from "./queryOneEntity";
25
+ import { CategoryNode, collectCategoryIds } from "./collectCategoryIds";
18
26
 
19
27
  export async function queryProducts(
20
28
  conditions: ListConditions,
@@ -31,13 +39,54 @@ export async function queryProducts(
31
39
 
32
40
  let where = {};
33
41
  if (categorySlug) {
34
- where = {
35
- [ProductAssciations.category]: {
36
- slug: {
37
- _eq: categorySlug,
42
+ //先查询分类以及其子分类
43
+ const category = await queryOneEntity<
44
+ ProductCategory,
45
+ ProductCategoryBoolExp,
46
+ ProductCategoryOrderBy,
47
+ ProductCategoryDistinctExp
48
+ >(
49
+ new ProductCategoryQueryOptions(
50
+ [
51
+ ProductCategoryFields.id,
52
+ ProductCategoryFields.slug,
53
+ ],
54
+ {
55
+ where: {
56
+ slug: {
57
+ _eq: categorySlug,
58
+ },
59
+ },
60
+ }
61
+ //支持三级结构
62
+ ).children(new ProductCategoryQueryOptions(
63
+ [
64
+ ProductCategoryFields.id,
65
+ ProductCategoryFields.slug,
66
+ ],
67
+ {
68
+ where: {
69
+ slug: {
70
+ _eq: categorySlug,
71
+ },
72
+ },
73
+ }
74
+ ).children([])),
75
+ envVariables
76
+ );
77
+ if (category) {
78
+ const categoryIds = collectCategoryIds(
79
+ category as unknown as CategoryNode
80
+ );
81
+ where = {
82
+ [ProductAssciations.category]: {
83
+ id: {
84
+ _in: categoryIds,
85
+ },
38
86
  },
39
- },
40
- };
87
+ };
88
+ }
89
+
41
90
  }
42
91
 
43
92
  // 默认查询字段