@plakboek/db 0.3.0 → 0.4.0

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 (3) hide show
  1. package/README.md +4 -1
  2. package/dist/index.js +202 -13
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -72,7 +72,10 @@ tables (`"user"`, `session`, `account`, `verification`, `two_factor`) and
72
72
  the append-only `audit_log`. `0002_content_engine` follows it, creating the
73
73
  content engine tables (`content_types`, `content_type_fields`,
74
74
  `content_entries`, `entry_revisions` and their history, seed, reference and
75
- settings tables) used by `@plakboek/content`. See
75
+ settings tables) used by `@plakboek/content`. `0003_page_block_engine`
76
+ creates the page and block-tree tables (`pages`, `page_blocks`,
77
+ `block_revisions`, `page_publications`, `page_url_history`,
78
+ `page_engine_settings`) used by `@plakboek/pages`. See
76
79
  [`MIGRATIONS.md`](./MIGRATIONS.md) for how later migrations are added, the
77
80
  transactional-vs-existence-guarded-statement decision, and what each thrown
78
81
  error class means.
package/dist/index.js CHANGED
@@ -143,15 +143,17 @@ async function withMigrationLock(client, fn, options) {
143
143
  *
144
144
  * `0001_auth_core` is the first shipped entry: the better-auth core tables
145
145
  * and the audit log. `0002_content_engine` is the second: the content-type,
146
- * field and entry tables `@plakboek/content` builds on. The runner's
147
- * failure modes (ordering, checksums, partial application, locking) are
148
- * exercised through fixture registries in this package's own tests
149
- * (tests/fixtures/migrations.ts).
146
+ * field and entry tables `@plakboek/content` builds on. `0003_page_block_engine`
147
+ * is the third: the page and block-tree tables `@plakboek/pages` builds on.
148
+ * The runner's failure modes (ordering, checksums, partial application,
149
+ * locking) are exercised through fixture registries in this package's own
150
+ * tests (tests/fixtures/migrations.ts).
150
151
  */
