@stacksjs/cms 0.70.87 → 0.70.90
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/dist/index.js +1 -1
- package/dist/posts/fetch.d.ts +1 -1
- package/dist/posts/store.d.ts +4 -3
- package/dist/results.d.ts +22 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var Df=Object.defineProperty;var uf=(f)=>f;function lf(f,M){this[f]=uf.bind(null,M)}var Q=(f,M)=>{for(var B in M)Df(f,B,{get:M[B],enumerable:!0,configurable:!0,set:lf.bind(M,B)})};var nf=import.meta.require;var V={};Q(V,{update:()=>u,store:()=>p,findOrCreate:()=>m,findByUuid:()=>q,findByUserId:()=>c,findByName:()=>I,findByEmail:()=>T,fetchById:()=>R,fetchAll:()=>v,destroy:()=>N});var h;async function x(){return h??=import("@stacksjs/database").then((f)=>f.db),h}async function N(f){let M=await x();try{let B=await M.deleteFrom("authors").where("id","=",f).returningAll().executeTakeFirst();if(!B)throw Error(`Author with ID ${f} not found`);return B}catch(B){if(B instanceof Error)throw TypeError(`Author with ID ${f} not found`);throw B}}async function R(f){return await(await x()).selectFrom("authors").where("id","=",f).selectAll().executeTakeFirst()}async function v(){return await(await x()).selectFrom("authors").selectAll().execute()}async function I(f){return await(await x()).selectFrom("authors").where("name","=",f).selectAll().executeTakeFirst()}async function T(f){return await(await x()).selectFrom("authors").where("email","=",f).selectAll().executeTakeFirst()}async function q(f){return await(await x()).selectFrom("authors").where("uuid","=",f).selectAll().executeTakeFirst()}async function c(f){return await(await x()).selectFrom("authors").where("user_id","=",f).selectAll().executeTakeFirst()}var{randomUUIDv7:w}=globalThis.Bun;import{HttpError as Z}from"@stacksjs/error-handling";import{formatDate as X,isUniqueViolation as y}from"@stacksjs/orm";async function m(f){let M=await x();try{let B=await M.selectFrom("authors").where((S)=>S.or([S("email","=",f.email),S("name","=",f.name)])).selectAll().executeTakeFirst();if(B)return B;let P=await M.selectFrom("users").where("email","=",f.email).selectAll().executeTakeFirst();if(!P){let S=await M.insertInto("users").values({email:f.email,name:f.name,password:w(),uuid:w(),created_at:X(new Date),updated_at:X(new Date)}).returningAll().executeTakeFirst();if(!S)throw Error("Failed to create user");P=S}let C={user_id:P.id,name:f.name,email:f.email,created_at:X(new Date),updated_at:X(new Date)},G=await M.insertInto("authors").values(C).returningAll().executeTakeFirst();if(!G)throw Error("Failed to create author");return G}catch(B){if(B instanceof Z)throw B;if(y(B))throw new Z(409,"An author with this email already exists");if(B instanceof Error)throw TypeError(`Failed to find or create author: ${B.message}`);throw B}}async function p(f){let M=await x();try{let B=await M.selectFrom("authors").where((G)=>G.or([G("email","=",f.email),G("name","=",f.name)])).selectAll().executeTakeFirst();if(B)return B;let P={user_id:f.user_id,name:f.name,email:f.email,created_at:X(new Date),updated_at:X(new Date)},C=await M.insertInto("authors").values(P).returningAll().executeTakeFirst();if(!C)throw Error("Failed to create author");return C}catch(B){if(B instanceof Z)throw B;if(y(B))throw new Z(409,"An author with this email already exists");if(B instanceof Error)throw TypeError(`Failed to find or create author: ${B.message}`);throw B}}import{HttpError as D}from"@stacksjs/error-handling";import{formatDate as gf,isUniqueViolation as bf}from"@stacksjs/orm";async function u(f,M){let B=await x();try{let P={...M,updated_at:gf(new Date)};return await B.updateTable("authors").set(P).where("id","=",f).returningAll().executeTakeFirst()}catch(P){if(P instanceof D)throw P;if(bf(P))throw new D(409,"An author with this email already exists");if(P instanceof Error)throw TypeError(`Failed to update author: ${P.message}`);throw P}}var E={};Q(E,{update:()=>t,storeCategorizableModel:()=>r,store:()=>d,fetchWithPosts:()=>b,fetchBySlug:()=>g,fetchByName:()=>n,fetchById:()=>$,fetchAll:()=>l,destroy:()=>s,bulkStore:()=>e,bulkDestroy:()=>i});import{slugify as EB}from"ts-slug";async function $(f){return await(await x()).selectFrom("categorizables").where("id","=",f).where("is_active","=",!0).selectAll().executeTakeFirst()}async function l(){return await(await x()).selectFrom("categorizables").selectAll().execute()}async function n(f){return await(await x()).selectFrom("categorizables").where("name","=",f).where("is_active","=",!0).selectAll().execute()}async function g(f){return await(await x()).selectFrom("categorizables").where("slug","=",f).where("is_active","=",!0).selectAll().executeTakeFirst()}async function b(f){return await(await x()).selectFrom("categorizables").where("id","=",f).where("is_active","=",!0).selectAll().executeTakeFirst()}async function s(f){let M=await x();try{if(!await $(f))throw Error(`Category with ID ${f} not found`);return(await M.deleteFrom("categorizables").where("id","=",f).executeTakeFirst()).numDeletedRows>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete category: ${B.message}`);throw B}}async function i(f){let M=await x();if(!f.length)return 0;try{let B=await M.deleteFrom("categorizables").where("id","in",f).executeTakeFirst();return Number(B.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete categories in bulk: ${B.message}`);throw B}}import{slugify as o}from"ts-slug";async function d(f){let M=await x();try{if(!f.name||f.name.trim()==="")throw Error("Category name is required");if(!f.categorizable_type||f.categorizable_type.trim()==="")throw Error("Category categorizable_type is required");let B={name:f.name,slug:o(f.name),description:f.description,categorizable_type:f.categorizable_type,is_active:f.is_active??!0},P=await M.insertInto("categorizables").values(B).returningAll().executeTakeFirst();if(!P)throw Error("Failed to create category");return P}catch(B){if(B instanceof Error)throw TypeError(`Failed to create category: ${B.message}`);throw B}}async function r(f){let M=await x();try{let B={category_id:f.category_id,categorizable_type:f.categorizable_type},P=await M.insertInto("categorizable_models").values(B).returningAll().executeTakeFirst();if(!P)throw Error("Failed to create categorizable model relationship");return P}catch(B){if(B instanceof Error)throw TypeError(`Failed to create categorizable model relationship: ${B.message}`);throw B}}async function e(f){let M=await x();try{return await M.transaction(async(P)=>{let C=[];for(let G of f){let S={name:G.name,slug:o(G.name),description:G.description,categorizable_type:G.categorizable_type,is_active:G.is_active??!0},A=await P.insertInto("categorizables").values(S).returningAll().executeTakeFirst();if(!A)throw Error(`Failed to create category: ${G.name}`);C.push(A)}return C})}catch(B){if(B instanceof Error)throw TypeError(`Failed to create categories: ${B.message}`);throw B}}import{slugify as sf}from"ts-slug";async function t(f){let M=await x();try{let B=f.id;if(!B)throw Error("Category ID is required for update");if(f.name!==void 0){if(f.name.trim()==="")throw Error("Category name cannot be empty");f.slug=sf(f.name)}let{id:P,...C}=f,G=await M.updateTable("categorizables").set(C).where("id","=",B).returningAll().executeTakeFirst();if(!G)throw Error("Failed to update category");return G}catch(B){if(B instanceof Error)throw TypeError(`Failed to update category: ${B.message}`);throw B}}var _={};Q(_,{update:()=>Jf,store:()=>Af,fetchStatusDistributionDonut:()=>Cf,fetchPostsWithMostComments:()=>H,fetchMonthlyCommentCounts:()=>Gf,fetchCommentsByStatus:()=>Mf,fetchCommentsByCommentables:()=>ff,fetchComments:()=>a,fetchCommentCountByPeriod:()=>Bf,fetchCommentCountBarGraph:()=>xf,fetchCommentById:()=>z,destroy:()=>Sf,calculateApprovalRate:()=>Pf,bulkDestroy:()=>Wf});import{formatDate as Y}from"@stacksjs/orm";async function a(f={}){let B=(await x()).selectFrom("commentables");if(f.status)B=B.where("status","=",f.status);if(f.commentables_id)B=B.where("commentables_id","=",f.commentables_id);if(f.commentables_type)B=B.where("commentables_type","=",f.commentables_type);if(f.limit)B=B.limit(f.limit);if(f.offset)B=B.offset(f.offset);return B.selectAll().execute()}async function z(f){return(await x()).selectFrom("commentables").where("id","=",f).selectAll().executeTakeFirst()}async function ff(f,M,B={}){let C=(await x()).selectFrom("commentables").where("commentables_id","=",f).where("commentables_type","=",M);if(B.status)C=C.where("status","=",B.status);if(B.limit)C=C.limit(B.limit);if(B.offset)C=C.offset(B.offset);return C.selectAll().execute()}async function Bf(f){let M=await x();try{let B=await M.selectFrom("commentables").where("created_at",">=",new Date(Date.now()-f*24*60*60*1000).toISOString()).count();return Number(B)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch comment count: ${B.message}`);throw B}}async function Mf(f,M={}){let B=await x();try{let P=B.selectFrom("commentables").where("status","=",f);if(M.limit)P=P.limit(M.limit);if(M.offset)P=P.offset(M.offset);return P.selectAll().execute()}catch(P){if(P instanceof Error)throw TypeError(`Failed to fetch comments by status: ${P.message}`);throw P}}async function Pf(){let f=await x();try{let[M,B]=await Promise.all([f.selectFrom("commentables").where("status","=","approved").count(),f.selectFrom("commentables").count()]),P=Number(M||0),C=Number(B||0),G=C>0?P/C*100:0;return{approved:P,total:C,rate:G}}catch(M){if(M instanceof Error)throw TypeError(`Failed to calculate approval rate: ${M.message}`);throw M}}async function H(f,M={}){let B=await x();try{let P=B.selectFrom("posts").leftJoin("commentables","posts.id","=","commentables.commentables_id").where("commentables.created_at",">=",Y(f.startDate)).where("commentables.created_at","<=",Y(f.endDate)).select(["posts.id","posts.title"]).groupBy("posts.id","posts.title").orderBy("posts.id","desc");if(M.limit)P=P.limit(M.limit);return(await P.execute()).map((G)=>({id:Number(G.id),title:String(G.title),comment_count:Number(G.comment_count||0)}))}catch(P){if(P instanceof Error)throw TypeError(`Failed to fetch posts with most comments: ${P.message}`);throw P}}async function xf(f,M={}){try{let B=await H(f,M);return{labels:B.map((P)=>P.title),values:B.map((P)=>P.comment_count)}}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch bar graph data: ${B.message}`);throw B}}async function Cf(f){let M=await x();try{let P=await M.selectFrom("commentables").where("created_at",">=",Y(f.startDate)).where("created_at","<=",Y(f.endDate)).select(["status"]).groupBy("status").execute(),C=P.reduce((W,L)=>W+Number(L.count||0),0),G=P.map((W)=>String(W.status)),S=P.map((W)=>Number(W.count||0)),A=S.map((W)=>C>0?W/C*100:0);return{labels:G,values:S,percentages:A}}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch status distribution: ${B.message}`);throw B}}async function Gf(f){let M=await x();try{let B=await M.selectFrom("commentables").where("created_at",">=",Y(f.startDate)).where("created_at","<=",Y(f.endDate)).select(["created_at"]).groupBy("created_at").orderBy("created_at","asc").execute(),P=new Map;B.forEach((S)=>{if(!S.created_at)return;let A=new Date(S.created_at),W=`${A.getFullYear()}-${String(A.getMonth()+1).padStart(2,"0")}`;P.set(W,(P.get(W)||0)+Number(S.count||0))});let C=[],G=[];return P.forEach((S,A)=>{let[W,L]=A.split("-");C.push(new Date(Number(W),Number(L)-1).toLocaleString("default",{month:"short",year:"numeric"})),G.push(S)}),{labels:C,values:G}}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch monthly comment counts: ${B.message}`);throw B}}async function Sf(f){let M=await x();try{if(!await z(f))throw Error(`Comment with ID ${f} not found`);return(await M.deleteFrom("commentables").where("id","=",f).executeTakeFirst()).numDeletedRows>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete comment: ${B.message}`);throw B}}async function Wf(f){let M=await x();if(!f.length)return 0;try{let B=await M.deleteFrom("commentables").where("id","in",f).executeTakeFirst();return Number(B.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete comments in bulk: ${B.message}`);throw B}}async function Af(f){let M=await x();try{if(!f.title||f.title.trim()==="")throw Error("Comment title is required");if(!f.body||f.body.trim()==="")throw Error("Comment body is required");if(!f.commentables_type||f.commentables_type.trim()==="")throw Error("Comment commentables_type is required");let B=["pending","approved","rejected"];if(f.status&&!B.includes(f.status))throw Error(`Invalid comment status: ${f.status}`);let P={title:f.title,body:f.body,status:f.status,commentables_id:f.commentables_id,commentables_type:f.commentables_type,user_id:f.user_id,is_active:f.is_active},C=await M.insertInto("commentables").values(P).returningAll().executeTakeFirst();if(!C)throw Error("Failed to create comment");return C}catch(B){if(B instanceof Error)throw TypeError(`Failed to store comment: ${B.message}`);throw B}}import{formatDate as of}from"@stacksjs/orm";async function Jf(f,M){let B=await x();try{if(M.title!==void 0&&M.title.trim()==="")throw Error("Comment title cannot be empty");if(M.body!==void 0&&M.body.trim()==="")throw Error("Comment body cannot be empty");let P=["pending","approved","rejected"];if(M.status!==void 0&&!P.includes(M.status))throw Error(`Invalid comment status: ${M.status}`);let C={updated_at:of(new Date)};if(M.title!==void 0)C.title=M.title;if(M.body!==void 0)C.body=M.body;if(M.status!==void 0)C.status=M.status;if(M.commentables_id!==void 0)C.commentables_id=M.commentables_id;if(M.commentables_type!==void 0)C.commentables_type=M.commentables_type;if(M.approved_at!==void 0)C.approved_at=M.approved_at;if(M.rejected_at!==void 0)C.rejected_at=M.rejected_at;return await B.updateTable("commentables").set(C).where("id","=",f).execute(),await B.selectFrom("commentables").where("id","=",f).selectAll().executeTakeFirst()}catch(P){if(P instanceof Error)throw TypeError(`Failed to update comment: ${P.message}`);throw P}}var j={};Q(j,{update:()=>Kf,store:()=>kf,fetchPublishedAfter:()=>Zf,fetchByMinViews:()=>Xf,fetchByMinConversions:()=>Yf,fetchById:()=>O,fetchByAuthor:()=>Qf,fetchAll:()=>Lf,destroy:()=>$f,bulkDestroy:()=>zf});async function O(f){return await(await x()).selectFrom("pages").where("id","=",f).selectAll().executeTakeFirst()}async function Lf(){return await(await x()).selectFrom("pages").selectAll().execute()}async function Qf(f){return await(await x()).selectFrom("pages").where("author_id","=",f).selectAll().execute()}async function Xf(f){return await(await x()).selectFrom("pages").where("views",">=",f).selectAll().execute()}async function Yf(f){return await(await x()).selectFrom("pages").where("conversions",">=",f).selectAll().execute()}async function Zf(f){return await(await x()).selectFrom("pages").where("published_at",">",f).selectAll().execute()}async function $f(f){let M=await x();try{if(!await O(f))throw Error(`Page with ID ${f} not found`);return(await M.deleteFrom("pages").where("id","=",f).executeTakeFirst()).numDeletedRows>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete page: ${B.message}`);throw B}}async function zf(f){let M=await x();if(!f.length)return 0;try{let B=await M.deleteFrom("pages").where("id","in",f).executeTakeFirst();return Number(B.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete pages in bulk: ${B.message}`);throw B}}import{formatDate as Of}from"@stacksjs/orm";async function kf(f){let M=await x();try{let B={author_id:f.author_id,title:f.title,template:f.template,views:f.views||0,conversions:f.conversions||0,created_at:Of(new Date),updated_at:Of(new Date)},P=await M.insertInto("pages").values(B).returningAll().executeTakeFirst();if(!P)throw Error("Failed to create page");return P}catch(B){if(B instanceof Error)throw TypeError(`Failed to create page: ${B.message}`);throw B}}import{formatDate as df}from"@stacksjs/orm";async function Kf(f,M){let B=await x();try{let P={...M,updated_at:df(new Date)},C=await B.updateTable("pages").set(P).where("id","=",f).returningAll().executeTakeFirst();if(!C)throw Error("Failed to update page");return C}catch(P){if(P instanceof Error)throw TypeError(`Failed to update page: ${P.message}`);throw P}}var U={};Q(U,{update:()=>Tf,sync:()=>If,store:()=>Nf,fetchPublishedAfter:()=>Uf,fetchByStatus:()=>Ef,fetchByMinViews:()=>jf,fetchById:()=>k,fetchByCategory:()=>Hf,fetchByAuthor:()=>_f,fetchAll:()=>Vf,detach:()=>vf,destroy:()=>Ff,bulkDestroy:()=>hf,attach:()=>Rf});async function k(f){return await(await x()).selectFrom("posts").where("id","=",f).selectAll().executeTakeFirst()}async function Vf(){return await(await x()).selectFrom("posts").selectAll().execute()}async function Ef(f){return await(await x()).selectFrom("posts").where("status","=",f).selectAll().execute()}async function Hf(f){return await(await x()).selectFrom("posts").where("category","=",f).selectAll().execute()}async function _f(f){return await(await x()).selectFrom("posts").where("author","=",f).selectAll().execute()}async function jf(f){return await(await x()).selectFrom("posts").where("views",">=",f).selectAll().execute()}async function Uf(f){return await(await x()).selectFrom("posts").where("published_at",">",f).selectAll().execute()}async function Ff(f){let M=await x();try{if(!await k(f))throw Error(`Post with ID ${f} not found`);await M.deleteFrom("categorizable_models").where("categorizable_id","=",f).where("categorizable_type","=","posts").execute();let P=await M.deleteFrom("posts").where("id","=",f).executeTakeFirst();return P?.numDeletedRows?P.numDeletedRows>0:!1}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete post: ${B.message}`);throw B}}async function hf(f){let M=await x();if(!f.length)return 0;try{let P=(await M.selectFrom("posts").select(["id"]).where("id","in",f).execute()).map((G)=>G.id);if(!P.length)return 0;await M.deleteFrom("categorizable_models").where("categorizable_id","in",P).where("categorizable_type","=","posts").execute();let C=await M.deleteFrom("posts").where("id","in",P).executeTakeFirst();return Number(C?.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete posts in bulk: ${B.message}`);throw B}}var{randomUUIDv7:rf}=globalThis.Bun;import{formatDate as K}from"@stacksjs/orm";var ef="Draft";async function Nf(f){let M=await x();try{if(!f.title||typeof f.title==="string"&&f.title.trim()==="")throw Error("Post title is required");let B=f.content||f.body,P=f,C={author_id:P.author_id,uuid:rf(),title:f.title,poster:f.poster,content:B,body:f.body,category:f.category,excerpt:f.excerpt,is_featured:P.is_featured?Date.now():void 0,views:f.views||0,published_at:P.published_at||Date.now(),status:f.status||ef},G=await M.insertInto("posts").values(C).returningAll().executeTakeFirst();if(!G)throw Error("Failed to create post");return G}catch(B){if(B instanceof Error)throw TypeError(`Failed to create post: ${B.message}`);throw B}}async function Rf(f,M,B){let P=await x();try{let C=M==="categorizable_models"?"categorizable_id":"taggable_id",G=M==="categorizable_models"?"categorizable_type":"taggable_type",S=M==="categorizable_models"?"category_id":"tag_id",A=B.map((W)=>({[S]:W,[C]:f,[G]:"posts",created_at:K(new Date),updated_at:K(new Date)}));for(let W of A)await P.insertInto(M).values(W).execute()}catch(C){if(C instanceof Error)throw TypeError(`Failed to attach records: ${C.message}`);throw C}}async function vf(f,M,B){let P=await x();try{let C=M==="categorizable_models"?"categorizable_id":"taggable_id",G=M==="categorizable_models"?"categorizable_type":"taggable_type",S=P.deleteFrom(M).where(C,"=",f).where(G,"=","posts");if(B)S=S.where("id","in",B);await S.execute()}catch(C){if(C instanceof Error)throw TypeError(`Failed to detach records: ${C.message}`);throw C}}async function If(f,M,B){let P=await x();try{let C=M==="categorizable_models"?"categorizable_id":"taggable_id",G=M==="categorizable_models"?"categorizable_type":"taggable_type",A=(await P.selectFrom(M).select(["id"]).where(C,"=",f).where(G,"=","posts").execute()).map((J)=>J.id),W=A.filter((J)=>!B.includes(J)),L=B.filter((J)=>!A.includes(J));if(W.length>0)await P.deleteFrom(M).where(C,"=",f).where(G,"=","posts").where("id","in",W).execute();if(L.length>0){let J=L.map(()=>({[C]:f,[G]:"posts",created_at:K(new Date),updated_at:K(new Date)}));await P.insertInto(M).values(J).execute()}}catch(C){if(C instanceof Error)throw TypeError(`Failed to sync records: ${C.message}`);throw C}}async function Tf(f,M){let B=await x();try{if(M.title!==void 0&&typeof M.title==="string"&&M.title.trim()==="")throw Error("Post title cannot be empty");let P=["published","draft","archived"];if(M.status!==void 0&&typeof M.status==="string"&&!P.includes(M.status))throw Error(`Invalid post status: ${M.status}`);if(M.views!==void 0&&typeof M.views==="number"&&M.views<0)throw Error("Views count cannot be negative");let C=await B.updateTable("posts").set(M).where("id","=",f).returningAll().executeTakeFirst();if(!C)throw Error("Failed to update post");return C}catch(P){if(P instanceof Error)throw TypeError(`Failed to update post: ${P.message}`);throw P}}var F={};Q(F,{update:()=>pf,store:()=>wf,fetchTags:()=>mf,fetchTagById:()=>yf,destroy:()=>qf,bulkDestroy:()=>cf});async function qf(f){let M=await x();if(!await M.selectFrom("taggables").where("id","=",f).selectAll().executeTakeFirst())throw Error(`Tag with ID ${f} not found`);await M.deleteFrom("taggables").where("id","=",f).executeTakeFirst()}async function cf(f){let M=await x();try{await M.deleteFrom("taggables").where("id","in",f).execute()}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete tags: ${B.message}`);throw B}}import{slugify as tf}from"ts-slug";async function wf(f){let M=await x();try{if(!f.name||f.name.trim()==="")throw Error("Tag name is required");if(!f.taggable_type||f.taggable_type.trim()==="")throw Error("Tag taggable_type is required");let B=tf(f.name);if(await M.selectFrom("taggables").selectAll().where("slug","=",B).executeTakeFirst())throw Error(`Tag with unique slug "${B}" already exists`);let C={name:f.name,slug:B,description:f.description,is_active:f.is_active??!0,taggable_type:f.taggable_type};if(f.taggable_id!==void 0)C.taggable_id=f.taggable_id;let G=await M.insertInto("taggables").values(C).returningAll().executeTakeFirst();if(!G)throw Error("Failed to create tag");return G}catch(B){if(B instanceof Error)throw TypeError(`Failed to store tag: ${B.message}`);throw B}}async function yf(f){let M=await x();try{let B=await M.selectFrom("taggables").where("id","=",f).selectAll().executeTakeFirst();if(!B)return;return B}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch tag: ${B.message}`);throw B}}async function mf(){let f=await x();try{return await f.selectFrom("taggables").where("is_active","=",!0).selectAll().execute()}catch(M){if(M instanceof Error)throw TypeError(`Failed to fetch tags: ${M.message}`);throw M}}import{uniqueSlug as af}from"@stacksjs/slug";async function pf(f){let M=await x();try{let B=f.id;if(!B)throw Error("Tag ID is required for update");if(f.name!==void 0){if(f.name.trim()==="")throw Error("Tag name cannot be empty");let S=await af(f.name,{table:"taggables",column:"slug"});if(await M.selectFrom("taggables").selectAll().where("slug","=",S).where("id","!=",B).executeTakeFirst())throw Error(`Tag with unique slug "${S}" already exists`);f.slug=S}let{id:P,...C}=f,G=await M.updateTable("taggables").set(C).where("id","=",B).returningAll().executeTakeFirst();if(!G)throw Error("Failed to update tag");return G}catch(B){if(B instanceof Error)throw TypeError(`Failed to update tag: ${B.message}`);throw B}}var fB={posts:U,postCategories:E,tags:F,comments:_,authors:V,pages:j},nM=fB;export{F as tags,U as posts,j as pages,nM as default,_ as comments,fB as cms,E as categorizable,V as authors};
|
|
2
|
+
var nM=Object.defineProperty;var bM=(M)=>M;function iM(M,f){this[M]=bM.bind(null,f)}var K=(M,f)=>{for(var B in f)nM(M,B,{get:f[B],enumerable:!0,configurable:!0,set:iM.bind(f,B)})};var sM=import.meta.require;var U={};K(U,{update:()=>i,store:()=>n,findOrCreate:()=>l,findByUuid:()=>p,findByUserId:()=>D,findByName:()=>w,findByEmail:()=>m,fetchById:()=>k,fetchAll:()=>y,destroy:()=>c});var R;async function P(){return R??=import("@stacksjs/database").then((M)=>M.db),R}async function c(M){let f=await P();try{let B=await f.deleteFrom("authors").where("id","=",M).returningAll().executeTakeFirst();if(!B)throw Error(`Author with ID ${M} not found`);return B}catch(B){if(B instanceof Error)throw TypeError(`Author with ID ${M} not found`);throw B}}async function k(M){return await(await P()).selectFrom("authors").where("id","=",M).selectAll().executeTakeFirst()}async function y(){return await(await P()).selectFrom("authors").selectAll().execute()}async function w(M){return await(await P()).selectFrom("authors").where("name","=",M).selectAll().executeTakeFirst()}async function m(M){return await(await P()).selectFrom("authors").where("email","=",M).selectAll().executeTakeFirst()}async function p(M){return await(await P()).selectFrom("authors").where("uuid","=",M).selectAll().executeTakeFirst()}async function D(M){return await(await P()).selectFrom("authors").where("user_id","=",M).selectAll().executeTakeFirst()}var{randomUUIDv7:g}=globalThis.Bun;function $(M){return!!M&&typeof M==="object"&&"id"in M}function E(M){if($(M)&&typeof M.id==="number")return M.id;let f=M?.lastInsertRowid;if(f===void 0||f===null)return;let B=Number(f);return Number.isSafeInteger(B)&&B>0?B:void 0}async function z(M,f,B){if($(B))return B;let x=E(B);if(x===void 0)return;return await M.selectFrom(f).where("id","=",x).selectAll().executeTakeFirst()}function F(M,f){let B=M?.[f]??M?.changes;if(typeof B==="number"||typeof B==="bigint")return Number(B);let x=B?.changes;return typeof x==="number"||typeof x==="bigint"?Number(x):0}import{HttpError as S}from"@stacksjs/error-handling";import{formatDate as Q,isUniqueViolation as u}from"@stacksjs/orm";async function l(M){let f=await P();try{let B=await f.selectFrom("authors").where((Y)=>Y.or([Y("email","=",M.email),Y("name","=",M.name)])).selectAll().executeTakeFirst();if(B)return B;let x=await f.selectFrom("users").where("email","=",M.email).selectAll().executeTakeFirst();if(!x){let Y=await f.insertInto("users").values({email:M.email,name:M.name,password:g(),uuid:g(),created_at:Q(new Date),updated_at:Q(new Date)}).returningAll().executeTakeFirst();if(x=await z(f,"users",Y),!x)throw Error("Failed to create user")}let C={user_id:x.id,name:M.name,email:M.email,created_at:Q(new Date),updated_at:Q(new Date)},J=await f.insertInto("authors").values(C).returningAll().executeTakeFirst(),X=await z(f,"authors",J);if(!X)throw Error("Failed to create author");return X}catch(B){if(B instanceof S)throw B;if(u(B))throw new S(409,"An author with this email already exists");if(B instanceof Error)throw TypeError(`Failed to find or create author: ${B.message}`);throw B}}async function n(M){let f=await P();try{let B=await f.selectFrom("authors").where((X)=>X.or([X("email","=",M.email),X("name","=",M.name)])).selectAll().executeTakeFirst();if(B)return B;let x={user_id:M.user_id,name:M.name,email:M.email,created_at:Q(new Date),updated_at:Q(new Date)},C=await f.insertInto("authors").values(x).returningAll().executeTakeFirst(),J=await z(f,"authors",C);if(!J)throw Error("Failed to create author");return J}catch(B){if(B instanceof S)throw B;if(u(B))throw new S(409,"An author with this email already exists");if(B instanceof Error)throw TypeError(`Failed to find or create author: ${B.message}`);throw B}}import{HttpError as b}from"@stacksjs/error-handling";import{formatDate as oM,isUniqueViolation as dM}from"@stacksjs/orm";async function i(M,f){let B=await P();try{let x={...f,updated_at:oM(new Date)},C=await B.updateTable("authors").set(x).where("id","=",M).returningAll().executeTakeFirst();if($(C))return C;return await k(M)}catch(x){if(x instanceof b)throw x;if(dM(x))throw new b(409,"An author with this email already exists");if(x instanceof Error)throw TypeError(`Failed to update author: ${x.message}`);throw x}}var N={};K(N,{update:()=>xM,storeCategorizableModel:()=>BM,store:()=>MM,fetchWithPosts:()=>r,fetchBySlug:()=>d,fetchByName:()=>o,fetchById:()=>V,fetchAll:()=>s,destroy:()=>e,bulkStore:()=>fM,bulkDestroy:()=>t});import{slugify as qB}from"ts-slug";async function V(M){return await(await P()).selectFrom("categorizables").where("id","=",M).where("is_active","=",!0).selectAll().executeTakeFirst()}async function s(){return await(await P()).selectFrom("categorizables").selectAll().execute()}async function o(M){return await(await P()).selectFrom("categorizables").where("name","=",M).where("is_active","=",!0).selectAll().execute()}async function d(M){return await(await P()).selectFrom("categorizables").where("slug","=",M).where("is_active","=",!0).selectAll().executeTakeFirst()}async function r(M){return await(await P()).selectFrom("categorizables").where("id","=",M).where("is_active","=",!0).selectAll().executeTakeFirst()}async function e(M){let f=await P();try{if(!await V(M))throw Error(`Category with ID ${M} not found`);return(await f.deleteFrom("categorizables").where("id","=",M).executeTakeFirst()).numDeletedRows>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete category: ${B.message}`);throw B}}async function t(M){let f=await P();if(!M.length)return 0;try{let B=await f.deleteFrom("categorizables").where("id","in",M).executeTakeFirst();return Number(B.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete categories in bulk: ${B.message}`);throw B}}import{slugify as a}from"ts-slug";async function MM(M){let f=await P();try{if(!M.name||M.name.trim()==="")throw Error("Category name is required");if(!M.categorizable_type||M.categorizable_type.trim()==="")throw Error("Category categorizable_type is required");let B={name:M.name,slug:a(M.name),description:M.description,categorizable_type:M.categorizable_type,is_active:M.is_active??!0},x=await f.insertInto("categorizables").values(B).returningAll().executeTakeFirst(),C=await z(f,"categorizables",x);if(!C)throw Error("Failed to create category");return C}catch(B){if(B instanceof Error)throw TypeError(`Failed to create category: ${B.message}`);throw B}}async function BM(M){let f=await P();try{let B={category_id:M.category_id,categorizable_type:M.categorizable_type},x=await f.insertInto("categorizable_models").values(B).returningAll().executeTakeFirst(),C=await z(f,"categorizable_models",x);if(!C)throw Error("Failed to create categorizable model relationship");return C}catch(B){if(B instanceof Error)throw TypeError(`Failed to create categorizable model relationship: ${B.message}`);throw B}}async function fM(M){let f=await P();try{return await f.transaction(async(x)=>{let C=[];for(let J of M){let X={name:J.name,slug:a(J.name),description:J.description,categorizable_type:J.categorizable_type,is_active:J.is_active??!0},Y=await x.insertInto("categorizables").values(X).returningAll().executeTakeFirst(),Z=await z(x,"categorizables",Y);if(!Z)throw Error(`Failed to create category: ${J.name}`);C.push(Z)}return C})}catch(B){if(B instanceof Error)throw TypeError(`Failed to create categories: ${B.message}`);throw B}}import{slugify as rM}from"ts-slug";async function xM(M){let f=await P();try{let B=M.id;if(!B)throw Error("Category ID is required for update");if(M.name!==void 0){if(M.name.trim()==="")throw Error("Category name cannot be empty");M.slug=rM(M.name)}let{id:x,...C}=M,J=await f.updateTable("categorizables").set(C).where("id","=",B).returningAll().executeTakeFirst();if($(J))return J;let X=await V(B);if(!X)throw Error("Failed to update category");return X}catch(B){if(B instanceof Error)throw TypeError(`Failed to update category: ${B.message}`);throw B}}var q={};K(q,{update:()=>KM,store:()=>OM,fetchStatusDistributionDonut:()=>$M,fetchPostsWithMostComments:()=>h,fetchMonthlyCommentCounts:()=>zM,fetchCommentsByStatus:()=>XM,fetchCommentsByCommentables:()=>CM,fetchComments:()=>PM,fetchCommentCountByPeriod:()=>JM,fetchCommentCountBarGraph:()=>ZM,fetchCommentById:()=>_,destroy:()=>GM,calculateApprovalRate:()=>YM,bulkDestroy:()=>AM});import{formatDate as L}from"@stacksjs/orm";async function PM(M={}){let B=(await P()).selectFrom("commentables");if(M.status)B=B.where("status","=",M.status);if(M.commentables_id)B=B.where("commentables_id","=",M.commentables_id);if(M.commentables_type)B=B.where("commentables_type","=",M.commentables_type);if(M.limit)B=B.limit(M.limit);if(M.offset)B=B.offset(M.offset);return B.selectAll().execute()}async function _(M){return(await P()).selectFrom("commentables").where("id","=",M).selectAll().executeTakeFirst()}async function CM(M,f,B={}){let C=(await P()).selectFrom("commentables").where("commentables_id","=",M).where("commentables_type","=",f);if(B.status)C=C.where("status","=",B.status);if(B.limit)C=C.limit(B.limit);if(B.offset)C=C.offset(B.offset);return C.selectAll().execute()}async function JM(M){let f=await P();try{let B=await f.selectFrom("commentables").where("created_at",">=",new Date(Date.now()-M*24*60*60*1000).toISOString()).count();return Number(B)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch comment count: ${B.message}`);throw B}}async function XM(M,f={}){let B=await P();try{let x=B.selectFrom("commentables").where("status","=",M);if(f.limit)x=x.limit(f.limit);if(f.offset)x=x.offset(f.offset);return x.selectAll().execute()}catch(x){if(x instanceof Error)throw TypeError(`Failed to fetch comments by status: ${x.message}`);throw x}}async function YM(){let M=await P();try{let[f,B]=await Promise.all([M.selectFrom("commentables").where("status","=","approved").count(),M.selectFrom("commentables").count()]),x=Number(f||0),C=Number(B||0),J=C>0?x/C*100:0;return{approved:x,total:C,rate:J}}catch(f){if(f instanceof Error)throw TypeError(`Failed to calculate approval rate: ${f.message}`);throw f}}async function h(M,f={}){let B=await P();try{let x=B.selectFrom("posts").leftJoin("commentables","posts.id","=","commentables.commentables_id").where("commentables.created_at",">=",L(M.startDate)).where("commentables.created_at","<=",L(M.endDate)).select(["posts.id","posts.title"]).groupBy("posts.id","posts.title").orderBy("posts.id","desc");if(f.limit)x=x.limit(f.limit);return(await x.execute()).map((J)=>({id:Number(J.id),title:String(J.title),comment_count:Number(J.comment_count||0)}))}catch(x){if(x instanceof Error)throw TypeError(`Failed to fetch posts with most comments: ${x.message}`);throw x}}async function ZM(M,f={}){try{let B=await h(M,f);return{labels:B.map((x)=>x.title),values:B.map((x)=>x.comment_count)}}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch bar graph data: ${B.message}`);throw B}}async function $M(M){let f=await P();try{let x=await f.selectFrom("commentables").where("created_at",">=",L(M.startDate)).where("created_at","<=",L(M.endDate)).select(["status"]).groupBy("status").execute(),C=x.reduce((Z,O)=>Z+Number(O.count||0),0),J=x.map((Z)=>String(Z.status)),X=x.map((Z)=>Number(Z.count||0)),Y=X.map((Z)=>C>0?Z/C*100:0);return{labels:J,values:X,percentages:Y}}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch status distribution: ${B.message}`);throw B}}async function zM(M){let f=await P();try{let B=await f.selectFrom("commentables").where("created_at",">=",L(M.startDate)).where("created_at","<=",L(M.endDate)).select(["created_at"]).groupBy("created_at").orderBy("created_at","asc").execute(),x=new Map;B.forEach((X)=>{if(!X.created_at)return;let Y=new Date(X.created_at),Z=`${Y.getFullYear()}-${String(Y.getMonth()+1).padStart(2,"0")}`;x.set(Z,(x.get(Z)||0)+Number(X.count||0))});let C=[],J=[];return x.forEach((X,Y)=>{let[Z,O]=Y.split("-");C.push(new Date(Number(Z),Number(O)-1).toLocaleString("default",{month:"short",year:"numeric"})),J.push(X)}),{labels:C,values:J}}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch monthly comment counts: ${B.message}`);throw B}}async function GM(M){let f=await P();try{if(!await _(M))throw Error(`Comment with ID ${M} not found`);return(await f.deleteFrom("commentables").where("id","=",M).executeTakeFirst()).numDeletedRows>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete comment: ${B.message}`);throw B}}async function AM(M){let f=await P();if(!M.length)return 0;try{let B=await f.deleteFrom("commentables").where("id","in",M).executeTakeFirst();return Number(B.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete comments in bulk: ${B.message}`);throw B}}async function OM(M){let f=await P();try{if(!M.title||M.title.trim()==="")throw Error("Comment title is required");if(!M.body||M.body.trim()==="")throw Error("Comment body is required");if(!M.commentables_type||M.commentables_type.trim()==="")throw Error("Comment commentables_type is required");let B=["pending","approved","rejected"];if(M.status&&!B.includes(M.status))throw Error(`Invalid comment status: ${M.status}`);let x={title:M.title,body:M.body,status:M.status,commentables_id:M.commentables_id,commentables_type:M.commentables_type,user_id:M.user_id,is_active:M.is_active},C=await f.insertInto("commentables").values(x).returningAll().executeTakeFirst(),J=await z(f,"commentables",C);if(!J)throw Error("Failed to create comment");return J}catch(B){if(B instanceof Error)throw TypeError(`Failed to store comment: ${B.message}`);throw B}}import{formatDate as eM}from"@stacksjs/orm";async function KM(M,f){let B=await P();try{if(f.title!==void 0&&f.title.trim()==="")throw Error("Comment title cannot be empty");if(f.body!==void 0&&f.body.trim()==="")throw Error("Comment body cannot be empty");let x=["pending","approved","rejected"];if(f.status!==void 0&&!x.includes(f.status))throw Error(`Invalid comment status: ${f.status}`);let C={updated_at:eM(new Date)};if(f.title!==void 0)C.title=f.title;if(f.body!==void 0)C.body=f.body;if(f.status!==void 0)C.status=f.status;if(f.commentables_id!==void 0)C.commentables_id=f.commentables_id;if(f.commentables_type!==void 0)C.commentables_type=f.commentables_type;if(f.approved_at!==void 0)C.approved_at=f.approved_at;if(f.rejected_at!==void 0)C.rejected_at=f.rejected_at;return await B.updateTable("commentables").set(C).where("id","=",M).execute(),await B.selectFrom("commentables").where("id","=",M).selectAll().executeTakeFirst()}catch(x){if(x instanceof Error)throw TypeError(`Failed to update comment: ${x.message}`);throw x}}var T={};K(T,{update:()=>EM,store:()=>WM,fetchPublishedAfter:()=>kM,fetchByMinViews:()=>LM,fetchByMinConversions:()=>HM,fetchById:()=>H,fetchByAuthor:()=>VM,fetchAll:()=>QM,destroy:()=>SM,bulkDestroy:()=>_M});async function H(M){return await(await P()).selectFrom("pages").where("id","=",M).selectAll().executeTakeFirst()}async function QM(){return await(await P()).selectFrom("pages").selectAll().execute()}async function VM(M){return await(await P()).selectFrom("pages").where("author_id","=",M).selectAll().execute()}async function LM(M){return await(await P()).selectFrom("pages").where("views",">=",M).selectAll().execute()}async function HM(M){return await(await P()).selectFrom("pages").where("conversions",">=",M).selectAll().execute()}async function kM(M){return await(await P()).selectFrom("pages").where("published_at",">",M).selectAll().execute()}async function SM(M){let f=await P();try{if(!await H(M))throw Error(`Page with ID ${M} not found`);return(await f.deleteFrom("pages").where("id","=",M).executeTakeFirst()).numDeletedRows>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete page: ${B.message}`);throw B}}async function _M(M){let f=await P();if(!M.length)return 0;try{let B=await f.deleteFrom("pages").where("id","in",M).executeTakeFirst();return Number(B.numDeletedRows)||0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete pages in bulk: ${B.message}`);throw B}}import{formatDate as jM}from"@stacksjs/orm";async function WM(M){let f=await P();try{let B={author_id:M.author_id,title:M.title,template:M.template,views:M.views||0,conversions:M.conversions||0,created_at:jM(new Date),updated_at:jM(new Date)},x=await f.insertInto("pages").values(B).returningAll().executeTakeFirst(),C=await z(f,"pages",x);if(!C)throw Error("Failed to create page");return C}catch(B){if(B instanceof Error)throw TypeError(`Failed to create page: ${B.message}`);throw B}}import{formatDate as tM}from"@stacksjs/orm";async function EM(M,f){let B=await P();try{let x={...f,updated_at:tM(new Date)},C=await B.updateTable("pages").set(x).where("id","=",M).returningAll().executeTakeFirst();if($(C))return C;let J=await H(M);if(!J)throw Error("Failed to update page");return J}catch(x){if(x instanceof Error)throw TypeError(`Failed to update page: ${x.message}`);throw x}}var v={};K(v,{update:()=>mM,sync:()=>wM,store:()=>RM,fetchPublishedAfter:()=>TM,fetchByStatus:()=>UM,fetchByMinViews:()=>qM,fetchById:()=>G,fetchByCategory:()=>NM,fetchByAuthor:()=>hM,fetchAll:()=>FM,detach:()=>yM,destroy:()=>vM,bulkDestroy:()=>IM,attach:()=>cM});async function G(M){return await(await P()).selectFrom("posts").where("id","=",M).selectAll().executeTakeFirst()}async function FM(){return await(await P()).selectFrom("posts").selectAll().execute()}async function UM(M){return await(await P()).selectFrom("posts").where("status","=",M).selectAll().execute()}async function NM(M){return await(await P()).selectFrom("posts").where("category","=",M).selectAll().execute()}async function hM(M){return await(await P()).selectFrom("posts").where("author_id","=",M).selectAll().execute()}async function qM(M){return await(await P()).selectFrom("posts").where("views",">=",M).selectAll().execute()}async function TM(M){return await(await P()).selectFrom("posts").where("published_at",">",M).selectAll().execute()}async function vM(M){let f=await P();try{if(!await G(M))throw Error(`Post with ID ${M} not found`);let x=await f.deleteFrom("posts").where("id","=",M).executeTakeFirst();return F(x,"numDeletedRows")>0}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete post: ${B.message}`);throw B}}async function IM(M){let f=await P();if(!M.length)return 0;try{let x=(await f.selectFrom("posts").select(["id"]).where("id","in",M).execute()).map((J)=>J.id);if(!x.length)return 0;let C=0;for(let J of x){let X=await f.deleteFrom("posts").where("id","=",J).executeTakeFirst();C+=F(X,"numDeletedRows")}return C}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete posts in bulk: ${B.message}`);throw B}}var{randomUUIDv7:aM}=globalThis.Bun;import{formatDate as j}from"@stacksjs/orm";var MB="draft",BB="published";async function RM(M){let f=await P();try{if(!M.title||typeof M.title==="string"&&M.title.trim()==="")throw Error("Post title is required");let B=M.content||M.body,x=M,C=M.status||MB,J={author_id:x.author_id,uuid:aM(),title:M.title,poster:M.poster,content:B,excerpt:M.excerpt,is_featured:x.is_featured?Date.now():void 0,views:M.views||0,published_at:x.published_at??(C===BB?Date.now():void 0),status:C},X=await f.insertInto("posts").values(J).returningAll().executeTakeFirst();if($(X))return X;let Y=E(X),Z=Y===void 0?void 0:await G(Y);if(!Z)throw Error("Failed to create post");return Z}catch(B){if(B instanceof Error)throw TypeError(`Failed to create post: ${B.message}`);throw B}}async function cM(M,f,B){let x=await P();try{let C=f==="categorizable_models"?"categorizable_id":"taggable_id",J=f==="categorizable_models"?"categorizable_type":"taggable_type",X=f==="categorizable_models"?"category_id":"tag_id",Y=B.map((Z)=>({[X]:Z,[C]:M,[J]:"posts",created_at:j(new Date),updated_at:j(new Date)}));for(let Z of Y)await x.insertInto(f).values(Z).execute()}catch(C){if(C instanceof Error)throw TypeError(`Failed to attach records: ${C.message}`);throw C}}async function yM(M,f,B){let x=await P();try{let C=f==="categorizable_models"?"categorizable_id":"taggable_id",J=f==="categorizable_models"?"categorizable_type":"taggable_type",X=x.deleteFrom(f).where(C,"=",M).where(J,"=","posts");if(B)X=X.where("id","in",B);await X.execute()}catch(C){if(C instanceof Error)throw TypeError(`Failed to detach records: ${C.message}`);throw C}}async function wM(M,f,B){let x=await P();try{let C=f==="categorizable_models"?"categorizable_id":"taggable_id",J=f==="categorizable_models"?"categorizable_type":"taggable_type",Y=(await x.selectFrom(f).select(["id"]).where(C,"=",M).where(J,"=","posts").execute()).map((A)=>A.id),Z=Y.filter((A)=>!B.includes(A)),O=B.filter((A)=>!Y.includes(A));if(Z.length>0)await x.deleteFrom(f).where(C,"=",M).where(J,"=","posts").where("id","in",Z).execute();if(O.length>0){let A=O.map(()=>({[C]:M,[J]:"posts",created_at:j(new Date),updated_at:j(new Date)}));await x.insertInto(f).values(A).execute()}}catch(C){if(C instanceof Error)throw TypeError(`Failed to sync records: ${C.message}`);throw C}}async function mM(M,f){let B=await P();try{if(f.title!==void 0&&typeof f.title==="string"&&f.title.trim()==="")throw Error("Post title cannot be empty");let x=["published","draft","archived"];if(f.status!==void 0&&typeof f.status==="string"&&!x.includes(f.status))throw Error(`Invalid post status: ${f.status}`);if(f.views!==void 0&&typeof f.views==="number"&&f.views<0)throw Error("Views count cannot be negative");let C=await B.updateTable("posts").set(f).where("id","=",M).returningAll().executeTakeFirst();if($(C))return C;let J=await G(M);if(!J)throw Error(`Post with ID ${M} not found`);return J}catch(x){if(x instanceof Error)throw TypeError(`Failed to update post: ${x.message}`);throw x}}var I={};K(I,{update:()=>lM,store:()=>gM,fetchTags:()=>uM,fetchTagById:()=>W,destroy:()=>pM,bulkDestroy:()=>DM});async function pM(M){let f=await P();if(!await f.selectFrom("taggables").where("id","=",M).selectAll().executeTakeFirst())throw Error(`Tag with ID ${M} not found`);await f.deleteFrom("taggables").where("id","=",M).executeTakeFirst()}async function DM(M){let f=await P();try{await f.deleteFrom("taggables").where("id","in",M).execute()}catch(B){if(B instanceof Error)throw TypeError(`Failed to delete tags: ${B.message}`);throw B}}import{slugify as fB}from"ts-slug";async function gM(M){let f=await P();try{if(!M.name||M.name.trim()==="")throw Error("Tag name is required");if(!M.taggable_type||M.taggable_type.trim()==="")throw Error("Tag taggable_type is required");let B=fB(M.name);if(await f.selectFrom("taggables").selectAll().where("slug","=",B).executeTakeFirst())throw Error(`Tag with unique slug "${B}" already exists`);let C={name:M.name,slug:B,description:M.description,is_active:M.is_active??!0,taggable_type:M.taggable_type};if(M.taggable_id!==void 0)C.taggable_id=M.taggable_id;let J=await f.insertInto("taggables").values(C).returningAll().executeTakeFirst(),X=await z(f,"taggables",J);if(!X)throw Error("Failed to create tag");return X}catch(B){if(B instanceof Error)throw TypeError(`Failed to store tag: ${B.message}`);throw B}}async function W(M){let f=await P();try{let B=await f.selectFrom("taggables").where("id","=",M).selectAll().executeTakeFirst();if(!B)return;return B}catch(B){if(B instanceof Error)throw TypeError(`Failed to fetch tag: ${B.message}`);throw B}}async function uM(){let M=await P();try{return await M.selectFrom("taggables").where("is_active","=",!0).selectAll().execute()}catch(f){if(f instanceof Error)throw TypeError(`Failed to fetch tags: ${f.message}`);throw f}}import{uniqueSlug as xB}from"@stacksjs/slug";async function lM(M){let f=await P();try{let B=M.id;if(!B)throw Error("Tag ID is required for update");if(M.name!==void 0){if(M.name.trim()==="")throw Error("Tag name cannot be empty");let Y=await xB(M.name,{table:"taggables",column:"slug"});if(await f.selectFrom("taggables").selectAll().where("slug","=",Y).where("id","!=",B).executeTakeFirst())throw Error(`Tag with unique slug "${Y}" already exists`);M.slug=Y}let{id:x,...C}=M,J=await f.updateTable("taggables").set(C).where("id","=",B).returningAll().executeTakeFirst();if($(J))return J;let X=await W(B);if(!X)throw Error("Failed to update tag");return X}catch(B){if(B instanceof Error)throw TypeError(`Failed to update tag: ${B.message}`);throw B}}var PB={posts:v,postCategories:N,tags:I,comments:q,authors:U,pages:T},Ax=PB;export{I as tags,v as posts,T as pages,Ax as default,q as comments,PB as cms,N as categorizable,U as authors};
|
package/dist/posts/fetch.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare function fetchByCategory(category: string): Promise<PostJsonRespo
|
|
|
17
17
|
/**
|
|
18
18
|
* Fetch posts by author
|
|
19
19
|
*/
|
|
20
|
-
export declare function fetchByAuthor(
|
|
20
|
+
export declare function fetchByAuthor(authorId: number): Promise<PostJsonResponse[]>;
|
|
21
21
|
/**
|
|
22
22
|
* Fetch posts with minimum views
|
|
23
23
|
*/
|
package/dist/posts/store.d.ts
CHANGED
|
@@ -33,8 +33,9 @@ export declare function detach(postId: number, tableName: 'categorizable_models'
|
|
|
33
33
|
* @returns Promise<void>
|
|
34
34
|
*/
|
|
35
35
|
export declare function sync(postId: number, tableName: 'categorizable_models' | 'taggable_models', ids: number[]): Promise<void>;
|
|
36
|
-
|
|
37
|
-
export declare const
|
|
38
|
-
export declare const
|
|
36
|
+
// Must match the CHECK constraint on posts.status.
|
|
37
|
+
export declare const POST_STATUS_DRAFT: 'draft';
|
|
38
|
+
export declare const POST_STATUS_PUBLISHED: 'published';
|
|
39
|
+
export declare const POST_STATUS_ARCHIVED: 'archived';
|
|
39
40
|
declare type PostJsonResponse = ModelRow<typeof Post>;
|
|
40
41
|
declare type NewPost = NewModelData<typeof Post>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True when the driver honoured RETURNING and gave back an actual row.
|
|
3
|
+
*/
|
|
4
|
+
export declare function isRow<T extends object>(value: unknown): value is T;
|
|
5
|
+
/**
|
|
6
|
+
* The id of a freshly inserted row, or undefined if the driver reported none.
|
|
7
|
+
*/
|
|
8
|
+
export declare function insertedId(result: unknown): number | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The row a write actually produced.
|
|
11
|
+
*
|
|
12
|
+
* Returns the driver's RETURNING row when there is one, otherwise re-selects
|
|
13
|
+
* the row by the id it reported.
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveWrittenRow<T extends object>(db: { selectFrom: (table: string) => any }, table: string, result: unknown): Promise<T | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* The number of rows affected by a delete/update.
|
|
18
|
+
*
|
|
19
|
+
* The count arrives as a bare number on some drivers and nested under
|
|
20
|
+
* `{ changes }` on SQLite.
|
|
21
|
+
*/
|
|
22
|
+
export declare function affectedRows(result: unknown, key: 'numDeletedRows' | 'numUpdatedRows'): number;
|