@rimelight/cms 0.0.9 → 0.0.11

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.
@@ -0,0 +1,59 @@
1
+ import { boolean, jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core"
2
+ import type { Localized } from "../core/types/pages"
3
+ import type { BaseBlock } from "../core/types/blocks"
4
+
5
+ export type CollectionMode = "document" | "data" | "singleton"
6
+
7
+ export interface DynamicFieldDefinition {
8
+ type:
9
+ | "text"
10
+ | "number"
11
+ | "boolean"
12
+ | "enum"
13
+ | "media"
14
+ | "relation"
15
+ | "currency"
16
+ | "array"
17
+ | "json"
18
+ label: Localized
19
+ description?: Localized
20
+ required?: boolean
21
+ defaultValue?: any
22
+ options?: { label: Localized; value: string }[]
23
+ allowedCollections?: string[]
24
+ validation?: {
25
+ min?: number
26
+ max?: number
27
+ pattern?: string
28
+ }
29
+ }
30
+
31
+ export interface EcomSettings {
32
+ enabled: boolean
33
+ currencyDefault: string
34
+ trackInventory: boolean
35
+ hasVariants: boolean
36
+ allowDigitalDownloads: boolean
37
+ stripeSync?: boolean
38
+ }
39
+
40
+ export const contentTypes = pgTable("content_types", {
41
+ id: uuid("id").defaultRandom().notNull().primaryKey(),
42
+ slug: text("slug").notNull().unique(),
43
+ name: jsonb("name").$type<Localized>().notNull(),
44
+ description: jsonb("description").$type<Localized>(),
45
+ icon: text("icon").default("i-lucide-box").notNull(),
46
+ mode: text("mode").$type<CollectionMode>().default("document").notNull(),
47
+
48
+ fieldSchema: jsonb("field_schema")
49
+ .$type<Record<string, { label: Localized; fields: Record<string, DynamicFieldDefinition> }>>()
50
+ .default({})
51
+ .notNull(),
52
+
53
+ ecomSettings: jsonb("ecom_settings").$type<EcomSettings>(),
54
+ initialBlocks: jsonb("initial_blocks").$type<BaseBlock[]>().default([]).notNull(),
55
+ isSystem: boolean("is_system").default(false).notNull(),
56
+
57
+ createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
58
+ updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(() => new Date())
59
+ })
@@ -9,3 +9,4 @@ export * from "./search.ts"
9
9
  export * from "./site_settings.ts"
10
10
  export * from "./taxonomies.ts"
11
11
  export * from "./bylines.ts"
12
+ export * from "./content_types.ts"
@@ -342,3 +342,38 @@ export type Byline = typeof bylines.$inferSelect
342
342
  export type NewByline = typeof bylines.$inferInsert
343
343
  export type SiteSettings = typeof siteSettings.$inferSelect
344
344
  export type InsertSiteSettings = typeof siteSettings.$inferInsert
345
+
346
+ // 12. Dynamic Content Types
347
+ export const contentTypes = sqliteTable(
348
+ "content_types",
349
+ {
350
+ id: text("id")
351
+ .$defaultFn(() => crypto.randomUUID())
352
+ .primaryKey(),
353
+ slug: text("slug").notNull(),
354
+ name: text("name", { mode: "json" }).$type<Localized>().notNull(),
355
+ description: text("description", { mode: "json" }).$type<Localized>(),
356
+ icon: text("icon").default("i-lucide-box").notNull(),
357
+ mode: text("mode").$type<"document" | "data" | "singleton">().default("document").notNull(),
358
+ fieldSchema: text("field_schema", { mode: "json" })
359
+ .$type<Record<string, any>>()
360
+ .default(sql`'{}'`)
361
+ .notNull(),
362
+ ecomSettings: text("ecom_settings", { mode: "json" }).$type<Record<string, any>>(),
363
+ initialBlocks: text("initial_blocks", { mode: "json" })
364
+ .$type<BaseBlock[]>()
365
+ .default(sql`'[]'`)
366
+ .notNull(),
367
+ isSystem: integer("is_system", { mode: "boolean" }).default(false).notNull(),
368
+ createdAt: integer("created_at", { mode: "timestamp" })
369
+ .$defaultFn(() => new Date())
370
+ .notNull(),
371
+ updatedAt: integer("updated_at", { mode: "timestamp" })
372
+ .$defaultFn(() => new Date())
373
+ .$onUpdate(() => new Date())
374
+ },
375
+ (table) => [uniqueIndex("content_types_slug_idx").on(table.slug)]
376
+ )
377
+
378
+ export type ContentType = typeof contentTypes.$inferSelect
379
+ export type InsertContentType = typeof contentTypes.$inferInsert
@@ -36,23 +36,34 @@ export async function indexPageForSearch(
36
36
  .where(and(eq(contentSearchIndex.pageId, page.id), eq(contentSearchIndex.locale, locale)))
37
37
  .limit(1)
38
38
 
39
+ const hasSearchVector = "searchVector" in contentSearchIndex
40
+
41
+ const updatePayload: Record<string, any> = {
42
+ titleText,
43
+ contentText
44
+ }
45
+ if (hasSearchVector) {
46
+ updatePayload["searchVector"] =
47
+ sql`to_tsvector('english', ${titleText} || ' ' || ${contentText})`
48
+ }
49
+
39
50
  if (existing.length > 0) {
40
51
  await db
41
52
  .update(contentSearchIndex)
42
- .set({
43
- titleText,
44
- contentText,
45
- searchVector: sql`to_tsvector('english', ${titleText} || ' ' || ${contentText})`
46
- })
53
+ .set(updatePayload)
47
54
  .where(eq(contentSearchIndex.id, existing[0].id))
48
55
  } else {
49
- await db.insert(contentSearchIndex).values({
56
+ const insertPayload: Record<string, any> = {
50
57
  pageId: page.id,
51
58
  locale,
52
59
  titleText,
53
- contentText,
54
- searchVector: sql`to_tsvector('english', ${titleText} || ' ' || ${contentText})`
55
- })
60
+ contentText
61
+ }
62
+ if (hasSearchVector) {
63
+ insertPayload["searchVector"] =
64
+ sql`to_tsvector('english', ${titleText} || ' ' || ${contentText})`
65
+ }
66
+ await db.insert(contentSearchIndex).values(insertPayload)
56
67
  }
57
68
  } catch (err) {
58
69
  console.error(`[Search Indexer] Failed to index page ${page.id} for locale ${locale}:`, err)
@@ -32,6 +32,22 @@ export const DEFAULT_SITE_SETTINGS: InsertSiteSettings = {
32
32
  }
33
33
  }
34
34
 
35
+ function fallbackSettings(overrides?: Partial<InsertSiteSettings>): SiteSettings {
36
+ return {
37
+ id: overrides?.id ?? "default",
38
+ name: overrides?.name ?? DEFAULT_SITE_SETTINGS.name,
39
+ description: overrides?.description ?? DEFAULT_SITE_SETTINGS.description,
40
+ url: overrides?.url ?? DEFAULT_SITE_SETTINGS.url,
41
+ ogImage: overrides?.ogImage ?? DEFAULT_SITE_SETTINGS.ogImage,
42
+ author: overrides?.author ?? DEFAULT_SITE_SETTINGS.author,
43
+ email: overrides?.email ?? "",
44
+ branding: overrides?.branding ?? DEFAULT_SITE_SETTINGS.branding,
45
+ seo: overrides?.seo ?? DEFAULT_SITE_SETTINGS.seo,
46
+ createdAt: new Date(),
47
+ updatedAt: new Date()
48
+ }
49
+ }
50
+
35
51
  /**
36
52
  * Retrieves the dynamic site settings from the database. If no settings are found, seeds the
37
53
  * default record and returns it.
@@ -41,13 +57,7 @@ export async function getSiteSettings(
41
57
  fallbackDefaults?: Partial<InsertSiteSettings>
42
58
  ): Promise<SiteSettings> {
43
59
  if (!db) {
44
- // eslint-disable-next-line typescript/no-unsafe-type-assertion
45
- return {
46
- ...DEFAULT_SITE_SETTINGS,
47
- ...fallbackDefaults,
48
- createdAt: new Date(),
49
- updatedAt: new Date()
50
- } as SiteSettings
60
+ return fallbackSettings(fallbackDefaults)
51
61
  }
52
62
 
53
63
  try {
@@ -71,13 +81,7 @@ export async function getSiteSettings(
71
81
  return inserted[0]
72
82
  } catch (error) {
73
83
  console.warn("Failed to fetch site settings from DB, using fallback:", error)
74
- // eslint-disable-next-line typescript/no-unsafe-type-assertion
75
- return {
76
- ...DEFAULT_SITE_SETTINGS,
77
- ...fallbackDefaults,
78
- createdAt: new Date(),
79
- updatedAt: new Date()
80
- } as SiteSettings
84
+ return fallbackSettings(fallbackDefaults)
81
85
  }
82
86
  }
83
87
 
@@ -0,0 +1,249 @@
1
+ import { pageTemplates } from "../schema/page_templates.ts"
2
+ import {
3
+ BLOG_POST_DEFINITION,
4
+ DOCUMENT_DEFINITION,
5
+ CHARACTER_DEFINITION,
6
+ HERO_DEFINITION,
7
+ SERIES_DEFINITION,
8
+ PATCH_NOTE_DEFINITION,
9
+ LOCATION_DEFINITION,
10
+ ITEM_DEFINITION,
11
+ SPECIES_DEFINITION,
12
+ SKILL_DEFINITION
13
+ } from "../core/page-definitions.ts"
14
+
15
+ export interface BuiltinTemplate {
16
+ slug: string
17
+ title: { en: string }
18
+ description: { en: string }
19
+ pageType: string
20
+ version: number
21
+ allowedRoles: string[]
22
+ rolePermissions: {
23
+ whoCanCreate: string[]
24
+ whoCanEdit: string[]
25
+ whoCanReview: string[]
26
+ whoCanView: string[]
27
+ }
28
+ approvalRules: {
29
+ allowSelfApproval: boolean
30
+ minApprovals: number
31
+ }
32
+ defaultProperties: Record<string, any>
33
+ initialBlocks: any[]
34
+ }
35
+
36
+ export function getBuiltinTemplates(): BuiltinTemplate[] {
37
+ return [
38
+ {
39
+ slug: "blog-post",
40
+ title: { en: "Blog Post" },
41
+ description: {
42
+ en: "Standard blog article with category metadata, author attribution, and formatted headings"
43
+ },
44
+ pageType: "blog",
45
+ version: 1,
46
+ allowedRoles: [],
47
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
48
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
49
+ defaultProperties: BLOG_POST_DEFINITION.properties,
50
+ initialBlocks: [
51
+ {
52
+ id: crypto.randomUUID(),
53
+ type: "HeadingBlock",
54
+ props: { level: 2, text: "Introduction" },
55
+ isTemplated: false
56
+ },
57
+ {
58
+ id: crypto.randomUUID(),
59
+ type: "ParagraphBlock",
60
+ props: { content: "Start writing your blog article here..." },
61
+ isTemplated: false
62
+ }
63
+ ]
64
+ },
65
+ {
66
+ slug: "documentation",
67
+ title: { en: "Documentation Guide" },
68
+ description: {
69
+ en: "Technical documentation page with structured sections, callouts, and code blocks"
70
+ },
71
+ pageType: "doc",
72
+ version: 1,
73
+ allowedRoles: [],
74
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
75
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
76
+ defaultProperties: DOCUMENT_DEFINITION.properties,
77
+ initialBlocks: [
78
+ {
79
+ id: crypto.randomUUID(),
80
+ type: "HeadingBlock",
81
+ props: { level: 2, text: "Overview" },
82
+ isTemplated: false
83
+ },
84
+ {
85
+ id: crypto.randomUUID(),
86
+ type: "CalloutBlock",
87
+ props: { variant: "info", content: "Key guide details and prerequisites go here." },
88
+ isTemplated: false
89
+ },
90
+ {
91
+ id: crypto.randomUUID(),
92
+ type: "HeadingBlock",
93
+ props: { level: 2, text: "Usage" },
94
+ isTemplated: false
95
+ }
96
+ ]
97
+ },
98
+ {
99
+ slug: "character-profile",
100
+ title: { en: "Character Profile" },
101
+ description: {
102
+ en: "Franchise character sheet with identity, characteristics, inventory, and backstory lore"
103
+ },
104
+ pageType: "character",
105
+ version: 1,
106
+ allowedRoles: [],
107
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
108
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
109
+ defaultProperties: CHARACTER_DEFINITION.properties,
110
+ initialBlocks: CHARACTER_DEFINITION.initialBlocks ? CHARACTER_DEFINITION.initialBlocks() : []
111
+ },
112
+ {
113
+ slug: "hero-sheet",
114
+ title: { en: "Hero Combat Sheet" },
115
+ description: {
116
+ en: "Hero character with combat stats, progression mechanics, and playstyle lore"
117
+ },
118
+ pageType: "hero",
119
+ version: 1,
120
+ allowedRoles: [],
121
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
122
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
123
+ defaultProperties: HERO_DEFINITION.properties,
124
+ initialBlocks: HERO_DEFINITION.initialBlocks ? HERO_DEFINITION.initialBlocks() : []
125
+ },
126
+ {
127
+ slug: "series-overview",
128
+ title: { en: "Series Overview" },
129
+ description: {
130
+ en: "Media show / series page with genre, season metadata, and episode overviews"
131
+ },
132
+ pageType: "series",
133
+ version: 1,
134
+ allowedRoles: [],
135
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
136
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
137
+ defaultProperties: SERIES_DEFINITION.properties,
138
+ initialBlocks: SERIES_DEFINITION.initialBlocks ? SERIES_DEFINITION.initialBlocks() : []
139
+ },
140
+ {
141
+ slug: "patch-note",
142
+ title: { en: "Release / Patch Note" },
143
+ description: {
144
+ en: "Version changelog, release highlights, balance adjustments, and bug fixes"
145
+ },
146
+ pageType: "patchnote",
147
+ version: 1,
148
+ allowedRoles: [],
149
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
150
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
151
+ defaultProperties: PATCH_NOTE_DEFINITION.properties,
152
+ initialBlocks: [
153
+ {
154
+ id: crypto.randomUUID(),
155
+ type: "HeadingBlock",
156
+ props: { level: 2, text: "Release Highlights" },
157
+ isTemplated: false
158
+ },
159
+ {
160
+ id: crypto.randomUUID(),
161
+ type: "HeadingBlock",
162
+ props: { level: 3, text: "Bug Fixes & Improvements" },
163
+ isTemplated: false
164
+ }
165
+ ]
166
+ },
167
+ {
168
+ slug: "location-guide",
169
+ title: { en: "World Location" },
170
+ description: { en: "Worldbuilding geography, climate conditions, and notable landmarks" },
171
+ pageType: "location",
172
+ version: 1,
173
+ allowedRoles: [],
174
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
175
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
176
+ defaultProperties: LOCATION_DEFINITION.properties,
177
+ initialBlocks: []
178
+ },
179
+ {
180
+ slug: "game-item",
181
+ title: { en: "Item & Equipment" },
182
+ description: { en: "Item rarity, pricing, quest associations, and equipment stats" },
183
+ pageType: "item",
184
+ version: 1,
185
+ allowedRoles: [],
186
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
187
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
188
+ defaultProperties: ITEM_DEFINITION.properties,
189
+ initialBlocks: []
190
+ },
191
+ {
192
+ slug: "species-entry",
193
+ title: { en: "Species Entry" },
194
+ description: { en: "Biological taxonomy, lifespan, and homeworld association" },
195
+ pageType: "species",
196
+ version: 1,
197
+ allowedRoles: [],
198
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
199
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
200
+ defaultProperties: SPECIES_DEFINITION.properties,
201
+ initialBlocks: []
202
+ },
203
+ {
204
+ slug: "skill-entry",
205
+ title: { en: "Skill & Ability" },
206
+ description: { en: "Ability cooldown, mana cost, and damage type mechanics" },
207
+ pageType: "skill",
208
+ version: 1,
209
+ allowedRoles: [],
210
+ rolePermissions: { whoCanCreate: [], whoCanEdit: [], whoCanReview: [], whoCanView: [] },
211
+ approvalRules: { allowSelfApproval: true, minApprovals: 1 },
212
+ defaultProperties: SKILL_DEFINITION.properties,
213
+ initialBlocks: []
214
+ }
215
+ ]
216
+ }
217
+
218
+ /**
219
+ * Ensures built-in page templates exist in the database. If page_templates table is empty,
220
+ * populates it with starter templates.
221
+ */
222
+ export async function ensureDefaultTemplates(
223
+ dbInstance: any,
224
+ templateTable = pageTemplates
225
+ ): Promise<number> {
226
+ if (!dbInstance) return 0
227
+
228
+ try {
229
+ const existing = await dbInstance.select({ id: templateTable.id }).from(templateTable)
230
+ if (existing.length > 0) {
231
+ return existing.length
232
+ }
233
+
234
+ const defaultTemplates = getBuiltinTemplates()
235
+ for (const tmpl of defaultTemplates) {
236
+ try {
237
+ await dbInstance.insert(templateTable).values(tmpl)
238
+ } catch (insertErr) {
239
+ console.warn(`Failed to seed template ${tmpl.slug}:`, insertErr)
240
+ }
241
+ }
242
+
243
+ const seeded = await dbInstance.select().from(templateTable)
244
+ return seeded.length
245
+ } catch (err) {
246
+ console.warn("Error checking or seeding default templates:", err)
247
+ return 0
248
+ }
249
+ }
@@ -58,7 +58,6 @@ export function r2(options: R2StorageOptions = {}): StorageConfig {
58
58
  export async function getR2Bucket(locals?: any, bindingName = "BLOB"): Promise<any> {
59
59
  // 1. Try cloudflare:workers standard export (Astro v6+)
60
60
  try {
61
- // @ts-ignore
62
61
  const cf = await import("cloudflare:workers")
63
62
  if (cf?.env && cf.env[bindingName]) {
64
63
  return cf.env[bindingName]