151
- const MIGRATIONS = Object.freeze([{
152
- name: "0001_auth_core",
153
- transactional: true,
154
- sql: `
152
+ const MIGRATIONS = Object.freeze([
153
+ {
154
+ name: "0001_auth_core",
155
+ transactional: true,
156
+ sql: `
155
157
  CREATE TABLE IF NOT EXISTS "user" (
156
158
  "id" text PRIMARY KEY NOT NULL,
157
159
  "name" text NOT NULL,
@@ -257,10 +259,11 @@ CREATE INDEX IF NOT EXISTS "audit_log_actor_idx"
257
259
  CREATE INDEX IF NOT EXISTS "audit_log_created_at_idx"
258
260
  ON "audit_log" USING btree ("created_at");
259
261
  `
260
- }, {
261
- name: "0002_content_engine",
262
- transactional: true,
263
- sql: `
262
+ },
263
+ {
264
+ name: "0002_content_engine",
265
+ transactional: true,
266
+ sql: `
264
267
  CREATE TABLE IF NOT EXISTS "content_types" (
265
268
  "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
266
269
  "key" text NOT NULL,
@@ -504,7 +507,193 @@ CREATE INDEX IF NOT EXISTS "content_entry_references_target_idx"
504
507
  CREATE INDEX IF NOT EXISTS "content_field_key_history_field_idx"
505
508
  ON "content_field_key_history" USING btree ("field_id", "id");
506
509
  `
507
- }]);
510
+ },
511
+ {
512
+ name: "0003_page_block_engine",
513
+ transactional: true,
514
+ sql: `
515
+ CREATE TABLE IF NOT EXISTS "pages" (
516
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
517
+ "translation_group" uuid NOT NULL,
518
+ "locale" text NOT NULL,
519
+ "parent_page_id" uuid REFERENCES "pages" ("id") ON DELETE RESTRICT,
520
+ "slug" text NOT NULL,
521
+ "slug_source" text NOT NULL DEFAULT 'generated',
522
+ "path" text NOT NULL,
523
+ "resolved_path" text,
524
+ "title" text NOT NULL,
525
+ "status" text NOT NULL DEFAULT 'draft',
526
+ "seo" jsonb,
527
+ "version" integer NOT NULL DEFAULT 1,
528
+ "live_publication_id" uuid,
529
+ "locked_by" text,
530
+ "locked_at" timestamp with time zone,
531
+ "created_by" text,
532
+ "updated_by" text,
533
+ "created_at" timestamp with time zone NOT NULL,
534
+ "updated_at" timestamp with time zone NOT NULL,
535
+ "published_at" timestamp with time zone,
536
+ "first_published_at" timestamp with time zone,
537
+ "scheduled_at" timestamp with time zone,
538
+ "trashed_at" timestamp with time zone,
539
+ CONSTRAINT "pages_locked_by_user_id_fk" FOREIGN KEY ("locked_by")
540
+ REFERENCES "user" ("id") ON DELETE SET NULL,
541
+ CONSTRAINT "pages_created_by_user_id_fk" FOREIGN KEY ("created_by")
542
+ REFERENCES "user" ("id") ON DELETE SET NULL,
543
+ CONSTRAINT "pages_updated_by_user_id_fk" FOREIGN KEY ("updated_by")
544
+ REFERENCES "user" ("id") ON DELETE SET NULL,
545
+ CONSTRAINT "pages_locale_path_unique" UNIQUE ("locale", "path"),
546
+ CONSTRAINT "pages_group_locale_unique" UNIQUE ("translation_group", "locale"),
547
+ CONSTRAINT "pages_status_check"
548
+ CHECK ("status" IN ('draft', 'published', 'scheduled', 'trashed')),
549
+ CONSTRAINT "pages_slug_source_check"
550
+ CHECK ("slug_source" IN ('generated', 'manual')),
551
+ CONSTRAINT "pages_scheduled_at_check"
552
+ CHECK ("status" <> 'scheduled' OR "scheduled_at" IS NOT NULL),
553
+ CONSTRAINT "pages_resolved_path_check"
554
+ CHECK ("resolved_path" IS NULL OR "status" = 'published'),
555
+ CONSTRAINT "pages_lock_pair_check"
556
+ CHECK (("locked_by" IS NULL) = ("locked_at" IS NULL)),
557
+ CONSTRAINT "pages_path_check"
558
+ CHECK ("path" <> '' AND "path" NOT LIKE '/%' AND "path" NOT LIKE '%/')
559
+ );
560
+
561
+ CREATE TABLE IF NOT EXISTS "page_blocks" (
562
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
563
+ "owner_type" text NOT NULL,
564
+ "owner_id" uuid NOT NULL,
565
+ "locale" text NOT NULL,
566
+ "parent_block_id" uuid REFERENCES "page_blocks" ("id") ON DELETE CASCADE,
567
+ "block_type" text NOT NULL,
568
+ "props" jsonb NOT NULL DEFAULT '{}'::jsonb,
569
+ "schema_version" integer NOT NULL,
570
+ "depth" integer NOT NULL,
571
+ "sort_order" integer NOT NULL,
572
+ "version" integer NOT NULL DEFAULT 1,
573
+ "created_by" text,
574
+ "updated_by" text,
575
+ "created_at" timestamp with time zone NOT NULL,
576
+ "updated_at" timestamp with time zone NOT NULL,
577
+ CONSTRAINT "page_blocks_created_by_user_id_fk" FOREIGN KEY ("created_by")
578
+ REFERENCES "user" ("id") ON DELETE SET NULL,
579
+ CONSTRAINT "page_blocks_updated_by_user_id_fk" FOREIGN KEY ("updated_by")
580
+ REFERENCES "user" ("id") ON DELETE SET NULL,
581
+ CONSTRAINT "page_blocks_owner_type_check" CHECK ("owner_type" IN ('page')),
582
+ CONSTRAINT "page_blocks_props_object_check"
583
+ CHECK (jsonb_typeof("props") = 'object'),
584
+ CONSTRAINT "page_blocks_schema_version_check" CHECK ("schema_version" >= 1),
585
+ CONSTRAINT "page_blocks_depth_check" CHECK ("depth" >= 0)
586
+ );
587
+
588
+ CREATE TABLE IF NOT EXISTS "block_revisions" (
589
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
590
+ "block_id" uuid REFERENCES "page_blocks" ("id") ON DELETE SET NULL,
591
+ "owner_type" text NOT NULL,
592
+ "owner_id" uuid NOT NULL,
593
+ "locale" text NOT NULL,
594
+ "revision_batch_id" uuid NOT NULL,
595
+ "change_type" text NOT NULL,
596
+ "kind" text NOT NULL,
597
+ "block_type" text NOT NULL,
598
+ "parent_block_id" uuid,
599
+ "sort_order" integer NOT NULL,
600
+ "depth" integer NOT NULL,
601
+ "props" jsonb NOT NULL,
602
+ "schema_version" integer NOT NULL,
603
+ "author_id" text,
604
+ "created_at" timestamp with time zone NOT NULL,
605
+ CONSTRAINT "block_revisions_author_id_user_id_fk" FOREIGN KEY ("author_id")
606
+ REFERENCES "user" ("id") ON DELETE SET NULL,
607
+ CONSTRAINT "block_revisions_change_type_check"
608
+ CHECK ("change_type" IN ('create', 'update', 'move', 'delete')),
609
+ CONSTRAINT "block_revisions_kind_check" CHECK ("kind" IN ('save', 'publish')),
610
+ CONSTRAINT "block_revisions_props_object_check"
611
+ CHECK (jsonb_typeof("props") = 'object')
612
+ );
613
+
614
+ CREATE TABLE IF NOT EXISTS "page_publications" (
615
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
616
+ "page_id" uuid NOT NULL,
617
+ "locale" text NOT NULL,
618
+ "is_draft" boolean NOT NULL DEFAULT false,
619
+ "snapshot" jsonb NOT NULL,
620
+ "revision_manifest" jsonb NOT NULL,
621
+ "manifest_hash" text NOT NULL,
622
+ "published_by" text,
623
+ "published_at" timestamp with time zone NOT NULL,
624
+ CONSTRAINT "page_publications_page_id_fk" FOREIGN KEY ("page_id")
625
+ REFERENCES "pages" ("id") ON DELETE CASCADE,
626
+ CONSTRAINT "page_publications_published_by_user_id_fk" FOREIGN KEY ("published_by")
627
+ REFERENCES "user" ("id") ON DELETE SET NULL,
628
+ CONSTRAINT "page_publications_snapshot_object_check"
629
+ CHECK (jsonb_typeof("snapshot") = 'object'),
630
+ CONSTRAINT "page_publications_manifest_object_check"
631
+ CHECK (jsonb_typeof("revision_manifest") = 'object')
632
+ );
633
+
634
+ DO $$
635
+ BEGIN
636
+ IF NOT EXISTS (
637
+ SELECT 1 FROM pg_constraint WHERE conname = 'pages_live_publication_id_fk'
638
+ ) THEN
639
+ ALTER TABLE "pages"
640
+ ADD CONSTRAINT "pages_live_publication_id_fk"
641
+ FOREIGN KEY ("live_publication_id") REFERENCES "page_publications" ("id") ON DELETE SET NULL;
642
+ END IF;
643
+ END $$;
644
+
645
+ CREATE TABLE IF NOT EXISTS "page_url_history" (
646
+ "id" bigserial PRIMARY KEY,
647
+ "page_id" uuid,
648
+ "translation_group" uuid NOT NULL,
649
+ "locale" text NOT NULL,
650
+ "old_path" text NOT NULL,
651
+ "reason" text NOT NULL,
652
+ "changed_at" timestamp with time zone NOT NULL,
653
+ CONSTRAINT "page_url_history_page_id_fk" FOREIGN KEY ("page_id")
654
+ REFERENCES "pages" ("id") ON DELETE SET NULL,
655
+ CONSTRAINT "page_url_history_reason_check" CHECK (
656
+ "reason" IN ('slug_changed', 'moved', 'pattern_changed', 'unpublished', 'trashed', 'deleted')
657
+ )
658
+ );
659
+
660
+ CREATE TABLE IF NOT EXISTS "page_engine_settings" (
661
+ "id" smallint PRIMARY KEY DEFAULT 1,
662
+ "page_edit_locking" boolean NOT NULL DEFAULT true,
663
+ "url_pattern" text NOT NULL DEFAULT '{locale}/{path}',
664
+ "updated_at" timestamp with time zone NOT NULL DEFAULT now(),
665
+ CONSTRAINT "page_engine_settings_id_check" CHECK ("id" = 1)
666
+ );
667
+
668
+ INSERT INTO "page_engine_settings" ("id") VALUES (1) ON CONFLICT ("id") DO NOTHING;
669
+
670
+ CREATE INDEX IF NOT EXISTS "pages_parent_idx" ON "pages" USING btree ("parent_page_id");
671
+ CREATE INDEX IF NOT EXISTS "pages_locale_status_idx" ON "pages" USING btree ("locale", "status");
672
+ CREATE INDEX IF NOT EXISTS "pages_translation_group_idx" ON "pages" USING btree ("translation_group");
673
+ CREATE INDEX IF NOT EXISTS "pages_locale_path_prefix_idx"
674
+ ON "pages" USING btree ("locale", "path" text_pattern_ops);
675
+ CREATE UNIQUE INDEX IF NOT EXISTS "pages_locale_resolved_path_unique"
676
+ ON "pages" USING btree ("locale", "resolved_path")
677
+ WHERE "resolved_path" IS NOT NULL;
678
+ CREATE INDEX IF NOT EXISTS "page_blocks_owner_idx"
679
+ ON "page_blocks" USING btree ("owner_type", "owner_id", "locale");
680
+ CREATE INDEX IF NOT EXISTS "page_blocks_parent_sort_idx"
681
+ ON "page_blocks" USING btree ("parent_block_id", "sort_order");
682
+ CREATE INDEX IF NOT EXISTS "page_blocks_type_version_idx"
683
+ ON "page_blocks" USING btree ("block_type", "schema_version");
684
+ CREATE INDEX IF NOT EXISTS "block_revisions_owner_created_idx"
685
+ ON "block_revisions" USING btree ("owner_type", "owner_id", "locale", "created_at" DESC);
686
+ CREATE INDEX IF NOT EXISTS "block_revisions_batch_idx"
687
+ ON "block_revisions" USING btree ("revision_batch_id");
688
+ CREATE INDEX IF NOT EXISTS "block_revisions_block_idx"
689
+ ON "block_revisions" USING btree ("block_id", "created_at" DESC);
690
+ CREATE INDEX IF NOT EXISTS "page_publications_page_draft_published_idx"
691
+ ON "page_publications" USING btree ("page_id", "is_draft", "published_at" DESC);
692
+ CREATE INDEX IF NOT EXISTS "page_url_history_locale_path_idx"
693
+ ON "page_url_history" USING btree ("locale", "old_path");
694
+ `
695
+ }
696
+ ]);
508
697
  //#endregion
509
698
  //#region src/migrate.ts
510
699
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plakboek/db",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Postgres connection factory and migration-lock primitives for Plakboek CMS installations",
5
5
  "homepage": "https://github.com/flovan/plakboek/tree/main/packages/db#readme",
6
6
  "bugs": {