@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 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 _ } from "./search-params.js";
1
+ import { articleSortByVsQuery as u } from "./search-params.js";
2
2
  import "zod";
3
- import { PAGE_SIZE as E } from "../cfg.js";
4
- class D {
3
+ import { PAGE_SIZE as f } from "../cfg.js";
4
+ class y {
5
5
  db;
6
- constructor(r) {
7
- this.db = r;
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(r) {
14
- const { data: t, error: a } = await this.db.from("articles").select("*").eq("slug", r).eq("status", "Published").single();
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: l, error: s } = await this.db.from("tag_articles").select("tag_id").eq("article_id", t.id);
18
- if (s)
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
- s.message
21
+ i.message
22
22
  ), { ...t, tags: [] };
23
- const i = l?.map((o) => o.tag_id) || [];
24
- let e = [];
25
- if (i.length > 0) {
26
- const { data: o, error: n } = await this.db.from("tags").select("name").in("id", i);
27
- n ? console.error(
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
- n.message
30
- ) : e = o.map((c) => c.name);
29
+ c.message
30
+ ) : r = o.map((l) => l.name);
31
31
  }
32
- return { ...t, tags: e };
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: r, error: t } = await this.db.rpc("get_table_row_count", {
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) : r || 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: r, error: t } = await this.db.from("tags").select("name");
49
- return t ? (console.error("Error fetching tags:", t.message), []) : r?.map((a) => a.name) ?? [];
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(r) {
55
- const { page: t, search: a, sortBy: l, tags: s } = r, i = _[l] ?? _.latest;
56
- let e = this.db.from("articles").select("*", { count: "exact" }).eq("status", "Published");
57
- if (a && (e = e.ilike("title", `%${a}%`)), s && s.length > 0) {
58
- const { data: f, error: u } = await this.db.from("tags").select("id").in("name", s);
59
- if (u || !f?.length)
60
- return console.error("Error fetching tag IDs:", u?.message), { items: [], total: 0 };
61
- const w = f.map((m) => m.id), { data: h, error: b } = await this.db.from("tag_articles").select("article_id").in("tag_id", w);
62
- if (b || !h?.length)
63
- return console.error(
64
- "Error fetching articles by tags:",
65
- b?.message
66
- ), { items: [], total: 0 };
67
- const p = Array.from(
68
- new Set(h.map((m) => m.article_id))
69
- );
70
- if (p.length === 0)
71
- return { items: [], total: 0 };
72
- e = e.in("id", p);
73
- }
74
- e = e.order(i.column, { ascending: i.ascending });
75
- const o = t * E, n = o + E - 1;
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(r) {
84
- const { data: t, error: a } = await this.db.storage.from("content").download(`articles/${r}/description.html`);
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 "${r}":`,
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: ${r}`
84
+ `No article description file found for article ID: ${e}`
90
85
  ), null);
91
86
  }
92
87
  }
93
88
  export {
94
- D as ArticleService
89
+ y as ArticleService
95
90
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@k34a/blog",
3
3
  "description": "Create and share articles with your audience.",
4
4
  "private": false,
5
- "version": "0.0.13",
5
+ "version": "0.0.15",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {