@beechcms/api 0.4.0-preview.12 → 0.4.0-preview.13

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.
Files changed (85) hide show
  1. package/assets/dashboard/BeechLogo.svg +18 -18
  2. package/assets/dashboard/BeechLogoLIght.svg +48 -48
  3. package/assets/dashboard/assets/index-CewtCjom.css +1 -0
  4. package/assets/dashboard/assets/{index-CTSuGxlX.js → index-FQ6JhvRH.js} +99 -99
  5. package/assets/dashboard/beechLogoDark.svg +48 -48
  6. package/assets/dashboard/index.html +18 -18
  7. package/assets/dashboard/sol.svg +3 -3
  8. package/assets/dashboard/undraw_enter_nwx3.svg +36 -36
  9. package/migrations/0000_v040_base.sql +213 -213
  10. package/package.json +2 -2
  11. package/src/auth/constants.ts +10 -10
  12. package/src/auth/login.ts +91 -91
  13. package/src/auth/refresh.ts +127 -127
  14. package/src/factory.ts +347 -325
  15. package/src/features/content/constants.ts +10 -10
  16. package/src/features/content/handlers/create.ts +153 -163
  17. package/src/features/content/handlers/delete.ts +76 -85
  18. package/src/features/content/handlers/facets.ts +45 -45
  19. package/src/features/content/handlers/get.ts +116 -116
  20. package/src/features/content/handlers/list.ts +88 -88
  21. package/src/features/content/handlers/update.ts +207 -216
  22. package/src/features/content/index.ts +20 -20
  23. package/src/features/draft/draft.handler.ts +272 -272
  24. package/src/features/draft/index.ts +1 -1
  25. package/src/features/email/email.provider.ts +38 -38
  26. package/src/features/email/email.service.ts +80 -80
  27. package/src/features/email/email.types.ts +98 -98
  28. package/src/features/email/index.ts +28 -28
  29. package/src/features/email/providers/resend.ts +63 -63
  30. package/src/features/email/templates/password-changed.ts +59 -59
  31. package/src/features/email/templates/password-reset.ts +64 -64
  32. package/src/features/email/templates/shell.ts +92 -93
  33. package/src/features/notifications/index.ts +1 -1
  34. package/src/features/notifications/notifications.handler.ts +130 -88
  35. package/src/features/password-reset/index.ts +15 -15
  36. package/src/features/password-reset/request.ts +106 -88
  37. package/src/features/password-reset/reset.ts +128 -110
  38. package/src/features/rotate-field/index.ts +1 -1
  39. package/src/features/rotate-field/rotate-field.handler.ts +132 -82
  40. package/src/features/rotate-field/rotate-field.schema.ts +13 -9
  41. package/src/features/schema/schema.handler.ts +16 -16
  42. package/src/features/settings/settings.handler.ts +414 -267
  43. package/src/features/setup/index.ts +96 -59
  44. package/src/features/stats/index.ts +1 -1
  45. package/src/features/stats/stats.handler.ts +458 -395
  46. package/src/index.ts +24 -24
  47. package/src/media-utils.ts +78 -78
  48. package/src/middleware/repository.middleware.ts +24 -18
  49. package/src/middleware/storage.middleware.ts +21 -0
  50. package/src/middleware.ts +67 -67
  51. package/src/public/access-policy.ts +23 -23
  52. package/src/public/api-key-middleware.ts +53 -53
  53. package/src/public/index.ts +12 -12
  54. package/src/public/problem-details.ts +48 -42
  55. package/src/public/public-add.ts +166 -166
  56. package/src/public/public-edit.ts +158 -158
  57. package/src/public/public-errors.ts +15 -15
  58. package/src/public/public-read.ts +216 -216
  59. package/src/public/public-routes.ts +84 -31
  60. package/src/public/query-builder.ts +152 -152
  61. package/src/public/rate-limit-middleware.ts +42 -42
  62. package/src/public/response-builder.ts +26 -26
  63. package/src/public/sanitize.ts +65 -65
  64. package/src/public/slug-utils.ts +14 -14
  65. package/src/search-utils.ts +192 -192
  66. package/src/search.ts +72 -72
  67. package/src/shared/activity-logger.ts +79 -79
  68. package/src/shared/apply-policies.ts +63 -63
  69. package/src/shared/base.repository.d1.ts +28 -28
  70. package/src/shared/content-utils.ts +82 -108
  71. package/src/shared/content.repository.d1.ts +382 -382
  72. package/src/shared/fts-sync.ts +4 -4
  73. package/src/shared/idempotency.repository.d1.ts +56 -45
  74. package/src/shared/media.repository.d1.ts +64 -0
  75. package/src/shared/notification-service.ts +56 -56
  76. package/src/shared/query-utils.ts +137 -137
  77. package/src/shared/storage/factory.ts +40 -0
  78. package/src/shared/storage/r2-binding-bucket.ts +81 -0
  79. package/src/shared/storage/s3-bucket.ts +163 -0
  80. package/src/shared/storage-utils.ts +36 -36
  81. package/src/shared/system-stats.repository.d1.ts +44 -0
  82. package/src/types.ts +46 -41
  83. package/src/upload.ts +179 -331
  84. package/src/widget.ts +349 -349
  85. package/assets/dashboard/assets/index-ye3325L9.css +0 -1
