@stacksjs/cms 0.70.111 → 0.70.113
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/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
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};
|
|
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("email","=",M.email).orWhere("name","=",M.name).selectAll().executeTakeFirst();if(B)return B;let x=await f.selectFrom("users").where("email","=",M.email).selectAll().executeTakeFirst();if(!x){let Z=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",Z),!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("email","=",M.email).orWhere("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},Z=await x.insertInto("categorizables").values(X).returningAll().executeTakeFirst(),Y=await z(x,"categorizables",Z);if(!Y)throw Error(`Failed to create category: ${J.name}`);C.push(Y)}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((Y,O)=>Y+Number(O.count||0),0),J=x.map((Y)=>String(Y.status)),X=x.map((Y)=>Number(Y.count||0)),Z=X.map((Y)=>C>0?Y/C*100:0);return{labels:J,values:X,percentages:Z}}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 Z=new Date(X.created_at),Y=`${Z.getFullYear()}-${String(Z.getMonth()+1).padStart(2,"0")}`;x.set(Y,(x.get(Y)||0)+Number(X.count||0))});let C=[],J=[];return x.forEach((X,Z)=>{let[Y,O]=Z.split("-");C.push(new Date(Number(Y),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 Z=E(X),Y=Z===void 0?void 0:await G(Z);if(!Y)throw Error("Failed to create post");return Y}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",Z=B.map((Y)=>({[X]:Y,[C]:M,[J]:"posts",created_at:j(new Date),updated_at:j(new Date)}));for(let Y of Z)await x.insertInto(f).values(Y).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",Z=(await x.selectFrom(f).select(["id"]).where(C,"=",M).where(J,"=","posts").execute()).map((A)=>A.id),Y=Z.filter((A)=>!B.includes(A)),O=B.filter((A)=>!Z.includes(A));if(Y.length>0)await x.deleteFrom(f).where(C,"=",M).where(J,"=","posts").where("id","in",Y).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 Z=await xB(M.name,{table:"taggables",column:"slug"});if(await f.selectFrom("taggables").selectAll().where("slug","=",Z).where("id","!=",B).executeTakeFirst())throw Error(`Tag with unique slug "${Z}" already exists`);M.slug=Z}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};
|