@k34a/blog 0.0.13 → 0.0.15
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/README.md +12 -0
- package/dist/services/articles.js +49 -54
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,18 @@ npm install @mantine/core @tabler/icons-react react @supabase/supabase-js zod
|
|
|
38
38
|
|
|
39
39
|
---
|
|
40
40
|
|
|
41
|
+
# ⚙️ Setup (DB)
|
|
42
|
+
|
|
43
|
+
Run the following command in your supabase SQL editor to setup a view.
|
|
44
|
+
|
|
45
|
+
```sql
|
|
46
|
+
create view article_with_tags as
|
|
47
|
+
select a.*, t.name as tag
|
|
48
|
+
from articles a
|
|
49
|
+
left join tag_articles ta on ta.article_id = a.id
|
|
50
|
+
left join tags t on t.id = ta.tag_id;
|
|
51
|
+
```
|
|
52
|
+
|
|
41
53
|
# ⚙️ Setup (Backend)
|
|
42
54
|
|
|
43
55
|
Create an instance of `ArticleService` anywhere in your backend/server code.
|
|
@@ -1,95 +1,90 @@
|
|
|
1
|
-
import { articleSortByVsQuery as
|
|
1
|
+
import { articleSortByVsQuery as u } from "./search-params.js";
|
|
2
2
|
import "zod";
|
|
3
|
-
import { PAGE_SIZE as
|
|
4
|
-
class
|
|
3
|
+
import { PAGE_SIZE as f } from "../cfg.js";
|
|
4
|
+
class y {
|
|
5
5
|
db;
|
|
6
|
-
constructor(
|
|
7
|
-
this.db =
|
|
6
|
+
constructor(e) {
|
|
7
|
+
this.db = e;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Fetch a single article by slug.
|
|
11
11
|
* Includes its tags.
|
|
12
12
|
*/
|
|
13
|
-
async getBySlug(
|
|
14
|
-
const { data: t, error: a } = await this.db.from("articles").select("*").eq("slug",
|
|
13
|
+
async getBySlug(e) {
|
|
14
|
+
const { data: t, error: a } = await this.db.from("articles").select("*").eq("slug", e).eq("status", "Published").single();
|
|
15
15
|
if (a)
|
|
16
16
|
return console.error("Error fetching article details:", a.message), null;
|
|
17
|
-
const { data:
|
|
18
|
-
if (
|
|
17
|
+
const { data: d, error: i } = await this.db.from("tag_articles").select("tag_id").eq("article_id", t.id);
|
|
18
|
+
if (i)
|
|
19
19
|
return console.error(
|
|
20
20
|
"Error fetching article tag links:",
|
|
21
|
-
|
|
21
|
+
i.message
|
|
22
22
|
), { ...t, tags: [] };
|
|
23
|
-
const
|
|
24
|
-
let
|
|
25
|
-
if (
|
|
26
|
-
const { data: o, error:
|
|
27
|
-
|
|
23
|
+
const n = d?.map((o) => o.tag_id) || [];
|
|
24
|
+
let r = [];
|
|
25
|
+
if (n.length > 0) {
|
|
26
|
+
const { data: o, error: c } = await this.db.from("tags").select("name").in("id", n);
|
|
27
|
+
c ? console.error(
|
|
28
28
|
"Error fetching article tag names:",
|
|
29
|
-
|
|
30
|
-
) :
|
|
29
|
+
c.message
|
|
30
|
+
) : r = o.map((l) => l.name);
|
|
31
31
|
}
|
|
32
|
-
return { ...t, tags:
|
|
32
|
+
return { ...t, tags: r };
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Get total number of articles.
|
|
36
36
|
*/
|
|
37
37
|
async getCount() {
|
|
38
|
-
const { data:
|
|
38
|
+
const { data: e, error: t } = await this.db.rpc("get_table_row_count", {
|
|
39
39
|
arg_schema_name: "public",
|
|
40
40
|
arg_table_name: "articles"
|
|
41
41
|
}).single();
|
|
42
|
-
return t ? (console.error("Error fetching articles count:", t), 0) :
|
|
42
|
+
return t ? (console.error("Error fetching articles count:", t), 0) : e || 0;
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Get all available tag names.
|
|
46
46
|
*/
|
|
47
47
|
async getTagNames() {
|
|
48
|
-
const { data:
|
|
49
|
-
return t ? (console.error("Error fetching tags:", t.message), []) :
|
|
48
|
+
const { data: e, error: t } = await this.db.from("tags").select("name");
|
|
49
|
+
return t ? (console.error("Error fetching tags:", t.message), []) : e?.map((a) => a.name) ?? [];
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Paginated list of articles with filters and sorting.
|
|
53
53
|
*/
|
|
54
|
-
async list(
|
|
55
|
-
const { page: t, search: a, sortBy:
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
e = e.range(o, n);
|
|
77
|
-
const { data: c, error: g, count: d } = await e;
|
|
78
|
-
return console.log({ data: c, error: g, count: d }), g ? (console.error("Error fetching articles:", g), { items: [], total: 0 }) : {
|
|
79
|
-
items: c || [],
|
|
80
|
-
total: d || 0
|
|
54
|
+
async list(e) {
|
|
55
|
+
const { page: t, search: a, sortBy: d, tags: i } = e, n = u[d] ?? u.latest;
|
|
56
|
+
let r = this.db.from("article_with_tags").select("*", { count: "exact" }).eq("status", "Published");
|
|
57
|
+
a && (r = r.ilike("title", `%${a}%`)), i && i.length > 0 && (r = r.in("tag", i)), r = r.order(n.column, { ascending: n.ascending });
|
|
58
|
+
const o = t * f, c = o + f - 1;
|
|
59
|
+
console.log({ from: o, to: c }), r = r.range(o, c);
|
|
60
|
+
const { data: l, error: m, count: h } = await r;
|
|
61
|
+
if (console.log({ data: l, error: m, count: h }), m)
|
|
62
|
+
return console.error("Error fetching articles:", m), { items: [], total: 0 };
|
|
63
|
+
const g = /* @__PURE__ */ new Map();
|
|
64
|
+
for (const s of l ?? [])
|
|
65
|
+
g.has(s.id) || g.set(s.id, {
|
|
66
|
+
id: s.id,
|
|
67
|
+
title: s.title,
|
|
68
|
+
description: s.description,
|
|
69
|
+
slug: s.slug,
|
|
70
|
+
created_at: s.created_at,
|
|
71
|
+
banner_image: s.banner_image
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
items: [...g.values()],
|
|
75
|
+
total: Object.keys(g).length
|
|
81
76
|
};
|
|
82
77
|
}
|
|
83
|
-
async getDescription(
|
|
84
|
-
const { data: t, error: a } = await this.db.storage.from("content").download(`articles/${
|
|
78
|
+
async getDescription(e) {
|
|
79
|
+
const { data: t, error: a } = await this.db.storage.from("content").download(`articles/${e}/description.html`);
|
|
85
80
|
return a ? (console.error(
|
|
86
|
-
`Error fetching article description for article ID "${
|
|
81
|
+
`Error fetching article description for article ID "${e}":`,
|
|
87
82
|
a.message
|
|
88
83
|
), null) : t ? await t.text() : (console.warn(
|
|
89
|
-
`No article description file found for article ID: ${
|
|
84
|
+
`No article description file found for article ID: ${e}`
|
|
90
85
|
), null);
|
|
91
86
|
}
|
|
92
87
|
}
|
|
93
88
|
export {
|
|
94
|
-
|
|
89
|
+
y as ArticleService
|
|
95
90
|
};
|