@@ -1,382 +1,382 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import {
3
- ContentRepository,
4
- EntryNotFoundError,
5
- RepositoryError,
6
- SlugConflictError,
7
- Seed,
8
- SelectOptions,
9
- buildSelectQuery,
10
- deserializeFromDb,
11
- serializeForDb,
12
- } from '@beechcms/core'
13
- import { BaseD1Repository } from './base.repository.d1'
14
-
15
- export class D1ContentRepository extends BaseD1Repository implements ContentRepository {
16
- /**
17
- * Helper to deserialize a DB row using the Seed's branch definitions.
18
- */
19
- private rowToData(seed: Seed, row: any): Record<string, any> {
20
- const data: Record<string, any> = {
21
- id: row.id,
22
- slug: row.slug,
23
- status: row.status,
24
- created_at: row.created_at,
25
- updated_at: row.updated_at,
26
- }
27
-
28
- for (const branch of seed.branches) {
29
- if (Object.hasOwn(row, branch.alias)) {
30
- data[branch.alias] = deserializeFromDb(branch, row[branch.alias])
31
- }
32
- }
33
-
34
- return data
35
- }
36
-
37
- async findMany(
38
- seed: Seed,
39
- options: SelectOptions
40
- ): Promise<{ items: Record<string, any>[]; total: number }> {
41
- try {
42
- const { sql, bindings } = buildSelectQuery(seed, options)
43
-
44
- // We need the total count for pagination.
45
- // We build a count query by replacing the SELECT part.
46
- // Note: buildSelectQuery might have joins and where clauses.
47
- const countSql = sql
48
- .replace(/SELECT .* FROM/, 'SELECT COUNT(*) as total FROM')
49
- .replace(/ ORDER BY .*$/, '')
50
- .replace(/ LIMIT \? OFFSET \?$/, '')
51
-
52
- const countBindings = bindings.slice(0, bindings.length - (options.pagination ? 2 : 0))
53
-
54
- const [results, countResult] = await this.db.batch([
55
- this.db.prepare(sql).bind(...bindings),
56
- this.db.prepare(countSql).bind(...countBindings)
57
- ])
58
-
59
- const items = (results.results || []).map((row) => this.rowToData(seed, row))
60
- const total = (countResult.results?.[0] as any)?.total || 0
61
-
62
- return { items, total }
63
- } catch (error) {
64
- throw this.mapError(error, `findMany(${seed.slug})`)
65
- }
66
- }
67
-
68
- async findById(seed: Seed, id: string): Promise<Record<string, any>> {
69
- try {
70
- const table = this.getTableName(seed.slug)
71
- const row = await this.db
72
- .prepare(`SELECT * FROM ${table} WHERE id = ? LIMIT 1`)
73
- .bind(id)
74
- .first()
75
-
76
- if (!row) {
77
- throw new EntryNotFoundError(`Entry ${id} not found in ${seed.slug}`)
78
- }
79
-
80
- return this.rowToData(seed, row)
81
- } catch (error) {
82
- if (error instanceof EntryNotFoundError) throw error
83
- throw this.mapError(error, `findById(${seed.slug}, ${id})`)
84
- }
85
- }
86
-
87
- async findBySlug(seed: Seed, slug: string): Promise<Record<string, any>> {
88
- try {
89
- const table = this.getTableName(seed.slug)
90
- const row = await this.db
91
- .prepare(`SELECT * FROM ${table} WHERE slug = ? LIMIT 1`)
92
- .bind(slug)
93
- .first()
94
-
95
- if (!row) {
96
- throw new EntryNotFoundError(`Entry with slug "${slug}" not found in ${seed.slug}`)
97
- }
98
-
99
- return this.rowToData(seed, row)
100
- } catch (error) {
101
- if (error instanceof EntryNotFoundError) throw error
102
- throw this.mapError(error, `findBySlug(${seed.slug}, ${slug})`)
103
- }
104
- }
105
-
106
- async getFacets(seed: Seed): Promise<{
107
- statuses: Record<string, number>
108
- tagsByColumn: Record<string, string[]>
109
- }> {
110
- try {
111
- const table = this.getTableName(seed.slug)
112
-
113
- // Status counts
114
- const statusResults = await this.db
115
- .prepare(`SELECT status, COUNT(*) as count FROM ${table} GROUP BY status`)
116
- .all()
117
-
118
- const statuses: Record<string, number> = {}
119
- for (const row of statusResults.results || []) {
120
- statuses[row.status as string] = row.count as number
121
- }
122
-
123
- // Tags facets (for branches of type 'tags')
124
- const tagsByColumn: Record<string, string[]> = {}
125
- const tagBranches = seed.branches.filter(b => b.type === 'tags')
126
-
127
- for (const branch of tagBranches) {
128
- // SQLite json_each for tags stored as JSON arrays
129
- const tagResults = await this.db
130
- .prepare(`SELECT DISTINCT value FROM ${table}, json_each(${table}.${branch.alias}) WHERE value IS NOT NULL`)
131
- .all()
132
- tagsByColumn[branch.alias] = (tagResults.results || []).map(r => r.value as string)
133
- }
134
-
135
- return { statuses, tagsByColumn }
136
- } catch (error) {
137
- throw this.mapError(error, `getFacets(${seed.slug})`)
138
- }
139
- }
140
-
141
- async existsSlug(seed: Seed, slug: string, excludeId?: string): Promise<boolean> {
142
- try {
143
- const table = this.getTableName(seed.slug)
144
- let sql = `SELECT 1 FROM ${table} WHERE slug = ?`
145
- const bindings: any[] = [slug]
146
-
147
- if (excludeId) {
148
- sql += ` AND id != ?`
149
- bindings.push(excludeId)
150
- }
151
-
152
- const row = await this.db.prepare(sql).bind(...bindings).first()
153
- return row !== null
154
- } catch (error) {
155
- throw this.mapError(error, `existsSlug(${seed.slug}, ${slug})`)
156
- }
157
- }
158
-
159
- async create(
160
- seed: Seed,
161
- id: string,
162
- slug: string,
163
- status: string,
164
- data: Record<string, any>
165
- ): Promise<void> {
166
- try {
167
- if (await this.existsSlug(seed, slug)) {
168
- throw new SlugConflictError(`Slug "${slug}" already exists for ${seed.slug}`)
169
- }
170
-
171
- const table = this.getTableName(seed.slug)
172
- const cols = ['id', 'slug', 'status']
173
- const placeholders = ['?', '?', '?']
174
- const bindings: any[] = [id, slug, status]
175
-
176
- for (const branch of seed.branches) {
177
- if (Object.hasOwn(data, branch.alias)) {
178
- cols.push(branch.alias)
179
- placeholders.push('?')
180
- bindings.push(serializeForDb(branch, data[branch.alias]))
181
- }
182
- }
183
-
184
- const sql = `INSERT INTO ${table} (${cols.join(', ')}) VALUES (${placeholders.join(', ')})`
185
- await this.db.prepare(sql).bind(...bindings).run()
186
- } catch (error) {
187
- if (error instanceof SlugConflictError) throw error
188
- throw this.mapError(error, `create(${seed.slug})`)
189
- }
190
- }
191
-
192
- async update(
193
- seed: Seed,
194
- id: string,
195
- data: Record<string, any>,
196
- status?: string
197
- ): Promise<void> {
198
- try {
199
- const table = this.getTableName(seed.slug)
200
- const setParts: string[] = []
201
- const bindings: any[] = []
202
-
203
- if (status) {
204
- setParts.push('status = ?')
205
- bindings.push(status)
206
- }
207
-
208
- for (const branch of seed.branches) {
209
- if (Object.hasOwn(data, branch.alias)) {
210
- setParts.push(`${branch.alias} = ?`)
211
- bindings.push(serializeForDb(branch, data[branch.alias]))
212
- }
213
- }
214
-
215
- if (setParts.length === 0) return
216
-
217
- setParts.push('updated_at = (unixepoch())')
218
-
219
- const sql = `UPDATE ${table} SET ${setParts.join(', ')} WHERE id = ?`
220
- bindings.push(id)
221
-
222
- const result = await this.db.prepare(sql).bind(...bindings).run()
223
- if (result.meta.changes === 0) {
224
- throw new EntryNotFoundError(`Entry ${id} not found in ${seed.slug}`)
225
- }
226
- } catch (error) {
227
- if (error instanceof EntryNotFoundError) throw error
228
- throw this.mapError(error, `update(${seed.slug}, ${id})`)
229
- }
230
- }
231
-
232
- async delete(seed: Seed, id: string): Promise<{ row: Record<string, any> }> {
233
- try {
234
- const table = this.getTableName(seed.slug)
235
-
236
- // We need to return the row for R2 cleanup
237
- const row = await this.db
238
- .prepare(`SELECT * FROM ${table} WHERE id = ?`)
239
- .bind(id)
240
- .first()
241
-
242
- if (!row) {
243
- throw new EntryNotFoundError(`Entry ${id} not found in ${seed.slug}`)
244
- }
245
-
246
- await this.db.prepare(`DELETE FROM ${table} WHERE id = ?`).bind(id).run()
247
-
248
- return { row: this.rowToData(seed, row) }
249
- } catch (error) {
250
- if (error instanceof EntryNotFoundError) throw error
251
- throw this.mapError(error, `delete(${seed.slug}, ${id})`)
252
- }
253
- }
254
-
255
- async saveDraft(seed: Seed, entryId: string, data: Record<string, any>): Promise<void> {
256
- try {
257
- if (!seed.allowDrafts) {
258
- throw new RepositoryError(`Drafts not allowed for ${seed.slug}`)
259
- }
260
-
261
- const table = this.getTableName(seed.slug, true)
262
- const cols = ['entry_id']
263
- const placeholders = ['?']
264
- const bindings: any[] = [entryId]
265
- const updateParts: string[] = []
266
-
267
- for (const branch of seed.branches) {
268
- if (Object.hasOwn(data, branch.alias)) {
269
- const val = serializeForDb(branch, data[branch.alias])
270
- cols.push(branch.alias)
271
- placeholders.push('?')
272
- bindings.push(val)
273
- updateParts.push(`${branch.alias} = EXCLUDED.${branch.alias}`)
274
- }
275
- }
276
-
277
- updateParts.push('updated_at = (unixepoch())')
278
-
279
- const sql = `
280
- INSERT INTO ${table} (${cols.join(', ')})
281
- VALUES (${placeholders.join(', ')})
282
- ON CONFLICT(entry_id) DO UPDATE SET ${updateParts.join(', ')}
283
- `
284
- await this.db.prepare(sql).bind(...bindings).run()
285
- } catch (error) {
286
- throw this.mapError(error, `saveDraft(${seed.slug}, ${entryId})`)
287
- }
288
- }
289
-
290
- async getDraft(seed: Seed, entryId: string): Promise<Record<string, any> | null> {
291
- try {
292
- if (!seed.allowDrafts) return null
293
-
294
- const table = this.getTableName(seed.slug, true)
295
- const row = await this.db
296
- .prepare(`SELECT * FROM ${table} WHERE entry_id = ?`)
297
- .bind(entryId)
298
- .first()
299
-
300
- if (!row) return null
301
-
302
- // Filter out nulls from draft row (only include provided fields)
303
- const data: Record<string, any> = {}
304
- for (const branch of seed.branches) {
305
- if (row[branch.alias] !== null) {
306
- data[branch.alias] = deserializeFromDb(branch, row[branch.alias])
307
- }
308
- }
309
-
310
- return data
311
- } catch (error) {
312
- throw this.mapError(error, `getDraft(${seed.slug}, ${entryId})`)
313
- }
314
- }
315
-
316
- async hasDraft(seed: Seed, entryId: string): Promise<boolean> {
317
- try {
318
- if (!seed.allowDrafts) return false
319
- const table = this.getTableName(seed.slug, true)
320
- const row = await this.db
321
- .prepare(`SELECT 1 FROM ${table} WHERE entry_id = ? LIMIT 1`)
322
- .bind(entryId)
323
- .first()
324
- return row !== null
325
- } catch (error) {
326
- throw this.mapError(error, `hasDraft(${seed.slug}, ${entryId})`)
327
- }
328
- }
329
-
330
- async publishDraft(seed: Seed, entryId: string): Promise<void> {
331
- try {
332
- if (!seed.allowDrafts) return
333
-
334
- const draftTable = this.getTableName(seed.slug, true)
335
- const liveTable = this.getTableName(seed.slug)
336
-
337
- const draftRow = await this.db
338
- .prepare(`SELECT * FROM ${draftTable} WHERE entry_id = ?`)
339
- .bind(entryId)
340
- .first()
341
-
342
- if (!draftRow) {
343
- throw new EntryNotFoundError(`No draft found for ${entryId} in ${seed.slug}`)
344
- }
345
-
346
- // Build UPDATE for live table
347
- const setParts: string[] = []
348
- const bindings: any[] = []
349
-
350
- for (const branch of seed.branches) {
351
- if (draftRow[branch.alias] !== null) {
352
- setParts.push(`${branch.alias} = ?`)
353
- bindings.push(draftRow[branch.alias])
354
- }
355
- }
356
-
357
- setParts.push('updated_at = (unixepoch())')
358
-
359
- const updateSql = `UPDATE ${liveTable} SET ${setParts.join(', ')} WHERE id = ?`
360
- bindings.push(entryId)
361
-
362
- // Atomic batch
363
- await this.db.batch([
364
- this.db.prepare(updateSql).bind(...bindings),
365
- this.db.prepare(`DELETE FROM ${draftTable} WHERE entry_id = ?`).bind(entryId)
366
- ])
367
- } catch (error) {
368
- if (error instanceof EntryNotFoundError) throw error
369
- throw this.mapError(error, `publishDraft(${seed.slug}, ${entryId})`)
370
- }
371
- }
372
-
373
- async deleteDraft(seed: Seed, entryId: string): Promise<void> {
374
- try {
375
- if (!seed.allowDrafts) return
376
- const table = this.getTableName(seed.slug, true)
377
- await this.db.prepare(`DELETE FROM ${table} WHERE entry_id = ?`).bind(entryId).run()
378
- } catch (error) {
379
- throw this.mapError(error, `deleteDraft(${seed.slug}, ${entryId})`)
380
- }
381
- }
382
- }
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ import {
3
+ ContentRepository,
4
+ EntryNotFoundError,
5
+ RepositoryError,
6
+ SlugConflictError,
7
+ Seed,
8
+ SelectOptions,
9
+ buildSelectQuery,
10
+ deserializeFromDb,
11
+ serializeForDb,
12
+ } from '@beechcms/core'
13
+ import { BaseD1Repository } from './base.repository.d1'
14
+
15
+ export class D1ContentRepository extends BaseD1Repository implements ContentRepository {
16
+ /**
17
+ * Helper to deserialize a DB row using the Seed's branch definitions.
18
+ */
19
+ private rowToData(seed: Seed, row: any): Record<string, any> {
20
+ const data: Record<string, any> = {
21
+ id: row.id,
22
+ slug: row.slug,
23
+ status: row.status,
24
+ created_at: row.created_at,
25
+ updated_at: row.updated_at,
26
+ }
27
+
28
+ for (const branch of seed.branches) {
29
+ if (Object.hasOwn(row, branch.alias)) {
30
+ data[branch.alias] = deserializeFromDb(branch, row[branch.alias])
31
+ }
32
+ }
33
+
34
+ return data
35
+ }
36
+
37
+ async findMany(
38
+ seed: Seed,
39
+ options: SelectOptions
40
+ ): Promise<{ items: Record<string, any>[]; total: number }> {
41
+ try {
42
+ const { sql, bindings } = buildSelectQuery(seed, options)
43
+
44
+ // We need the total count for pagination.
45
+ // We build a count query by replacing the SELECT part.
46
+ // Note: buildSelectQuery might have joins and where clauses.
47
+ const countSql = sql
48
+ .replace(/SELECT .* FROM/, 'SELECT COUNT(*) as total FROM')
49
+ .replace(/ ORDER BY .*$/, '')
50
+ .replace(/ LIMIT \? OFFSET \?$/, '')
51
+
52
+ const countBindings = bindings.slice(0, bindings.length - (options.pagination ? 2 : 0))
53
+
54
+ const [batchResults, totalCountResult] = await this.database.batch([
55
+ this.database.prepare(sql).bind(...bindings),
56
+ this.database.prepare(countSql).bind(...countBindings)
57
+ ])
58
+
59
+ const contentEntries = (batchResults.results || []).map((entryRow) => this.rowToData(seed, entryRow))
60
+ const totalEntriesCount = (totalCountResult.results?.[0] as any)?.total || 0
61
+
62
+ return { items: contentEntries, total: totalEntriesCount }
63
+ } catch (error) {
64
+ throw this.mapError(error, `findMany(${seed.slug})`)
65
+ }
66
+ }
67
+
68
+ async findById(seed: Seed, id: string): Promise<Record<string, any>> {
69
+ try {
70
+ const tableName = this.getTableName(seed.slug)
71
+ const entryRow = await this.database
72
+ .prepare(`SELECT * FROM ${tableName} WHERE id = ? LIMIT 1`)
73
+ .bind(id)
74
+ .first()
75
+
76
+ if (!entryRow) {
77
+ throw new EntryNotFoundError(`Entry ${id} not found in ${seed.slug}`)
78
+ }
79
+
80
+ return this.rowToData(seed, entryRow)
81
+ } catch (error) {
82
+ if (error instanceof EntryNotFoundError) throw error
83
+ throw this.mapError(error, `findById(${seed.slug}, ${id})`)
84
+ }
85
+ }
86
+
87
+ async findBySlug(seed: Seed, slug: string): Promise<Record<string, any>> {
88
+ try {
89
+ const tableName = this.getTableName(seed.slug)
90
+ const entryRow = await this.database
91
+ .prepare(`SELECT * FROM ${tableName} WHERE slug = ? LIMIT 1`)
92
+ .bind(slug)
93
+ .first()
94
+
95
+ if (!entryRow) {
96
+ throw new EntryNotFoundError(`Entry with slug "${slug}" not found in ${seed.slug}`)
97
+ }
98
+
99
+ return this.rowToData(seed, entryRow)
100
+ } catch (error) {
101
+ if (error instanceof EntryNotFoundError) throw error
102
+ throw this.mapError(error, `findBySlug(${seed.slug}, ${slug})`)
103
+ }
104
+ }
105
+
106
+ async getFacets(seed: Seed): Promise<{
107
+ statuses: Record<string, number>
108
+ tagsByColumn: Record<string, string[]>
109
+ }> {
110
+ try {
111
+ const tableName = this.getTableName(seed.slug)
112
+
113
+ // Retrieve count per status
114
+ const statusResults = await this.database
115
+ .prepare(`SELECT status, COUNT(*) as count FROM ${tableName} GROUP BY status`)
116
+ .all()
117
+
118
+ const statusesCount: Record<string, number> = {}
119
+ for (const statusRow of statusResults.results || []) {
120
+ statusesCount[statusRow.status as string] = statusRow.count as number
121
+ }
122
+
123
+ // Collect unique tags for branches of type 'tags'
124
+ const tagsByColumn: Record<string, string[]> = {}
125
+ const tagBranches = seed.branches.filter(branch => branch.type === 'tags')
126
+
127
+ for (const branch of tagBranches) {
128
+ // Use SQLite json_each to expand tags stored as JSON arrays
129
+ const tagResults = await this.database
130
+ .prepare(`SELECT DISTINCT value FROM ${tableName}, json_each(${tableName}.${branch.alias}) WHERE value IS NOT NULL`)
131
+ .all()
132
+ tagsByColumn[branch.alias] = (tagResults.results || []).map(row => row.value as string)
133
+ }
134
+
135
+ return { statuses: statusesCount, tagsByColumn }
136
+ } catch (error) {
137
+ throw this.mapError(error, `getFacets(${seed.slug})`)
138
+ }
139
+ }
140
+
141
+ async existsSlug(seed: Seed, slug: string, excludeId?: string): Promise<boolean> {
142
+ try {
143
+ const tableName = this.getTableName(seed.slug)
144
+ let sql = `SELECT 1 FROM ${tableName} WHERE slug = ?`
145
+ const queryBindings: any[] = [slug]
146
+
147
+ if (excludeId) {
148
+ sql += ` AND id != ?`
149
+ queryBindings.push(excludeId)
150
+ }
151
+
152
+ const entryExistsResult = await this.database.prepare(sql).bind(...queryBindings).first()
153
+ return entryExistsResult !== null
154
+ } catch (error) {
155
+ throw this.mapError(error, `existsSlug(${seed.slug}, ${slug})`)
156
+ }
157
+ }
158
+
159
+ async create(
160
+ seed: Seed,
161
+ id: string,
162
+ slug: string,
163
+ status: string,
164
+ data: Record<string, any>
165
+ ): Promise<void> {
166
+ try {
167
+ if (await this.existsSlug(seed, slug)) {
168
+ throw new SlugConflictError(`Slug "${slug}" already exists for ${seed.slug}`)
169
+ }
170
+
171
+ const tableName = this.getTableName(seed.slug)
172
+ const columnNames = ['id', 'slug', 'status']
173
+ const placeholders = ['?', '?', '?']
174
+ const queryBindings: any[] = [id, slug, status]
175
+
176
+ for (const branch of seed.branches) {
177
+ if (Object.hasOwn(data, branch.alias)) {
178
+ columnNames.push(branch.alias)
179
+ placeholders.push('?')
180
+ queryBindings.push(serializeForDb(branch, data[branch.alias]))
181
+ }
182
+ }
183
+
184
+ const sql = `INSERT INTO ${tableName} (${columnNames.join(', ')}) VALUES (${placeholders.join(', ')})`
185
+ await this.database.prepare(sql).bind(...queryBindings).run()
186
+ } catch (error) {
187
+ if (error instanceof SlugConflictError) throw error
188
+ throw this.mapError(error, `create(${seed.slug})`)
189
+ }
190
+ }
191
+
192
+ async update(
193
+ seed: Seed,
194
+ id: string,
195
+ data: Record<string, any>,
196
+ status?: string
197
+ ): Promise<void> {
198
+ try {
199
+ const tableName = this.getTableName(seed.slug)
200
+ const updateClauses: string[] = []
201
+ const queryBindings: any[] = []
202
+
203
+ if (status) {
204
+ updateClauses.push('status = ?')
205
+ queryBindings.push(status)
206
+ }
207
+
208
+ for (const branch of seed.branches) {
209
+ if (Object.hasOwn(data, branch.alias)) {
210
+ updateClauses.push(`${branch.alias} = ?`)
211
+ queryBindings.push(serializeForDb(branch, data[branch.alias]))
212
+ }
213
+ }
214
+
215
+ if (updateClauses.length === 0) return
216
+
217
+ updateClauses.push('updated_at = (unixepoch())')
218
+
219
+ const sql = `UPDATE ${tableName} SET ${updateClauses.join(', ')} WHERE id = ?`
220
+ queryBindings.push(id)
221
+
222
+ const updateResult = await this.database.prepare(sql).bind(...queryBindings).run()
223
+ if (updateResult.meta.changes === 0) {
224
+ throw new EntryNotFoundError(`Entry ${id} not found in ${seed.slug}`)
225
+ }
226
+ } catch (error) {
227
+ if (error instanceof EntryNotFoundError) throw error
228
+ throw this.mapError(error, `update(${seed.slug}, ${id})`)
229
+ }
230
+ }
231
+
232
+ async delete(seed: Seed, id: string): Promise<{ row: Record<string, any> }> {
233
+ try {
234
+ const tableName = this.getTableName(seed.slug)
235
+
236
+ // Retrieve the row before deletion to allow for potential cleanup (e.g., media files in R2)
237
+ const entryRow = await this.database
238
+ .prepare(`SELECT * FROM ${tableName} WHERE id = ?`)
239
+ .bind(id)
240
+ .first()
241
+
242
+ if (!entryRow) {
243
+ throw new EntryNotFoundError(`Entry ${id} not found in ${seed.slug}`)
244
+ }
245
+
246
+ await this.database.prepare(`DELETE FROM ${tableName} WHERE id = ?`).bind(id).run()
247
+
248
+ return { row: this.rowToData(seed, entryRow) }
249
+ } catch (error) {
250
+ if (error instanceof EntryNotFoundError) throw error
251
+ throw this.mapError(error, `delete(${seed.slug}, ${id})`)
252
+ }
253
+ }
254
+
255
+ async saveDraft(seed: Seed, entryId: string, data: Record<string, any>): Promise<void> {
256
+ try {
257
+ if (!seed.allowDrafts) {
258
+ throw new RepositoryError(`Drafts not allowed for ${seed.slug}`)
259
+ }
260
+
261
+ const draftTableName = this.getTableName(seed.slug, true)
262
+ const columnNames = ['entry_id']
263
+ const placeholders = ['?']
264
+ const queryBindings: any[] = [entryId]
265
+ const updateClauses: string[] = []
266
+
267
+ for (const branch of seed.branches) {
268
+ if (Object.hasOwn(data, branch.alias)) {
269
+ const serializedValue = serializeForDb(branch, data[branch.alias])
270
+ columnNames.push(branch.alias)
271
+ placeholders.push('?')
272
+ queryBindings.push(serializedValue)
273
+ updateClauses.push(`${branch.alias} = EXCLUDED.${branch.alias}`)
274
+ }
275
+ }
276
+
277
+ updateClauses.push('updated_at = (unixepoch())')
278
+
279
+ const sql = `
280
+ INSERT INTO ${draftTableName} (${columnNames.join(', ')})
281
+ VALUES (${placeholders.join(', ')})
282
+ ON CONFLICT(entry_id) DO UPDATE SET ${updateClauses.join(', ')}
283
+ `
284
+ await this.database.prepare(sql).bind(...queryBindings).run()
285
+ } catch (error) {
286
+ throw this.mapError(error, `saveDraft(${seed.slug}, ${entryId})`)
287
+ }
288
+ }
289
+
290
+ async getDraft(seed: Seed, entryId: string): Promise<Record<string, any> | null> {
291
+ try {
292
+ if (!seed.allowDrafts) return null
293
+
294
+ const draftTableName = this.getTableName(seed.slug, true)
295
+ const draftRow = await this.database
296
+ .prepare(`SELECT * FROM ${draftTableName} WHERE entry_id = ?`)
297
+ .bind(entryId)
298
+ .first()
299
+
300
+ if (!draftRow) return null
301
+
302
+ // Filter out nulls from draft row (only include explicitly provided fields)
303
+ const draftData: Record<string, any> = {}
304
+ for (const branch of seed.branches) {
305
+ if (draftRow[branch.alias] !== null) {
306
+ draftData[branch.alias] = deserializeFromDb(branch, draftRow[branch.alias])
307
+ }
308
+ }
309
+
310
+ return draftData
311
+ } catch (error) {
312
+ throw this.mapError(error, `getDraft(${seed.slug}, ${entryId})`)
313
+ }
314
+ }
315
+
316
+ async hasDraft(seed: Seed, entryId: string): Promise<boolean> {
317
+ try {
318
+ if (!seed.allowDrafts) return false
319
+ const draftTableName = this.getTableName(seed.slug, true)
320
+ const draftExistsResult = await this.database
321
+ .prepare(`SELECT 1 FROM ${draftTableName} WHERE entry_id = ? LIMIT 1`)
322
+ .bind(entryId)
323
+ .first()
324
+ return draftExistsResult !== null
325
+ } catch (error) {
326
+ throw this.mapError(error, `hasDraft(${seed.slug}, ${entryId})`)
327
+ }
328
+ }
329
+
330
+ async publishDraft(seed: Seed, entryId: string): Promise<void> {
331
+ try {
332
+ if (!seed.allowDrafts) return
333
+
334
+ const draftTableName = this.getTableName(seed.slug, true)
335
+ const liveTableName = this.getTableName(seed.slug)
336
+
337
+ const draftRow = await this.database
338
+ .prepare(`SELECT * FROM ${draftTableName} WHERE entry_id = ?`)
339
+ .bind(entryId)
340
+ .first()
341
+
342
+ if (!draftRow) {
343
+ throw new EntryNotFoundError(`No draft found for ${entryId} in ${seed.slug}`)
344
+ }
345
+
346
+ // Build UPDATE clause for the live table using data from the draft
347
+ const updateClauses: string[] = []
348
+ const queryBindings: any[] = []
349
+
350
+ for (const branch of seed.branches) {
351
+ if (draftRow[branch.alias] !== null) {
352
+ updateClauses.push(`${branch.alias} = ?`)
353
+ queryBindings.push(draftRow[branch.alias])
354
+ }
355
+ }
356
+
357
+ updateClauses.push('updated_at = (unixepoch())')
358
+
359
+ const updateSql = `UPDATE ${liveTableName} SET ${updateClauses.join(', ')} WHERE id = ?`
360
+ queryBindings.push(entryId)
361
+
362
+ // Execute batch to atomically update live table and delete draft
363
+ await this.database.batch([
364
+ this.database.prepare(updateSql).bind(...queryBindings),
365
+ this.database.prepare(`DELETE FROM ${draftTableName} WHERE entry_id = ?`).bind(entryId)
366
+ ])
367
+ } catch (error) {
368
+ if (error instanceof EntryNotFoundError) throw error
369
+ throw this.mapError(error, `publishDraft(${seed.slug}, ${entryId})`)
370
+ }
371
+ }
372
+
373
+ async deleteDraft(seed: Seed, entryId: string): Promise<void> {
374
+ try {
375
+ if (!seed.allowDrafts) return
376
+ const draftTableName = this.getTableName(seed.slug, true)
377
+ await this.database.prepare(`DELETE FROM ${draftTableName} WHERE entry_id = ?`).bind(entryId).run()
378
+ } catch (error) {
379
+ throw this.mapError(error, `deleteDraft(${seed.slug}, ${entryId})`)
380
+ }
381
+ }
382
+ }