@stacksjs/cms 0.72.6 → 0.72.8

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.
@@ -1,5 +1,19 @@
1
1
  /**
2
- * Create a new page
2
+ * Create a new page.
3
+ *
4
+ * A page belongs to a site and is found by its path, so `site_id` decides
5
+ * which of the two ways this runs.
6
+ *
7
+ * WITH a site, the work belongs to `createPageDocument`: it validates the
8
+ * blocks, derives a unique path under the parent, and writes the columns the
9
+ * serving layer reads. This function used to write `author_id`, `title`,
10
+ * `template`, `views` and `conversions` and nothing else - so a page created
11
+ * through it had no `site_id`, no `path` and no `blocks`, and `resolvePublishedPage`
12
+ * could never return it. The insert succeeded, the caller got a row back, and
13
+ * the page simply did not exist as far as the website was concerned.
14
+ *
15
+ * WITHOUT a site, the legacy shape is preserved for the single-site apps that
16
+ * predate site scoping, rather than throwing at them.
3
17
  *
4
18
  * @param data The page data to create
5
19
  * @returns The created page record
@@ -1 +1 @@
1
- import{getDb}from"../database";import{resolveWrittenRow}from"../results";import{formatDate}from"@stacksjs/orm";export async function store(data){const db=await getDb();try{const pageData={author_id:data.author_id,title:data.title,template:data.template,views:data.views||0,conversions:data.conversions||0,created_at:formatDate(new Date),updated_at:formatDate(new Date)},written=await db.insertInto("pages").values(pageData).returningAll().executeTakeFirst(),result=await resolveWrittenRow(db,"pages",written);if(!result)throw Error("Failed to create page");return result}catch(error){if(error instanceof Error)throw TypeError(`Failed to create page: ${error.message}`);throw error}}
1
+ import{formatDate}from"@stacksjs/orm";import{getDb}from"../database";import{resolveWrittenRow}from"../results";import{createPageDocument}from"./document";export async function store(data){const db=await getDb();if(data.site_id){const saved=await createPageDocument(Number(data.site_id),{title:String(data.title??""),slug:String(data.slug??data.path??data.title??""),parentId:data.parent_id===null||data.parent_id===void 0?void 0:Number(data.parent_id),authorId:data.author_id===null||data.author_id===void 0?void 0:Number(data.author_id),template:data.template===null||data.template===void 0?void 0:String(data.template),metaDescription:data.meta_description===null||data.meta_description===void 0?void 0:String(data.meta_description),status:data.status,blocks:parseIncomingBlocks(data.blocks)}),result=await db.selectFrom("pages").selectAll().where("id","=",saved.id).executeTakeFirst();if(!result)throw Error("Failed to create page");return result}try{const pageData={author_id:data.author_id,title:data.title,template:data.template,views:data.views||0,conversions:data.conversions||0,created_at:formatDate(new Date),updated_at:formatDate(new Date)},written=await db.insertInto("pages").values(pageData).returningAll().executeTakeFirst(),result=await resolveWrittenRow(db,"pages",written);if(!result)throw Error("Failed to create page");return result}catch(error){if(error instanceof Error)throw TypeError(`Failed to create page: ${error.message}`);throw error}}function parseIncomingBlocks(blocks){if(Array.isArray(blocks))return blocks;if(typeof blocks==="string"&&blocks.trim())try{const parsed=JSON.parse(blocks);return Array.isArray(parsed)?parsed:[]}catch{return[]}return[]}
@@ -1,5 +1,13 @@
1
1
  /**
2
- * Update a page
2
+ * Update a page.
3
+ *
4
+ * A change to `slug` or `blocks` is not a column write: the slug decides the
5
+ * page's `path` (and every child's path under it), and blocks have to be
6
+ * validated and snapshotted into a revision before they replace what is live.
7
+ * Those go through `updatePageDocument`, which does all three.
8
+ *
9
+ * Everything else - title alone, template, meta description, view counters -
10
+ * is a plain column update, and stays one.
3
11
  *
4
12
  * @param id The id of the page to update
5
13
  * @param data The page data to update
@@ -1 +1 @@
1
- import{getDb}from"../database";import{fetchById}from"./fetch";import{isRow}from"../results";import{formatDate}from"@stacksjs/orm";export async function update(id,data){const db=await getDb();try{const updateData={...data,updated_at:formatDate(new Date)},result=await db.updateTable("pages").set(updateData).where("id","=",id).returningAll().executeTakeFirst();if(isRow(result))return result;const page=await fetchById(id);if(!page)throw Error("Failed to update page");return page}catch(error){if(error instanceof Error)throw TypeError(`Failed to update page: ${error.message}`);throw error}}
1
+ import{getDb}from"../database";import{fetchById}from"./fetch";import{isRow}from"../results";import{formatDate}from"@stacksjs/orm";export async function update(id,data){const db=await getDb();if(data.slug!==void 0||data.blocks!==void 0){const current=await db.selectFrom("pages").where("id","=",id).select(["id","site_id","title","slug"]).executeTakeFirst();if(!current)throw TypeError(`Failed to update page: page ${id} not found`);if(current.site_id){const{updatePageDocument}=await import("./document");await updatePageDocument(Number(current.site_id),id,{title:String(data.title??current.title),slug:data.slug===void 0?current.slug??void 0:String(data.slug),template:data.template===null||data.template===void 0?void 0:String(data.template),metaDescription:data.meta_description===null||data.meta_description===void 0?void 0:String(data.meta_description),status:data.status,blocks:data.blocks});const saved=await fetchById(id);if(!saved)throw Error("Failed to update page");return saved}}try{const updateData={...data,updated_at:formatDate(new Date)},result=await db.updateTable("pages").set(updateData).where("id","=",id).returningAll().executeTakeFirst();if(isRow(result))return result;const page=await fetchById(id);if(!page)throw Error("Failed to update page");return page}catch(error){if(error instanceof Error)throw TypeError(`Failed to update page: ${error.message}`);throw error}}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/cms",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.72.6",
5
+ "version": "0.72.8",
6
6
  "description": "Stacks cms utilities.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -58,8 +58,8 @@
58
58
  "prepublishOnly": "bun run build"
59
59
  },
60
60
  "dependencies": {
61
- "@stacksjs/sites": "0.72.6",
62
- "@stacksjs/validation": "0.72.6",
61
+ "@stacksjs/sites": "0.72.8",
62
+ "@stacksjs/validation": "0.72.8",
63
63
  "ts-slug": "^0.1.0",
64
64
  "ts-spreadsheets": "^0.2.0"
65
65
  },